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 6418588ebb4 Add more test cases on RepositoryTupleSwapperEngine 
(#32464)
6418588ebb4 is described below

commit 6418588ebb4e244379225f29f935c484b4c9b2b5
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Aug 11 16:40:44 2024 +0800

    Add more test cases on RepositoryTupleSwapperEngine (#32464)
    
    * Add more test cases on RepositoryTupleSwapperEngine
    
    * Add more test cases on RepositoryTupleSwapperEngine
    
    * Add more test cases on RepositoryTupleSwapperEngine
    
    * Add more test cases on RepositoryTupleSwapperEngine
    
    * Add more test cases on RepositoryTupleSwapperEngine
    
    * Add more test cases on RepositoryTupleSwapperEngine
---
 .../tuple/RepositoryTupleSwapperEngineTest.java    | 85 ++++++++++++++++++++--
 .../LeafRuleConfiguration.java}                    | 23 ++----
 .../leaf/LeafYamlRuleConfigurationSwapper.java     | 48 ++++++++++++
 .../fixture/node/NodeYamlRuleConfiguration.java    |  9 ++-
 .../fixture/node/RuleNodePathProviderFixture.java  |  2 +-
 ...onfig.swapper.rule.YamlRuleConfigurationSwapper | 18 +++++
 6 files changed, 159 insertions(+), 26 deletions(-)

diff --git 
a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngineTest.java
 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngineTest.java
index 084141b558f..3e59cedb2e9 100644
--- 
a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngineTest.java
+++ 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngineTest.java
@@ -26,6 +26,7 @@ import 
org.apache.shardingsphere.mode.tuple.fixture.none.NoneYamlRuleConfigurati
 import org.junit.jupiter.api.Test;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -54,8 +55,7 @@ class RepositoryTupleSwapperEngineTest {
     
     @Test
     void assertSwapToRepositoryTuplesWithEmptyNodeYamlRuleConfiguration() {
-        Collection<RepositoryTuple> actual = new 
RepositoryTupleSwapperEngine().swapToRepositoryTuples(new 
NodeYamlRuleConfiguration());
-        assertTrue(actual.isEmpty());
+        assertTrue(new 
RepositoryTupleSwapperEngine().swapToRepositoryTuples(new 
NodeYamlRuleConfiguration()).isEmpty());
     }
     
     @Test
@@ -72,12 +72,13 @@ class RepositoryTupleSwapperEngineTest {
         leaf.setValue("leaf");
         yamlRuleConfig.setLeaf(leaf);
         yamlRuleConfig.setGens(Collections.singleton("value"));
+        yamlRuleConfig.setGen("single_gen");
         List<RepositoryTuple> actual = new ArrayList<>(new 
RepositoryTupleSwapperEngine().swapToRepositoryTuples(yamlRuleConfig));
-        assertThat(actual.size(), is(9));
+        assertThat(actual.size(), is(10));
         assertThat(actual.get(0).getKey(), is("map_value/k"));
         assertThat(actual.get(0).getValue(), is("value: v" + 
System.lineSeparator()));
         assertThat(actual.get(1).getKey(), is("collection_value"));
-        assertThat(actual.get(1).getValue(), is("- 
!!org.apache.shardingsphere.mode.tuple.fixture.leaf.LeafYamlRuleConfiguration" 
+ System.lineSeparator() + "  value: foo" + System.lineSeparator()));
+        assertThat(actual.get(1).getValue(), is("- !LEAF" + 
System.lineSeparator() + "  value: foo" + System.lineSeparator()));
         assertThat(actual.get(2).getKey(), is("string_value"));
         assertThat(actual.get(2).getValue(), is("str"));
         assertThat(actual.get(3).getKey(), is("boolean_value"));
@@ -92,6 +93,8 @@ class RepositoryTupleSwapperEngineTest {
         assertThat(actual.get(7).getValue(), is("value: leaf" + 
System.lineSeparator()));
         assertThat(actual.get(8).getKey(), is("gens/gen: value"));
         assertThat(actual.get(8).getValue(), is("value"));
+        assertThat(actual.get(9).getKey(), is("gen"));
+        assertThat(actual.get(9).getValue(), is("single_gen"));
     }
     
     @Test
@@ -101,16 +104,84 @@ class RepositoryTupleSwapperEngineTest {
     
     @Test
     void 
assertSwapToYamlRuleConfigurationWithoutGlobalLeafYamlRuleConfiguration() {
-        assertFalse(new 
RepositoryTupleSwapperEngine().swapToYamlRuleConfiguration(
-                Collections.singleton(new 
RepositoryTuple("/rules/invalid/versions/0", "value: foo" + 
System.lineSeparator())), GlobalLeafYamlRuleConfiguration.class).isPresent());
+        assertFalse(new 
RepositoryTupleSwapperEngine().swapToYamlRuleConfiguration(Collections.singleton(new
 RepositoryTuple("invalid", "")), 
GlobalLeafYamlRuleConfiguration.class).isPresent());
     }
     
     @Test
     void 
assertSwapToYamlRuleConfigurationWithGlobalLeafYamlRuleConfiguration() {
         Optional<YamlRuleConfiguration> actual = new 
RepositoryTupleSwapperEngine().swapToYamlRuleConfiguration(
-                Collections.singleton(new 
RepositoryTuple("/rules/leaf/versions/0", "value: foo" + 
System.lineSeparator())), GlobalLeafYamlRuleConfiguration.class);
+                Collections.singleton(new 
RepositoryTuple("/rules/leaf/versions/0", "value: foo")), 
GlobalLeafYamlRuleConfiguration.class);
         assertTrue(actual.isPresent());
         GlobalLeafYamlRuleConfiguration actualYamlConfig = 
(GlobalLeafYamlRuleConfiguration) actual.get();
         assertThat(actualYamlConfig.getValue(), is("foo"));
     }
+    
+    @Test
+    void 
assertSwapToYamlRuleConfigurationWithInvalidLeafYamlRuleConfiguration() {
+        assertFalse(new 
RepositoryTupleSwapperEngine().swapToYamlRuleConfiguration(Collections.singleton(new
 RepositoryTuple("/invalid", "foo")), 
LeafYamlRuleConfiguration.class).isPresent());
+    }
+    
+    @Test
+    void 
assertSwapToYamlRuleConfigurationWithInvalidNodeYamlRuleConfiguration() {
+        Optional<YamlRuleConfiguration> actual = new 
RepositoryTupleSwapperEngine().swapToYamlRuleConfiguration(
+                Collections.singleton(new RepositoryTuple("/invalid", "foo")), 
NodeYamlRuleConfiguration.class);
+        assertFalse(actual.isPresent());
+    }
+    
+    @Test
+    void assertSwapToYamlRuleConfigurationWithEmptyNodeYamlRuleConfiguration() 
{
+        Optional<YamlRuleConfiguration> actual = new 
RepositoryTupleSwapperEngine().swapToYamlRuleConfiguration(
+                Collections.singleton(new 
RepositoryTuple("/metadata/foo_db/rules/node/string_value/versions/0", "")), 
NodeYamlRuleConfiguration.class);
+        assertTrue(actual.isPresent());
+        NodeYamlRuleConfiguration actualYamlConfig = 
(NodeYamlRuleConfiguration) actual.get();
+        assertThat(actualYamlConfig.getStringValue(), is(""));
+    }
+    
+    @Test
+    void assertSwapToYamlRuleConfigurationWithNodeYamlRuleConfiguration() {
+        Optional<YamlRuleConfiguration> actual = new 
RepositoryTupleSwapperEngine().swapToYamlRuleConfiguration(Arrays.asList(
+                new 
RepositoryTuple("/metadata/foo_db/rules/node/map_value/k/versions/0", "v"),
+                new 
RepositoryTuple("/metadata/foo_db/rules/node/collection_value/versions/0", "- 
!LEAF" + System.lineSeparator() + "  value: foo"),
+                new 
RepositoryTuple("/metadata/foo_db/rules/node/string_value/versions/0", "str"),
+                new 
RepositoryTuple("/metadata/foo_db/rules/node/boolean_value/versions/0", "true"),
+                new 
RepositoryTuple("/metadata/foo_db/rules/node/integer_value/versions/0", "1"),
+                new 
RepositoryTuple("/metadata/foo_db/rules/node/long_value/versions/0", "10"),
+                new 
RepositoryTuple("/metadata/foo_db/rules/node/enum_value/versions/0", "FOO")), 
NodeYamlRuleConfiguration.class);
+        assertTrue(actual.isPresent());
+        NodeYamlRuleConfiguration actualYamlConfig = 
(NodeYamlRuleConfiguration) actual.get();
+        assertThat(actualYamlConfig.getMapValue().size(), is(1));
+        assertThat(actualYamlConfig.getMapValue().get("k").getValue(), 
is("v"));
+        assertThat(actualYamlConfig.getCollectionValue().size(), is(1));
+        
assertThat(actualYamlConfig.getCollectionValue().iterator().next().getValue(), 
is("foo"));
+        assertThat(actualYamlConfig.getStringValue(), is("str"));
+        assertTrue(actualYamlConfig.getBooleanValue());
+        assertThat(actualYamlConfig.getIntegerValue(), is(1));
+        assertThat(actualYamlConfig.getLongValue(), is(10L));
+        assertThat(actualYamlConfig.getEnumValue(), 
is(NodeYamlRuleConfigurationEnum.FOO));
+    }
+    
+    @Test
+    void assertSwapToEmptyRuleConfigurations() {
+        assertTrue(new 
RepositoryTupleSwapperEngine().swapToRuleConfigurations(Collections.emptyList()).isEmpty());
+    }
+    
+    @Test
+    void assertSwapToRuleConfigurations() {
+        assertTrue(new 
RepositoryTupleSwapperEngine().swapToRuleConfigurations(Collections.singleton(new
 RepositoryTuple("/rules/leaf/versions/0", "value: foo"))).isEmpty());
+    }
+    
+    @Test
+    void assertSwapToEmptyRuleConfiguration() {
+        assertFalse(new 
RepositoryTupleSwapperEngine().swapToRuleConfiguration("leaf", 
Collections.emptyList()).isPresent());
+    }
+    
+    @Test
+    void assertSwapToNotFoundRuleConfiguration() {
+        assertFalse(new 
RepositoryTupleSwapperEngine().swapToRuleConfiguration("invalid", 
Collections.singleton(new RepositoryTuple("/rules/leaf/versions/0", "value: 
foo"))).isPresent());
+    }
+    
+    @Test
+    void assertSwapToRuleConfiguration() {
+        assertFalse(new 
RepositoryTupleSwapperEngine().swapToRuleConfiguration("leaf", 
Collections.singleton(new RepositoryTuple("/rules/leaf/versions/0", "value: 
foo"))).isPresent());
+    }
 }
diff --git 
a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/RuleNodePathProviderFixture.java
 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafRuleConfiguration.java
similarity index 56%
copy from 
mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/RuleNodePathProviderFixture.java
copy to 
mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafRuleConfiguration.java
index 95ad9b501d4..42ff00e0c97 100644
--- 
a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/RuleNodePathProviderFixture.java
+++ 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafRuleConfiguration.java
@@ -15,24 +15,15 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.mode.tuple.fixture.node;
+package org.apache.shardingsphere.mode.tuple.fixture.leaf;
 
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import org.apache.shardingsphere.mode.path.rule.RuleNodePath;
-import org.apache.shardingsphere.mode.spi.RuleNodePathProvider;
 
-import java.util.Arrays;
-
-public final class RuleNodePathProviderFixture implements RuleNodePathProvider 
{
-    
-    @Override
-    public RuleNodePath getRuleNodePath() {
-        return new RuleNodePath("node", Arrays.asList("map_value", "gens"),
-                Arrays.asList("collection_value", "string_value", 
"boolean_value", "integer_value", "long_value", "enum_value", "leaf"));
-    }
+@RequiredArgsConstructor
+@Getter
+public final class LeafRuleConfiguration implements RuleConfiguration {
     
-    @Override
-    public Class<? extends RuleConfiguration> getType() {
-        return RuleConfiguration.class;
-    }
+    private final String value;
 }
diff --git 
a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafYamlRuleConfigurationSwapper.java
 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafYamlRuleConfigurationSwapper.java
new file mode 100644
index 00000000000..09cf5bfb630
--- /dev/null
+++ 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafYamlRuleConfigurationSwapper.java
@@ -0,0 +1,48 @@
+/*
+ * 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.mode.tuple.fixture.leaf;
+
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
+
+public final class LeafYamlRuleConfigurationSwapper implements 
YamlRuleConfigurationSwapper<LeafYamlRuleConfiguration, LeafRuleConfiguration> {
+    
+    @Override
+    public LeafYamlRuleConfiguration swapToYamlConfiguration(final 
LeafRuleConfiguration data) {
+        return new LeafYamlRuleConfiguration(data.getValue());
+    }
+    
+    @Override
+    public LeafRuleConfiguration swapToObject(final LeafYamlRuleConfiguration 
yamlConfig) {
+        return new LeafRuleConfiguration(yamlConfig.getValue());
+    }
+    
+    @Override
+    public String getRuleTagName() {
+        return "LEAF";
+    }
+    
+    @Override
+    public int getOrder() {
+        return 10000;
+    }
+    
+    @Override
+    public Class<LeafRuleConfiguration> getTypeClass() {
+        return null;
+    }
+}
diff --git 
a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfiguration.java
 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfiguration.java
index a390cc0ca60..731dcd3bf5b 100644
--- 
a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfiguration.java
+++ 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfiguration.java
@@ -28,6 +28,7 @@ import 
org.apache.shardingsphere.mode.tuple.annotation.RepositoryTupleKeyListNam
 import 
org.apache.shardingsphere.mode.tuple.fixture.leaf.LeafYamlRuleConfiguration;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.Map;
 
@@ -37,7 +38,7 @@ import java.util.Map;
 public final class NodeYamlRuleConfiguration implements YamlRuleConfiguration {
     
     @RepositoryTupleField(type = Type.OTHER)
-    private Map<String, LeafYamlRuleConfiguration> mapValue;
+    private Map<String, LeafYamlRuleConfiguration> mapValue = new HashMap<>();
     
     @RepositoryTupleField(type = Type.OTHER)
     private Collection<LeafYamlRuleConfiguration> collectionValue = new 
LinkedList<>();
@@ -62,7 +63,11 @@ public final class NodeYamlRuleConfiguration implements 
YamlRuleConfiguration {
     
     @RepositoryTupleField(type = Type.OTHER)
     
@RepositoryTupleKeyListNameGenerator(RepositoryTupleKeyListNameGeneratorFixture.class)
-    private Collection<String> gens;
+    private Collection<String> gens = new LinkedList<>();
+    
+    @RepositoryTupleField(type = Type.OTHER)
+    
@RepositoryTupleKeyListNameGenerator(RepositoryTupleKeyListNameGeneratorFixture.class)
+    private String gen;
     
     @Override
     public Class<? extends RuleConfiguration> getRuleConfigurationType() {
diff --git 
a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/RuleNodePathProviderFixture.java
 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/RuleNodePathProviderFixture.java
index 95ad9b501d4..085b81f226a 100644
--- 
a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/RuleNodePathProviderFixture.java
+++ 
b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/RuleNodePathProviderFixture.java
@@ -28,7 +28,7 @@ public final class RuleNodePathProviderFixture implements 
RuleNodePathProvider {
     @Override
     public RuleNodePath getRuleNodePath() {
         return new RuleNodePath("node", Arrays.asList("map_value", "gens"),
-                Arrays.asList("collection_value", "string_value", 
"boolean_value", "integer_value", "long_value", "enum_value", "leaf"));
+                Arrays.asList("collection_value", "string_value", 
"boolean_value", "integer_value", "long_value", "enum_value", "gen", "leaf"));
     }
     
     @Override
diff --git 
a/mode/api/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper
 
b/mode/api/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper
new file mode 100644
index 00000000000..ea7696eaf55
--- /dev/null
+++ 
b/mode/api/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.mode.tuple.fixture.leaf.LeafYamlRuleConfigurationSwapper

Reply via email to