You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/04/11 05:28:25 UTC

[shardingsphere] branch master updated: Complete ShadowRuleConfigurationYamlIT (#25040)

This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 7d555f9a404 Complete ShadowRuleConfigurationYamlIT (#25040)
7d555f9a404 is described below

commit 7d555f9a4043ee283279c0a0e7978eaef2d56513
Author: Liu Xiao <42...@users.noreply.github.com>
AuthorDate: Tue Apr 11 13:28:14 2023 +0800

    Complete ShadowRuleConfigurationYamlIT (#25040)
---
 .../shadow/yaml/ShadowRuleConfigurationYamlIT.java | 92 +++++++++++++++++++++-
 1 file changed, 91 insertions(+), 1 deletion(-)

diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/ShadowRuleConfigurationYamlIT.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/ShadowRuleConfigurationYamlIT.java
index e17afeaecee..bcf7b50e98c 100644
--- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/ShadowRuleConfigurationYamlIT.java
+++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/yaml/ShadowRuleConfigurationYamlIT.java
@@ -18,8 +18,17 @@
 package org.apache.shardingsphere.shadow.yaml;
 
 import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
+import org.apache.shardingsphere.shadow.yaml.config.YamlShadowRuleConfiguration;
 import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationIT;
 
+import java.util.Arrays;
+import java.util.Optional;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
 class ShadowRuleConfigurationYamlIT extends YamlRuleConfigurationIT {
     
     ShadowRuleConfigurationYamlIT() {
@@ -28,6 +37,87 @@ class ShadowRuleConfigurationYamlIT extends YamlRuleConfigurationIT {
     
     @Override
     protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) {
-        // TODO finish assertion
+        assertDataSourceMap(actual);
+        Optional<YamlShadowRuleConfiguration> shadowRuleConfiguration = actual.getRules().stream()
+                .filter(each -> each instanceof YamlShadowRuleConfiguration).findFirst().map(optional -> (YamlShadowRuleConfiguration) optional);
+        assertTrue(shadowRuleConfiguration.isPresent());
+        assertThat(shadowRuleConfiguration.get().getTables().size(), is(3));
+        assertTOrder(shadowRuleConfiguration.get());
+        assertTOrderItem(shadowRuleConfiguration.get());
+        assertTAddress(shadowRuleConfiguration.get());
+        assertThat(shadowRuleConfiguration.get().getShadowAlgorithms().size(), is(4));
+        assertUserIdInsertMatchAlgorithm(shadowRuleConfiguration.get());
+        assertUserIdUpdateMatchAlgorithm(shadowRuleConfiguration.get());
+        assertUserIdSelectMatchAlgorithm(shadowRuleConfiguration.get());
+        assertSqlHintAlgorithm(shadowRuleConfiguration.get());
+    }
+
+    private void assertDataSourceMap(final YamlRootConfiguration actual) {
+        assertThat(actual.getDataSources().size(), is(2));
+        assertTrue(actual.getDataSources().containsKey("ds"));
+        assertTrue(actual.getDataSources().containsKey("shadow_ds"));
+    }
+
+    private void assertTOrder(final YamlShadowRuleConfiguration actual) {
+        assertThat(actual.getTables().get("t_order").getDataSourceNames().iterator().next(), is("shadowDataSource"));
+        assertThat(actual.getTables().get("t_order").getShadowAlgorithmNames().size(), is(2));
+        assertTrue(actual.getTables().get("t_order").getShadowAlgorithmNames().containsAll(Arrays.asList("user-id-insert-match-algorithm", "user-id-select-match-algorithm")));
+    }
+
+    private void assertTOrderItem(final YamlShadowRuleConfiguration actual) {
+        assertThat(actual.getTables().get("t_order_item").getDataSourceNames().iterator().next(), is("shadowDataSource"));
+        assertThat(actual.getTables().get("t_order_item").getShadowAlgorithmNames().size(), is(3));
+        assertTrue(actual.getTables().get("t_order_item").getShadowAlgorithmNames().containsAll(
+                Arrays.asList("user-id-insert-match-algorithm", "user-id-update-match-algorithm", "user-id-select-match-algorithm")));
+    }
+
+    private void assertTAddress(final YamlShadowRuleConfiguration actual) {
+        assertThat(actual.getTables().get("t_address").getDataSourceNames().iterator().next(), is("shadowDataSource"));
+        assertThat(actual.getTables().get("t_address").getShadowAlgorithmNames().size(), is(3));
+        assertTrue(actual.getTables().get("t_address").getShadowAlgorithmNames().containsAll(Arrays.asList("user-id-insert-match-algorithm", "user-id-select-match-algorithm", "sql-hint-algorithm")));
+    }
+
+    private void assertUserIdInsertMatchAlgorithm(final YamlShadowRuleConfiguration actual) {
+        assertThat(actual.getShadowAlgorithms().get("user-id-insert-match-algorithm").getType(), is("REGEX_MATCH"));
+        Properties props = actual.getShadowAlgorithms().get("user-id-insert-match-algorithm").getProps();
+        assertThat(props.size(), is(3));
+        assertTrue(props.containsKey("operation"));
+        assertThat(props.getProperty("operation"), is("insert"));
+        assertTrue(props.containsKey("column"));
+        assertThat(props.getProperty("column"), is("user_id"));
+        assertTrue(props.containsKey("regex"));
+        assertThat(props.getProperty("regex"), is("[1]"));
+    }
+
+    private void assertUserIdUpdateMatchAlgorithm(final YamlShadowRuleConfiguration actual) {
+        assertThat(actual.getShadowAlgorithms().get("user-id-update-match-algorithm").getType(), is("REGEX_MATCH"));
+        Properties props = actual.getShadowAlgorithms().get("user-id-update-match-algorithm").getProps();
+        assertThat(props.size(), is(3));
+        assertTrue(props.containsKey("operation"));
+        assertThat(props.getProperty("operation"), is("update"));
+        assertTrue(props.containsKey("column"));
+        assertThat(props.getProperty("column"), is("user_id"));
+        assertTrue(props.containsKey("regex"));
+        assertThat(props.getProperty("regex"), is("[1]"));
+    }
+
+    private void assertUserIdSelectMatchAlgorithm(final YamlShadowRuleConfiguration actual) {
+        assertThat(actual.getShadowAlgorithms().get("user-id-select-match-algorithm").getType(), is("REGEX_MATCH"));
+        Properties props = actual.getShadowAlgorithms().get("user-id-select-match-algorithm").getProps();
+        assertThat(props.size(), is(3));
+        assertTrue(props.containsKey("operation"));
+        assertThat(props.getProperty("operation"), is("select"));
+        assertTrue(props.containsKey("column"));
+        assertThat(props.getProperty("column"), is("user_id"));
+        assertTrue(props.containsKey("regex"));
+        assertThat(props.getProperty("regex"), is("[1]"));
+    }
+
+    private void assertSqlHintAlgorithm(final YamlShadowRuleConfiguration actual) {
+        assertThat(actual.getShadowAlgorithms().get("sql-hint-algorithm").getType(), is("SQL_HINT"));
+        Properties props = actual.getShadowAlgorithms().get("sql-hint-algorithm").getProps();
+        assertThat(props.size(), is(2));
+        assertTrue((boolean) props.get("shadow"));
+        assertThat(props.get("foo"), is("bar"));
     }
 }