You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2021/06/18 13:44:07 UTC
[shardingsphere] branch master updated: Refactor
DatabaseDiscoveryRuleSegment (#10871)
This is an automated email from the ASF dual-hosted git repository.
wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new e02a92a Refactor DatabaseDiscoveryRuleSegment (#10871)
e02a92a is described below
commit e02a92a0959d867853bd7c4157c1041f929ef782
Author: Liang Zhang <te...@163.com>
AuthorDate: Fri Jun 18 21:43:42 2021 +0800
Refactor DatabaseDiscoveryRuleSegment (#10871)
* For code format
* Refactor DatabaseDiscoveryRuleSegment
---
.../shardingsphere-agent-core/pom.xml | 2 +-
.../base/advice/SchemaMetaDataLoaderAdvice.java | 2 +-
...atabaseDiscoveryRuleStatementConverterTest.java | 18 +++----
.../DatabaseDiscoveryRuleSQLStatementVisitor.java | 12 ++---
.../segment/DatabaseDiscoveryRuleSegment.java | 12 ++---
...terDatabaseDiscoveryRuleBackendHandlerTest.java | 58 ++++++++--------------
...ateDatabaseDiscoveryRuleBackendHandlerTest.java | 38 ++++++--------
7 files changed, 55 insertions(+), 87 deletions(-)
diff --git a/shardingsphere-agent/shardingsphere-agent-core/pom.xml b/shardingsphere-agent/shardingsphere-agent-core/pom.xml
index abf28e2..306b9a7 100644
--- a/shardingsphere-agent/shardingsphere-agent-core/pom.xml
+++ b/shardingsphere-agent/shardingsphere-agent-core/pom.xml
@@ -40,7 +40,7 @@
<artifactId>snakeyaml</artifactId>
</dependency>
</dependencies>
-
+
<build>
<plugins>
<plugin>
diff --git a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-logging/shardingsphere-agent-logging-base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/SchemaMetaDataLoaderAdvice.java b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-logging/shardingsphere-agent-logging-base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/SchemaMetaDataLoaderAdvice.java
index 6cd4971..798f56c 100644
--- a/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-logging/shardingsphere-agent-logging-base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/SchemaMetaDataLoaderAdvice.java
+++ b/shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-logging/shardingsphere-agent-logging-base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/SchemaMetaDataLoaderAdvice.java
@@ -35,7 +35,7 @@ public final class SchemaMetaDataLoaderAdvice implements InstanceMethodAroundAdv
public void beforeMethod(final AdviceTargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
ElapsedTimeThreadLocal.INSTANCE.set(System.currentTimeMillis());
}
-
+
@Override
public void afterMethod(final AdviceTargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
try {
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/yaml/converter/DatabaseDiscoveryRuleStatementConverterTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/yaml/converter/DatabaseDiscoveryRuleStatementConverterTest.java
index 70ada8e..f2a5ab3 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/yaml/converter/DatabaseDiscoveryRuleStatementConverterTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/yaml/converter/DatabaseDiscoveryRuleStatementConverterTest.java
@@ -31,11 +31,10 @@ import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;
public final class DatabaseDiscoveryRuleStatementConverterTest {
-
+
@Test
public void assertConvert() {
- YamlDatabaseDiscoveryRuleConfiguration yamlDatabaseDiscoveryRuleConfiguration
- = DatabaseDiscoveryRuleStatementConverter.convert(buildDatabaseDiscoveryRuleSegments());
+ YamlDatabaseDiscoveryRuleConfiguration yamlDatabaseDiscoveryRuleConfiguration = DatabaseDiscoveryRuleStatementConverter.convert(buildDatabaseDiscoveryRuleSegments());
assertNotNull(yamlDatabaseDiscoveryRuleConfiguration);
assertThat(yamlDatabaseDiscoveryRuleConfiguration.getDataSources().keySet(), is(Collections.singleton("pr_ds")));
assertThat(yamlDatabaseDiscoveryRuleConfiguration.getDataSources().get("pr_ds").getDataSourceNames(), is(Arrays.asList("resource0", "resource1")));
@@ -44,15 +43,10 @@ public final class DatabaseDiscoveryRuleStatementConverterTest {
assertThat(yamlDatabaseDiscoveryRuleConfiguration.getDiscoveryTypes().get("pr_ds_MGR").getType(), is("MGR"));
assertThat(yamlDatabaseDiscoveryRuleConfiguration.getDiscoveryTypes().get("pr_ds_MGR").getProps().get("test"), is("value"));
}
-
+
private Collection<DatabaseDiscoveryRuleSegment> buildDatabaseDiscoveryRuleSegments() {
- DatabaseDiscoveryRuleSegment segment = new DatabaseDiscoveryRuleSegment();
- segment.setName("pr_ds");
- segment.setDiscoveryTypeName("MGR");
- segment.setDataSources(Arrays.asList("resource0", "resource1"));
- Properties properties = new Properties();
- properties.setProperty("test", "value");
- segment.setProps(properties);
- return Collections.singleton(segment);
+ Properties props = new Properties();
+ props.setProperty("test", "value");
+ return Collections.singleton(new DatabaseDiscoveryRuleSegment("pr_ds", Arrays.asList("resource0", "resource1"), "MGR", props));
}
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryRuleSQLStatementVisitor.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryRuleSQLSt [...]
index b684825..fff16b2 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryRuleSQLStatementVisitor.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryRuleSQLStatementVisitor.java
@@ -17,11 +17,11 @@
package org.apache.shardingsphere.dbdiscovery.distsql.parser.core;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryRuleSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.AlterDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.CreateDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.ShowDatabaseDiscoveryRulesStatement;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryRuleSegment;
import org.apache.shardingsphere.distsql.parser.autogen.DatabaseDiscoveryRuleStatementBaseVisitor;
import org.apache.shardingsphere.distsql.parser.autogen.DatabaseDiscoveryRuleStatementParser.AlgorithmDefinitionContext;
import org.apache.shardingsphere.distsql.parser.autogen.DatabaseDiscoveryRuleStatementParser.AlgorithmPropertiesContext;
@@ -39,6 +39,7 @@ import org.apache.shardingsphere.sql.parser.api.visitor.SQLVisitor;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import java.util.Collection;
import java.util.Properties;
import java.util.stream.Collectors;
@@ -69,12 +70,9 @@ public final class DatabaseDiscoveryRuleSQLStatementVisitor extends DatabaseDisc
@Override
public ASTNode visitDatabaseDiscoveryRuleDefinition(final DatabaseDiscoveryRuleDefinitionContext ctx) {
- DatabaseDiscoveryRuleSegment result = new DatabaseDiscoveryRuleSegment();
- result.setName(ctx.ruleName().getText());
- result.setDataSources(ctx.resources().resourceName().stream().map(each -> new IdentifierValue(each.getText()).getValue()).collect(Collectors.toList()));
- result.setDiscoveryTypeName(ctx.algorithmDefinition().algorithmName().getText());
- result.setProps(getAlgorithmProperties(ctx.algorithmDefinition().algorithmProperties()));
- return result;
+ Collection<String> dataSources = ctx.resources().resourceName().stream().map(each -> new IdentifierValue(each.getText()).getValue()).collect(Collectors.toList());
+ return new DatabaseDiscoveryRuleSegment(ctx.ruleName().getText(),
+ dataSources, ctx.algorithmDefinition().algorithmName().getText(), getAlgorithmProperties(ctx.algorithmDefinition().algorithmProperties()));
}
@Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryRuleSegment.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryRuleSegment.java
index 9fd8ce7..ab6be51 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryRuleSegment.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryRuleSegment.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.dbdiscovery.distsql.parser.segment;
import lombok.Getter;
-import lombok.Setter;
+import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import java.util.Collection;
@@ -27,15 +27,15 @@ import java.util.Properties;
/**
* Database discovery rule segment.
*/
+@RequiredArgsConstructor
@Getter
-@Setter
public final class DatabaseDiscoveryRuleSegment implements ASTNode {
- private String name;
+ private final String name;
- private Collection<String> dataSources;
+ private final Collection<String> dataSources;
- private String discoveryTypeName;
+ private final String discoveryTypeName;
- private Properties props;
+ private final Properties props;
}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterDatabaseDiscoveryRuleBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterDatabaseDiscoveryRuleBackendHandlerTest.java
index 487a9ba..9bb6efb 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterDatabaseDiscoveryRuleBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterDatabaseDiscoveryRuleBackendHandlerTest.java
@@ -47,6 +47,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
+import java.util.Properties;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -75,34 +76,30 @@ public final class AlterDatabaseDiscoveryRuleBackendHandlerTest {
@Mock
private ShardingSphereRuleMetaData ruleMetaData;
-
+
@Mock
private ShardingSphereResource shardingSphereResource;
-
+
@Mock
private DatabaseDiscoveryDataSourceRuleConfiguration databaseDiscoveryDataSourceRuleConfiguration;
- private AlterDatabaseDiscoveryRuleBackendHandler handler = new AlterDatabaseDiscoveryRuleBackendHandler(sqlStatement, backendConnection);
+ private final AlterDatabaseDiscoveryRuleBackendHandler handler = new AlterDatabaseDiscoveryRuleBackendHandler(sqlStatement, backendConnection);
@Before
public void setUp() {
ShardingSphereServiceLoader.register(DatabaseDiscoveryType.class);
ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
- when(metaDataContexts.getAllSchemaNames()).thenReturn(Collections.singletonList("test"));
+ when(metaDataContexts.getAllSchemaNames()).thenReturn(Collections.singleton("test"));
when(metaDataContexts.getMetaData(eq("test"))).thenReturn(shardingSphereMetaData);
when(shardingSphereMetaData.getRuleMetaData()).thenReturn(ruleMetaData);
}
@Test
public void assertExecute() {
- DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment();
- databaseDiscoveryRuleSegment.setName("ha_group");
- databaseDiscoveryRuleSegment.setDataSources(Arrays.asList("ds_0", "ds_1"));
- databaseDiscoveryRuleSegment.setDiscoveryTypeName("TEST");
- when(sqlStatement.getRules()).thenReturn(Collections.singletonList(databaseDiscoveryRuleSegment));
- when(ruleMetaData.getConfigurations()).thenReturn(Collections
- .singletonList(new DatabaseDiscoveryRuleConfiguration(new LinkedList<>(Collections
- .singleton(databaseDiscoveryDataSourceRuleConfiguration)), Maps.newHashMap())));
+ DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment("ha_group", Arrays.asList("ds_0", "ds_1"), "TEST", new Properties());
+ when(sqlStatement.getRules()).thenReturn(Collections.singleton(databaseDiscoveryRuleSegment));
+ when(ruleMetaData.getConfigurations()).thenReturn(
+ Collections.singleton(new DatabaseDiscoveryRuleConfiguration(new LinkedList<>(Collections.singleton(databaseDiscoveryDataSourceRuleConfiguration)), Maps.newHashMap())));
when(databaseDiscoveryDataSourceRuleConfiguration.getName()).thenReturn("ha_group");
when(shardingSphereMetaData.getResource()).thenReturn(shardingSphereResource);
Map<String, DataSource> dataSourceMap = mock(Map.class);
@@ -118,42 +115,31 @@ public final class AlterDatabaseDiscoveryRuleBackendHandlerTest {
when(ruleMetaData.getConfigurations()).thenReturn(Collections.emptyList());
handler.execute("test", sqlStatement);
}
-
+
@Test(expected = DatabaseDiscoveryRulesNotExistedException.class)
public void assertExecuteWithNoAlteredDatabaseDiscoveryRule() {
- DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment();
- databaseDiscoveryRuleSegment.setName("ha_group");
- databaseDiscoveryRuleSegment.setDataSources(Arrays.asList("ds_0", "ds_1"));
- databaseDiscoveryRuleSegment.setDiscoveryTypeName("TEST");
- when(sqlStatement.getRules()).thenReturn(Collections.singletonList(databaseDiscoveryRuleSegment));
- when(ruleMetaData.getConfigurations()).thenReturn(Collections.singletonList(new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), Maps.newHashMap())));
+ DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment("ha_group", Arrays.asList("ds_0", "ds_1"), "TEST", new Properties());
+ when(sqlStatement.getRules()).thenReturn(Collections.singleton(databaseDiscoveryRuleSegment));
+ when(ruleMetaData.getConfigurations()).thenReturn(Collections.singleton(new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), Collections.emptyMap())));
handler.execute("test", sqlStatement);
}
@Test(expected = ResourceNotExistedException.class)
public void assertExecuteWithNotExistResources() {
- DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment();
- databaseDiscoveryRuleSegment.setName("ha_group");
- databaseDiscoveryRuleSegment.setDataSources(Arrays.asList("ds_0", "ds_1"));
- databaseDiscoveryRuleSegment.setDiscoveryTypeName("TEST");
- when(sqlStatement.getRules()).thenReturn(Collections.singletonList(databaseDiscoveryRuleSegment));
- when(ruleMetaData.getConfigurations()).thenReturn(Collections
- .singletonList(new DatabaseDiscoveryRuleConfiguration(Collections
- .singleton(databaseDiscoveryDataSourceRuleConfiguration), Maps.newHashMap())));
+ DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment("ha_group", Arrays.asList("ds_0", "ds_1"), "TEST", new Properties());
+ when(sqlStatement.getRules()).thenReturn(Collections.singleton(databaseDiscoveryRuleSegment));
+ when(ruleMetaData.getConfigurations()).thenReturn(
+ Collections.singleton(new DatabaseDiscoveryRuleConfiguration(Collections.singleton(databaseDiscoveryDataSourceRuleConfiguration), Collections.emptyMap())));
when(databaseDiscoveryDataSourceRuleConfiguration.getName()).thenReturn("ha_group");
handler.execute("test", sqlStatement);
}
-
+
@Test(expected = InvalidDatabaseDiscoveryTypesException.class)
public void assertExecuteWithInvalidDiscoveryTypes() {
- DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment();
- databaseDiscoveryRuleSegment.setName("ha_group");
- databaseDiscoveryRuleSegment.setDataSources(Arrays.asList("ds_0", "ds_1"));
- databaseDiscoveryRuleSegment.setDiscoveryTypeName("notExistType");
- when(sqlStatement.getRules()).thenReturn(Collections.singletonList(databaseDiscoveryRuleSegment));
- when(ruleMetaData.getConfigurations()).thenReturn(Collections
- .singletonList(new DatabaseDiscoveryRuleConfiguration(Collections
- .singleton(databaseDiscoveryDataSourceRuleConfiguration), Maps.newHashMap())));
+ DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment("ha_group", Arrays.asList("ds_0", "ds_1"), "notExistType", new Properties());
+ when(sqlStatement.getRules()).thenReturn(Collections.singleton(databaseDiscoveryRuleSegment));
+ when(ruleMetaData.getConfigurations()).thenReturn(
+ Collections.singleton(new DatabaseDiscoveryRuleConfiguration(Collections.singleton(databaseDiscoveryDataSourceRuleConfiguration), Collections.emptyMap())));
when(databaseDiscoveryDataSourceRuleConfiguration.getName()).thenReturn("ha_group");
when(shardingSphereMetaData.getResource()).thenReturn(shardingSphereResource);
Map<String, DataSource> dataSourceMap = mock(Map.class);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateDatabaseDiscoveryRuleBackendHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateDatabaseDiscoveryRuleBackendHandlerTest.java
index 8e32c87..760731f 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateDatabaseDiscoveryRuleBackendHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateDatabaseDiscoveryRuleBackendHandlerTest.java
@@ -46,6 +46,7 @@ import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
+import java.util.Properties;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -74,7 +75,7 @@ public final class CreateDatabaseDiscoveryRuleBackendHandlerTest {
@Mock
private ShardingSphereRuleMetaData ruleMetaData;
-
+
@Mock
private ShardingSphereResource shardingSphereResource;
@@ -84,18 +85,15 @@ public final class CreateDatabaseDiscoveryRuleBackendHandlerTest {
public void setUp() {
ShardingSphereServiceLoader.register(DatabaseDiscoveryType.class);
ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
- when(metaDataContexts.getAllSchemaNames()).thenReturn(Collections.singletonList("test"));
+ when(metaDataContexts.getAllSchemaNames()).thenReturn(Collections.singleton("test"));
when(metaDataContexts.getMetaData(eq("test"))).thenReturn(shardingSphereMetaData);
when(shardingSphereMetaData.getRuleMetaData()).thenReturn(ruleMetaData);
}
@Test
public void assertExecute() {
- DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment();
- databaseDiscoveryRuleSegment.setName("pr_ds");
- databaseDiscoveryRuleSegment.setDataSources(Arrays.asList("ds_read_0", "ds_read_1"));
- databaseDiscoveryRuleSegment.setDiscoveryTypeName("TEST");
- when(sqlStatement.getRules()).thenReturn(Collections.singletonList(databaseDiscoveryRuleSegment));
+ DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment("pr_ds", Arrays.asList("ds_read_0", "ds_read_1"), "TEST", new Properties());
+ when(sqlStatement.getRules()).thenReturn(Collections.singleton(databaseDiscoveryRuleSegment));
when(shardingSphereMetaData.getResource()).thenReturn(shardingSphereResource);
Map<String, DataSource> dataSourceMap = mock(Map.class);
when(shardingSphereResource.getDataSources()).thenReturn(dataSourceMap);
@@ -109,32 +107,24 @@ public final class CreateDatabaseDiscoveryRuleBackendHandlerTest {
public void assertExecuteWithDuplicateRuleNames() {
DatabaseDiscoveryDataSourceRuleConfiguration databaseDiscoveryDataSourceRuleConfiguration
= new DatabaseDiscoveryDataSourceRuleConfiguration("pr_ds", Collections.emptyList(), "test");
- when(ruleMetaData.getConfigurations()).thenReturn(Collections.singletonList(new DatabaseDiscoveryRuleConfiguration(Collections
- .singleton(databaseDiscoveryDataSourceRuleConfiguration), Maps.newHashMap())));
- DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment();
- databaseDiscoveryRuleSegment.setName("pr_ds");
- databaseDiscoveryRuleSegment.setDataSources(Arrays.asList("ds_read_0", "ds_read_1"));
- databaseDiscoveryRuleSegment.setDiscoveryTypeName("TEST");
- when(sqlStatement.getRules()).thenReturn(Collections.singletonList(databaseDiscoveryRuleSegment));
+ when(ruleMetaData.getConfigurations()).thenReturn(
+ Collections.singleton(new DatabaseDiscoveryRuleConfiguration(Collections.singleton(databaseDiscoveryDataSourceRuleConfiguration), Maps.newHashMap())));
+ DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment("pr_ds", Arrays.asList("ds_read_0", "ds_read_1"), "TEST", new Properties());
+ when(sqlStatement.getRules()).thenReturn(Collections.singleton(databaseDiscoveryRuleSegment));
handler.execute("test", sqlStatement);
}
@Test(expected = ResourceNotExistedException.class)
public void assertExecuteWithNotExistResources() {
- DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment();
- databaseDiscoveryRuleSegment.setName("pr_ds");
- databaseDiscoveryRuleSegment.setDataSources(Arrays.asList("ds_read_0", "ds_read_1"));
- when(sqlStatement.getRules()).thenReturn(Collections.singletonList(databaseDiscoveryRuleSegment));
+ DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment("pr_ds", Arrays.asList("ds_read_0", "ds_read_1"), null, new Properties());
+ when(sqlStatement.getRules()).thenReturn(Collections.singleton(databaseDiscoveryRuleSegment));
handler.execute("test", sqlStatement);
}
-
+
@Test(expected = InvalidDatabaseDiscoveryTypesException.class)
public void assertExecuteWithDatabaseDiscoveryType() {
- DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment();
- databaseDiscoveryRuleSegment.setName("pr_ds");
- databaseDiscoveryRuleSegment.setDataSources(Arrays.asList("ds_read_0", "ds_read_1"));
- databaseDiscoveryRuleSegment.setDiscoveryTypeName("notExistDiscoveryType");
- when(sqlStatement.getRules()).thenReturn(Collections.singletonList(databaseDiscoveryRuleSegment));
+ DatabaseDiscoveryRuleSegment databaseDiscoveryRuleSegment = new DatabaseDiscoveryRuleSegment("pr_ds", Arrays.asList("ds_read_0", "ds_read_1"), "notExistDiscoveryType", new Properties());
+ when(sqlStatement.getRules()).thenReturn(Collections.singleton(databaseDiscoveryRuleSegment));
when(shardingSphereMetaData.getResource()).thenReturn(shardingSphereResource);
Map<String, DataSource> dataSourceMap = mock(Map.class);
when(shardingSphereResource.getDataSources()).thenReturn(dataSourceMap);