Skip to content

Commit

Permalink
Fix Unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion committed Nov 21, 2024
1 parent ffdd44a commit 6af9d2f
Showing 1 changed file with 54 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.alibaba.nacos.config.server.service.ConfigOperationService;
import com.alibaba.nacos.config.server.service.repository.ConfigInfoPersistService;
import com.alibaba.nacos.core.auth.AuthFilter;
import com.alibaba.nacos.core.code.ControllerMethodsCache;
import com.alibaba.nacos.persistence.model.Page;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -52,7 +53,9 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -91,6 +94,9 @@ class ConfigControllerV2Test {
@Mock
private AuthConfigs authConfigs;

@Mock
private ControllerMethodsCache controllerMethodsCache;

private ConfigControllerV2 configControllerV2;

private MockMvc mockmvc;
Expand Down Expand Up @@ -126,16 +132,17 @@ void testGetConfig() throws Exception {

doAnswer(x -> {
x.getArgument(1, HttpServletResponse.class).setStatus(200);
x.getArgument(1, HttpServletResponse.class).setContentType(com.alibaba.nacos.common.http.param.MediaType.APPLICATION_JSON);
x.getArgument(1, HttpServletResponse.class)
.setContentType(com.alibaba.nacos.common.http.param.MediaType.APPLICATION_JSON);
x.getArgument(1, HttpServletResponse.class).getWriter().print(JacksonUtils.toJson(stringResult));
return null;
}).when(inner).doGetConfig(any(HttpServletRequest.class), any(HttpServletResponse.class), eq(TEST_DATA_ID), eq(TEST_GROUP),
eq(TEST_NAMESPACE_ID), eq(TEST_TAG), eq(null), anyString(), eq(true));
}).when(inner).doGetConfig(any(HttpServletRequest.class), any(HttpServletResponse.class), eq(TEST_DATA_ID),
eq(TEST_GROUP), eq(TEST_NAMESPACE_ID), eq(TEST_TAG), eq(null), anyString(), eq(true));

configControllerV2.getConfig(request, response, TEST_DATA_ID, TEST_GROUP, TEST_NAMESPACE_ID, TEST_TAG);

verify(inner).doGetConfig(eq(request), eq(response), eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID), eq(TEST_TAG),
eq(null), anyString(), eq(true));
verify(inner).doGetConfig(eq(request), eq(response), eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID),
eq(TEST_TAG), eq(null), anyString(), eq(true));
JsonNode resNode = JacksonUtils.toObj(response.getContentAsString());
Integer errCode = JacksonUtils.toObj(resNode.get("code").toString(), Integer.class);
String actContent = JacksonUtils.toObj(resNode.get("data").toString(), String.class);
Expand All @@ -154,7 +161,8 @@ void testPublishConfig() throws Exception {
configForm.setContent(TEST_CONTENT);
MockHttpServletRequest request = new MockHttpServletRequest();

when(configOperationService.publishConfig(any(ConfigForm.class), any(ConfigRequestInfo.class), anyString())).thenReturn(true);
when(configOperationService.publishConfig(any(ConfigForm.class), any(ConfigRequestInfo.class),
anyString())).thenReturn(true);

Result<Boolean> booleanResult = configControllerV2.publishConfig(configForm, request);

Expand Down Expand Up @@ -196,13 +204,13 @@ void testPublishConfigWhenNameSpaceIsPublic() throws Exception {
configForm.setContent(TEST_CONTENT);
MockHttpServletRequest request = new MockHttpServletRequest();

when(configOperationService.publishConfig(any(ConfigForm.class), any(ConfigRequestInfo.class), anyString())).thenAnswer(
(Answer<Boolean>) invocation -> {
if (invocation.getArgument(0, ConfigForm.class).getNamespaceId().equals(TEST_NAMESPACE_ID)) {
return true;
}
return false;
});
when(configOperationService.publishConfig(any(ConfigForm.class), any(ConfigRequestInfo.class),
anyString())).thenAnswer((Answer<Boolean>) invocation -> {
if (invocation.getArgument(0, ConfigForm.class).getNamespaceId().equals(TEST_NAMESPACE_ID)) {
return true;
}
return false;
});

Result<Boolean> booleanResult = configControllerV2.publishConfig(configForm, request);

Expand All @@ -217,12 +225,13 @@ void testDeleteConfigWhenNameSpaceIsPublic() throws Exception {

MockHttpServletRequest request = new MockHttpServletRequest();

when(configOperationService.deleteConfig(eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID), eq(TEST_TAG), any(),
any())).thenReturn(true);
Result<Boolean> booleanResult = configControllerV2.deleteConfig(request, TEST_DATA_ID, TEST_GROUP, TEST_NAMESPACE_ID_PUBLIC,
TEST_TAG);
when(configOperationService.deleteConfig(eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID), eq(TEST_TAG),
any(), any())).thenReturn(true);
Result<Boolean> booleanResult = configControllerV2.deleteConfig(request, TEST_DATA_ID, TEST_GROUP,
TEST_NAMESPACE_ID_PUBLIC, TEST_TAG);

verify(configOperationService).deleteConfig(eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID), eq(TEST_TAG), any(), any());
verify(configOperationService).deleteConfig(eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID),
eq(TEST_TAG), any(), any());

assertEquals(ErrorCode.SUCCESS.getCode(), booleanResult.getCode());
assertTrue(booleanResult.getData());
Expand All @@ -233,12 +242,14 @@ void testDeleteConfig() throws Exception {

MockHttpServletRequest request = new MockHttpServletRequest();

when(configOperationService.deleteConfig(eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID), eq(TEST_TAG), any(),
any())).thenReturn(true);
when(configOperationService.deleteConfig(eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID), eq(TEST_TAG),
any(), any())).thenReturn(true);

Result<Boolean> booleanResult = configControllerV2.deleteConfig(request, TEST_DATA_ID, TEST_GROUP, TEST_NAMESPACE_ID, TEST_TAG);
Result<Boolean> booleanResult = configControllerV2.deleteConfig(request, TEST_DATA_ID, TEST_GROUP,
TEST_NAMESPACE_ID, TEST_TAG);

verify(configOperationService).deleteConfig(eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID), eq(TEST_TAG), any(), any());
verify(configOperationService).deleteConfig(eq(TEST_DATA_ID), eq(TEST_GROUP), eq(TEST_NAMESPACE_ID),
eq(TEST_TAG), any(), any());

assertEquals(ErrorCode.SUCCESS.getCode(), booleanResult.getCode());
assertTrue(booleanResult.getData());
Expand All @@ -258,11 +269,14 @@ void testGetConfigByDetail() throws Exception {
Map<String, Object> configAdvanceInfo = new HashMap<>(8);
configAdvanceInfo.put("content", "server.port");

when(configInfoPersistService.findConfigInfo4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(page);
when(configInfoPersistService.findConfigInfo4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(
page);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_V2_PATH + "/searchDetail")
.param("search", "accurate").param("dataId", "test").param("group", "test").param("appName", "").param("tenant", "")
.param("config_tags", "").param("pageNo", "1").param("pageSize", "10").param("config_detail", "server.port");
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(
Constants.CONFIG_CONTROLLER_V2_PATH + "/searchDetail").param("search", "accurate")
.param("dataId", "test").param("group", "test").param("appName", "").param("tenant", "")
.param("config_tags", "").param("pageNo", "1").param("pageSize", "10")
.param("config_detail", "server.port");
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
String actualValue = response.getContentAsString();

Expand Down Expand Up @@ -290,11 +304,13 @@ void testGetConfigFuzzyByDetail() throws Exception {
Map<String, Object> configAdvanceInfo = new HashMap<>(8);
configAdvanceInfo.put("content", "server.port");

when(configInfoPersistService.findConfigInfoLike4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(page);
when(configInfoPersistService.findConfigInfoLike4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(
page);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_V2_PATH + "/searchDetail")
.param("search", "blur").param("dataId", "test").param("group", "test").param("appName", "").param("tenant", "")
.param("config_tags", "").param("pageNo", "1").param("pageSize", "10").param("config_detail", "server.port");
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(
Constants.CONFIG_CONTROLLER_V2_PATH + "/searchDetail").param("search", "blur").param("dataId", "test")
.param("group", "test").param("appName", "").param("tenant", "").param("config_tags", "")
.param("pageNo", "1").param("pageSize", "10").param("config_detail", "server.port");
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
String actualValue = response.getContentAsString();

Expand All @@ -311,10 +327,14 @@ void testGetConfigFuzzyByDetail() throws Exception {
@Test
void testGetConfigAuthFilter() throws Exception {
when(authConfigs.isAuthEnabled()).thenReturn(true);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_V2_PATH + "/searchDetail")
.param("search", "accurate").param("dataId", "test").param("group", "test").param("appName", "").param("tenant", "")
.param("config_tags", "").param("pageNo", "1").param("pageSize", "10").param("config_detail", "server.port");
Method method = Arrays.stream(ConfigControllerV2.class.getMethods())
.filter(m -> m.getName().equals("searchConfigByDetails")).findFirst().get();
when(controllerMethodsCache.getMethod(any(HttpServletRequest.class))).thenReturn(method);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(
Constants.CONFIG_CONTROLLER_V2_PATH + "/searchDetail").param("search", "accurate")
.param("dataId", "test").param("group", "test").param("appName", "").param("tenant", "")
.param("config_tags", "").param("pageNo", "1").param("pageSize", "10")
.param("config_detail", "server.port");
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();

assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatus());
Expand Down

0 comments on commit 6af9d2f

Please sign in to comment.