From 5e4840ed31e9221b1da0c9091dae7e67cbf85fc1 Mon Sep 17 00:00:00 2001 From: chenhao26-nineteen Date: Fri, 24 May 2024 10:54:21 +0800 Subject: [PATCH 1/6] add spring-trpc junit --- ...FilterTRpcConfigManagerCustomizerTest.java | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java new file mode 100644 index 0000000000..ef7073a3b4 --- /dev/null +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java @@ -0,0 +1,116 @@ +/* + * Tencent is pleased to support the open source community by making tRPC available. + * + * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * All rights reserved. + * + * If you have downloaded a copy of the tRPC source code from Tencent, + * please note that tRPC source code is licensed under the Apache 2.0 License, + * A copy of the Apache 2.0 License can be found in the LICENSE file. + */ + +package com.tencent.trpc.spring.context.configuration; + +import com.tencent.trpc.core.common.ConfigManager; +import com.tencent.trpc.core.common.config.ClientConfig; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import org.junit.Assert; +import org.junit.Test; + + +public class AddFilterTRpcConfigManagerCustomizerTest { + + + private static final String KEY = "OrderKey:b131d74c753540db859c626c564aa8ce"; + + @Test + public void testConstructor() { + AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); + Assert.assertNotNull(addFilterTRpcConfigManagerCustomizer); + } + + @Test + public void testAddClientFilters() { + AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); + AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addClientFilters( + "filter1", "filter2"); + Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); + } + + @Test + public void testAddServerFilters() { + AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); + AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addServerFilters( + "filter1", "filter2"); + Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); + } + + @Test + public void testCustomize() { + AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); + + ConfigManager instance = ConfigManager.getInstance(); + ClientConfig clientConfig = new ClientConfig(); + List list = Arrays.asList("filter1", "filter2"); + clientConfig.setFilters(list); + instance.setClientConfig(clientConfig); + addFilterTRpcConfigManagerCustomizer.customize(instance); + List filters = instance.getClientConfig().getFilters(); + + Assert.assertEquals(filters.size(), list.size()); + Assert.assertEquals(list, instance.getClientConfig().getFilters()); + } + + @Test + public void testGetOrder() { + AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer2(); + Assert.assertEquals(Integer.MAX_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); + Integer value = 1024; + System.setProperty(KEY, String.valueOf(value)); + Assert.assertEquals((long) value, addFilterTRpcConfigManagerCustomizer.getOrder()); + } + + static final class AddFilterTRpcConfigManagerCustomizer2 extends AddFilterTRpcConfigManagerCustomizer { + + @Override + public int getOrder() { + return System.getProperty(KEY) == null ? super.getOrder() : Integer.parseInt(System.getProperty(KEY)); + } + } + + @Test + public void testEmptyIfNull() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + + AddFilterTRpcConfigManagerCustomizer customizer = new AddFilterTRpcConfigManagerCustomizer(); + + Method method = customizer.getClass().getDeclaredMethod("emptyIfNull", List.class); + boolean accessible = method.isAccessible(); + method.setAccessible(true); + List list1 = Arrays.asList("filter1", "filter2", "filter3"); + Object invoke = method.invoke(customizer, list1); + if (invoke instanceof List) { + List resList = (List) invoke; + Assert.assertEquals(list1.size(), resList.size()); + } + method.setAccessible(accessible); + } + + @Test + public void testMerge() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + AddFilterTRpcConfigManagerCustomizer customizer = new AddFilterTRpcConfigManagerCustomizer(); + List list1 = Arrays.asList("filter1", "filter2", "filter3"); + List list2 = Arrays.asList("filter4", "filter5", "filter6"); + Method method = customizer.getClass().getDeclaredMethod("merge", List.class, List.class); + boolean accessible = method.isAccessible(); + method.setAccessible(true); + Object invoke = method.invoke(customizer, list1, list2); + if (invoke instanceof List) { + List resList = (List) invoke; + Assert.assertEquals(list1.size() + list2.size(), resList.size()); + } + method.setAccessible(accessible); + } +} From 77b1351a7f135dc23a03e51ca44dbf6329caf21d Mon Sep 17 00:00:00 2001 From: chenhao26-nineteen Date: Fri, 24 May 2024 12:54:59 +0800 Subject: [PATCH 2/6] fix code style and adjust private method call --- ...FilterTRpcConfigManagerCustomizerTest.java | 111 +++++++++++------- 1 file changed, 67 insertions(+), 44 deletions(-) diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java index ef7073a3b4..317b65c131 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java @@ -12,31 +12,46 @@ package com.tencent.trpc.spring.context.configuration; import com.tencent.trpc.core.common.ConfigManager; +import com.tencent.trpc.core.common.config.BackendConfig; import com.tencent.trpc.core.common.config.ClientConfig; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; - public class AddFilterTRpcConfigManagerCustomizerTest { - private static final String KEY = "OrderKey:b131d74c753540db859c626c564aa8ce"; + private static final String FILTER_ONE = "filter1"; + + private static final String FILTER_TWO = "filter2"; + + private static final String FILTER_THREE = "filter3"; + + private static final String FILTER_FOUR = "filter4"; + + private static final String BACKEND_MAP_KEY = "backend"; + + private static final Integer ORDER_VALUE = 1024; + + private static AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer; + + @Before + public void setUp() { + addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); + } + @Test public void testConstructor() { - AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); Assert.assertNotNull(addFilterTRpcConfigManagerCustomizer); } @Test public void testAddClientFilters() { - AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addClientFilters( - "filter1", "filter2"); + FILTER_ONE, FILTER_TWO); Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); } @@ -44,17 +59,15 @@ public void testAddClientFilters() { public void testAddServerFilters() { AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addServerFilters( - "filter1", "filter2"); + FILTER_ONE, FILTER_TWO); Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); } @Test public void testCustomize() { - AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); - ConfigManager instance = ConfigManager.getInstance(); ClientConfig clientConfig = new ClientConfig(); - List list = Arrays.asList("filter1", "filter2"); + List list = Arrays.asList(FILTER_ONE, FILTER_TWO); clientConfig.setFilters(list); instance.setClientConfig(clientConfig); addFilterTRpcConfigManagerCustomizer.customize(instance); @@ -66,51 +79,61 @@ public void testCustomize() { @Test public void testGetOrder() { - AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer2(); Assert.assertEquals(Integer.MAX_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); - Integer value = 1024; - System.setProperty(KEY, String.valueOf(value)); - Assert.assertEquals((long) value, addFilterTRpcConfigManagerCustomizer.getOrder()); + addFilterTRpcConfigManagerCustomizer = new TestAddFilterTRpcConfigManagerCustomizer(); + Assert.assertEquals((long) ORDER_VALUE, addFilterTRpcConfigManagerCustomizer.getOrder()); } - static final class AddFilterTRpcConfigManagerCustomizer2 extends AddFilterTRpcConfigManagerCustomizer { + static final class TestAddFilterTRpcConfigManagerCustomizer extends AddFilterTRpcConfigManagerCustomizer { @Override public int getOrder() { - return System.getProperty(KEY) == null ? super.getOrder() : Integer.parseInt(System.getProperty(KEY)); + return ORDER_VALUE; } } @Test - public void testEmptyIfNull() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - - AddFilterTRpcConfigManagerCustomizer customizer = new AddFilterTRpcConfigManagerCustomizer(); - - Method method = customizer.getClass().getDeclaredMethod("emptyIfNull", List.class); - boolean accessible = method.isAccessible(); - method.setAccessible(true); - List list1 = Arrays.asList("filter1", "filter2", "filter3"); - Object invoke = method.invoke(customizer, list1); - if (invoke instanceof List) { - List resList = (List) invoke; - Assert.assertEquals(list1.size(), resList.size()); - } - method.setAccessible(accessible); + public void testConstructorWithNullEmpty() { + addFilterTRpcConfigManagerCustomizer.addClientFilters(FILTER_ONE, FILTER_TWO); + addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(null, null); + + ClientConfig clientConfig = new ClientConfig(); + BackendConfig backendConfig = new BackendConfig(); + + backendConfig.setFilters(Arrays.asList(FILTER_THREE, FILTER_FOUR)); + clientConfig.getBackendConfigMap().put(BACKEND_MAP_KEY, backendConfig); + + ConfigManager configManager = ConfigManager.getInstance(); + configManager.setClientConfig(clientConfig); + + addFilterTRpcConfigManagerCustomizer.customize(configManager); + List expected = Arrays.asList(FILTER_THREE, FILTER_FOUR); + Assert.assertEquals(2, backendConfig.getFilters().size()); + Assert.assertEquals(expected, backendConfig.getFilters()); + } @Test - public void testMerge() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - AddFilterTRpcConfigManagerCustomizer customizer = new AddFilterTRpcConfigManagerCustomizer(); - List list1 = Arrays.asList("filter1", "filter2", "filter3"); - List list2 = Arrays.asList("filter4", "filter5", "filter6"); - Method method = customizer.getClass().getDeclaredMethod("merge", List.class, List.class); - boolean accessible = method.isAccessible(); - method.setAccessible(true); - Object invoke = method.invoke(customizer, list1, list2); - if (invoke instanceof List) { - List resList = (List) invoke; - Assert.assertEquals(list1.size() + list2.size(), resList.size()); - } - method.setAccessible(accessible); + public void testMerge() { + // add client filter + addFilterTRpcConfigManagerCustomizer.addClientFilters(FILTER_ONE, FILTER_TWO); + + ClientConfig clientConfig = new ClientConfig(); + BackendConfig backendConfig = new BackendConfig(); + + backendConfig.setFilters(Arrays.asList(FILTER_THREE, FILTER_FOUR)); + clientConfig.getBackendConfigMap().put(BACKEND_MAP_KEY, backendConfig); + + ConfigManager configManager = ConfigManager.getInstance(); + configManager.setClientConfig(clientConfig); + + // call customize method + addFilterTRpcConfigManagerCustomizer.customize(configManager); + + List expected = Arrays.asList(FILTER_ONE, FILTER_TWO, FILTER_THREE, FILTER_FOUR); + Assert.assertEquals(expected, backendConfig.getFilters()); + + expected = Arrays.asList(FILTER_ONE, FILTER_TWO, FILTER_THREE); + Assert.assertNotEquals(expected, backendConfig.getFilters()); } } From 31c817c668b0d4ff45f24f30fd9d6b0967bcc71a Mon Sep 17 00:00:00 2001 From: chenhao26-nineteen Date: Fri, 24 May 2024 12:56:01 +0800 Subject: [PATCH 3/6] remove no use code --- .../configuration/AddFilterTRpcConfigManagerCustomizerTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java index 317b65c131..2c1192d33a 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java @@ -22,8 +22,6 @@ public class AddFilterTRpcConfigManagerCustomizerTest { - private static final String KEY = "OrderKey:b131d74c753540db859c626c564aa8ce"; - private static final String FILTER_ONE = "filter1"; private static final String FILTER_TWO = "filter2"; From 0c47ac4c8f84e2a57f4003828fd34207516e56f4 Mon Sep 17 00:00:00 2001 From: chenhao26-nineteen Date: Fri, 24 May 2024 12:57:50 +0800 Subject: [PATCH 4/6] remove no use code and change constant --- .../AddFilterTRpcConfigManagerCustomizerTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java index 2c1192d33a..e1a3e66a40 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java @@ -106,9 +106,8 @@ public void testConstructorWithNullEmpty() { addFilterTRpcConfigManagerCustomizer.customize(configManager); List expected = Arrays.asList(FILTER_THREE, FILTER_FOUR); - Assert.assertEquals(2, backendConfig.getFilters().size()); + Assert.assertEquals(expected.size(), backendConfig.getFilters().size()); Assert.assertEquals(expected, backendConfig.getFilters()); - } @Test From 3704ad97d6b92fda173109b75d3f7ffe31f556fb Mon Sep 17 00:00:00 2001 From: chenhao26-nineteen Date: Fri, 24 May 2024 13:00:43 +0800 Subject: [PATCH 5/6] remove new instance code --- .../configuration/AddFilterTRpcConfigManagerCustomizerTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java index e1a3e66a40..b5343f8ffc 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java @@ -55,7 +55,6 @@ public void testAddClientFilters() { @Test public void testAddServerFilters() { - AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(); AddFilterTRpcConfigManagerCustomizer customizer = addFilterTRpcConfigManagerCustomizer.addServerFilters( FILTER_ONE, FILTER_TWO); Assert.assertEquals(addFilterTRpcConfigManagerCustomizer, customizer); From abd95f947cff33997ee701f03de95492fbf51bae Mon Sep 17 00:00:00 2001 From: chenhao26-nineteen Date: Fri, 24 May 2024 17:05:08 +0800 Subject: [PATCH 6/6] adjust testConstructorWithNullEmpty --- ...FilterTRpcConfigManagerCustomizerTest.java | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java index b5343f8ffc..123448802a 100644 --- a/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java +++ b/trpc-spring-support/trpc-spring/src/test/java/com/tencent/trpc/spring/context/configuration/AddFilterTRpcConfigManagerCustomizerTest.java @@ -14,6 +14,8 @@ import com.tencent.trpc.core.common.ConfigManager; import com.tencent.trpc.core.common.config.BackendConfig; import com.tencent.trpc.core.common.config.ClientConfig; +import com.tencent.trpc.core.common.config.ServerConfig; +import com.tencent.trpc.core.common.config.ServiceConfig; import java.util.Arrays; import java.util.List; import org.junit.Assert; @@ -32,6 +34,16 @@ public class AddFilterTRpcConfigManagerCustomizerTest { private static final String BACKEND_MAP_KEY = "backend"; + private static final String SERVER_FILTER_ONE = "server-filter1"; + + private static final String SERVER_FILTER_TWO = "server-filter2"; + + private static final String SERVICE_FILTER_THREE = "service-filter3"; + + private static final String SERVICE_FILTER_FOUR = "service-filter4"; + + private static final String SERVICE_BACKEND_MAP_KEY = "service-backend"; + private static final Integer ORDER_VALUE = 1024; private static AddFilterTRpcConfigManagerCustomizer addFilterTRpcConfigManagerCustomizer; @@ -91,22 +103,32 @@ public int getOrder() { @Test public void testConstructorWithNullEmpty() { - addFilterTRpcConfigManagerCustomizer.addClientFilters(FILTER_ONE, FILTER_TWO); addFilterTRpcConfigManagerCustomizer = new AddFilterTRpcConfigManagerCustomizer(null, null); + addFilterTRpcConfigManagerCustomizer.addClientFilters(FILTER_ONE, FILTER_TWO); + addFilterTRpcConfigManagerCustomizer.addServerFilters(SERVER_FILTER_ONE, SERVER_FILTER_TWO); ClientConfig clientConfig = new ClientConfig(); BackendConfig backendConfig = new BackendConfig(); - backendConfig.setFilters(Arrays.asList(FILTER_THREE, FILTER_FOUR)); clientConfig.getBackendConfigMap().put(BACKEND_MAP_KEY, backendConfig); + ServerConfig serverConfig = new ServerConfig(); + ServiceConfig serviceConfig = new ServiceConfig(); + serviceConfig.setFilters(Arrays.asList(SERVICE_FILTER_THREE, SERVICE_FILTER_FOUR)); + serverConfig.getServiceMap().put(SERVICE_BACKEND_MAP_KEY, serviceConfig); + ConfigManager configManager = ConfigManager.getInstance(); configManager.setClientConfig(clientConfig); + configManager.setServerConfig(serverConfig); addFilterTRpcConfigManagerCustomizer.customize(configManager); - List expected = Arrays.asList(FILTER_THREE, FILTER_FOUR); - Assert.assertEquals(expected.size(), backendConfig.getFilters().size()); - Assert.assertEquals(expected, backendConfig.getFilters()); + List clientExpected = Arrays.asList(FILTER_ONE, FILTER_TWO, FILTER_THREE, FILTER_FOUR); + // backendConfig filters test + Assert.assertEquals(clientExpected.size(), backendConfig.getFilters().size()); + // serviceConfig filters test + List serverExpected = Arrays.asList(SERVER_FILTER_ONE, SERVER_FILTER_TWO, SERVICE_FILTER_THREE, + SERVICE_FILTER_FOUR); + Assert.assertEquals(serverExpected.size(), serviceConfig.getFilters().size()); } @Test