-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b05801b
commit 46564b4
Showing
3 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...test/java/com/alibaba/nacos/core/controller/compatibility/ApiCompatibilityConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
228 changes: 228 additions & 0 deletions
228
...test/java/com/alibaba/nacos/core/controller/compatibility/ApiCompatibilityFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ava/com/alibaba/nacos/core/controller/compatibility/ApiCompatibilitySpringConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/*")); | ||
} | ||
} |