Skip to content

Commit

Permalink
Add test cases for RepositoryTupleSwapperEngine.swapToRepositoryTuple…
Browse files Browse the repository at this point in the history
…s() (#32460)

* Refactor RepositoryTupleSwapperEngine

* Add RepositoryTupleSwapperEngineTest

* Add test cases for RepositoryTupleSwapperEngine.swapToRepositoryTuples()
  • Loading branch information
terrymanu authored Aug 10, 2024
1 parent 701dbd2 commit 200708a
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public Collection<RepositoryTuple> swapToRepositoryTuples(final YamlRuleConfigur
Collection<RepositoryTuple> 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));
Expand Down Expand Up @@ -110,8 +109,8 @@ private Collection<RepositoryTuple> 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()));
Expand Down Expand Up @@ -190,7 +189,6 @@ private Optional<YamlRuleConfiguration> swapToYamlRuleConfiguration(final Collec
@SneakyThrows(ReflectiveOperationException.class)
private void setFieldValue(final YamlRuleConfiguration yamlRuleConfig, final Collection<Field> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<RepositoryTuple> 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<RepositoryTuple> 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<RepositoryTuple> 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()));
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends RuleConfiguration> getRuleConfigurationType() {
return RuleConfiguration.class;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, LeafYamlRuleConfiguration> mapValue;

@RepositoryTupleField(type = Type.OTHER)
private Collection<LeafYamlRuleConfiguration> 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<? extends RuleConfiguration> getRuleConfigurationType() {
return RuleConfiguration.class;
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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<? extends RuleConfiguration> getType() {
return RuleConfiguration.class;
}
}
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.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<? extends RuleConfiguration> getRuleConfigurationType() {
return RuleConfiguration.class;
}
}
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.node.RuleNodePathProviderFixture

0 comments on commit 200708a

Please sign in to comment.