forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FOR 5094 controller] add unit tests in nacos-core. (alibaba#6307)
* [nacos-core] [unit test] add unit test for CoreOpsControllerTest, NacosClusterControllerTest. * [ISSUE alibaba#5904] add ServerLoaderControllerTest.
- Loading branch information
1 parent
b9e388b
commit 81e2f43
Showing
4 changed files
with
359 additions
and
1 deletion.
There are no files selected for viewing
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
91 changes: 91 additions & 0 deletions
91
core/src/test/java/com/alibaba/nacos/core/controller/CoreOpsControllerTest.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,91 @@ | ||
/* | ||
* Copyright 1999-2021 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.core.controller; | ||
|
||
import com.alibaba.nacos.common.model.RestResult; | ||
import com.alibaba.nacos.common.model.RestResultUtils; | ||
import com.alibaba.nacos.consistency.IdGenerator; | ||
import com.alibaba.nacos.consistency.cp.CPProtocol; | ||
import com.alibaba.nacos.core.distributed.ProtocolManager; | ||
import com.alibaba.nacos.core.distributed.id.IdGeneratorManager; | ||
import com.alibaba.nacos.core.distributed.id.SnowFlowerIdGenerator; | ||
import com.alibaba.nacos.sys.env.EnvUtil; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.springframework.mock.env.MockEnvironment; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* {@link CoreOpsController} unit test. | ||
* | ||
* @author chenglu | ||
* @date 2021-07-07 22:20 | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class CoreOpsControllerTest { | ||
|
||
@InjectMocks | ||
private CoreOpsController coreOpsController; | ||
|
||
@Mock | ||
private ProtocolManager protocolManager; | ||
|
||
@Mock | ||
private IdGeneratorManager idGeneratorManager; | ||
|
||
@Before | ||
public void setUp() { | ||
EnvUtil.setEnvironment(new MockEnvironment()); | ||
} | ||
|
||
@Test | ||
public void testRaftOps() { | ||
Mockito.when(protocolManager.getCpProtocol()).thenAnswer(invocationOnMock -> { | ||
CPProtocol cpProtocol = Mockito.mock(CPProtocol.class); | ||
Mockito.when(cpProtocol.execute(Mockito.anyMap())).thenReturn(RestResultUtils.success("res")); | ||
return cpProtocol; | ||
}); | ||
|
||
RestResult<String> result = coreOpsController.raftOps(new HashMap<>()); | ||
Assert.assertEquals("res", result.getData()); | ||
} | ||
|
||
@Test | ||
public void testIdInfo() { | ||
Map<String, IdGenerator> idGeneratorMap = new HashMap<>(); | ||
idGeneratorMap.put("1", new SnowFlowerIdGenerator()); | ||
Mockito.when(idGeneratorManager.getGeneratorMap()).thenReturn(idGeneratorMap); | ||
|
||
RestResult<Map<String, Map<Object, Object>>> res = coreOpsController.idInfo(); | ||
Assert.assertEquals(2, res.getData().get("1").size()); | ||
} | ||
|
||
@Test | ||
public void testSetLogLevel() { | ||
String res = coreOpsController.setLogLevel("1", "info"); | ||
Assert.assertEquals("200", res); | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
core/src/test/java/com/alibaba/nacos/core/controller/NacosClusterControllerTest.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,121 @@ | ||
/* | ||
* Copyright 1999-2021 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.core.controller; | ||
|
||
import com.alibaba.nacos.common.model.RestResult; | ||
import com.alibaba.nacos.core.cluster.Member; | ||
import com.alibaba.nacos.core.cluster.NodeState; | ||
import com.alibaba.nacos.core.cluster.ServerMemberManager; | ||
import com.alibaba.nacos.sys.env.EnvUtil; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.springframework.mock.env.MockEnvironment; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* {@link NacosClusterController} unit test. | ||
* | ||
* @author chenglu | ||
* @date 2021-07-07 22:53 | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class NacosClusterControllerTest { | ||
|
||
@InjectMocks | ||
private NacosClusterController nacosClusterController; | ||
|
||
@Mock | ||
private ServerMemberManager serverMemberManager; | ||
|
||
@Before | ||
public void setUp() { | ||
EnvUtil.setEnvironment(new MockEnvironment()); | ||
} | ||
|
||
@Test | ||
public void testSelf() { | ||
Member self = new Member(); | ||
Mockito.when(serverMemberManager.getSelf()).thenReturn(self); | ||
|
||
RestResult<Member> result = nacosClusterController.self(); | ||
Assert.assertEquals(self, result.getData()); | ||
} | ||
|
||
@Test | ||
public void testListNodes() { | ||
Member member1 = new Member(); | ||
member1.setIp("1.1.1.1"); | ||
List<Member> members = Arrays.asList(member1); | ||
Mockito.when(serverMemberManager.allMembers()).thenReturn(members); | ||
|
||
RestResult<Collection<Member>> result = nacosClusterController.listNodes("1.1.1.1"); | ||
Assert.assertEquals(1, result.getData().size()); | ||
} | ||
|
||
@Test | ||
public void testListSimpleNodes() { | ||
Mockito.when(serverMemberManager.getMemberAddressInfos()).thenReturn(Collections.singleton("1.1.1.1")); | ||
|
||
RestResult<Collection<String>> result = nacosClusterController.listSimpleNodes(); | ||
Assert.assertEquals(1, result.getData().size()); | ||
} | ||
|
||
@Test | ||
public void testGetHealth() { | ||
Member self = new Member(); | ||
self.setState(NodeState.UP); | ||
Mockito.when(serverMemberManager.getSelf()).thenReturn(self); | ||
|
||
RestResult<String> result = nacosClusterController.getHealth(); | ||
Assert.assertEquals(NodeState.UP.name(), result.getData()); | ||
} | ||
|
||
@Test | ||
public void testReport() { | ||
Mockito.when(serverMemberManager.update(Mockito.any())).thenReturn(true); | ||
|
||
Member member = new Member(); | ||
member.setIp("1.1.1.1"); | ||
member.setPort(8848); | ||
member.setAddress("test"); | ||
RestResult<String> result = nacosClusterController.report(member); | ||
Assert.assertTrue(Boolean.parseBoolean(result.getData())); | ||
} | ||
|
||
@Test | ||
public void testSwitchLookup() { | ||
RestResult<String> result = nacosClusterController.switchLookup("test"); | ||
Assert.assertTrue(result.ok()); | ||
} | ||
|
||
@Test | ||
public void testLeave() throws Exception { | ||
RestResult<String> result = nacosClusterController.leave(Collections.singletonList("1.1.1.1")); | ||
Assert.assertEquals("ok", result.getData()); | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
core/src/test/java/com/alibaba/nacos/core/controller/ServerLoaderControllerTest.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,147 @@ | ||
/* | ||
* Copyright 1999-2021 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.core.controller; | ||
|
||
import com.alibaba.nacos.api.ability.ServerAbilities; | ||
import com.alibaba.nacos.api.ability.ServerRemoteAbility; | ||
import com.alibaba.nacos.api.exception.NacosException; | ||
import com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse; | ||
import com.alibaba.nacos.core.cluster.Member; | ||
import com.alibaba.nacos.core.cluster.ServerMemberManager; | ||
import com.alibaba.nacos.core.cluster.remote.ClusterRpcClientProxy; | ||
import com.alibaba.nacos.core.remote.Connection; | ||
import com.alibaba.nacos.core.remote.ConnectionManager; | ||
import com.alibaba.nacos.core.remote.core.ServerLoaderInfoRequestHandler; | ||
import com.alibaba.nacos.core.remote.core.ServerReloaderRequestHandler; | ||
import com.alibaba.nacos.sys.env.EnvUtil; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.mock.env.MockEnvironment; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* {@link ServerLoaderController} unit test. | ||
* | ||
* @author chenglu | ||
* @date 2021-07-07 23:10 | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class ServerLoaderControllerTest { | ||
|
||
@InjectMocks | ||
private ServerLoaderController serverLoaderController; | ||
|
||
@Mock | ||
private ConnectionManager connectionManager; | ||
|
||
@Mock | ||
private ServerMemberManager serverMemberManager; | ||
|
||
@Mock | ||
private ClusterRpcClientProxy clusterRpcClientProxy; | ||
|
||
@Mock | ||
private ServerReloaderRequestHandler serverReloaderRequestHandler; | ||
|
||
@Mock | ||
private ServerLoaderInfoRequestHandler serverLoaderInfoRequestHandler; | ||
|
||
@Test | ||
public void testCurrentClients() { | ||
Mockito.when(connectionManager.currentClients()).thenReturn(new HashMap<>()); | ||
|
||
ResponseEntity<Map<String, Connection>> result = serverLoaderController.currentClients(); | ||
Assert.assertEquals(0, result.getBody().size()); | ||
} | ||
|
||
@Test | ||
public void testReloadCount() { | ||
ResponseEntity<String> result = serverLoaderController.reloadCount(1, "1.1.1.1"); | ||
Assert.assertEquals("success", result.getBody()); | ||
} | ||
|
||
@Test | ||
public void testSmartReload() throws NacosException { | ||
EnvUtil.setEnvironment(new MockEnvironment()); | ||
Member member = new Member(); | ||
member.setIp("1.1.1.1"); | ||
member.setPort(8848); | ||
ServerAbilities serverAbilities = new ServerAbilities(); | ||
ServerRemoteAbility serverRemoteAbility = new ServerRemoteAbility(); | ||
serverRemoteAbility.setSupportRemoteConnection(true); | ||
serverAbilities.setRemoteAbility(serverRemoteAbility); | ||
member.setAbilities(serverAbilities); | ||
Mockito.when(serverMemberManager.allMembersWithoutSelf()).thenReturn(Collections.singletonList(member)); | ||
|
||
Map<String, String> metrics = new HashMap<>(); | ||
metrics.put("conCount", "1"); | ||
metrics.put("sdkConCount", "1"); | ||
ServerLoaderInfoResponse serverLoaderInfoResponse = new ServerLoaderInfoResponse(); | ||
serverLoaderInfoResponse.setLoaderMetrics(metrics); | ||
Mockito.when(serverLoaderInfoRequestHandler.handle(Mockito.any(), Mockito.any())).thenReturn(serverLoaderInfoResponse); | ||
|
||
Mockito.when(serverMemberManager.getSelf()).thenReturn(member); | ||
|
||
MockHttpServletRequest httpServletRequest = new MockHttpServletRequest(); | ||
ResponseEntity<String> result = serverLoaderController.smartReload(httpServletRequest, "1", null); | ||
|
||
Assert.assertEquals("Ok", result.getBody()); | ||
|
||
} | ||
|
||
@Test | ||
public void testReloadSingle() { | ||
ResponseEntity<String> result = serverLoaderController.reloadSingle("111", "1.1.1.1"); | ||
Assert.assertEquals("success", result.getBody()); | ||
} | ||
|
||
@Test | ||
public void testLoaderMetrics() throws NacosException { | ||
EnvUtil.setEnvironment(new MockEnvironment()); | ||
Member member = new Member(); | ||
member.setIp("1.1.1.1"); | ||
member.setPort(8848); | ||
ServerAbilities serverAbilities = new ServerAbilities(); | ||
ServerRemoteAbility serverRemoteAbility = new ServerRemoteAbility(); | ||
serverRemoteAbility.setSupportRemoteConnection(true); | ||
serverAbilities.setRemoteAbility(serverRemoteAbility); | ||
member.setAbilities(serverAbilities); | ||
Mockito.when(serverMemberManager.allMembersWithoutSelf()).thenReturn(Collections.singletonList(member)); | ||
|
||
Map<String, String> metrics = new HashMap<>(); | ||
metrics.put("conCount", "1"); | ||
ServerLoaderInfoResponse serverLoaderInfoResponse = new ServerLoaderInfoResponse(); | ||
serverLoaderInfoResponse.setLoaderMetrics(metrics); | ||
Mockito.when(serverLoaderInfoRequestHandler.handle(Mockito.any(), Mockito.any())).thenReturn(serverLoaderInfoResponse); | ||
|
||
Mockito.when(serverMemberManager.getSelf()).thenReturn(member); | ||
|
||
ResponseEntity<Map<String, Object>> result = serverLoaderController.loaderMetrics(); | ||
Assert.assertEquals(9, result.getBody().size()); | ||
} | ||
} |