Skip to content

Commit

Permalink
add trpc-container test code (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
missexception authored Jun 5, 2024
1 parent a41c9aa commit a8ba175
Show file tree
Hide file tree
Showing 16 changed files with 414 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("");
}

Expand All @@ -53,7 +55,6 @@ public void testGetString() {

try {
yamlUtils.getString(properties, "string");
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
Expand All @@ -68,7 +69,6 @@ public void testGetInteger() {

try {
yamlUtils.getInteger(properties, "integer");
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
Expand All @@ -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);
}
Expand All @@ -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<String> collection = yamlUtils.getStringList(properties, "collection");
Assert.assertNotNull(collection);
try {
yamlUtils.requireMap(Arrays.asList(1, 2), "key");
} catch (Exception e) {
Assert.assertTrue(e instanceof IllegalArgumentException);
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -184,4 +183,21 @@ public void testDoubleNoElement() {
environment.getDouble("global.namespace.not.exist");
}

@Test
public void testParseMap() {
Map<String, Object> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class);
DefaultPropertySourceParser propertySourceParser = new DefaultPropertySourceParser();
Map<String, Object> flattableMap = propertySourceParser.getFlattableMap(yamlConfigMap);
Assert.assertEquals(146, flattableMap.size());
}

@Test
public void testParseFlattableMap() {
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class);
DefaultPropertySourceParser propertySourceParser = new DefaultPropertySourceParser();
Map<String, Object> stringObjectMap = propertySourceParser.parseFlattableMap(yamlConfigMap);
Assert.assertEquals(4, stringObjectMap.size());
Boolean aBoolean = Configuration.toBooleanObject(true);
Assert.assertTrue(aBoolean);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class);
List<Map<String, Object>> maps = Arrays.asList(yamlConfigMap);
Map<String, BackendConfig> stringBackendConfigMap = BackendConfigParser.parseConfigMap(maps);
Assert.assertNotNull(stringBackendConfigMap);
}

@Test
public void testParseConfig() {
Map<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("trpc_java_config.yaml", Map.class);
BackendConfig backendConfig = BackendConfigParser.parseConfig(yamlConfigMap);
Assert.assertNotNull(backendConfig);
}

}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> yamlConfigMap = YamlParser.parseAsFromClassPath("listener_default.yaml", Map.class);
YamlUtils yamlUtils = new YamlUtils("Label[]");
Map<String, Object> map = yamlUtils.getMap(yamlConfigMap, ConfigConstants.GLOBAL);
GlobalConfig globalConfig = GlobalConfigParser.parseGlobalConfig(map);
Assert.assertNotNull(globalConfig.getNamespace());
Assert.assertNotNull(globalConfig.getEnvName());
GlobalConfigParser.parseGlobalConfig(null);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit a8ba175

Please sign in to comment.