module address upgrade junit4 to junit5 (#12084)

This commit is contained in:
shalk(xiao kun) 2024-05-15 10:37:53 +08:00 committed by GitHub
parent 47dd078545
commit 4e2bcb9277
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 88 additions and 84 deletions

View File

@ -18,55 +18,59 @@ package com.alibaba.nacos.address.component;
import com.alibaba.nacos.address.constant.AddressServerConstants; import com.alibaba.nacos.address.constant.AddressServerConstants;
import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.Instance;
import org.junit.Assert; import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class AddressServerGeneratorManagerTest { import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
class AddressServerGeneratorManagerTest {
@Test @Test
public void testGenerateProductName() { void testGenerateProductName() {
AddressServerGeneratorManager manager = new AddressServerGeneratorManager(); AddressServerGeneratorManager manager = new AddressServerGeneratorManager();
final String blankName = manager.generateProductName(""); final String blankName = manager.generateProductName("");
Assert.assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, blankName); assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, blankName);
final String defaultName = manager.generateProductName(AddressServerConstants.DEFAULT_PRODUCT); final String defaultName = manager.generateProductName(AddressServerConstants.DEFAULT_PRODUCT);
Assert.assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, defaultName); assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, defaultName);
final String testName = manager.generateProductName("test"); final String testName = manager.generateProductName("test");
Assert.assertEquals("nacos.as.test", testName); assertEquals("nacos.as.test", testName);
} }
@Test @Test
public void testGenerateInstancesByIps() { void testGenerateInstancesByIps() {
AddressServerGeneratorManager manager = new AddressServerGeneratorManager(); AddressServerGeneratorManager manager = new AddressServerGeneratorManager();
final List<Instance> empty = manager.generateInstancesByIps(null, null, null, null); final List<Instance> empty = manager.generateInstancesByIps(null, null, null, null);
Assert.assertNotNull(empty); assertNotNull(empty);
Assert.assertTrue(empty.isEmpty()); assertTrue(empty.isEmpty());
String[] ipArray = new String[]{"192.168.3.1:8848", "192.168.3.2:8848", "192.168.3.3:8848"}; String[] ipArray = new String[]{"192.168.3.1:8848", "192.168.3.2:8848", "192.168.3.3:8848"};
final List<Instance> instanceList = manager.generateInstancesByIps("DEFAULT_GROUP@@nacos.as.test", "test", "test", final List<Instance> instanceList = manager.generateInstancesByIps("DEFAULT_GROUP@@nacos.as.test", "test", "test",
ipArray); ipArray);
Assert.assertNotNull(instanceList); assertNotNull(instanceList);
Assert.assertFalse(instanceList.isEmpty()); assertFalse(instanceList.isEmpty());
Assert.assertEquals(3, instanceList.size()); assertEquals(3, instanceList.size());
final Instance instance1 = instanceList.get(0); final Instance instance1 = instanceList.get(0);
Assert.assertEquals("192.168.3.1", instance1.getIp()); assertEquals("192.168.3.1", instance1.getIp());
final Instance instance2 = instanceList.get(1); final Instance instance2 = instanceList.get(1);
Assert.assertEquals("192.168.3.2", instance2.getIp()); assertEquals("192.168.3.2", instance2.getIp());
final Instance instance3 = instanceList.get(2); final Instance instance3 = instanceList.get(2);
Assert.assertEquals("192.168.3.3", instance3.getIp()); assertEquals("192.168.3.3", instance3.getIp());
} }
@Test @Test
public void testGenerateResponseIps() { void testGenerateResponseIps() {
final List<com.alibaba.nacos.api.naming.pojo.Instance> instanceList = new ArrayList<>(); final List<com.alibaba.nacos.api.naming.pojo.Instance> instanceList = new ArrayList<>();
Instance instance1 = new Instance(); Instance instance1 = new Instance();
instance1.setIp("192.168.3.1"); instance1.setIp("192.168.3.1");
@ -92,19 +96,19 @@ public class AddressServerGeneratorManagerTest {
.append("192.168.3.1:8848").append('\n') .append("192.168.3.1:8848").append('\n')
.append("192.168.3.2:8848").append('\n') .append("192.168.3.2:8848").append('\n')
.append("192.168.3.3:8848").append('\n'); .append("192.168.3.3:8848").append('\n');
Assert.assertEquals(ret.toString(), ipListStr); assertEquals(ret.toString(), ipListStr);
} }
@Test @Test
public void testGenerateNacosServiceName() { void testGenerateNacosServiceName() {
AddressServerGeneratorManager manager = new AddressServerGeneratorManager(); AddressServerGeneratorManager manager = new AddressServerGeneratorManager();
final String containDefault = manager.generateNacosServiceName("DEFAULT_GROUP@@test"); final String containDefault = manager.generateNacosServiceName("DEFAULT_GROUP@@test");
Assert.assertEquals("DEFAULT_GROUP@@test", containDefault); assertEquals("DEFAULT_GROUP@@test", containDefault);
final String product = manager.generateNacosServiceName("product"); final String product = manager.generateNacosServiceName("product");
Assert.assertEquals("DEFAULT_GROUP@@product", product); assertEquals("DEFAULT_GROUP@@product", product);
} }
} }

View File

@ -17,38 +17,38 @@
package com.alibaba.nacos.address.component; package com.alibaba.nacos.address.component;
import com.alibaba.nacos.address.constant.AddressServerConstants; import com.alibaba.nacos.address.constant.AddressServerConstants;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class AddressServerManagerTests { class AddressServerManagerTests {
private static final AddressServerManager ADDRESS_SERVER_MANAGER = new AddressServerManager(); private static final AddressServerManager ADDRESS_SERVER_MANAGER = new AddressServerManager();
@Test @Test
public void getRawProductName() { void getRawProductName() {
assertEquals(AddressServerConstants.DEFAULT_PRODUCT, ADDRESS_SERVER_MANAGER.getRawProductName("")); assertEquals(AddressServerConstants.DEFAULT_PRODUCT, ADDRESS_SERVER_MANAGER.getRawProductName(""));
assertEquals(AddressServerConstants.DEFAULT_PRODUCT, assertEquals(AddressServerConstants.DEFAULT_PRODUCT,
ADDRESS_SERVER_MANAGER.getRawProductName(AddressServerConstants.DEFAULT_PRODUCT)); ADDRESS_SERVER_MANAGER.getRawProductName(AddressServerConstants.DEFAULT_PRODUCT));
assertEquals("otherProduct", ADDRESS_SERVER_MANAGER.getRawProductName("otherProduct")); assertEquals("otherProduct", ADDRESS_SERVER_MANAGER.getRawProductName("otherProduct"));
} }
@Test @Test
public void getDefaultClusterNameIfEmpty() { void getDefaultClusterNameIfEmpty() {
assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty("")); assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty(""));
assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER,
ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty(AddressServerConstants.DEFAULT_GET_CLUSTER)); ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty(AddressServerConstants.DEFAULT_GET_CLUSTER));
assertEquals("otherServerList", ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty("otherServerList")); assertEquals("otherServerList", ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty("otherServerList"));
} }
@Test @Test
public void testGetRawClusterName() { void testGetRawClusterName() {
assertEquals("serverList", ADDRESS_SERVER_MANAGER.getRawClusterName("serverList")); assertEquals("serverList", ADDRESS_SERVER_MANAGER.getRawClusterName("serverList"));
assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, ADDRESS_SERVER_MANAGER.getRawClusterName("")); assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, ADDRESS_SERVER_MANAGER.getRawClusterName(""));
} }
@Test @Test
public void testSplitIps() { void testSplitIps() {
final String[] emptyArr = ADDRESS_SERVER_MANAGER.splitIps(""); final String[] emptyArr = ADDRESS_SERVER_MANAGER.splitIps("");
assertEquals(0, emptyArr.length); assertEquals(0, emptyArr.length);
final String[] one = ADDRESS_SERVER_MANAGER.splitIps("192.168.1.12:8848"); final String[] one = ADDRESS_SERVER_MANAGER.splitIps("192.168.1.12:8848");

View File

@ -26,13 +26,13 @@ import com.alibaba.nacos.naming.core.InstanceOperator;
import com.alibaba.nacos.naming.core.v2.ServiceManager; import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager; import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.core.v2.pojo.Service;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@ -40,8 +40,8 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class AddressServerClusterControllerTest { class AddressServerClusterControllerTest {
@Mock @Mock
private InstanceOperator instanceOperator; private InstanceOperator instanceOperator;
@ -53,9 +53,9 @@ public class AddressServerClusterControllerTest {
private ClusterOperator clusterOperator; private ClusterOperator clusterOperator;
private MockMvc mockMvc; private MockMvc mockMvc;
@Before @BeforeEach
public void before() { void before() {
mockMvc = MockMvcBuilders.standaloneSetup( mockMvc = MockMvcBuilders.standaloneSetup(
new AddressServerClusterController(instanceOperator, metadataManager, clusterOperator, new AddressServerClusterController(instanceOperator, metadataManager, clusterOperator,
new AddressServerManager(), new AddressServerGeneratorManager())).build(); new AddressServerManager(), new AddressServerGeneratorManager())).build();
@ -63,30 +63,30 @@ public class AddressServerClusterControllerTest {
.newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false); .newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false);
ServiceManager.getInstance().getSingleton(service); ServiceManager.getInstance().getSingleton(service);
} }
@After @AfterEach
public void tearDown() { void tearDown() {
Service service = Service Service service = Service
.newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false); .newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false);
ServiceManager.getInstance().removeSingleton(service); ServiceManager.getInstance().removeSingleton(service);
} }
@Test @Test
public void testPostCluster() throws Exception { void testPostCluster() throws Exception {
mockMvc.perform(post("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") mockMvc.perform(post("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList")
.param("ips", "192.168.3.1,192.168.3.2")).andExpect(status().isOk()); .param("ips", "192.168.3.1,192.168.3.2")).andExpect(status().isOk());
} }
@Test @Test
public void testPostClusterWithErrorIps() throws Exception { void testPostClusterWithErrorIps() throws Exception {
mockMvc.perform(post("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") mockMvc.perform(post("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList")
.param("ips", "192.168.1")).andExpect(status().isBadRequest()); .param("ips", "192.168.1")).andExpect(status().isBadRequest());
} }
@Test @Test
public void testPostClusterThrowException() throws Exception { void testPostClusterThrowException() throws Exception {
Mockito.doThrow(new NacosException(500, "create service error")).when(clusterOperator) Mockito.doThrow(new NacosException(500, "create service error")).when(clusterOperator)
.updateClusterMetadata(Mockito.eq(Constants.DEFAULT_NAMESPACE_ID), Mockito.eq( .updateClusterMetadata(Mockito.eq(Constants.DEFAULT_NAMESPACE_ID), Mockito.eq(
@ -97,34 +97,34 @@ public class AddressServerClusterControllerTest {
.param("ips", "192.168.1")).andExpect(status().isInternalServerError()); .param("ips", "192.168.1")).andExpect(status().isInternalServerError());
} }
@Test @Test
public void testDeleteCluster() throws Exception { void testDeleteCluster() throws Exception {
mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList")
.param("ips", "192.168.3.1,192.168.3.2")).andExpect(status().isOk()); .param("ips", "192.168.3.1,192.168.3.2")).andExpect(status().isOk());
} }
@Test @Test
public void testDeleteClusterCannotFindService() throws Exception { void testDeleteClusterCannotFindService() throws Exception {
tearDown(); tearDown();
mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList")
.param("ips", "192.168.3.1,192.168.3.2")).andExpect(status().isNotFound()); .param("ips", "192.168.3.1,192.168.3.2")).andExpect(status().isNotFound());
} }
@Test @Test
public void testDeleteClusterEmptyIps() throws Exception { void testDeleteClusterEmptyIps() throws Exception {
mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList")
.param("ips", "")).andExpect(status().isBadRequest()); .param("ips", "")).andExpect(status().isBadRequest());
} }
@Test @Test
public void testDeleteClusterErrorIps() throws Exception { void testDeleteClusterErrorIps() throws Exception {
mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList") mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList")
.param("ips", "192.168.1")).andExpect(status().isBadRequest()); .param("ips", "192.168.1")).andExpect(status().isBadRequest());
} }
@Test @Test
public void testDeleteClusterThrowException() throws Exception { void testDeleteClusterThrowException() throws Exception {
Mockito.doThrow(new NacosException(500, "remove service error")).when(instanceOperator) Mockito.doThrow(new NacosException(500, "remove service error")).when(instanceOperator)
.removeInstance(Mockito.eq(Constants.DEFAULT_NAMESPACE_ID), Mockito.eq( .removeInstance(Mockito.eq(Constants.DEFAULT_NAMESPACE_ID), Mockito.eq(
Constants.DEFAULT_GROUP + AddressServerConstants.GROUP_SERVICE_NAME_SEP + "nacos.as.default"), Constants.DEFAULT_GROUP + AddressServerConstants.GROUP_SERVICE_NAME_SEP + "nacos.as.default"),

View File

@ -26,12 +26,12 @@ import com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata;
import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager; import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager;
import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata; import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata;
import com.alibaba.nacos.naming.core.v2.pojo.Service; import com.alibaba.nacos.naming.core.v2.pojo.Service;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@ -43,8 +43,8 @@ import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class ServerListControllerTest { class ServerListControllerTest {
@Mock @Mock
private NamingMetadataManager metadataManager; private NamingMetadataManager metadataManager;
@ -55,23 +55,23 @@ public class ServerListControllerTest {
private Service service; private Service service;
private MockMvc mockMvc; private MockMvc mockMvc;
@Before @BeforeEach
public void before() { void before() {
this.mockMvc = MockMvcBuilders.standaloneSetup( this.mockMvc = MockMvcBuilders.standaloneSetup(
new ServerListController(new AddressServerGeneratorManager(), metadataManager, serviceStorage)).build(); new ServerListController(new AddressServerGeneratorManager(), metadataManager, serviceStorage)).build();
service = Service service = Service
.newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false); .newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false);
ServiceManager.getInstance().getSingleton(service); ServiceManager.getInstance().getSingleton(service);
} }
@After @AfterEach
public void tearDown() { void tearDown() {
ServiceManager.getInstance().removeSingleton(service); ServiceManager.getInstance().removeSingleton(service);
} }
@Test @Test
public void testGetCluster() throws Exception { void testGetCluster() throws Exception {
final Service service = Service final Service service = Service
.newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false); .newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false);
@ -86,16 +86,16 @@ public class ServerListControllerTest {
when(serviceStorage.getData(service)).thenReturn(serviceInfo); when(serviceStorage.getData(service)).thenReturn(serviceInfo);
mockMvc.perform(get("/nacos/serverList")).andExpect(status().isOk()); mockMvc.perform(get("/nacos/serverList")).andExpect(status().isOk());
} }
@Test @Test
public void testGetClusterCannotFindService() throws Exception { void testGetClusterCannotFindService() throws Exception {
tearDown(); tearDown();
mockMvc.perform(get("/default/serverList")).andExpect(status().isNotFound()); mockMvc.perform(get("/default/serverList")).andExpect(status().isNotFound());
} }
@Test @Test
public void testGetClusterCannotFindCluster() throws Exception { void testGetClusterCannotFindCluster() throws Exception {
mockMvc.perform(get("/nacos/serverList")).andExpect(status().isNotFound()); mockMvc.perform(get("/nacos/serverList")).andExpect(status().isNotFound());
} }