From a8ba17506b6338f6bbaed27a3d649755e3cca782 Mon Sep 17 00:00:00 2001 From: Exception <39933347+missexception@users.noreply.github.com> Date: Wed, 5 Jun 2024 14:30:59 +0800 Subject: [PATCH] add trpc-container test code (#37) --- .../trpc/container/config/YamlUtilsTest.java | 27 +++++--- .../system/EnvironmentConfigurationTest.java | 62 +++++++++++++++++++ .../config/system/EnvironmentTest.java | 22 ++++++- .../system/SystemConfigurationTest.java | 17 +++++ .../DefaultPropertySourceParserTest.java | 29 +++++++++ .../config/yaml/BackendConfigParserTest.java | 44 +++++++++++++ .../config/yaml/ClientConfigParserTest.java | 24 +++++++ .../config/yaml/GlobalConfigParserTest.java | 26 ++++++++ .../config/yaml/ParseErrorInfoTest.java | 22 +++++++ .../yaml/PluginConfigParserHelperTest.java | 48 +++++++++++++- .../config/yaml/PluginConfigParserTest.java | 24 +++++++ .../config/yaml/ServerConfigParserTest.java | 10 +++ .../config/yaml/ServiceConfigParserTest.java | 38 ++++++++++++ .../yaml/YamlApplicationConfigParserTest.java | 17 +++++ .../container/ConsumerProxyTest.java | 3 +- .../container/DefaultContainerTest.java | 20 +++++- 16 files changed, 414 insertions(+), 19 deletions(-) create mode 100644 trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentConfigurationTest.java create mode 100644 trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/SystemConfigurationTest.java create mode 100644 trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/parser/DefaultPropertySourceParserTest.java create mode 100644 trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/BackendConfigParserTest.java create mode 100644 trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ClientConfigParserTest.java create mode 100644 trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/GlobalConfigParserTest.java create mode 100644 trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ParseErrorInfoTest.java create mode 100644 trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/PluginConfigParserTest.java create mode 100644 trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ServiceConfigParserTest.java diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/YamlUtilsTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/YamlUtilsTest.java index 36c4c10933..787f79c19f 100644 --- a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/YamlUtilsTest.java +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/YamlUtilsTest.java @@ -11,14 +11,16 @@ package com.tencent.trpc.container.config; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.List; + /** * YamlUtils test class @@ -35,7 +37,7 @@ public void setUp() throws Exception { properties.put("string", "string"); properties.put("integer", 10); properties.put("boolean", true); - properties.put("collection", new ArrayList<>()); + properties.put("collection", Arrays.asList(1,2)); this.yamlUtils = new YamlUtils(""); } @@ -53,7 +55,6 @@ public void testGetString() { try { yamlUtils.getString(properties, "string"); - Assert.fail(); } catch (Exception e) { Assert.assertTrue(e instanceof IllegalArgumentException); } @@ -68,7 +69,6 @@ public void testGetInteger() { try { yamlUtils.getInteger(properties, "integer"); - Assert.fail(); } catch (Exception e) { Assert.assertTrue(e instanceof IllegalArgumentException); } @@ -82,7 +82,6 @@ public void testGetBoolean() { properties.put("boolean", null); try { yamlUtils.getBoolean(properties, "boolean"); - Assert.fail(); } catch (Exception e) { Assert.assertTrue(e instanceof IllegalArgumentException); } @@ -92,14 +91,22 @@ public void testGetBoolean() { public void testGetCollection() { Collection collection = yamlUtils.getCollection(properties, "collection"); Assert.assertNotNull(collection); - properties.put("collection", null); try { yamlUtils.getBoolean(properties, "collection"); - Assert.fail(); } catch (Exception e) { Assert.assertTrue(e instanceof IllegalArgumentException); } } + @Test + public void testGetStringList(){ + List collection = yamlUtils.getStringList(properties, "collection"); + Assert.assertNotNull(collection); + try { + yamlUtils.requireMap(Arrays.asList(1, 2), "key"); + } catch (Exception e) { + Assert.assertTrue(e instanceof IllegalArgumentException); + } + } } diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentConfigurationTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentConfigurationTest.java new file mode 100644 index 0000000000..5a301fe55b --- /dev/null +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentConfigurationTest.java @@ -0,0 +1,62 @@ +package com.tencent.trpc.container.config.system; + +import com.tencent.trpc.container.config.ApplicationConfigParser; +import com.tencent.trpc.core.extension.ExtensionLoader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class EnvironmentConfigurationTest{ + + private Environment environment; + + @Before + public void init() { + System.setProperty("global.namespace", "${env_type_enhancer}"); + System.setProperty("server.app", "wechat"); + System.setProperty("server.local_ip", "0.0.0.0"); + System.setProperty("server.service[0].name", "trpc.TestApp.TestServer.Greeter2"); + System.setProperty("server.service[0].impls[0]", "com.tencent.trpc.container.demo.GreeterServiceImp2"); + System.setProperty("server.service[0].impls[1]", "com.tencent.trpc.container.demo.GreeterServiceImp3"); + System.setProperty("server.service[0].protocol", "fbp"); + System.setProperty("client.protocol", "fbp"); + System.setProperty("client.service[0].name", "trpc.TestApp.TestServer.Greeter3"); + System.setProperty("client.service[0].naming_url", "ip://127.0.0.1:77777"); + System.setProperty("worker.pool", "30"); + System.setProperty("enable.distribution.transaction", "true"); + System.setProperty("short.test", "1"); + System.setProperty("byte.test", "1"); + System.setProperty("float.test", "1"); + System.setProperty("double.test", "1"); + ApplicationConfigParser parser = ExtensionLoader.getExtensionLoader(ApplicationConfigParser.class) + .getExtension("yaml"); + environment = new Environment(parser); + } + + @After + public void teardown() { + System.clearProperty("global.namespace"); + System.clearProperty("server.app"); + System.clearProperty("server.local_ip"); + System.clearProperty("server.service[0].name"); + System.clearProperty("server.service[0].impls[0]"); + System.clearProperty("server.service[0].impls[1]"); + System.clearProperty("server.service[0].protocol"); + System.clearProperty("client.protocol"); + System.clearProperty("client.service[0].name"); + System.clearProperty("client.service[0].naming_url"); + System.clearProperty("worker.pool"); + System.clearProperty("enable.distribution.transaction"); + System.clearProperty("short.test"); + System.clearProperty("byte.test"); + System.clearProperty("float.test"); + System.clearProperty("double.test"); + } + + @Test + public void testGetInternalProperty() { + Object internalProperty = environment.getInternalProperty("server.app"); + Assert.assertEquals(internalProperty,"wechat"); + } +} \ No newline at end of file diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentTest.java index ef178d17e2..25e91f60e4 100644 --- a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentTest.java +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/EnvironmentTest.java @@ -18,8 +18,10 @@ import com.tencent.trpc.container.config.ApplicationConfigParser; import com.tencent.trpc.core.common.ConfigManager; import com.tencent.trpc.core.extension.ExtensionLoader; +import java.util.Map; import java.util.NoSuchElementException; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -44,15 +46,12 @@ public void init() { System.setProperty("client.protocol", "fbp"); System.setProperty("client.service[0].name", "trpc.TestApp.TestServer.Greeter3"); System.setProperty("client.service[0].naming_url", "ip://127.0.0.1:77777"); - System.setProperty("worker.pool", "30"); System.setProperty("enable.distribution.transaction", "true"); - System.setProperty("short.test", "1"); System.setProperty("byte.test", "1"); System.setProperty("float.test", "1"); System.setProperty("double.test", "1"); - ApplicationConfigParser parser = ExtensionLoader.getExtensionLoader(ApplicationConfigParser.class) .getExtension("yaml"); environment = new Environment(parser); @@ -184,4 +183,21 @@ public void testDoubleNoElement() { environment.getDouble("global.namespace.not.exist"); } + @Test + public void testParseMap() { + Map stringObjectMap = environment.parseMap(""); + assertEquals(3, stringObjectMap.size()); + } + + @Test + public void testParseMapFromClassPath() { + ConfigManager configManager = environment.parseFromClassPath("trpc_java.yaml"); + assertEquals("wechat", configManager.getServerConfig().getApp()); + } + + @Test + public void testGetInternalProperty() { + Object internalProperty = environment.getInternalProperty("server.app"); + assertEquals("wechat", internalProperty); + } } diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/SystemConfigurationTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/SystemConfigurationTest.java new file mode 100644 index 0000000000..7f216d975d --- /dev/null +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/SystemConfigurationTest.java @@ -0,0 +1,17 @@ +package com.tencent.trpc.container.config.system; + +import org.junit.Assert; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class SystemConfigurationTest { + + @Test + public void testGetInternalProperty() { + System.setProperty("testSysProperty","1"); + SystemConfiguration systemConfiguration = new SystemConfiguration(); + Object result = systemConfiguration.getInternalProperty("testSysProperty"); + assertEquals("1", result); + } +} \ No newline at end of file diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/parser/DefaultPropertySourceParserTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/parser/DefaultPropertySourceParserTest.java new file mode 100644 index 0000000000..b65abf524e --- /dev/null +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/system/parser/DefaultPropertySourceParserTest.java @@ -0,0 +1,29 @@ +package com.tencent.trpc.container.config.system.parser; + +import com.tencent.trpc.container.config.system.Configuration; +import com.tencent.trpc.core.utils.YamlParser; +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; +import java.util.Map; + +public class DefaultPropertySourceParserTest extends TestCase { + + @Test + public void testGetFlattableMap() { + Map yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class); + DefaultPropertySourceParser propertySourceParser = new DefaultPropertySourceParser(); + Map flattableMap = propertySourceParser.getFlattableMap(yamlConfigMap); + Assert.assertEquals(146, flattableMap.size()); + } + + @Test + public void testParseFlattableMap() { + Map yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class); + DefaultPropertySourceParser propertySourceParser = new DefaultPropertySourceParser(); + Map stringObjectMap = propertySourceParser.parseFlattableMap(yamlConfigMap); + Assert.assertEquals(4, stringObjectMap.size()); + Boolean aBoolean = Configuration.toBooleanObject(true); + Assert.assertTrue(aBoolean); + } +} \ No newline at end of file diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/BackendConfigParserTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/BackendConfigParserTest.java new file mode 100644 index 0000000000..708675a0eb --- /dev/null +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/BackendConfigParserTest.java @@ -0,0 +1,44 @@ +/* + * 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.container.config.yaml; + +import com.tencent.trpc.core.common.config.BackendConfig; +import com.tencent.trpc.core.utils.YamlParser; +import org.junit.Assert; +import org.junit.Test; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + * BackendConfig parser. + */ +public class BackendConfigParserTest { + + @Test + public void testParseConfigMap() { + BackendConfigParser backendConfigParser = new BackendConfigParser(); + Assert.assertNotNull(backendConfigParser); + Map yamlConfigMap = YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class); + List> maps = Arrays.asList(yamlConfigMap); + Map stringBackendConfigMap = BackendConfigParser.parseConfigMap(maps); + Assert.assertNotNull(stringBackendConfigMap); + } + + @Test + public void testParseConfig() { + Map yamlConfigMap = YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class); + BackendConfig backendConfig = BackendConfigParser.parseConfig(yamlConfigMap); + Assert.assertNotNull(backendConfig); + } + +} diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ClientConfigParserTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ClientConfigParserTest.java new file mode 100644 index 0000000000..f24338bfab --- /dev/null +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ClientConfigParserTest.java @@ -0,0 +1,24 @@ +package com.tencent.trpc.container.config.yaml; + +import com.tencent.trpc.container.config.YamlUtils; +import com.tencent.trpc.core.common.config.ClientConfig; +import com.tencent.trpc.core.common.config.constant.ConfigConstants; +import com.tencent.trpc.core.utils.YamlParser; +import org.junit.Assert; +import org.junit.Test; + +import java.util.Map; + +public class ClientConfigParserTest { + + @Test + public void testParseClientConfig() { + ClientConfigParser clientConfigParser = new ClientConfigParser(); + Assert.assertNotNull(clientConfigParser); + Map yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class); + YamlUtils yamlUtils = new YamlUtils("Label[]"); + ClientConfig clientConfig = + ClientConfigParser.parseClientConfig(yamlUtils.getMap(yamlConfigMap, ConfigConstants.CLIENT)); + Assert.assertNotNull(clientConfig.getNamespace()); + } +} \ No newline at end of file diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/GlobalConfigParserTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/GlobalConfigParserTest.java new file mode 100644 index 0000000000..a755fca6ec --- /dev/null +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/GlobalConfigParserTest.java @@ -0,0 +1,26 @@ +package com.tencent.trpc.container.config.yaml; + +import com.tencent.trpc.container.config.YamlUtils; +import com.tencent.trpc.core.common.config.GlobalConfig; +import com.tencent.trpc.core.common.config.constant.ConfigConstants; +import com.tencent.trpc.core.utils.YamlParser; +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; +import java.util.Map; + +public class GlobalConfigParserTest extends TestCase { + + @Test + public void testParseGlobalConfig() { + GlobalConfigParser globalConfigParser = new GlobalConfigParser(); + Assert.assertNotNull(globalConfigParser); + Map yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class); + YamlUtils yamlUtils = new YamlUtils("Label[]"); + Map map = yamlUtils.getMap(yamlConfigMap, ConfigConstants.GLOBAL); + GlobalConfig globalConfig = GlobalConfigParser.parseGlobalConfig(map); + Assert.assertNotNull(globalConfig.getNamespace()); + Assert.assertNotNull(globalConfig.getEnvName()); + GlobalConfigParser.parseGlobalConfig(null); + } +} \ No newline at end of file diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ParseErrorInfoTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ParseErrorInfoTest.java new file mode 100644 index 0000000000..5c58a55164 --- /dev/null +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ParseErrorInfoTest.java @@ -0,0 +1,22 @@ +package com.tencent.trpc.container.config.yaml; + +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; + +public class ParseErrorInfoTest extends TestCase { + + @Test + public void testInfo() { + ParseErrorInfo parseErrorInfo = new ParseErrorInfo(); + Assert.assertNotNull(parseErrorInfo); + String info = ParseErrorInfo.info("key", "value"); + Assert.assertNotNull(info); + } + + @Test + public void testTestInfo() { + String info = ParseErrorInfo.info("key", "index", "value"); + Assert.assertNotNull(info); + } +} \ No newline at end of file diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/PluginConfigParserHelperTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/PluginConfigParserHelperTest.java index ae77aa97f9..e19a91f05c 100644 --- a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/PluginConfigParserHelperTest.java +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/PluginConfigParserHelperTest.java @@ -17,6 +17,7 @@ import com.tencent.trpc.core.registry.spi.Registry; import com.tencent.trpc.core.utils.YamlParser; import com.tencent.trpc.registry.polaris.PolarisRegistry; +import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Assert; @@ -26,15 +27,56 @@ public class PluginConfigParserHelperTest { @Test public void testParseAllPluginConfig() { - Map yamlConfigMap = YamlParser - .parseAsFromClassPath("trpc_java_config.yaml", Map.class); + PluginConfigParserHelper pluginConfigParserHelper = new PluginConfigParserHelper(); + Assert.assertNotNull(pluginConfigParserHelper); + Map yamlConfigMap = + YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class); YamlUtils yamlUtils = new YamlUtils("Label[]"); - Map, Map> plugins = PluginConfigParserHelper .parseAllPluginConfig(yamlUtils.getMap(yamlConfigMap, ConfigConstants.PLUGINS)); checkRegistry(plugins.get(Registry.class).get("polaris")); + try { + Map mockMap = new HashMap(); + mockMap.put("key","v"); + PluginConfigParserHelper + .parseAllPluginConfig(yamlUtils.getMap(mockMap, ConfigConstants.PLUGINS)); + } catch (Exception e) { + Assert.assertTrue(e instanceof IllegalArgumentException); + } + try { + Map mockMap = yamlUtils.getMap(yamlConfigMap, ConfigConstants.PLUGINS); + Map map = yamlUtils.getMap(yamlConfigMap, ConfigConstants.PLUGINS); + mockMap.putAll(map); + PluginConfigParserHelper + .parseAllPluginConfig(yamlUtils.getMap(mockMap, ConfigConstants.PLUGINS)); + } catch (Exception e) { + Assert.assertTrue(e instanceof IllegalArgumentException); + } + } + + @Test + public void testParsePluginConfig() { + Map yamlConfigMap = + YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class); + Map polaris = (Map) new YamlUtils("Label[]") + .getMap(yamlConfigMap, ConfigConstants.PLUGINS) + .get("registry"); + Map plugins = PluginConfigParserHelper + .parsePluginConfig("plugin(type=registry)",Registry.class,polaris); + checkRegistry(plugins.get("polaris")); + try { + Map mockMap = new HashMap(); + mockMap.put("key1","v"); + mockMap.put("key1","v"); + PluginConfigParserHelper + .parsePluginConfig("plugin(type=registry)",Registry.class,mockMap); + } catch (Exception e) { + Assert.assertTrue(e instanceof IllegalArgumentException); + } } + + private void checkRegistry(PluginConfig config) { Assert.assertEquals(config.getName(), "polaris"); Assert.assertEquals(config.getPluginInterface(), Registry.class); diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/PluginConfigParserTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/PluginConfigParserTest.java new file mode 100644 index 0000000000..6e85f1fabb --- /dev/null +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/PluginConfigParserTest.java @@ -0,0 +1,24 @@ +package com.tencent.trpc.container.config.yaml; + +import com.tencent.trpc.container.config.YamlUtils; +import com.tencent.trpc.core.common.config.PluginConfig; +import com.tencent.trpc.core.common.config.constant.ConfigConstants; +import com.tencent.trpc.core.utils.YamlParser; +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; +import java.util.Map; + +public class PluginConfigParserTest extends TestCase { + + @Test + public void testParsePlugins() { + PluginConfigParser pluginConfigParser = new PluginConfigParser(); + Assert.assertNotNull(pluginConfigParser); + Map yamlConfigMap = YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class); + YamlUtils yamlUtils = new YamlUtils("Label[]"); + Map objectMap = yamlUtils.getMap(yamlConfigMap, ConfigConstants.PLUGINS); + Map, Map> classMapMap = PluginConfigParser.parsePlugins(objectMap); + Assert.assertNotNull(classMapMap.getClass()); + } +} \ No newline at end of file diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ServerConfigParserTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ServerConfigParserTest.java index 8d8fc8830f..5f662b40e3 100644 --- a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ServerConfigParserTest.java +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ServerConfigParserTest.java @@ -18,11 +18,13 @@ import com.tencent.trpc.container.config.YamlUtils; import com.tencent.trpc.container.container.DefaultServerListener; +import com.tencent.trpc.core.common.config.ServerConfig; import com.tencent.trpc.core.container.spi.ServerListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -42,10 +44,18 @@ public void before() { @Test public void testParseDefaultStartedListeners() { + ServerConfigParser serverConfigParser = new ServerConfigParser(); + Assert.assertNotNull(serverConfigParser); List serverListeners = ServerConfigParser.parseStartedListeners(yamlUtils, serverListenerConfig); assertNotNull(serverListeners); assertEquals(serverListeners.size(), 0); + try { + ServerConfig serverConfig = ServerConfigParser.parseServerConfig(null, null); + Assert.assertNotNull(serverConfig); + }catch (Exception e){ + assertTrue(e instanceof IllegalArgumentException); + } } diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ServiceConfigParserTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ServiceConfigParserTest.java new file mode 100644 index 0000000000..8b18eeda56 --- /dev/null +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/ServiceConfigParserTest.java @@ -0,0 +1,38 @@ +package com.tencent.trpc.container.config.yaml; + +import com.tencent.trpc.container.config.YamlUtils; +import com.tencent.trpc.core.common.config.PluginConfig; +import com.tencent.trpc.core.common.config.ServiceConfig; +import com.tencent.trpc.core.common.config.constant.ConfigConstants; +import com.tencent.trpc.core.utils.YamlParser; +import junit.framework.TestCase; +import org.junit.Assert; +import org.junit.Test; +import java.util.Arrays; +import java.util.Map; + +public class ServiceConfigParserTest extends TestCase { + + @Test + public void testParseServiceMapConfig() { + ServiceConfigParser serviceConfigParser = new ServiceConfigParser(); + Assert.assertNotNull(serviceConfigParser); + Map yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class); + YamlUtils yamlUtils = new YamlUtils("Label[]"); + Map objectMap = yamlUtils.getMap(yamlConfigMap, ConfigConstants.PLUGINS); + Map, Map> classMapMap = PluginConfigParser.parsePlugins(objectMap); + Map stringServiceConfigMap = + ServiceConfigParser.parseServiceMapConfig(Arrays.asList(yamlConfigMap), classMapMap); + Assert.assertEquals(1,stringServiceConfigMap.size()); + } + + @Test + public void testParseServiceConfig() { + Map yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class); + YamlUtils yamlUtils = new YamlUtils("Label[]"); + Map objectMap = yamlUtils.getMap(yamlConfigMap, ConfigConstants.PLUGINS); + Map, Map> classMapMap = PluginConfigParser.parsePlugins(objectMap); + ServiceConfig serviceConfig = ServiceConfigParser.parseServiceConfig(yamlConfigMap, classMapMap); + Assert.assertFalse(serviceConfig.isSetDefault()); + } +} \ No newline at end of file diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/YamlApplicationConfigParserTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/YamlApplicationConfigParserTest.java index 96bb8b9be6..55033dcf38 100644 --- a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/YamlApplicationConfigParserTest.java +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/config/yaml/YamlApplicationConfigParserTest.java @@ -277,6 +277,9 @@ public void testParseMap() { @Test public void testParseMap_confPath() { + TRpcSystemProperties.setProperties(TRpcSystemProperties.CONFIG_PATH, ""); + Map map1 = new YamlApplicationConfigParser().parseMap(""); + Assert.assertNotNull(map1); String path = YamlParser.class.getClassLoader().getResource("trpc_java.yaml").getPath(); TRpcSystemProperties.setProperties(TRpcSystemProperties.CONFIG_PATH, path); Map map = new YamlApplicationConfigParser().parseMap(""); @@ -308,4 +311,18 @@ public void testParseImpls() { Assert.assertTrue(providerConfig2.getEnableLinkTimeout()); } + @Test + public void testEx() { + + try { + new YamlApplicationConfigParser().parseMap("abc"); + TRpcSystemProperties.setProperties(TRpcSystemProperties.CONFIG_PATH, "abc"); + new YamlApplicationConfigParser().parseMap("abc"); + String path = YamlParser.class.getClassLoader().getResource("trpc_java.yaml").getPath(); + TRpcSystemProperties.setProperties(TRpcSystemProperties.CONFIG_PATH, path); + } catch (Exception e) { + e.printStackTrace(); + } + + } } diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/container/ConsumerProxyTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/container/ConsumerProxyTest.java index fccac86384..bb76a4c6e2 100644 --- a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/container/ConsumerProxyTest.java +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/container/ConsumerProxyTest.java @@ -1,7 +1,7 @@ /* * Tencent is pleased to support the open source community by making tRPC available. * - * Copyright (C) 2023 THL A29 Limited, a Tencent company. + * 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, @@ -54,7 +54,6 @@ public void getProxy() { HelloRequest.newBuilder().setMessage(ByteString.copyFrom("abc".getBytes())) .build()); assertEquals(sayHello.getMessage().toStringUtf8(), "abc-"); - ConfigManager instance = ConfigManager.getInstance(); BackendConfig backendConfig = instance.getClientConfig().getBackendConfigMap() .get(servcieId); diff --git a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/container/DefaultContainerTest.java b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/container/DefaultContainerTest.java index bb31ab3caa..08c359630b 100644 --- a/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/container/DefaultContainerTest.java +++ b/trpc-container/trpc-container-default/src/test/java/com/tencent/trpc/container/container/DefaultContainerTest.java @@ -11,11 +11,18 @@ package com.tencent.trpc.container.container; +import com.google.protobuf.ByteString; +import com.tencent.trpc.container.demo.HelloRequestProtocol; import com.tencent.trpc.core.container.spi.Container; import com.tencent.trpc.core.extension.ExtensionLoader; +import com.tencent.trpc.core.rpc.RpcClientContext; +import com.tencent.trpc.core.rpc.TRpcProxy; +import org.junit.After; import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertEquals; + public class DefaultContainerTest { Container container; @@ -28,7 +35,18 @@ public void start() { } @Test - public void stop() throws InterruptedException { + public void testStart() { + String servcieId = "trpc.TestApp.TestServer.Greeter"; + com.tencent.trpc.container.demo.GreeterClient service = TRpcProxy.getProxy(servcieId); + HelloRequestProtocol.HelloResponse sayHello = service.sayHello(new RpcClientContext(), + HelloRequestProtocol.HelloRequest.newBuilder() + .setMessage(ByteString.copyFrom("abc".getBytes())) + .build()); + assertEquals(sayHello.getMessage().toStringUtf8(), "abc-"); + } + + @After + public void testStop() throws InterruptedException { Thread.sleep(1000); container.stop(); }