From 200708a7a613bcede192c29f21af495362c5463a Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sat, 10 Aug 2024 23:22:44 +0800 Subject: [PATCH] Add test cases for RepositoryTupleSwapperEngine.swapToRepositoryTuples() (#32460) * Refactor RepositoryTupleSwapperEngine * Add RepositoryTupleSwapperEngineTest * Add test cases for RepositoryTupleSwapperEngine.swapToRepositoryTuples() --- .../tuple/RepositoryTupleSwapperEngine.java | 6 +- .../RepositoryTupleSwapperEngineTest.java | 89 +++++++++++++++++++ .../leaf/LeafYamlRuleConfiguration.java | 41 +++++++++ .../node/NodeYamlRuleConfiguration.java | 66 ++++++++++++++ .../node/NodeYamlRuleConfigurationEnum.java | 23 +++++ .../node/RuleNodePathProviderFixture.java | 38 ++++++++ .../none/NoneYamlRuleConfiguration.java | 29 ++++++ ...ardingsphere.mode.spi.RuleNodePathProvider | 18 ++++ 8 files changed, 306 insertions(+), 4 deletions(-) create mode 100644 mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngineTest.java create mode 100644 mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafYamlRuleConfiguration.java create mode 100644 mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfiguration.java create mode 100644 mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfigurationEnum.java create mode 100644 mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/RuleNodePathProviderFixture.java create mode 100644 mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/none/NoneYamlRuleConfiguration.java create mode 100644 mode/api/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.spi.RuleNodePathProvider diff --git a/mode/api/src/main/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngine.java b/mode/api/src/main/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngine.java index ac4fc77c8c606..b7e064f9321ae 100644 --- a/mode/api/src/main/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngine.java +++ b/mode/api/src/main/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngine.java @@ -72,7 +72,6 @@ public Collection swapToRepositoryTuples(final YamlRuleConfigur Collection result = new LinkedList<>(); RuleNodePath ruleNodePath = TypedSPILoader.getService(RuleNodePathProvider.class, yamlRuleConfig.getRuleConfigurationType()).getRuleNodePath(); for (Field each : getFields(yamlRuleConfig.getClass())) { - @SuppressWarnings("deprecation") boolean isAccessible = each.isAccessible(); each.setAccessible(true); result.addAll(swapToRepositoryTuples(yamlRuleConfig, ruleNodePath, each)); @@ -110,8 +109,8 @@ private Collection swapToRepositoryTuples(final YamlRuleConfigu ? Collections.emptyList() : Collections.singleton(new RepositoryTuple(ruleNodePath.getUniqueItem(tupleName).getPath(), YamlEngine.marshal(fieldValue))); } - if (fieldValue instanceof String && !((String) fieldValue).isEmpty()) { - return Collections.singleton(new RepositoryTuple(ruleNodePath.getUniqueItem(tupleName).getPath(), fieldValue.toString())); + if (fieldValue instanceof String) { + return ((String) fieldValue).isEmpty() ? Collections.emptyList() : Collections.singleton(new RepositoryTuple(ruleNodePath.getUniqueItem(tupleName).getPath(), fieldValue.toString())); } if (fieldValue instanceof Boolean || fieldValue instanceof Integer || fieldValue instanceof Long) { return Collections.singleton(new RepositoryTuple(ruleNodePath.getUniqueItem(tupleName).getPath(), fieldValue.toString())); @@ -190,7 +189,6 @@ private Optional swapToYamlRuleConfiguration(final Collec @SneakyThrows(ReflectiveOperationException.class) private void setFieldValue(final YamlRuleConfiguration yamlRuleConfig, final Collection fields, final RuleNodePath ruleNodePath, final RepositoryTuple repositoryTuple) { for (Field each : fields) { - @SuppressWarnings("deprecation") boolean isAccessible = each.isAccessible(); each.setAccessible(true); setFieldValue(yamlRuleConfig, each, ruleNodePath, repositoryTuple); 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 new file mode 100644 index 0000000000000..6264914a1dec2 --- /dev/null +++ b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/RepositoryTupleSwapperEngineTest.java @@ -0,0 +1,89 @@ +/* + * 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; + +import org.apache.shardingsphere.mode.tuple.fixture.leaf.LeafYamlRuleConfiguration; +import org.apache.shardingsphere.mode.tuple.fixture.node.NodeYamlRuleConfiguration; +import org.apache.shardingsphere.mode.tuple.fixture.node.NodeYamlRuleConfigurationEnum; +import org.apache.shardingsphere.mode.tuple.fixture.none.NoneYamlRuleConfiguration; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class RepositoryTupleSwapperEngineTest { + + @Test + void assertSwapToRepositoryTuplesWithoutRepositoryTupleEntityAnnotation() { + assertTrue(new RepositoryTupleSwapperEngine().swapToRepositoryTuples(new NoneYamlRuleConfiguration()).isEmpty()); + } + + @Test + void assertSwapToRepositoryTuplesWithLeafYamlRuleConfiguration() { + Collection actual = new RepositoryTupleSwapperEngine().swapToRepositoryTuples(new LeafYamlRuleConfiguration("foo")); + assertThat(actual.size(), is(1)); + RepositoryTuple actualTuple = actual.iterator().next(); + assertThat(actualTuple.getKey(), is("leaf")); + assertThat(actualTuple.getValue(), is("value: foo" + System.lineSeparator())); + } + + @Test + void assertSwapToRepositoryTuplesWithEmptyNodeYamlRuleConfiguration() { + Collection actual = new RepositoryTupleSwapperEngine().swapToRepositoryTuples(new NodeYamlRuleConfiguration()); + assertTrue(actual.isEmpty()); + } + + @Test + void assertSwapToRepositoryTuplesWithNodeYamlRuleConfiguration() { + NodeYamlRuleConfiguration yamlRuleConfig = new NodeYamlRuleConfiguration(); + yamlRuleConfig.setMapValue(Collections.singletonMap("k", new LeafYamlRuleConfiguration("v"))); + yamlRuleConfig.setCollectionValue(Collections.singletonList(new LeafYamlRuleConfiguration("foo"))); + yamlRuleConfig.setStringValue("str"); + yamlRuleConfig.setBooleanValue(true); + yamlRuleConfig.setIntegerValue(1); + yamlRuleConfig.setLongValue(10L); + yamlRuleConfig.setEnumValue(NodeYamlRuleConfigurationEnum.FOO); + LeafYamlRuleConfiguration leaf = new LeafYamlRuleConfiguration(); + leaf.setValue("leaf"); + yamlRuleConfig.setLeaf(leaf); + List actual = new ArrayList<>(new RepositoryTupleSwapperEngine().swapToRepositoryTuples(yamlRuleConfig)); + assertThat(actual.size(), is(8)); + 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(2).getKey(), is("string_value")); + assertThat(actual.get(2).getValue(), is("str")); + assertThat(actual.get(3).getKey(), is("boolean_value")); + assertThat(actual.get(3).getValue(), is("true")); + assertThat(actual.get(4).getKey(), is("integer_value")); + assertThat(actual.get(4).getValue(), is("1")); + assertThat(actual.get(5).getKey(), is("long_value")); + assertThat(actual.get(5).getValue(), is("10")); + assertThat(actual.get(6).getKey(), is("enum_value")); + assertThat(actual.get(6).getValue(), is("FOO")); + assertThat(actual.get(7).getKey(), is("leaf")); + assertThat(actual.get(7).getValue(), is("value: leaf" + System.lineSeparator())); + } +} diff --git a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafYamlRuleConfiguration.java b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafYamlRuleConfiguration.java new file mode 100644 index 0000000000000..c8c3bd87a6c40 --- /dev/null +++ b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/leaf/LeafYamlRuleConfiguration.java @@ -0,0 +1,41 @@ +/* + * 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 lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; +import org.apache.shardingsphere.mode.tuple.annotation.RepositoryTupleEntity; + +@RepositoryTupleEntity(value = "leaf", leaf = true) +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter +public final class LeafYamlRuleConfiguration implements YamlRuleConfiguration { + + private String value; + + @Override + public Class getRuleConfigurationType() { + return RuleConfiguration.class; + } +} 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 new file mode 100644 index 0000000000000..70488f7fa672e --- /dev/null +++ b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfiguration.java @@ -0,0 +1,66 @@ +/* + * 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.node; + +import lombok.Getter; +import lombok.Setter; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; +import org.apache.shardingsphere.mode.tuple.annotation.RepositoryTupleEntity; +import org.apache.shardingsphere.mode.tuple.annotation.RepositoryTupleField; +import org.apache.shardingsphere.mode.tuple.annotation.RepositoryTupleField.Type; +import org.apache.shardingsphere.mode.tuple.fixture.leaf.LeafYamlRuleConfiguration; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.Map; + +@RepositoryTupleEntity(value = "node") +@Getter +@Setter +public final class NodeYamlRuleConfiguration implements YamlRuleConfiguration { + + @RepositoryTupleField(type = Type.OTHER) + private Map mapValue; + + @RepositoryTupleField(type = Type.OTHER) + private Collection collectionValue = new LinkedList<>(); + + @RepositoryTupleField(type = Type.OTHER) + private String stringValue = ""; + + @RepositoryTupleField(type = Type.OTHER) + private Boolean booleanValue; + + @RepositoryTupleField(type = Type.OTHER) + private Integer integerValue; + + @RepositoryTupleField(type = Type.OTHER) + private Long longValue; + + @RepositoryTupleField(type = Type.OTHER) + private NodeYamlRuleConfigurationEnum enumValue; + + @RepositoryTupleField(type = Type.OTHER) + private LeafYamlRuleConfiguration leaf; + + @Override + public Class getRuleConfigurationType() { + return RuleConfiguration.class; + } +} diff --git a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfigurationEnum.java b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfigurationEnum.java new file mode 100644 index 0000000000000..269bd9f4c80ff --- /dev/null +++ b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/NodeYamlRuleConfigurationEnum.java @@ -0,0 +1,23 @@ +/* + * 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.node; + +public enum NodeYamlRuleConfigurationEnum { + + FOO +} 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 new file mode 100644 index 0000000000000..91b9db178c9a6 --- /dev/null +++ b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/node/RuleNodePathProviderFixture.java @@ -0,0 +1,38 @@ +/* + * 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.node; + +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; +import java.util.Collections; + +public final class RuleNodePathProviderFixture implements RuleNodePathProvider { + + @Override + public RuleNodePath getRuleNodePath() { + return new RuleNodePath("node", Collections.singleton("map_value"), Arrays.asList("collection_value", "string_value", "boolean_value", "integer_value", "long_value", "enum_value", "leaf")); + } + + @Override + public Class getType() { + return RuleConfiguration.class; + } +} diff --git a/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/none/NoneYamlRuleConfiguration.java b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/none/NoneYamlRuleConfiguration.java new file mode 100644 index 0000000000000..ce34dff1ebd7b --- /dev/null +++ b/mode/api/src/test/java/org/apache/shardingsphere/mode/tuple/fixture/none/NoneYamlRuleConfiguration.java @@ -0,0 +1,29 @@ +/* + * 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.none; + +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; + +public final class NoneYamlRuleConfiguration implements YamlRuleConfiguration { + + @Override + public Class getRuleConfigurationType() { + return RuleConfiguration.class; + } +} diff --git a/mode/api/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.spi.RuleNodePathProvider b/mode/api/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.spi.RuleNodePathProvider new file mode 100644 index 0000000000000..de07f25a25885 --- /dev/null +++ b/mode/api/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.spi.RuleNodePathProvider @@ -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.node.RuleNodePathProviderFixture