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

menghaoran 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 09741b9  Add unit test for shardingsphere-shadow-core (#12417)
09741b9 is described below

commit 09741b92e875f7d04fa38f5f9bb5898a2a70c567
Author: liguoping <[email protected]>
AuthorDate: Wed Sep 15 13:54:36 2021 +0800

    Add unit test for shardingsphere-shadow-core (#12417)
    
    * Refactor SELECT_SQL_BY_ID_ACROSS_SINGLE_AND_SHARDING_TABLES_ORDER_BY 
statement with shardingsphere-integration-test
    
    * Add unit test for shardingsphere-shadow-core
    
    * code style check
    
    * delete the extra blank lines.
---
 .../NoteShadowAlgorithmDeterminerTest.java         | 116 +++++++++++++++++++++
 1 file changed, 116 insertions(+)

diff --git 
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminerTest.java
 
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminerTest.java
new file mode 100644
index 0000000..589bafc
--- /dev/null
+++ 
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/future/engine/determiner/algorithm/NoteShadowAlgorithmDeterminerTest.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package 
org.apache.shardingsphere.shadow.route.future.engine.determiner.algorithm;
+
+import 
org.apache.shardingsphere.shadow.algorithm.config.AlgorithmProvidedShadowRuleConfiguration;
+import 
org.apache.shardingsphere.shadow.algorithm.shadow.note.SimpleSQLNoteShadowAlgorithm;
+import 
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import 
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.api.shadow.column.ShadowOperationType;
+import org.apache.shardingsphere.shadow.api.shadow.note.NoteShadowAlgorithm;
+import 
org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowAlgorithmDeterminer;
+import 
org.apache.shardingsphere.shadow.route.future.engine.determiner.ShadowDetermineCondition;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class NoteShadowAlgorithmDeterminerTest {
+
+    private ShadowAlgorithmDeterminer shadowAlgorithmDeterminer;
+
+    @Before
+    public void init() {
+        shadowAlgorithmDeterminer = new 
NoteShadowAlgorithmDeterminer(createNoteShadowAlgorithm());
+    }
+
+    private NoteShadowAlgorithm createNoteShadowAlgorithm() {
+        NoteShadowAlgorithm<String> noteShadowAlgorithm = new 
SimpleSQLNoteShadowAlgorithm();
+        noteShadowAlgorithm.setProps(createProps());
+        noteShadowAlgorithm.init();
+        return noteShadowAlgorithm;
+    }
+
+    private Properties createProps() {
+        Properties properties = new Properties();
+        properties.setProperty("shadow", "true");
+        properties.setProperty("user_id", "1");
+        return properties;
+    }
+
+    @Test
+    public void assertIsShadow() {
+        
assertThat(shadowAlgorithmDeterminer.isShadow(createShadowDetermineCondition(), 
new ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()), "t_order"), 
is(true));
+    }
+
+    private AlgorithmProvidedShadowRuleConfiguration 
createAlgorithmProvidedShadowRuleConfiguration() {
+        AlgorithmProvidedShadowRuleConfiguration result = new 
AlgorithmProvidedShadowRuleConfiguration("shadow", Arrays.asList("ds", "ds1"), 
Arrays.asList("shadow_ds", "shadow_ds1"));
+        result.setEnable(true);
+        result.setDataSources(createDataSources());
+        result.setTables(createTables());
+        result.setShadowAlgorithms(createShadowAlgorithms());
+        return result;
+    }
+
+    private Map<String, ShadowAlgorithm> createShadowAlgorithms() {
+        Map<String, ShadowAlgorithm> result = new LinkedHashMap<>();
+        result.put("user_id-insert-regex-algorithm", 
createNoteShadowAlgorithm());
+        return result;
+    }
+
+    private Map<String, ShadowTableConfiguration> createTables() {
+        Map<String, ShadowTableConfiguration> result = new LinkedHashMap<>();
+        result.put("t_order", new 
ShadowTableConfiguration(createShadowAlgorithmNames()));
+        return result;
+    }
+
+    private Collection<String> createShadowAlgorithmNames() {
+        Collection<String> result = new LinkedList<>();
+        result.add("user_id-insert-regex-algorithm");
+        return result;
+    }
+
+    private Map<String, ShadowDataSourceConfiguration> createDataSources() {
+        Map<String, ShadowDataSourceConfiguration> result = new 
LinkedHashMap<>();
+        result.put("ds-data-source", new ShadowDataSourceConfiguration("ds", 
"ds_shadow"));
+        result.put("ds1-data-source", new ShadowDataSourceConfiguration("ds1", 
"ds1_shadow"));
+        return result;
+    }
+
+    private ShadowDetermineCondition createShadowDetermineCondition() {
+        ShadowDetermineCondition shadowDetermineCondition = new 
ShadowDetermineCondition(ShadowOperationType.INSERT);
+        shadowDetermineCondition.initSqlNotes(createSqlNotes());
+        return shadowDetermineCondition;
+    }
+
+    private Collection<String> createSqlNotes() {
+        Collection<String> result = new LinkedList<>();
+        result.add("/*user_id=1,shadow=true*/");
+        return result;
+    }
+}

Reply via email to