Skip to content

Commit

Permalink
module address upgrade junit4 to junit5 (#12084)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalk committed May 15, 2024
1 parent 47dd078 commit 4e2bcb9
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,59 @@

import com.alibaba.nacos.address.constant.AddressServerConstants;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
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
public void testGenerateProductName() {
void testGenerateProductName() {
AddressServerGeneratorManager manager = new AddressServerGeneratorManager();
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);
Assert.assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, defaultName);
assertEquals(AddressServerConstants.ALIWARE_NACOS_DEFAULT_PRODUCT_NAME, defaultName);

final String testName = manager.generateProductName("test");
Assert.assertEquals("nacos.as.test", testName);
assertEquals("nacos.as.test", testName);

}

@Test
public void testGenerateInstancesByIps() {
void testGenerateInstancesByIps() {
AddressServerGeneratorManager manager = new AddressServerGeneratorManager();
final List<Instance> empty = manager.generateInstancesByIps(null, null, null, null);
Assert.assertNotNull(empty);
Assert.assertTrue(empty.isEmpty());
assertNotNull(empty);
assertTrue(empty.isEmpty());

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",
ipArray);
Assert.assertNotNull(instanceList);
Assert.assertFalse(instanceList.isEmpty());
Assert.assertEquals(3, instanceList.size());
assertNotNull(instanceList);
assertFalse(instanceList.isEmpty());
assertEquals(3, instanceList.size());

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);
Assert.assertEquals("192.168.3.2", instance2.getIp());
assertEquals("192.168.3.2", instance2.getIp());

final Instance instance3 = instanceList.get(2);
Assert.assertEquals("192.168.3.3", instance3.getIp());
assertEquals("192.168.3.3", instance3.getIp());

}

@Test
public void testGenerateResponseIps() {
void testGenerateResponseIps() {
final List<com.alibaba.nacos.api.naming.pojo.Instance> instanceList = new ArrayList<>();
Instance instance1 = new Instance();
instance1.setIp("192.168.3.1");
Expand All @@ -92,19 +96,19 @@ public void testGenerateResponseIps() {
.append("192.168.3.1:8848").append('\n')
.append("192.168.3.2:8848").append('\n')
.append("192.168.3.3:8848").append('\n');
Assert.assertEquals(ret.toString(), ipListStr);
assertEquals(ret.toString(), ipListStr);

}

@Test
public void testGenerateNacosServiceName() {
void testGenerateNacosServiceName() {
AddressServerGeneratorManager manager = new AddressServerGeneratorManager();

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");
Assert.assertEquals("DEFAULT_GROUP@@product", product);
assertEquals("DEFAULT_GROUP@@product", product);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,38 @@
package com.alibaba.nacos.address.component;

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();

@Test
public void getRawProductName() {
void getRawProductName() {
assertEquals(AddressServerConstants.DEFAULT_PRODUCT, ADDRESS_SERVER_MANAGER.getRawProductName(""));
assertEquals(AddressServerConstants.DEFAULT_PRODUCT,
ADDRESS_SERVER_MANAGER.getRawProductName(AddressServerConstants.DEFAULT_PRODUCT));
assertEquals("otherProduct", ADDRESS_SERVER_MANAGER.getRawProductName("otherProduct"));
}

@Test
public void getDefaultClusterNameIfEmpty() {
void getDefaultClusterNameIfEmpty() {
assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty(""));
assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER,
ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty(AddressServerConstants.DEFAULT_GET_CLUSTER));
assertEquals("otherServerList", ADDRESS_SERVER_MANAGER.getDefaultClusterNameIfEmpty("otherServerList"));
}

@Test
public void testGetRawClusterName() {
void testGetRawClusterName() {
assertEquals("serverList", ADDRESS_SERVER_MANAGER.getRawClusterName("serverList"));
assertEquals(AddressServerConstants.DEFAULT_GET_CLUSTER, ADDRESS_SERVER_MANAGER.getRawClusterName(""));
}

@Test
public void testSplitIps() {
void testSplitIps() {
final String[] emptyArr = ADDRESS_SERVER_MANAGER.splitIps("");
assertEquals(0, emptyArr.length);
final String[] one = ADDRESS_SERVER_MANAGER.splitIps("192.168.1.12:8848");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@
import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
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.setup.MockMvcBuilders;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(MockitoJUnitRunner.class)
public class AddressServerClusterControllerTest {
@ExtendWith(MockitoExtension.class)
class AddressServerClusterControllerTest {

@Mock
private InstanceOperator instanceOperator;
Expand All @@ -53,40 +53,40 @@ public class AddressServerClusterControllerTest {
private ClusterOperator clusterOperator;

private MockMvc mockMvc;
@Before
public void before() {

@BeforeEach
void before() {
mockMvc = MockMvcBuilders.standaloneSetup(
new AddressServerClusterController(instanceOperator, metadataManager, clusterOperator,
new AddressServerManager(), new AddressServerGeneratorManager())).build();
Service service = Service
.newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false);
ServiceManager.getInstance().getSingleton(service);
}
@After
public void tearDown() {

@AfterEach
void tearDown() {
Service service = Service
.newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false);
ServiceManager.getInstance().removeSingleton(service);
}

@Test
public void testPostCluster() throws Exception {
void testPostCluster() throws Exception {

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());

}

@Test
public void testPostClusterWithErrorIps() throws Exception {
void testPostClusterWithErrorIps() throws Exception {
mockMvc.perform(post("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList")
.param("ips", "192.168.1")).andExpect(status().isBadRequest());
}

@Test
public void testPostClusterThrowException() throws Exception {
void testPostClusterThrowException() throws Exception {

Mockito.doThrow(new NacosException(500, "create service error")).when(clusterOperator)
.updateClusterMetadata(Mockito.eq(Constants.DEFAULT_NAMESPACE_ID), Mockito.eq(
Expand All @@ -97,34 +97,34 @@ public void testPostClusterThrowException() throws Exception {
.param("ips", "192.168.1")).andExpect(status().isInternalServerError());

}

@Test
public void testDeleteCluster() throws Exception {
void testDeleteCluster() throws Exception {
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());
}

@Test
public void testDeleteClusterCannotFindService() throws Exception {
void testDeleteClusterCannotFindService() throws Exception {
tearDown();
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());
}

@Test
public void testDeleteClusterEmptyIps() throws Exception {
void testDeleteClusterEmptyIps() throws Exception {
mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList")
.param("ips", "")).andExpect(status().isBadRequest());
}

@Test
public void testDeleteClusterErrorIps() throws Exception {
void testDeleteClusterErrorIps() throws Exception {
mockMvc.perform(delete("/nacos/v1/as/nodes").param("product", "default").param("cluster", "serverList")
.param("ips", "192.168.1")).andExpect(status().isBadRequest());
}

@Test
public void testDeleteClusterThrowException() throws Exception {
void testDeleteClusterThrowException() throws Exception {
Mockito.doThrow(new NacosException(500, "remove service error")).when(instanceOperator)
.removeInstance(Mockito.eq(Constants.DEFAULT_NAMESPACE_ID), Mockito.eq(
Constants.DEFAULT_GROUP + AddressServerConstants.GROUP_SERVICE_NAME_SEP + "nacos.as.default"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
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.pojo.Service;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.setup.MockMvcBuilders;

Expand All @@ -43,8 +43,8 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(MockitoJUnitRunner.class)
public class ServerListControllerTest {
@ExtendWith(MockitoExtension.class)
class ServerListControllerTest {

@Mock
private NamingMetadataManager metadataManager;
Expand All @@ -55,23 +55,23 @@ public class ServerListControllerTest {
private Service service;

private MockMvc mockMvc;
@Before
public void before() {

@BeforeEach
void before() {
this.mockMvc = MockMvcBuilders.standaloneSetup(
new ServerListController(new AddressServerGeneratorManager(), metadataManager, serviceStorage)).build();
service = Service
.newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false);
ServiceManager.getInstance().getSingleton(service);
}
@After
public void tearDown() {

@AfterEach
void tearDown() {
ServiceManager.getInstance().removeSingleton(service);
}

@Test
public void testGetCluster() throws Exception {
void testGetCluster() throws Exception {

final Service service = Service
.newService(Constants.DEFAULT_NAMESPACE_ID, Constants.DEFAULT_GROUP, "nacos.as.default", false);
Expand All @@ -86,16 +86,16 @@ public void testGetCluster() throws Exception {
when(serviceStorage.getData(service)).thenReturn(serviceInfo);
mockMvc.perform(get("/nacos/serverList")).andExpect(status().isOk());
}

@Test
public void testGetClusterCannotFindService() throws Exception {
void testGetClusterCannotFindService() throws Exception {
tearDown();
mockMvc.perform(get("/default/serverList")).andExpect(status().isNotFound());

}

@Test
public void testGetClusterCannotFindCluster() throws Exception {
void testGetClusterCannotFindCluster() throws Exception {
mockMvc.perform(get("/nacos/serverList")).andExpect(status().isNotFound());

}
Expand Down

0 comments on commit 4e2bcb9

Please sign in to comment.