Skip to content

Commit

Permalink
Add more test cases on RepositoryTupleSwapperEngine (apache#32464)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
terrymanu authored Aug 11, 2024
1 parent a16be98 commit 6418588
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -54,8 +55,7 @@ void assertSwapToRepositoryTuplesWithLeafYamlRuleConfiguration() {

@Test
void assertSwapToRepositoryTuplesWithEmptyNodeYamlRuleConfiguration() {
Collection<RepositoryTuple> actual = new RepositoryTupleSwapperEngine().swapToRepositoryTuples(new NodeYamlRuleConfiguration());
assertTrue(actual.isEmpty());
assertTrue(new RepositoryTupleSwapperEngine().swapToRepositoryTuples(new NodeYamlRuleConfiguration()).isEmpty());
}

@Test
Expand All @@ -72,12 +72,13 @@ void assertSwapToRepositoryTuplesWithNodeYamlRuleConfiguration() {
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"));
Expand All @@ -92,6 +93,8 @@ void assertSwapToRepositoryTuplesWithNodeYamlRuleConfiguration() {
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
Expand All @@ -101,16 +104,84 @@ void assertSwapToYamlRuleConfigurationWithoutRepositoryTupleEntityAnnotation() {

@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());
}
}
Original file line number Diff line number Diff line change
@@ -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.leaf;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;

@RequiredArgsConstructor
@Getter
public final class LeafRuleConfiguration implements RuleConfiguration {

private final String value;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;

Expand All @@ -37,7 +38,7 @@
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<>();
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6418588

Please sign in to comment.