Skip to content

Commit

Permalink
[FOR 5094 controller] add unit tests in nacos-core. (alibaba#6307)
Browse files Browse the repository at this point in the history
* [nacos-core] [unit test] add unit test for CoreOpsControllerTest, NacosClusterControllerTest.

* [ISSUE alibaba#5904] add ServerLoaderControllerTest.
  • Loading branch information
brotherlu-xcq authored Jul 8, 2021
1 parent b9e388b commit 81e2f43
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ public void onException(Throwable e) {
@GetMapping("/reloadClient")
public ResponseEntity<String> reloadSingle(@RequestParam String connectionId,
@RequestParam(value = "redirectAddress", required = false) String redirectAddress) {
Map<String, String> responseMap = new HashMap<>(3);
connectionManager.loadSingle(connectionId, redirectAddress);
return ResponseEntity.ok().body("success");
}
Expand Down
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);
}
}
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());
}
}
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());
}
}

0 comments on commit 81e2f43

Please sign in to comment.