From eaadac73f91767d6696bd63fdf7b9e253a50a51a Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Tue, 8 Sep 2020 13:05:05 +0800 Subject: [PATCH] Revise StandardBootstrapInitializerTest (#7312) --- .../FixtureConfigurationYamlSwapper.java | 4 +- .../fixture/FixtureRuleConfiguration.java | 6 +- .../StandardBootstrapInitializerTest.java | 221 ++++++++++-------- 3 files changed, 129 insertions(+), 102 deletions(-) diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/FixtureConfigurationYamlSwapper.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/FixtureConfigurationYamlSwapper.java index d3489102b7851..87ee475bfe005 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/FixtureConfigurationYamlSwapper.java +++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/FixtureConfigurationYamlSwapper.java @@ -45,8 +45,6 @@ public FixtureYamlRuleConfiguration swapToYamlConfiguration(final FixtureRuleCon @Override public FixtureRuleConfiguration swapToObject(final FixtureYamlRuleConfiguration yamlConfig) { - FixtureRuleConfiguration result = new FixtureRuleConfiguration(); - result.setName(yamlConfig.getName()); - return result; + return new FixtureRuleConfiguration(yamlConfig.getName()); } } diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/FixtureRuleConfiguration.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/FixtureRuleConfiguration.java index f77ad56728bfa..acf6f56bd728b 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/FixtureRuleConfiguration.java +++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/fixture/FixtureRuleConfiguration.java @@ -18,12 +18,12 @@ package org.apache.shardingsphere.proxy.fixture; import lombok.Getter; -import lombok.Setter; +import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.infra.config.RuleConfiguration; +@RequiredArgsConstructor @Getter -@Setter public final class FixtureRuleConfiguration implements RuleConfiguration { - private String name; + private final String name; } diff --git a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/init/impl/StandardBootstrapInitializerTest.java b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/init/impl/StandardBootstrapInitializerTest.java index 71d8b230b696d..b294dbf995917 100644 --- a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/init/impl/StandardBootstrapInitializerTest.java +++ b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/test/java/org/apache/shardingsphere/proxy/init/impl/StandardBootstrapInitializerTest.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.proxy.init.impl; -import com.google.common.collect.Lists; import org.apache.shardingsphere.infra.auth.Authentication; import org.apache.shardingsphere.infra.auth.ProxyUser; import org.apache.shardingsphere.infra.auth.yaml.config.YamlAuthenticationConfiguration; @@ -40,21 +39,22 @@ import org.junit.Test; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Properties; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; public final class StandardBootstrapInitializerTest { + private final StandardBootstrapInitializer initializer = new StandardBootstrapInitializer(); + @Before public void setUp() { ShardingSphereServiceLoader.register(YamlRuleConfigurationSwapper.class); @@ -62,115 +62,144 @@ public void setUp() { @Test public void assertGetProxyConfiguration() { - Map ruleConfigurations = generateYamlProxyRuleConfiguration(); - YamlProxyServerConfiguration serverConfiguration = generateYamlProxyServerConfiguration(); - YamlProxyConfiguration yamlConfig = mock(YamlProxyConfiguration.class); - when(yamlConfig.getRuleConfigurations()).thenReturn(ruleConfigurations); - when(yamlConfig.getServerConfiguration()).thenReturn(serverConfiguration); - ProxyConfiguration proxyConfiguration = new StandardBootstrapInitializer().getProxyConfiguration(yamlConfig); - assertSchemaDataSources(proxyConfiguration.getSchemaDataSources()); - assertSchemaRules(proxyConfiguration.getSchemaRules()); - assertAuthentication(proxyConfiguration.getAuthentication()); - assertProps(proxyConfiguration.getProps()); - } - - private Map generateYamlProxyRuleConfiguration() { - YamlDataSourceParameter yamlDataSourceParameter = new YamlDataSourceParameter(); - yamlDataSourceParameter.setUrl("jdbc:mysql://localhost:3306/demo_ds"); - yamlDataSourceParameter.setUsername("root"); - yamlDataSourceParameter.setPassword("root"); - yamlDataSourceParameter.setReadOnly(false); - yamlDataSourceParameter.setConnectionTimeoutMilliseconds(1000L); - yamlDataSourceParameter.setIdleTimeoutMilliseconds(2000L); - yamlDataSourceParameter.setMaxLifetimeMilliseconds(4000L); - yamlDataSourceParameter.setMaxPoolSize(20); - yamlDataSourceParameter.setMinPoolSize(10); - Map dataSources = new HashMap<>(); - dataSources.put("hikari", yamlDataSourceParameter); - YamlProxyRuleConfiguration yamlProxyRuleConfiguration = new YamlProxyRuleConfiguration(); - yamlProxyRuleConfiguration.setDataSources(dataSources); - FixtureYamlRuleConfiguration fixtureYamlRuleConfiguration = new FixtureYamlRuleConfiguration(); - fixtureYamlRuleConfiguration.setName("testRule"); - List rules = Lists.newArrayList(fixtureYamlRuleConfiguration); - yamlProxyRuleConfiguration.setRules(rules); - Map ruleConfigurations = new HashMap<>(); - ruleConfigurations.put("datasource-0", yamlProxyRuleConfiguration); - return ruleConfigurations; - } - - private YamlProxyServerConfiguration generateYamlProxyServerConfiguration() { - YamlProxyUserConfiguration yamlProxyUserConfiguration = new YamlProxyUserConfiguration(); - yamlProxyUserConfiguration.setPassword("root"); - yamlProxyUserConfiguration.setAuthorizedSchemas("ds-1,ds-2"); - Map users = new HashMap<>(); - users.put("root", yamlProxyUserConfiguration); - YamlAuthenticationConfiguration authentication = new YamlAuthenticationConfiguration(); - authentication.setUsers(users); - YamlProxyServerConfiguration serverConfiguration = new YamlProxyServerConfiguration(); - serverConfiguration.setAuthentication(authentication); - Properties props = new Properties(); - props.setProperty("alpha-1", "alpha-A"); - props.setProperty("beta-2", "beta-B"); - serverConfiguration.setProps(props); - return serverConfiguration; - } - - private void assertSchemaDataSources(final Map> schemaDataSources) { - assertThat(schemaDataSources.size(), is(1)); - assertTrue("there is no such key !", schemaDataSources.containsKey("datasource-0")); - Map dataSourceParameterMap = schemaDataSources.get("datasource-0"); + YamlProxyConfiguration yamlConfig = new YamlProxyConfiguration(createYamlProxyServerConfiguration(), createYamlProxyRuleConfigurationMap()); + ProxyConfiguration actual = initializer.getProxyConfiguration(yamlConfig); + assertProxyConfiguration(actual); + } + + private Map createYamlProxyRuleConfigurationMap() { + Map result = new HashMap<>(1, 1); + result.put("logic-db", createYamlProxyRuleConfiguration()); + return result; + } + + private YamlProxyRuleConfiguration createYamlProxyRuleConfiguration() { + YamlProxyRuleConfiguration result = new YamlProxyRuleConfiguration(); + result.setDataSources(createYamlDataSourceParameterMap()); + result.setRules(createYamlRuleConfigurations()); + return result; + } + + private Map createYamlDataSourceParameterMap() { + Map result = new HashMap<>(1, 1); + result.put("ds", createYamlDataSourceParameter()); + return result; + } + + private YamlDataSourceParameter createYamlDataSourceParameter() { + YamlDataSourceParameter result = new YamlDataSourceParameter(); + result.setUrl("jdbc:mysql://localhost:3306/ds"); + result.setUsername("root"); + result.setPassword("root"); + result.setReadOnly(false); + result.setConnectionTimeoutMilliseconds(1000L); + result.setIdleTimeoutMilliseconds(2000L); + result.setMaxLifetimeMilliseconds(4000L); + result.setMaxPoolSize(20); + result.setMinPoolSize(10); + return result; + } + + private Collection createYamlRuleConfigurations() { + FixtureYamlRuleConfiguration result = new FixtureYamlRuleConfiguration(); + result.setName("testRule"); + return Collections.singletonList(result); + } + + private YamlProxyServerConfiguration createYamlProxyServerConfiguration() { + YamlProxyServerConfiguration result = new YamlProxyServerConfiguration(); + result.setAuthentication(createYamlAuthenticationConfiguration()); + result.setProps(createProperties()); + return result; + } + + private YamlAuthenticationConfiguration createYamlAuthenticationConfiguration() { + Map users = new HashMap<>(1, 1); + users.put("root", createYamlProxyUserConfiguration()); + YamlAuthenticationConfiguration result = new YamlAuthenticationConfiguration(); + result.setUsers(users); + return result; + } + + private YamlProxyUserConfiguration createYamlProxyUserConfiguration() { + YamlProxyUserConfiguration result = new YamlProxyUserConfiguration(); + result.setPassword("root"); + result.setAuthorizedSchemas("ds-1,ds-2"); + return result; + } + + private Properties createProperties() { + Properties result = new Properties(); + result.setProperty("alpha-1", "alpha-A"); + result.setProperty("beta-2", "beta-B"); + return result; + } + + private void assertProxyConfiguration(final ProxyConfiguration actual) { + assertSchemaDataSources(actual.getSchemaDataSources()); + assertSchemaRules(actual.getSchemaRules()); + assertAuthentication(actual.getAuthentication()); + assertProps(actual.getProps()); + } + + private void assertSchemaDataSources(final Map> actual) { + assertThat(actual.size(), is(1)); + assertTrue(actual.containsKey("logic-db")); + Map dataSourceParameterMap = actual.get("logic-db"); assertThat(dataSourceParameterMap.size(), is(1)); - assertTrue("there is no such key !", dataSourceParameterMap.containsKey("hikari")); - DataSourceParameter dataSourceParameter = dataSourceParameterMap.get("hikari"); - assertThat(dataSourceParameter.getUrl(), is("jdbc:mysql://localhost:3306/demo_ds")); - assertThat(dataSourceParameter.getUsername(), is("root")); - assertThat(dataSourceParameter.getPassword(), is("root")); - assertThat(dataSourceParameter.isReadOnly(), is(false)); - assertThat(dataSourceParameter.getConnectionTimeoutMilliseconds(), is(1000L)); - assertThat(dataSourceParameter.getIdleTimeoutMilliseconds(), is(2000L)); - assertThat(dataSourceParameter.getMaxLifetimeMilliseconds(), is(4000L)); - assertThat(dataSourceParameter.getMaxPoolSize(), is(20)); - assertThat(dataSourceParameter.getMinPoolSize(), is(10)); - } - - private void assertSchemaRules(final Map> schemaRules) { - assertThat(schemaRules.size(), is(1)); - assertTrue("there is no such key !", schemaRules.containsKey("datasource-0")); - Collection ruleConfigurations = schemaRules.get("datasource-0"); + assertTrue(dataSourceParameterMap.containsKey("ds")); + assertDataSourceParameter(dataSourceParameterMap.get("ds")); + } + + private void assertDataSourceParameter(final DataSourceParameter actual) { + assertThat(actual.getUrl(), is("jdbc:mysql://localhost:3306/ds")); + assertThat(actual.getUsername(), is("root")); + assertThat(actual.getPassword(), is("root")); + assertFalse(actual.isReadOnly()); + assertThat(actual.getConnectionTimeoutMilliseconds(), is(1000L)); + assertThat(actual.getIdleTimeoutMilliseconds(), is(2000L)); + assertThat(actual.getMaxLifetimeMilliseconds(), is(4000L)); + assertThat(actual.getMaxPoolSize(), is(20)); + assertThat(actual.getMinPoolSize(), is(10)); + } + + private void assertSchemaRules(final Map> actual) { + assertThat(actual.size(), is(1)); + assertTrue(actual.containsKey("logic-db")); + Collection ruleConfigurations = actual.get("logic-db"); assertThat(ruleConfigurations.size(), is(1)); - RuleConfiguration ruleConfiguration = ruleConfigurations.iterator().next(); - assertThat(ruleConfiguration, instanceOf(FixtureRuleConfiguration.class)); - assertThat(((FixtureRuleConfiguration) ruleConfiguration).getName(), is("testRule")); + assertRuleConfiguration(ruleConfigurations.iterator().next()); + } + + private void assertRuleConfiguration(final RuleConfiguration actual) { + assertThat(actual, instanceOf(FixtureRuleConfiguration.class)); + assertThat(((FixtureRuleConfiguration) actual).getName(), is("testRule")); } - private void assertAuthentication(final Authentication authentication) { - assertThat(authentication.getUsers().size(), is(1)); - assertTrue("there is no such key !", authentication.getUsers().containsKey("root")); - ProxyUser proxyUser = authentication.getUsers().get("root"); + private void assertAuthentication(final Authentication actual) { + assertThat(actual.getUsers().size(), is(1)); + assertTrue(actual.getUsers().containsKey("root")); + ProxyUser proxyUser = actual.getUsers().get("root"); assertThat(proxyUser.getPassword(), is("root")); assertThat(proxyUser.getAuthorizedSchemas().size(), is(2)); - assertTrue("there is no such element !", proxyUser.getAuthorizedSchemas().contains("ds-1")); - assertTrue("there is no such element !", proxyUser.getAuthorizedSchemas().contains("ds-2")); + assertTrue(proxyUser.getAuthorizedSchemas().contains("ds-1")); + assertTrue(proxyUser.getAuthorizedSchemas().contains("ds-2")); } - private void assertProps(final Properties props) { - assertThat(props.getProperty("alpha-1"), is("alpha-A")); - assertThat(props.getProperty("beta-2"), is("beta-B")); + private void assertProps(final Properties actual) { + assertThat(actual.getProperty("alpha-1"), is("alpha-A")); + assertThat(actual.getProperty("beta-2"), is("beta-B")); } @Test public void assertDecorateSchemaContexts() { SchemaContexts schemaContexts = mock(SchemaContexts.class); - StandardBootstrapInitializer standardBootstrapInitializer = spy(StandardBootstrapInitializer.class); - SchemaContexts newSchemaContexts = standardBootstrapInitializer.decorateSchemaContexts(schemaContexts); - assertThat(schemaContexts, is(newSchemaContexts)); + assertThat(initializer.decorateSchemaContexts(schemaContexts), is(schemaContexts)); } @Test public void assertDecorateTransactionContexts() { TransactionContexts transactionContexts = mock(TransactionContexts.class); - StandardBootstrapInitializer standardBootstrapInitializer = spy(StandardBootstrapInitializer.class); - TransactionContexts newTransactionContexts = standardBootstrapInitializer.decorateTransactionContexts(transactionContexts); - assertThat(transactionContexts, is(newTransactionContexts)); + assertThat(initializer.decorateTransactionContexts(transactionContexts), is(transactionContexts)); } }