-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add ut for com.alibaba.nacos.naming.cluster * fix ut
- Loading branch information
Showing
6 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
naming/src/test/java/com/alibaba/nacos/naming/cluster/ServerStatusManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.alibaba.nacos.naming.cluster; | ||
|
||
import com.alibaba.nacos.naming.consistency.ConsistencyService; | ||
import com.alibaba.nacos.naming.misc.GlobalExecutor; | ||
import com.alibaba.nacos.naming.misc.SwitchDomain; | ||
import com.alibaba.nacos.sys.env.EnvUtil; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
import org.springframework.mock.env.MockEnvironment; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Optional; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ServerStatusManagerTest { | ||
|
||
@Before | ||
public void setUp() { | ||
EnvUtil.setEnvironment(new MockEnvironment()); | ||
} | ||
|
||
@Test | ||
public void testInit() { | ||
try (MockedStatic mocked = mockStatic(GlobalExecutor.class)) { | ||
ServerStatusManager serverStatusManager = new ServerStatusManager(mock(SwitchDomain.class)); | ||
serverStatusManager.init(); | ||
mocked.verify(() -> GlobalExecutor.registerServerStatusUpdater(any())); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetServerStatus() { | ||
ServerStatusManager serverStatusManager = new ServerStatusManager(mock(SwitchDomain.class)); | ||
ServerStatus serverStatus = serverStatusManager.getServerStatus(); | ||
Assert.assertEquals(ServerStatus.STARTING, serverStatus); | ||
|
||
} | ||
|
||
@Test | ||
public void testGetErrorMsg() throws NoSuchFieldException, IllegalAccessException { | ||
ServerStatusManager serverStatusManager = new ServerStatusManager(mock(SwitchDomain.class)); | ||
Field field = ServerStatusManager.class.getDeclaredField("consistencyService"); | ||
field.setAccessible(true); | ||
ConsistencyService consistencyService = mock(ConsistencyService.class); | ||
when(consistencyService.getErrorMsg()).thenReturn(Optional.empty()); | ||
field.set(serverStatusManager, consistencyService); | ||
Optional<String> errorMsg = serverStatusManager.getErrorMsg(); | ||
Assert.assertFalse(errorMsg.isPresent()); | ||
} | ||
|
||
@Test | ||
public void testUpdaterFromSwitch() { | ||
SwitchDomain switchDomain = mock(SwitchDomain.class); | ||
String expect = ServerStatus.DOWN.toString(); | ||
when(switchDomain.getOverriddenServerStatus()).thenReturn(expect); | ||
ServerStatusManager serverStatusManager = new ServerStatusManager(switchDomain); | ||
ServerStatusManager.ServerStatusUpdater updater = serverStatusManager.new ServerStatusUpdater(); | ||
//then | ||
updater.run(); | ||
//then | ||
ServerStatus serverStatus = serverStatusManager.getServerStatus(); | ||
Assert.assertEquals(expect, serverStatus.toString()); | ||
} | ||
|
||
@Test | ||
public void testUpdaterFromConsistency1() throws NoSuchFieldException, IllegalAccessException { | ||
SwitchDomain switchDomain = mock(SwitchDomain.class); | ||
ServerStatusManager serverStatusManager = new ServerStatusManager(switchDomain); | ||
Field field = ServerStatusManager.class.getDeclaredField("consistencyService"); | ||
field.setAccessible(true); | ||
ConsistencyService consistencyService = mock(ConsistencyService.class); | ||
when(consistencyService.isAvailable()).thenReturn(true); | ||
field.set(serverStatusManager, consistencyService); | ||
ServerStatusManager.ServerStatusUpdater updater = serverStatusManager.new ServerStatusUpdater(); | ||
//then | ||
updater.run(); | ||
//then | ||
Assert.assertEquals(ServerStatus.UP, serverStatusManager.getServerStatus()); | ||
} | ||
|
||
@Test | ||
public void testUpdaterFromConsistency2() throws NoSuchFieldException, IllegalAccessException { | ||
SwitchDomain switchDomain = mock(SwitchDomain.class); | ||
ServerStatusManager serverStatusManager = new ServerStatusManager(switchDomain); | ||
Field field = ServerStatusManager.class.getDeclaredField("consistencyService"); | ||
field.setAccessible(true); | ||
ConsistencyService consistencyService = mock(ConsistencyService.class); | ||
when(consistencyService.isAvailable()).thenReturn(false); | ||
field.set(serverStatusManager, consistencyService); | ||
ServerStatusManager.ServerStatusUpdater updater = serverStatusManager.new ServerStatusUpdater(); | ||
//then | ||
updater.run(); | ||
//then | ||
Assert.assertEquals(ServerStatus.DOWN, serverStatusManager.getServerStatus()); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...test/java/com/alibaba/nacos/naming/cluster/remote/request/AbstractClusterRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.alibaba.nacos.naming.cluster.remote.request; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class AbstractClusterRequestTest { | ||
|
||
@Test | ||
public void getModule() { | ||
AbstractClusterRequest request = new AbstractClusterRequest() { | ||
}; | ||
String actual = request.getModule(); | ||
assertEquals("cluster", actual); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../src/test/java/com/alibaba/nacos/naming/cluster/remote/request/DistroDataRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.alibaba.nacos.naming.cluster.remote.request; | ||
|
||
import com.alibaba.nacos.consistency.DataOperation; | ||
import com.alibaba.nacos.core.distributed.distro.entity.DistroData; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class DistroDataRequestTest { | ||
|
||
@Test | ||
public void testConstructor1() { | ||
DistroDataRequest req = new DistroDataRequest(); | ||
assertNotNull(req); | ||
} | ||
|
||
@Test | ||
public void testConstructor2() { | ||
DistroDataRequest req = new DistroDataRequest(mock(DistroData.class), mock(DataOperation.class)); | ||
assertNotNull(req); | ||
} | ||
|
||
@Test | ||
public void testGetterAndSetter() { | ||
DistroData distroData = mock(DistroData.class); | ||
DataOperation dataOperation = mock(DataOperation.class); | ||
DistroDataRequest req = new DistroDataRequest(); | ||
req.setDistroData(distroData); | ||
req.setDataOperation(dataOperation); | ||
assertEquals(distroData, req.getDistroData()); | ||
assertEquals(dataOperation, req.getDataOperation()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...rc/test/java/com/alibaba/nacos/naming/cluster/remote/response/DistroDataResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.alibaba.nacos.naming.cluster.remote.response; | ||
|
||
import com.alibaba.nacos.core.distributed.distro.entity.DistroData; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class DistroDataResponseTest { | ||
|
||
@Test | ||
public void test() { | ||
DistroDataResponse distroDataResponse = new DistroDataResponse(); | ||
DistroData distroData = mock(DistroData.class); | ||
distroDataResponse.setDistroData(distroData); | ||
assertEquals(distroData, distroDataResponse.getDistroData()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters