Skip to content

Commit

Permalink
Add unit test for compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion committed Nov 7, 2024
1 parent b05801b commit 46564b4
Show file tree
Hide file tree
Showing 3 changed files with 363 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.core.controller.compatibility;

import com.alibaba.nacos.sys.env.EnvUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.env.MockEnvironment;

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

class ApiCompatibilityConfigTest {

@BeforeEach
void setUp() {
EnvUtil.setEnvironment(new MockEnvironment());
}

@AfterEach
void tearDown() {
EnvUtil.setEnvironment(null);
ApiCompatibilityConfig config = ApiCompatibilityConfig.getInstance();
config.setConsoleApiCompatibility(false);
config.setClientApiCompatibility(true);
config.setAdminApiCompatibility(true);
}

@Test
void testGetInstance() {
ApiCompatibilityConfig instance1 = ApiCompatibilityConfig.getInstance();
ApiCompatibilityConfig instance2 = ApiCompatibilityConfig.getInstance();
assertEquals(instance1, instance2);
}

@Test
void testGetConfigWithDefaultValues() {
ApiCompatibilityConfig config = ApiCompatibilityConfig.getInstance();
config.getConfigFromEnv();
assertTrue(config.isClientApiCompatibility());
assertFalse(config.isConsoleApiCompatibility());
assertTrue(config.isAdminApiCompatibility());
assertEquals(
"ApiCompatibilityConfig{clientApiCompatibility=true, consoleApiCompatibility=false, adminApiCompatibility=true}",
config.printConfig());
}

@Test
void testGetConfigWithCustomValues() {
MockEnvironment properties = new MockEnvironment();
properties.setProperty(ApiCompatibilityConfig.CLIENT_API_COMPATIBILITY_KEY, "false");
properties.setProperty(ApiCompatibilityConfig.CONSOLE_API_COMPATIBILITY_KEY, "true");
properties.setProperty(ApiCompatibilityConfig.ADMIN_API_COMPATIBILITY_KEY, "false");
EnvUtil.setEnvironment(properties);

ApiCompatibilityConfig config = ApiCompatibilityConfig.getInstance();
config.getConfigFromEnv();

assertFalse(config.isClientApiCompatibility());
assertTrue(config.isConsoleApiCompatibility());
assertFalse(config.isAdminApiCompatibility());
assertEquals(
"ApiCompatibilityConfig{clientApiCompatibility=false, consoleApiCompatibility=true, adminApiCompatibility=false}",
config.printConfig());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.core.controller.compatibility;

import com.alibaba.nacos.core.code.ControllerMethodsCache;
import com.alibaba.nacos.plugin.auth.constant.ApiType;
import com.alibaba.nacos.sys.env.EnvUtil;
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.jupiter.MockitoExtension;
import org.springframework.mock.env.MockEnvironment;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.matches;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class ApiCompatibilityFilterTest {

@Mock
ControllerMethodsCache methodsCache;

@Mock
HttpServletRequest servletRequest;

@Mock
HttpServletResponse servletResponse;

@Mock
FilterChain filterChain;

ApiCompatibilityFilter filter;

@BeforeEach
void setUp() {
EnvUtil.setEnvironment(new MockEnvironment());
filter = new ApiCompatibilityFilter(methodsCache);
}

@AfterEach
void tearDown() {
EnvUtil.setEnvironment(null);
ApiCompatibilityConfig config = ApiCompatibilityConfig.getInstance();
config.setConsoleApiCompatibility(false);
config.setClientApiCompatibility(true);
config.setAdminApiCompatibility(true);
}

@Test
void testDoFilterWithoutMethod() throws ServletException, IOException {
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain).doFilter(servletRequest, servletResponse);
verify(servletResponse, never()).sendError(anyInt(), anyString());
}

@Test
void testDoFilterWithoutCompatibility() throws ServletException, IOException, NoSuchMethodException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod("testDoFilterWithoutCompatibility");
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain).doFilter(servletRequest, servletResponse);
verify(servletResponse, never()).sendError(anyInt(), anyString());
}

@Test
@Compatibility(apiType = ApiType.ADMIN_API)
void testDoFilterWithAdminApiAndCompatibilityEnabled() throws ServletException, IOException, NoSuchMethodException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod(
"testDoFilterWithAdminApiAndCompatibilityEnabled");
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
ApiCompatibilityConfig.getInstance().setAdminApiCompatibility(true);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain).doFilter(servletRequest, servletResponse);
verify(servletResponse, never()).sendError(anyInt(), anyString());
}

@Test
@Compatibility(apiType = ApiType.ADMIN_API)
void testDoFilterWithAdminApiAndCompatibilityDisabled()
throws ServletException, IOException, NoSuchMethodException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod(
"testDoFilterWithAdminApiAndCompatibilityDisabled");
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
ApiCompatibilityConfig.getInstance().setAdminApiCompatibility(false);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain, never()).doFilter(servletRequest, servletResponse);
verify(servletResponse).sendError(eq(HttpServletResponse.SC_GONE),
matches(".*" + ApiCompatibilityConfig.ADMIN_API_COMPATIBILITY_KEY + ".*"));
}

@Test
@Compatibility(apiType = ApiType.ADMIN_API, alternatives = "/test/admin")
void testDoFilterWithAdminApiAndCompatibilityDisabledAndAlternatives()
throws NoSuchMethodException, ServletException, IOException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod(
"testDoFilterWithAdminApiAndCompatibilityDisabledAndAlternatives");
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
ApiCompatibilityConfig.getInstance().setAdminApiCompatibility(false);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain, never()).doFilter(servletRequest, servletResponse);
verify(servletResponse).sendError(eq(HttpServletResponse.SC_GONE), matches(".*/test/admin.*"));
}

@Test
@Compatibility(apiType = ApiType.CONSOLE_API)
void testDoFilterWithConsoleApiAndCompatibilityEnabled()
throws ServletException, IOException, NoSuchMethodException {
ApiCompatibilityConfig.getInstance().setConsoleApiCompatibility(true);
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod(
"testDoFilterWithConsoleApiAndCompatibilityEnabled");
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain).doFilter(servletRequest, servletResponse);
verify(servletResponse, never()).sendError(anyInt(), anyString());
}

@Test
@Compatibility(apiType = ApiType.CONSOLE_API)
void testDoFilterWithConsoleApiAndCompatibilityDisabled()
throws ServletException, IOException, NoSuchMethodException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod(
"testDoFilterWithConsoleApiAndCompatibilityDisabled");
ApiCompatibilityConfig.getInstance().setConsoleApiCompatibility(false);
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain, never()).doFilter(servletRequest, servletResponse);
verify(servletResponse).sendError(eq(HttpServletResponse.SC_GONE),
matches(".*" + ApiCompatibilityConfig.CONSOLE_API_COMPATIBILITY_KEY + ".*"));
}

@Test
@Compatibility(apiType = ApiType.CONSOLE_API, alternatives = "/test/console")
void testDoFilterWithConsoleApiAndCompatibilityDisabledAndAlternatives()
throws NoSuchMethodException, ServletException, IOException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod(
"testDoFilterWithConsoleApiAndCompatibilityDisabledAndAlternatives");
ApiCompatibilityConfig.getInstance().setConsoleApiCompatibility(false);
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain, never()).doFilter(servletRequest, servletResponse);
verify(servletResponse).sendError(eq(HttpServletResponse.SC_GONE), matches(".*/test/console.*"));
}

@Test
@Compatibility(apiType = ApiType.OPEN_API)
void testDoFilterWithOpenApiAndCompatibilityEnabled() throws ServletException, IOException, NoSuchMethodException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod(
"testDoFilterWithOpenApiAndCompatibilityEnabled");
ApiCompatibilityConfig.getInstance().setClientApiCompatibility(true);
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain).doFilter(servletRequest, servletResponse);
verify(servletResponse, never()).sendError(anyInt(), anyString());
}

@Test
@Compatibility(apiType = ApiType.OPEN_API)
void testDoFilterWithOpenApiAndCompatibilityDisabled() throws ServletException, IOException, NoSuchMethodException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod(
"testDoFilterWithOpenApiAndCompatibilityDisabled");
ApiCompatibilityConfig.getInstance().setClientApiCompatibility(false);
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain, never()).doFilter(servletRequest, servletResponse);
verify(servletResponse).sendError(eq(HttpServletResponse.SC_GONE),
matches(".*" + ApiCompatibilityConfig.CLIENT_API_COMPATIBILITY_KEY + ".*"));
}

@Test
@Compatibility(apiType = ApiType.OPEN_API, alternatives = "/test/client")
void testDoFilterWithOpenApiAndCompatibilityDisabledAndAlternatives()
throws NoSuchMethodException, ServletException, IOException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod(
"testDoFilterWithOpenApiAndCompatibilityDisabledAndAlternatives");
ApiCompatibilityConfig.getInstance().setClientApiCompatibility(false);
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain, never()).doFilter(servletRequest, servletResponse);
verify(servletResponse).sendError(eq(HttpServletResponse.SC_GONE), matches(".*/test/client.*"));
}

@Test
@Compatibility(apiType = ApiType.INNER_API)
void testDoFilterWithInnerApi() throws NoSuchMethodException, ServletException, IOException {
Method method = ApiCompatibilityFilterTest.class.getDeclaredMethod("testDoFilterWithInnerApi");
when(methodsCache.getMethod(servletRequest)).thenReturn(method);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(filterChain).doFilter(servletRequest, servletResponse);
verify(servletResponse, never()).sendError(anyInt(), anyString());
}

@Test
void testDoFilterWithException() throws ServletException, IOException {
doThrow(new ServletException()).when(filterChain).doFilter(servletRequest, servletResponse);
filter.doFilter(servletRequest, servletResponse, filterChain);
verify(servletResponse).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
eq("Handle API Compatibility failed, please see log for detail."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.core.controller.compatibility;

import com.alibaba.nacos.core.code.ControllerMethodsCache;
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.jupiter.MockitoExtension;
import org.springframework.boot.web.servlet.FilterRegistrationBean;

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

@ExtendWith(MockitoExtension.class)
class ApiCompatibilitySpringConfigTest {

@Mock
ControllerMethodsCache controllerMethodsCache;

ApiCompatibilitySpringConfig apiCompatibilitySpringConfig;

@BeforeEach
void setUp() {
apiCompatibilitySpringConfig = new ApiCompatibilitySpringConfig();
}

@Test
public void testApiCompatibilityFilterRegistration() {
ApiCompatibilityFilter apiCompatibilityFilter = apiCompatibilitySpringConfig.apiCompatibilityFilter(
controllerMethodsCache);
FilterRegistrationBean<ApiCompatibilityFilter> registrationBean = apiCompatibilitySpringConfig.apiCompatibilityFilterRegistration(
apiCompatibilityFilter);
assertEquals(5, registrationBean.getOrder());
assertTrue(registrationBean.getUrlPatterns().contains("/v1/*"));
assertTrue(registrationBean.getUrlPatterns().contains("/v2/*"));
}
}

0 comments on commit 46564b4

Please sign in to comment.