Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor YamlRuleConfigurationUnmarshalIT #33031

Merged
merged 8 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,14 @@

package org.apache.shardingsphere.broadcast.it;

import org.apache.shardingsphere.broadcast.yaml.config.YamlBroadcastRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.broadcast.config.BroadcastRuleConfiguration;
import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT;

import java.util.ArrayList;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Arrays;

class BroadcastRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT {

BroadcastRuleConfigurationYamlUnmarshalIT() {
super("yaml/broadcast-rule.yaml");
}

@Override
protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) {
assertBroadcastRule((YamlBroadcastRuleConfiguration) actual.getRules().iterator().next());
}

private void assertBroadcastRule(final YamlBroadcastRuleConfiguration actual) {
assertThat(actual.getTables().size(), is(2));
assertThat(new ArrayList<>(actual.getTables()).get(0), is("foo_tbl"));
assertThat(new ArrayList<>(actual.getTables()).get(1), is("bar_tbl"));
super("yaml/broadcast-rule.yaml", new BroadcastRuleConfiguration(Arrays.asList("foo_tbl", "bar_tbl")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@

package org.apache.shardingsphere.encrypt.it;

import org.apache.shardingsphere.encrypt.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.config.rule.EncryptColumnItemRuleConfiguration;
import org.apache.shardingsphere.encrypt.config.rule.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.encrypt.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
Expand All @@ -31,12 +41,24 @@
class EncryptRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT {

EncryptRuleConfigurationYamlUnmarshalIT() {
super("yaml/encrypt-rule.yaml");
super("yaml/encrypt-rule.yaml", getExpectedRuleConfiguration());
}

private static EncryptRuleConfiguration getExpectedRuleConfiguration() {
EncryptColumnRuleConfiguration encryptColumnRuleConfig = new EncryptColumnRuleConfiguration("username",
new EncryptColumnItemRuleConfiguration("username_cipher", "aes_encryptor"));
encryptColumnRuleConfig.setAssistedQuery(new EncryptColumnItemRuleConfiguration("assisted_query_username", "assisted_encryptor"));
Collection<EncryptTableRuleConfiguration> tables = Collections.singletonList(new EncryptTableRuleConfiguration("t_user", Collections.singletonList(encryptColumnRuleConfig)));
Map<String, AlgorithmConfiguration> encryptors = new LinkedHashMap<>(2, 1F);
encryptors.put("aes_encryptor", new AlgorithmConfiguration("AES", PropertiesBuilder.build(new Property("aes-key-value", "123456abc"), new Property("digest-algorithm-name", "SHA-1"))));
encryptors.put("assisted_encryptor", new AlgorithmConfiguration("AES", PropertiesBuilder.build(new Property("aes-key-value", "123456abc"), new Property("digest-algorithm-name", "SHA-1"))));
return new EncryptRuleConfiguration(tables, encryptors);
}

@Override
protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) {
assertEncryptRule((YamlEncryptRuleConfiguration) actual.getRules().iterator().next());
protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) {
assertEncryptRule((YamlEncryptRuleConfiguration) actual);
return true;
}

private void assertEncryptRule(final YamlEncryptRuleConfiguration actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@

package org.apache.shardingsphere.mask.it;

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskColumnRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.yaml.config.YamlMaskRuleConfiguration;
import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -32,12 +43,23 @@
class MaskRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT {

MaskRuleConfigurationYamlUnmarshalIT() {
super("yaml/mask-rule.yaml");
super("yaml/mask-rule.yaml", getExpectedRuleConfiguration());
}

private static MaskRuleConfiguration getExpectedRuleConfiguration() {
Collection<MaskTableRuleConfiguration> tables = Collections.singletonList(new MaskTableRuleConfiguration("t_user",
Arrays.asList(new MaskColumnRuleConfiguration("telephone", "keep_first_n_last_m_mask"), new MaskColumnRuleConfiguration("password", "md5_mask"))));
Map<String, AlgorithmConfiguration> maskAlgorithms = new LinkedHashMap<>(2, 1F);
maskAlgorithms.put("keep_first_n_last_m_mask",
new AlgorithmConfiguration("KEEP_FIRST_N_LAST_M", PropertiesBuilder.build(new Property("first-n", 3), new Property("replace-char", "*"), new Property("last-m", 4))));
maskAlgorithms.put("md5_mask", new AlgorithmConfiguration("MD5", new Properties()));
return new MaskRuleConfiguration(tables, maskAlgorithms);
}

@Override
protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) {
assertMaskRule((YamlMaskRuleConfiguration) actual.getRules().iterator().next());
protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) {
assertMaskRule((YamlMaskRuleConfiguration) actual);
return true;
}

private void assertMaskRule(final YamlMaskRuleConfiguration actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,42 @@

package org.apache.shardingsphere.readwritesplitting.it;

import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.yaml.config.YamlReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class ReadwriteSplittingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT {

ReadwriteSplittingRuleConfigurationYamlUnmarshalIT() {
super("yaml/readwrite-splitting-rule.yaml");
super("yaml/readwrite-splitting-rule.yaml", getExpectedRuleConfiguration());
}

private static ReadwriteSplittingRuleConfiguration getExpectedRuleConfiguration() {
Collection<ReadwriteSplittingDataSourceGroupRuleConfiguration> dataSourceGroups = Arrays.asList(
new ReadwriteSplittingDataSourceGroupRuleConfiguration("ds_0", "write_ds_0", Arrays.asList("write_ds_0_read_0", "write_ds_0_read_1"), "roundRobin"),
new ReadwriteSplittingDataSourceGroupRuleConfiguration("ds_1", "write_ds_1", Arrays.asList("write_ds_1_read_0", "write_ds_1_read_1"), "random"));
Map<String, AlgorithmConfiguration> loadBalancers = new LinkedHashMap<>(2, 1F);
loadBalancers.put("random", new AlgorithmConfiguration("RANDOM", new Properties()));
loadBalancers.put("roundRobin", new AlgorithmConfiguration("ROUND_ROBIN", new Properties()));
return new ReadwriteSplittingRuleConfiguration(dataSourceGroups, loadBalancers);
}

@Override
protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) {
assertReadwriteSplittingRule((YamlReadwriteSplittingRuleConfiguration) actual.getRules().iterator().next());
protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) {
assertReadwriteSplittingRule((YamlReadwriteSplittingRuleConfiguration) actual);
return true;
}

private void assertReadwriteSplittingRule(final YamlReadwriteSplittingRuleConfiguration actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
/**
* Shadow data source mapper configuration.
*/
@RequiredArgsConstructor
@Getter
@Setter
@RequiredArgsConstructor
public final class ShadowDataSourceConfiguration {

private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,101 +17,40 @@

package org.apache.shardingsphere.shadow.it;

import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import org.apache.shardingsphere.shadow.yaml.config.YamlShadowRuleConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration;
import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration;
import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;

import java.util.Arrays;
import java.util.Optional;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Collections;

class ShadowRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT {

ShadowRuleConfigurationYamlUnmarshalIT() {
super("yaml/shadow-rule.yaml");
}

@Override
protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) {
Optional<YamlShadowRuleConfiguration> shadowRuleConfig = actual.getRules().stream()
.filter(each -> each instanceof YamlShadowRuleConfiguration).findFirst().map(optional -> (YamlShadowRuleConfiguration) optional);
assertTrue(shadowRuleConfig.isPresent());
assertThat(shadowRuleConfig.get().getTables().size(), is(3));
assertTOrder(shadowRuleConfig.get());
assertTOrderItem(shadowRuleConfig.get());
assertTAddress(shadowRuleConfig.get());
assertThat(shadowRuleConfig.get().getDefaultShadowAlgorithmName(), is("sql-hint-algorithm"));
assertThat(shadowRuleConfig.get().getShadowAlgorithms().size(), is(4));
assertUserIdInsertMatchAlgorithm(shadowRuleConfig.get());
assertUserIdUpdateMatchAlgorithm(shadowRuleConfig.get());
assertUserIdSelectMatchAlgorithm(shadowRuleConfig.get());
assertSqlHintAlgorithm(shadowRuleConfig.get());
}

private void assertTOrder(final YamlShadowRuleConfiguration actual) {
assertThat(actual.getTables().get("t_order").getDataSourceNames().iterator().next(), is("shadowDataSource"));
assertThat(actual.getTables().get("t_order").getShadowAlgorithmNames().size(), is(2));
assertTrue(actual.getTables().get("t_order").getShadowAlgorithmNames().containsAll(Arrays.asList("user-id-insert-match-algorithm", "user-id-select-match-algorithm")));
}

private void assertTOrderItem(final YamlShadowRuleConfiguration actual) {
assertThat(actual.getTables().get("t_order_item").getDataSourceNames().iterator().next(), is("shadowDataSource"));
assertThat(actual.getTables().get("t_order_item").getShadowAlgorithmNames().size(), is(3));
assertTrue(actual.getTables().get("t_order_item").getShadowAlgorithmNames().containsAll(
Arrays.asList("user-id-insert-match-algorithm", "user-id-update-match-algorithm", "user-id-select-match-algorithm")));
}

private void assertTAddress(final YamlShadowRuleConfiguration actual) {
assertThat(actual.getTables().get("t_address").getDataSourceNames().iterator().next(), is("shadowDataSource"));
assertThat(actual.getTables().get("t_address").getShadowAlgorithmNames().size(), is(3));
assertTrue(actual.getTables().get("t_address").getShadowAlgorithmNames().containsAll(Arrays.asList("user-id-insert-match-algorithm", "user-id-select-match-algorithm", "sql-hint-algorithm")));
}

private void assertUserIdInsertMatchAlgorithm(final YamlShadowRuleConfiguration actual) {
assertThat(actual.getShadowAlgorithms().get("user-id-insert-match-algorithm").getType(), is("REGEX_MATCH"));
Properties props = actual.getShadowAlgorithms().get("user-id-insert-match-algorithm").getProps();
assertThat(props.size(), is(3));
assertTrue(props.containsKey("operation"));
assertThat(props.getProperty("operation"), is("insert"));
assertTrue(props.containsKey("column"));
assertThat(props.getProperty("column"), is("user_id"));
assertTrue(props.containsKey("regex"));
assertThat(props.getProperty("regex"), is("[1]"));
}

private void assertUserIdUpdateMatchAlgorithm(final YamlShadowRuleConfiguration actual) {
assertThat(actual.getShadowAlgorithms().get("user-id-update-match-algorithm").getType(), is("REGEX_MATCH"));
Properties props = actual.getShadowAlgorithms().get("user-id-update-match-algorithm").getProps();
assertThat(props.size(), is(3));
assertTrue(props.containsKey("operation"));
assertThat(props.getProperty("operation"), is("update"));
assertTrue(props.containsKey("column"));
assertThat(props.getProperty("column"), is("user_id"));
assertTrue(props.containsKey("regex"));
assertThat(props.getProperty("regex"), is("[1]"));
}

private void assertUserIdSelectMatchAlgorithm(final YamlShadowRuleConfiguration actual) {
assertThat(actual.getShadowAlgorithms().get("user-id-select-match-algorithm").getType(), is("REGEX_MATCH"));
Properties props = actual.getShadowAlgorithms().get("user-id-select-match-algorithm").getProps();
assertThat(props.size(), is(3));
assertTrue(props.containsKey("operation"));
assertThat(props.getProperty("operation"), is("select"));
assertTrue(props.containsKey("column"));
assertThat(props.getProperty("column"), is("user_id"));
assertTrue(props.containsKey("regex"));
assertThat(props.getProperty("regex"), is("[1]"));
}

private void assertSqlHintAlgorithm(final YamlShadowRuleConfiguration actual) {
assertThat(actual.getShadowAlgorithms().get("sql-hint-algorithm").getType(), is("SQL_HINT"));
Properties props = actual.getShadowAlgorithms().get("sql-hint-algorithm").getProps();
assertThat(props.size(), is(2));
assertTrue((boolean) props.get("shadow"));
assertThat(props.getProperty("foo"), is("bar"));
super("yaml/shadow-rule.yaml", getExpectedRuleConfiguration());
}

private static ShadowRuleConfiguration getExpectedRuleConfiguration() {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
result.getDataSources().add(new ShadowDataSourceConfiguration("shadowDataSource", "ds", "ds_shadow"));
result.getTables().put("t_order", new ShadowTableConfiguration(
Collections.singletonList("shadowDataSource"), Arrays.asList("user-id-insert-match-algorithm", "user-id-select-match-algorithm")));
result.getTables().put("t_order_item", new ShadowTableConfiguration(
Collections.singletonList("shadowDataSource"), Arrays.asList("user-id-insert-match-algorithm", "user-id-update-match-algorithm", "user-id-select-match-algorithm")));
result.getTables().put("t_address", new ShadowTableConfiguration(
Collections.singletonList("shadowDataSource"), Arrays.asList("user-id-insert-match-algorithm", "user-id-select-match-algorithm", "sql-hint-algorithm")));
result.getShadowAlgorithms().put("user-id-insert-match-algorithm", new AlgorithmConfiguration("REGEX_MATCH",
PropertiesBuilder.build(new Property("regex", "[1]"), new Property("column", "user_id"), new Property("operation", "insert"))));
result.getShadowAlgorithms().put("user-id-update-match-algorithm", new AlgorithmConfiguration("REGEX_MATCH",
PropertiesBuilder.build(new Property("regex", "[1]"), new Property("column", "user_id"), new Property("operation", "update"))));
result.getShadowAlgorithms().put("user-id-select-match-algorithm", new AlgorithmConfiguration("REGEX_MATCH",
PropertiesBuilder.build(new Property("regex", "[1]"), new Property("column", "user_id"), new Property("operation", "select"))));
result.getShadowAlgorithms().put("sql-hint-algorithm", new AlgorithmConfiguration("SQL_HINT", PropertiesBuilder.build(new Property("shadow", true), new Property("foo", "bar"))));
result.setDefaultShadowAlgorithmName("sql-hint-algorithm");
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class ShardingConfigurationRepositoryTupleSwapperEngineIT extends RepositoryTupleSwapperEngineIT {

ShardingConfigurationRepositoryTupleSwapperEngineIT() {
super("yaml/sharding-rule.yaml");
super("yaml/sharding-rule-for-tuple.yaml");
}

@Override
Expand Down
Loading