Skip to content

Commit

Permalink
Fix Unit test in nacos naming module (alibaba#6031)
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion authored Jun 15, 2021
1 parent b23756f commit 6ca9c25
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class DistroConsistencyServiceImplTest extends BaseTest {

Expand Down Expand Up @@ -71,7 +70,6 @@ public class DistroConsistencyServiceImplTest extends BaseTest {

@Before
public void setUp() throws Exception {
when(globalConfig.getTaskDispatchPeriod()).thenReturn(2000);
distroConsistencyService = new DistroConsistencyServiceImpl(distroMapper, dataStore, serializer, switchDomain,
globalConfig, distroProtocol);
ReflectionTestUtils.setField(distroConsistencyService, "notifier", notifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,75 @@

import com.alibaba.nacos.core.exception.KvStorageException;
import com.alibaba.nacos.core.storage.kv.FileKvStorage;
import com.alibaba.nacos.sys.utils.DiskUtils;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.IOException;
import java.lang.reflect.Field;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class NamingKvStorageTest extends TestCase {

private NamingKvStorage namingKvStorage;

@Mock
private FileKvStorage baseDirStorageMock;

private final byte[] key = "fileName_test".getBytes();

private final String str = "str_test";

@Before
public void setUp() throws Exception {
super.setUp();
namingKvStorage = new NamingKvStorage("baseDir_test");

Field baseDirStorageField = NamingKvStorage.class.getDeclaredField("baseDirStorage");
baseDirStorageField.setAccessible(true);
baseDirStorageField.set(namingKvStorage, baseDirStorageMock);

when(baseDirStorageMock.get(key)).thenReturn(null);
}


@After
public void tearDown() throws IOException {
DiskUtils.deleteDirectory("baseDir_test");
}

@Test
public void testGet() throws KvStorageException {
namingKvStorage.get(key);
verify(baseDirStorageMock).get(key);
}

@Test
public void testPut() throws KvStorageException {
byte[] value = "value_test".getBytes();
namingKvStorage.put(key, value);
verify(baseDirStorageMock).put(key, value);
}

@Test
public void testDelete() throws KvStorageException {
namingKvStorage.delete(key);
verify(baseDirStorageMock).delete(key);
}

@Test
public void testDoSnapshot() throws KvStorageException {
namingKvStorage.doSnapshot(str);
verify(baseDirStorageMock).doSnapshot(str);
}

@Test
public void testSnapshotLoad() throws KvStorageException {
namingKvStorage.snapshotLoad(str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
import java.util.Collections;

import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -55,7 +58,7 @@ public class CatalogControllerTest {
private Cluster cluster;

@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
public void setUp() throws NoSuchFieldException, IllegalAccessException, NacosException {
catalogController = new CatalogController();
catalogServiceV1 = new CatalogServiceV1Impl(serviceManager);
ReflectionTestUtils.setField(catalogController, "serviceManager", serviceManager);
Expand All @@ -70,6 +73,7 @@ public void setUp() throws NoSuchFieldException, IllegalAccessException {
service.addCluster(cluster);
when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID,
TEST_GROUP_NAME + Constants.SERVICE_INFO_SPLITER + TEST_SERVICE_NAME)).thenReturn(service);
doCallRealMethod().when(serviceManager).checkServiceIsNull(eq(null), anyString(), anyString());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.hamcrest.CoreMatchers.isA;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.when;

@RunWith(SpringJUnit4ClassRunner.class)
Expand All @@ -62,6 +65,11 @@ public void before() {
mockmvc = MockMvcBuilders.standaloneSetup(clusterController).build();
ReflectionTestUtils.setField(clusterController, "upgradeJudgement", upgradeJudgement);
ReflectionTestUtils.setField(clusterController, "clusterOperatorV1", clusterOperatorV1);
try {
doCallRealMethod().when(serviceManager).checkServiceIsNull(eq(null), anyString(), anyString());
} catch (NacosException e) {
e.printStackTrace();
}
}

@Test
Expand All @@ -87,7 +95,7 @@ public void testUpdate() throws Exception {
@Test
public void testUpdateNoService() throws Exception {
expectedException.expectCause(isA(NacosException.class));
expectedException.expectMessage("service not found:test-service-not-found");
expectedException.expectMessage("service not found, namespace: public, serviceName: test-service-not-found");
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster").param("clusterName", TEST_CLUSTER_NAME)
.param("serviceName", "test-service-not-found").param("healthChecker", "{\"type\":\"HTTP\"}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.SwitchDomain.TcpHealthParams;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -64,6 +65,11 @@ public void before() {
cluster.init();
}

@After
public void tearDown() {
cluster.destroy();
}

@Test
public void updateCluster() {
Service service = new Service();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public void testSerializeServiceChecksum() {
assertTrue(actual.contains("\"serviceName2Checksum\":{\"test\":\"1234567890\"}"));
}

@Test
@Test(expected = NacosException.class)
public void testCheckServiceIsNull() throws NacosException {
serviceManager.createEmptyService(TEST_NAMESPACE, TEST_SERVICE_NAME, true);
String serviceName = "order-service";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.mockito.Mock;
import org.springframework.test.util.ReflectionTestUtils;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class ServiceOperatorV1ImplTest extends BaseTest {

private ServiceOperatorV1Impl serviceOperatorV1Impl;
Expand All @@ -49,11 +52,14 @@ public void setUp() {

@Test
public void testUpdate() throws NacosException {
serviceManager.createEmptyService(TEST_NAMESPACE, TEST_SERVICE_NAME, true);
String serviceName = "order-service";
String serviceName = "DEFAULT_GROUP@@order-service";
serviceManager.createEmptyService(TEST_NAMESPACE, serviceName, true);
com.alibaba.nacos.naming.core.v2.pojo.Service service = Service
.newService(TEST_NAMESPACE, NamingUtils.getGroupName(serviceName),
NamingUtils.getServiceName(serviceName));
serviceOperatorV1Impl.update(service, new ServiceMetadata());
ServiceMetadata metadata = new ServiceMetadata();
metadata.setProtectThreshold(0.1F);
serviceOperatorV1Impl.update(service, metadata);
assertThat(serviceManager.getService(TEST_NAMESPACE, serviceName).getProtectThreshold(), is(0.1F));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void tearDown() {
@Test
public void testUpgradeOneNode() throws Exception {
Collection<Member> members = mockMember("1.3.2", "1.3.2", "2.0.0");
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -107,7 +107,7 @@ public void testUpgradeOneNode() throws Exception {
@Test
public void testUpgradeOneFor14XNode() throws Exception {
Collection<Member> members = mockMember("1.4.0", "2.0.0", "2.0.0");
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -119,7 +119,7 @@ public void testUpgradeOneFor14XNode() throws Exception {
@Test
public void testUpgradeTwoNode() throws Exception {
Collection<Member> members = mockMember("", "2.0.0", "2.0.0");
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -137,7 +137,7 @@ public void testUpgradeCheckSucc() throws Exception {
while (iterator.hasNext()) {
iterator.next().setExtendVal(MemberMetaDataConstants.READY_TO_UPGRADE, true);
}
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -155,7 +155,7 @@ public void testUpgradeCheckSelfFail() throws Exception {
while (iterator.hasNext()) {
iterator.next().setExtendVal(MemberMetaDataConstants.READY_TO_UPGRADE, true);
}
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -176,7 +176,7 @@ public void testAlreadyUpgradedAndCheckSelfFail() throws Exception {
when(upgradeStates.isUpgraded()).thenReturn(true);
upgradeJudgement = new UpgradeJudgement(raftPeerSet, raftCore, versionJudgement, memberManager, serviceManager,
upgradeStates, doubleWriteDelayTaskEngine);
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -190,7 +190,7 @@ public void testUpgradeCheckOthersFail() throws Exception {
Collection<Member> members = mockMember("2.0.0", "2.0.0", "2.0.0");
when(doubleWriteDelayTaskEngine.isEmpty()).thenReturn(true);
members.iterator().next().setExtendVal(MemberMetaDataConstants.READY_TO_UPGRADE, true);
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -207,7 +207,7 @@ public void testAlreadyUpgradedAndCheckOthersFail() throws Exception {
when(upgradeStates.isUpgraded()).thenReturn(true);
upgradeJudgement = new UpgradeJudgement(raftPeerSet, raftCore, versionJudgement, memberManager, serviceManager,
upgradeStates, doubleWriteDelayTaskEngine);
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -221,7 +221,7 @@ public void testDowngradeOneFor14XNode() throws Exception {
upgradeJudgement.setUseGrpcFeatures(true);
upgradeJudgement.setUseJraftFeatures(true);
Collection<Member> members = mockMember("1.4.0", "2.0.0", "2.0.0");
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -236,7 +236,7 @@ public void testAlreadyUpgradedAndDowngradeOneFor14XNode() throws Exception {
when(upgradeStates.isUpgraded()).thenReturn(true);
upgradeJudgement = new UpgradeJudgement(raftPeerSet, raftCore, versionJudgement, memberManager, serviceManager,
upgradeStates, doubleWriteDelayTaskEngine);
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, never()).init();
verify(raftCore, never()).init();
verify(versionJudgement, never()).reset();
Expand All @@ -250,7 +250,7 @@ public void testDowngradeTwoNode() throws Exception {
upgradeJudgement.setUseGrpcFeatures(true);
upgradeJudgement.setUseJraftFeatures(true);
Collection<Member> members = mockMember("", "", "2.0.0");
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, atMostOnce()).init();
verify(raftCore, atMostOnce()).init();
verify(versionJudgement, atMostOnce()).reset();
Expand All @@ -265,7 +265,7 @@ public void testAlreadyUpgradedAndDowngradeTwoNode() throws Exception {
when(upgradeStates.isUpgraded()).thenReturn(true);
upgradeJudgement = new UpgradeJudgement(raftPeerSet, raftCore, versionJudgement, memberManager, serviceManager,
upgradeStates, doubleWriteDelayTaskEngine);
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, atMostOnce()).init();
verify(raftCore, atMostOnce()).init();
verify(versionJudgement, atMostOnce()).reset();
Expand All @@ -279,7 +279,7 @@ public void testDowngradeOneNode() throws Exception {
upgradeJudgement.setUseGrpcFeatures(true);
upgradeJudgement.setUseJraftFeatures(true);
Collection<Member> members = mockMember("1.3.2", "2.0.0", "2.0.0");
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, atMostOnce()).init();
verify(raftCore, atMostOnce()).init();
verify(versionJudgement, atMostOnce()).reset();
Expand All @@ -294,7 +294,7 @@ public void testAlreadyUpgradedAndDowngradeOneNode() throws Exception {
when(upgradeStates.isUpgraded()).thenReturn(true);
upgradeJudgement = new UpgradeJudgement(raftPeerSet, raftCore, versionJudgement, memberManager, serviceManager,
upgradeStates, doubleWriteDelayTaskEngine);
upgradeJudgement.onEvent(MembersChangeEvent.builder().members(members).build());
upgradeJudgement.onEvent(MembersChangeEvent.builder().triggers(members).build());
verify(raftPeerSet, atMostOnce()).init();
verify(raftCore, atMostOnce()).init();
verify(versionJudgement, atMostOnce()).reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.healthcheck.extend.HealthCheckExtendProvider;
import com.alibaba.nacos.naming.healthcheck.v2.HealthCheckTaskV2;
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.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.env.MockEnvironment;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand Down Expand Up @@ -58,6 +60,7 @@ public class HealthCheckProcessorV2DelegateTest {
public void setUp() {
healthCheckProcessorV2Delegate = new HealthCheckProcessorV2Delegate(healthCheckExtendProvider);
verify(healthCheckExtendProvider).init();
EnvUtil.setEnvironment(new MockEnvironment());
}

@Test
Expand Down
Loading

0 comments on commit 6ca9c25

Please sign in to comment.