Skip to content

Commit

Permalink
upgrade module naocs-console from junit4 to junit5 (#12136)
Browse files Browse the repository at this point in the history
  • Loading branch information
shalk committed May 29, 2024
1 parent 6fa3069 commit bc039bc
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@
import com.alibaba.nacos.naming.cluster.NamingReadinessCheckService;
import com.alibaba.nacos.naming.cluster.ServerStatus;
import com.alibaba.nacos.naming.cluster.ServerStatusManager;
import org.junit.After;
import org.junit.Assert;
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.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.ResponseEntity;

import java.lang.reflect.Field;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;

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

@InjectMocks
private HealthController healthController;
Expand All @@ -50,68 +50,66 @@ public class HealthControllerTest {
@Mock
private ServerStatusManager serverStatusManager;

@Before
public void setUp() {
@BeforeEach
void setUp() {
// auto register to module health checker holder.
new NamingReadinessCheckService(serverStatusManager);
new ConfigReadinessCheckService(configInfoPersistService);
}

@After
public void tearDown() throws IllegalAccessException, NoSuchFieldException {
@AfterEach
void tearDown() throws IllegalAccessException, NoSuchFieldException {
Field moduleHealthCheckersField = ModuleHealthCheckerHolder.class.getDeclaredField("moduleHealthCheckers");
moduleHealthCheckersField.setAccessible(true);
((List) moduleHealthCheckersField.get(ModuleHealthCheckerHolder.getInstance())).clear();
}

@Test
public void testLiveness() throws Exception {
void testLiveness() throws Exception {
ResponseEntity<String> response = healthController.liveness();
Assert.assertEquals(200, response.getStatusCodeValue());
assertEquals(200, response.getStatusCodeValue());
}

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

Mockito.when(configInfoPersistService.configInfoCount(any(String.class))).thenReturn(0);
Mockito.when(serverStatusManager.getServerStatus()).thenReturn(ServerStatus.UP);
ResponseEntity<String> response = healthController.readiness(null);
Assert.assertEquals(200, response.getStatusCodeValue());
Assert.assertEquals("OK", response.getBody());
assertEquals(200, response.getStatusCodeValue());
assertEquals("OK", response.getBody());
}

@Test
public void testReadinessBothFailure() {
void testReadinessBothFailure() {
// Config and Naming are not in readiness
Mockito.when(configInfoPersistService.configInfoCount(any(String.class)))
.thenThrow(new RuntimeException("HealthControllerTest.testReadiness"));
Mockito.when(serverStatusManager.getServerStatus())
.thenThrow(new RuntimeException("HealthControllerTest.testReadiness"));
Mockito.when(serverStatusManager.getServerStatus()).thenThrow(new RuntimeException("HealthControllerTest.testReadiness"));
ResponseEntity<String> response = healthController.readiness(null);
Assert.assertEquals(500, response.getStatusCodeValue());
Assert.assertEquals("naming and config not in readiness", response.getBody());
assertEquals(500, response.getStatusCodeValue());
assertEquals("naming and config not in readiness", response.getBody());
}

@Test
public void testReadinessConfigFailure() {
void testReadinessConfigFailure() {
// Config is not in readiness
Mockito.when(configInfoPersistService.configInfoCount(any(String.class)))
.thenThrow(new RuntimeException("HealthControllerTest.testReadiness"));
Mockito.when(serverStatusManager.getServerStatus()).thenReturn(ServerStatus.UP);
ResponseEntity<String> response = healthController.readiness(null);
Assert.assertEquals(500, response.getStatusCodeValue());
Assert.assertEquals("config not in readiness", response.getBody());
assertEquals(500, response.getStatusCodeValue());
assertEquals("config not in readiness", response.getBody());
}

@Test
public void testReadinessNamingFailure() {
void testReadinessNamingFailure() {
// Naming is not in readiness
Mockito.when(configInfoPersistService.configInfoCount(any(String.class))).thenReturn(0);
Mockito.when(serverStatusManager.getServerStatus())
.thenThrow(new RuntimeException("HealthControllerTest.testReadiness"));
Mockito.when(serverStatusManager.getServerStatus()).thenThrow(new RuntimeException("HealthControllerTest.testReadiness"));
ResponseEntity<String> response = healthController.readiness(null);
Assert.assertEquals(500, response.getStatusCodeValue());
Assert.assertEquals("naming not in readiness", response.getBody());
assertEquals(500, response.getStatusCodeValue());
assertEquals("naming not in readiness", response.getBody());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.core.namespace.repository.NamespacePersistService;
import com.alibaba.nacos.core.namespace.model.Namespace;
import com.alibaba.nacos.core.namespace.repository.NamespacePersistService;
import com.alibaba.nacos.core.service.NamespaceOperationService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.matches;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

@InjectMocks
private NamespaceController namespaceController;
Expand All @@ -53,13 +53,13 @@ public class NamespaceControllerTest {
@Mock
private NamespaceOperationService namespaceOperationService;

@Before
public void setUp() {
@BeforeEach
void setUp() {

}

@Test
public void testGetNamespaces() throws Exception {
void testGetNamespaces() throws Exception {
Namespace namespace = new Namespace("", "public");
when(namespaceOperationService.getNamespaceList()).thenReturn(Collections.singletonList(namespace));
RestResult<List<Namespace>> actual = namespaceController.getNamespaces();
Expand All @@ -69,20 +69,20 @@ public void testGetNamespaces() throws Exception {
}

@Test
public void testGetNamespaceByNamespaceId() throws Exception {
void testGetNamespaceByNamespaceId() throws Exception {
Namespace namespace = new Namespace("", "public", "", 0, 0, 0);
when(namespaceOperationService.getNamespace("")).thenReturn(namespace);
assertEquals(namespace, namespaceController.getNamespace(""));
}

@Test
public void testCreateNamespaceWithCustomId() throws Exception {
void testCreateNamespaceWithCustomId() throws Exception {
namespaceController.createNamespace("test-Id", "testName", "testDesc");
verify(namespaceOperationService).createNamespace("test-Id", "testName", "testDesc");
}

@Test
public void testCreateNamespaceWithIllegalName() {
void testCreateNamespaceWithIllegalName() {
assertFalse(namespaceController.createNamespace(null, "test@Name", "testDesc"));
assertFalse(namespaceController.createNamespace(null, "test#Name", "testDesc"));
assertFalse(namespaceController.createNamespace(null, "test$Name", "testDesc"));
Expand All @@ -91,21 +91,21 @@ public void testCreateNamespaceWithIllegalName() {
assertFalse(namespaceController.createNamespace(null, "test&Name", "testDesc"));
assertFalse(namespaceController.createNamespace(null, "test*Name", "testDesc"));
}

@Test
public void testCreateNamespaceWithNonUniqueId() throws Exception {
void testCreateNamespaceWithNonUniqueId() throws Exception {
when(namespacePersistService.tenantInfoCountByTenantId("test-Id")).thenReturn(1);
assertFalse(namespaceController.createNamespace("test-Id", "testNam2", "testDesc"));
}

@Test
public void testCreateNamespaceWithIllegalCustomId() throws Exception {
void testCreateNamespaceWithIllegalCustomId() throws Exception {
assertFalse(namespaceController.createNamespace("test.Id", "testName", "testDesc"));
verify(namespaceOperationService, never()).createNamespace("test.Id", "testName", "testDesc");
}

@Test
public void testCreateNamespaceWithLongCustomId() throws Exception {
void testCreateNamespaceWithLongCustomId() throws Exception {
StringBuilder longId = new StringBuilder();
for (int i = 0; i < 129; i++) {
longId.append("a");
Expand All @@ -115,22 +115,20 @@ public void testCreateNamespaceWithLongCustomId() throws Exception {
}

@Test
public void testCreateNamespaceWithAutoId() throws Exception {
void testCreateNamespaceWithAutoId() throws Exception {
assertFalse(namespaceController.createNamespace("", "testName", "testDesc"));
verify(namespaceOperationService)
.createNamespace(matches("[A-Za-z\\d]{8}-[A-Za-z\\d]{4}-[A-Za-z\\d]{4}-[A-Za-z\\d]{4}-[A-Za-z\\d]{12}"),
eq("testName"), eq("testDesc"));
verify(namespaceOperationService).createNamespace(
matches("[A-Za-z\\d]{8}-[A-Za-z\\d]{4}-[A-Za-z\\d]{4}-[A-Za-z\\d]{4}-[A-Za-z\\d]{12}"), eq("testName"), eq("testDesc"));
}

@Test
public void testCreateNamespaceFailure() throws NacosException {
when(namespaceOperationService.createNamespace(anyString(), anyString(), anyString()))
.thenThrow(new NacosException(500, "test"));
void testCreateNamespaceFailure() throws NacosException {
when(namespaceOperationService.createNamespace(anyString(), anyString(), anyString())).thenThrow(new NacosException(500, "test"));
assertFalse(namespaceController.createNamespace("", "testName", "testDesc"));
}

@Test
public void testCheckNamespaceIdExist() throws Exception {
void testCheckNamespaceIdExist() throws Exception {
when(namespacePersistService.tenantInfoCountByTenantId("public")).thenReturn(1);
when(namespacePersistService.tenantInfoCountByTenantId("123")).thenReturn(0);
assertFalse(namespaceController.checkNamespaceIdExist(""));
Expand All @@ -139,13 +137,13 @@ public void testCheckNamespaceIdExist() throws Exception {
}

@Test
public void testEditNamespace() {
void testEditNamespace() {
namespaceController.editNamespace("test-Id", "testName", "testDesc");
verify(namespaceOperationService).editNamespace("test-Id", "testName", "testDesc");
}

@Test
public void testEditNamespaceWithIllegalName() {
void testEditNamespaceWithIllegalName() {
assertFalse(namespaceController.createNamespace(null, "test@Name", "testDesc"));
assertFalse(namespaceController.createNamespace(null, "test#Name", "testDesc"));
assertFalse(namespaceController.createNamespace(null, "test$Name", "testDesc"));
Expand All @@ -156,7 +154,7 @@ public void testEditNamespaceWithIllegalName() {
}

@Test
public void deleteConfig() throws Exception {
void deleteConfig() throws Exception {
namespaceController.deleteNamespace("test-Id");
verify(namespaceOperationService).removeNamespace("test-Id");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
import com.alibaba.nacos.sys.env.Constants;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.web.MockHttpServletResponse;
Expand All @@ -35,6 +34,8 @@
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* ServerStateController unit test.
*
Expand All @@ -43,34 +44,33 @@
* @Date: 2022/8/13 10:54
* @Description: TODO
*/
@RunWith(MockitoJUnitRunner.class)
public class ServerStateControllerTest {
@ExtendWith(MockitoExtension.class)
class ServerStateControllerTest {

private static final String CONSOLE_URL = "/v1/console/server/state";

@InjectMocks
private ServerStateController serverStateController;

private MockMvc mockmvc;

private static final String CONSOLE_URL = "/v1/console/server/state";

private ConfigurableEnvironment environment;

@Before
public void setUp() {
@BeforeEach
void setUp() {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
mockmvc = MockMvcBuilders.standaloneSetup(serverStateController).build();
}

@Test
public void serverState() throws Exception {
void serverState() throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(CONSOLE_URL);
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
assertEquals(200, response.getStatus());
ObjectNode responseContent = JacksonUtils.toObj(response.getContentAsByteArray(), ObjectNode.class);
Assert.assertEquals(EnvUtil.STANDALONE_MODE_CLUSTER,
responseContent.get(Constants.STARTUP_MODE_STATE).asText());
Assert.assertEquals("null", responseContent.get(Constants.FUNCTION_MODE_STATE).asText());
Assert.assertEquals(VersionUtils.version, responseContent.get(Constants.NACOS_VERSION).asText());
assertEquals(EnvUtil.STANDALONE_MODE_CLUSTER, responseContent.get(Constants.STARTUP_MODE_STATE).asText());
assertEquals("null", responseContent.get(Constants.FUNCTION_MODE_STATE).asText());
assertEquals(VersionUtils.version, responseContent.get(Constants.NACOS_VERSION).asText());
}
}
Loading

0 comments on commit bc039bc

Please sign in to comment.