diff --git a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/it/BroadcastRuleConfigurationYamlUnmarshalIT.java b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/it/BroadcastRuleConfigurationYamlUnmarshalIT.java index ff9581c81bfe0..b094610c52891 100644 --- a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/it/BroadcastRuleConfigurationYamlUnmarshalIT.java +++ b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/it/BroadcastRuleConfigurationYamlUnmarshalIT.java @@ -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"))); } } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/it/EncryptRuleConfigurationYamlUnmarshalIT.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/it/EncryptRuleConfigurationYamlUnmarshalIT.java index 9febe3baf685e..a9253981b5f83 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/it/EncryptRuleConfigurationYamlUnmarshalIT.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/it/EncryptRuleConfigurationYamlUnmarshalIT.java @@ -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; @@ -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 tables = Collections.singletonList(new EncryptTableRuleConfiguration("t_user", Collections.singletonList(encryptColumnRuleConfig))); + Map 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) { diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/it/MaskRuleConfigurationYamlUnmarshalIT.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/it/MaskRuleConfigurationYamlUnmarshalIT.java index c4a22dd880c3c..7ba93af6f62c7 100644 --- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/it/MaskRuleConfigurationYamlUnmarshalIT.java +++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/it/MaskRuleConfigurationYamlUnmarshalIT.java @@ -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; @@ -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 tables = Collections.singletonList(new MaskTableRuleConfiguration("t_user", + Arrays.asList(new MaskColumnRuleConfiguration("telephone", "keep_first_n_last_m_mask"), new MaskColumnRuleConfiguration("password", "md5_mask")))); + Map 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) { diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/it/ReadwriteSplittingRuleConfigurationYamlUnmarshalIT.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/it/ReadwriteSplittingRuleConfigurationYamlUnmarshalIT.java index 9fd07c530dab2..100826bef055d 100644 --- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/it/ReadwriteSplittingRuleConfigurationYamlUnmarshalIT.java +++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/it/ReadwriteSplittingRuleConfigurationYamlUnmarshalIT.java @@ -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 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 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) { diff --git a/features/shadow/api/src/main/java/org/apache/shardingsphere/shadow/config/datasource/ShadowDataSourceConfiguration.java b/features/shadow/api/src/main/java/org/apache/shardingsphere/shadow/config/datasource/ShadowDataSourceConfiguration.java index 0ff5c48d76415..41611c5491b47 100644 --- a/features/shadow/api/src/main/java/org/apache/shardingsphere/shadow/config/datasource/ShadowDataSourceConfiguration.java +++ b/features/shadow/api/src/main/java/org/apache/shardingsphere/shadow/config/datasource/ShadowDataSourceConfiguration.java @@ -24,9 +24,9 @@ /** * Shadow data source mapper configuration. */ +@RequiredArgsConstructor @Getter @Setter -@RequiredArgsConstructor public final class ShadowDataSourceConfiguration { private final String name; diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/it/ShadowRuleConfigurationYamlUnmarshalIT.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/it/ShadowRuleConfigurationYamlUnmarshalIT.java index de4970fe764d0..e357e20017561 100644 --- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/it/ShadowRuleConfigurationYamlUnmarshalIT.java +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/it/ShadowRuleConfigurationYamlUnmarshalIT.java @@ -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 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; } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingConfigurationRepositoryTupleSwapperEngineIT.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingConfigurationRepositoryTupleSwapperEngineIT.java index 262b6fc6ed61b..62540f9163ff2 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingConfigurationRepositoryTupleSwapperEngineIT.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingConfigurationRepositoryTupleSwapperEngineIT.java @@ -31,7 +31,7 @@ class ShardingConfigurationRepositoryTupleSwapperEngineIT extends RepositoryTupleSwapperEngineIT { ShardingConfigurationRepositoryTupleSwapperEngineIT() { - super("yaml/sharding-rule.yaml"); + super("yaml/sharding-rule-for-tuple.yaml"); } @Override diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingRuleConfigurationYamlUnmarshalIT.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingRuleConfigurationYamlUnmarshalIT.java index fd332477bc29e..ada4ee8ca1075 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingRuleConfigurationYamlUnmarshalIT.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingRuleConfigurationYamlUnmarshalIT.java @@ -17,14 +17,29 @@ package org.apache.shardingsphere.sharding.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.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheConfiguration; +import org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheOptionsConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration; import org.apache.shardingsphere.sharding.yaml.config.cache.YamlShardingCacheConfiguration; import org.apache.shardingsphere.sharding.yaml.config.cache.YamlShardingCacheOptionsConfiguration; 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.ArrayList; -import java.util.Optional; +import java.util.Collections; +import java.util.Properties; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -33,22 +48,58 @@ class ShardingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { ShardingRuleConfigurationYamlUnmarshalIT() { - super("yaml/sharding-rule.yaml"); + super("yaml/sharding-rule.yaml", getExpectedRuleConfiguration()); + } + + private static ShardingRuleConfiguration getExpectedRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + ShardingTableRuleConfiguration userTableRuleConfig = new ShardingTableRuleConfiguration("t_user", "ds_${0..1}.t_user_${0..15}"); + userTableRuleConfig.setDatabaseShardingStrategy(new ComplexShardingStrategyConfiguration("region_id, user_id", "core_complex_fixture")); + userTableRuleConfig.setTableShardingStrategy(new ComplexShardingStrategyConfiguration("region_id, user_id", "core_complex_fixture")); + result.getTables().add(userTableRuleConfig); + ShardingTableRuleConfiguration stockTableRuleConfig = new ShardingTableRuleConfiguration("t_stock", "ds_${0..1}.t_stock{0..8}"); + stockTableRuleConfig.setDatabaseShardingStrategy(new HintShardingStrategyConfiguration("core_hint_fixture")); + stockTableRuleConfig.setTableShardingStrategy(new HintShardingStrategyConfiguration("core_hint_fixture")); + result.getTables().add(stockTableRuleConfig); + ShardingTableRuleConfiguration orderTableRuleConfig = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}"); + orderTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "table_inline")); + orderTableRuleConfig.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); + result.getTables().add(orderTableRuleConfig); + ShardingTableRuleConfiguration orderItemTableRuleConfig = new ShardingTableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${0..1}"); + orderItemTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "core_standard_fixture")); + result.getTables().add(orderItemTableRuleConfig); + result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", "t_order, t_order_item")); + result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "database_inline")); + result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); + result.setDefaultShardingColumn("order_id"); + result.setDefaultKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "snowflake")); + result.setDefaultAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singletonList("sharding_key_required_auditor"), true)); + result.getShardingAlgorithms().put("core_standard_fixture", new AlgorithmConfiguration("CORE.STANDARD.FIXTURE", new Properties())); + result.getShardingAlgorithms().put("core_complex_fixture", new AlgorithmConfiguration("CORE.COMPLEX.FIXTURE", new Properties())); + result.getShardingAlgorithms().put("core_hint_fixture", new AlgorithmConfiguration("CORE.HINT.FIXTURE", new Properties())); + result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${order_id % 2}")))); + result.getShardingAlgorithms().put("table_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${order_id % 2}")))); + result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); + result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties())); + result.setShardingCache(new ShardingCacheConfiguration(512, new ShardingCacheOptionsConfiguration(true, 65536, 262144))); + return result; } @Override - protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) { - Optional shardingRuleConfig = actual.getRules().stream() - .filter(each -> each instanceof YamlShardingRuleConfiguration).findFirst().map(optional -> (YamlShardingRuleConfiguration) optional); - assertTrue(shardingRuleConfig.isPresent()); - assertThat(shardingRuleConfig.get().getTables().size(), is(4)); - assertTUser(shardingRuleConfig.get()); - assertTStock(shardingRuleConfig.get()); - assertTOrder(shardingRuleConfig.get()); - assertTOrderItem(shardingRuleConfig.get()); - assertBindingTable(shardingRuleConfig.get()); - assertShardingCache(shardingRuleConfig.get()); - assertThat(shardingRuleConfig.get().getDefaultShardingColumn(), is("order_id")); + protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) { + assertShardingRule((YamlShardingRuleConfiguration) actual); + return true; + } + + private void assertShardingRule(final YamlShardingRuleConfiguration actual) { + assertThat(actual.getTables().size(), is(4)); + assertTUser(actual); + assertTStock(actual); + assertTOrder(actual); + assertTOrderItem(actual); + assertBindingTable(actual); + assertShardingCache(actual); + assertThat(actual.getDefaultShardingColumn(), is("order_id")); } private void assertTUser(final YamlShardingRuleConfiguration actual) { @@ -81,9 +132,8 @@ private void assertTOrderItem(final YamlShardingRuleConfiguration actual) { } private void assertBindingTable(final YamlShardingRuleConfiguration actual) { - assertThat(actual.getBindingTables().size(), is(2)); - assertThat(new ArrayList<>(actual.getBindingTables()).get(0), is("t_order, t_order_item")); - assertThat(new ArrayList<>(actual.getBindingTables()).get(1), is("foo:t_order, t_order_item")); + assertThat(actual.getBindingTables().size(), is(1)); + assertThat(new ArrayList<>(actual.getBindingTables()).get(0), is("foo:t_order, t_order_item")); } private void assertShardingCache(final YamlShardingRuleConfiguration actual) { diff --git a/features/sharding/core/src/test/resources/yaml/sharding-rule-for-tuple.yaml b/features/sharding/core/src/test/resources/yaml/sharding-rule-for-tuple.yaml new file mode 100644 index 0000000000000..77e669e608e71 --- /dev/null +++ b/features/sharding/core/src/test/resources/yaml/sharding-rule-for-tuple.yaml @@ -0,0 +1,101 @@ +# +# 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. +# + +rules: +- !SHARDING + tables: + t_user: + actualDataNodes: ds_${0..1}.t_user_${0..15} + databaseStrategy: + complex: + shardingColumns: region_id, user_id + shardingAlgorithmName: core_complex_fixture + tableStrategy: + complex: + shardingColumns: region_id, user_id + shardingAlgorithmName: core_complex_fixture + t_stock: + actualDataNodes: ds_${0..1}.t_stock{0..8} + databaseStrategy: + hint: + shardingAlgorithmName: core_hint_fixture + tableStrategy: + hint: + shardingAlgorithmName: core_hint_fixture + t_order: + actualDataNodes: ds_${0..1}.t_order_${0..1} + tableStrategy: + standard: + shardingColumn: order_id + shardingAlgorithmName: table_inline + keyGenerateStrategy: + column: order_id + keyGeneratorName: snowflake + t_order_item: + actualDataNodes: ds_${0..1}.t_order_item_${0..1} + tableStrategy: + standard: + shardingColumn: order_id + shardingAlgorithmName: core_standard_fixture + bindingTables: + - t_order, t_order_item + - foo:t_order, t_order_item + defaultDatabaseStrategy: + standard: + shardingColumn: order_id + shardingAlgorithmName: database_inline + defaultTableStrategy: + none: + defaultShardingColumn: order_id + defaultKeyGenerateStrategy: + column: id + keyGeneratorName: snowflake + defaultAuditStrategy: + auditorNames: + - sharding_key_required_auditor + allowHintDisable: true + + shardingAlgorithms: + core_standard_fixture: + type: CORE.STANDARD.FIXTURE + core_complex_fixture: + type: CORE.COMPLEX.FIXTURE + core_hint_fixture: + type: CORE.HINT.FIXTURE + database_inline: + type: INLINE + props: + algorithm-expression: ds_${order_id % 2} + table_inline: + type: INLINE + props: + algorithm-expression: t_order_${order_id % 2} + + keyGenerators: + snowflake: + type: SNOWFLAKE + + auditors: + sharding_key_required_auditor: + type: DML_SHARDING_CONDITIONS + + shardingCache: + allowedMaxSqlLength: 512 + routeCache: + softValues: true + initialCapacity: 65536 + maximumSize: 262144 diff --git a/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml b/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml index 5fc2064d4eb77..0f2944e40281f 100644 --- a/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml +++ b/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml @@ -52,8 +52,7 @@ rules: shardingColumn: order_id shardingAlgorithmName: core_standard_fixture bindingTables: - - t_order, t_order_item - - foo:t_order, t_order_item + - foo:t_order, t_order_item defaultDatabaseStrategy: standard: shardingColumn: order_id @@ -84,7 +83,7 @@ rules: type: INLINE props: algorithm-expression: t_order_${order_id % 2} - + keyGenerators: snowflake: type: SNOWFLAKE @@ -96,6 +95,6 @@ rules: shardingCache: allowedMaxSqlLength: 512 routeCache: + softValues: true initialCapacity: 65536 maximumSize: 262144 - softValues: true diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/fixture/YamlRuleConfigurationSwapperFixture.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/fixture/YamlRuleConfigurationSwapperFixture.java index 84cd9d13a9e7b..349e520d34cf4 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/fixture/YamlRuleConfigurationSwapperFixture.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/config/swapper/fixture/YamlRuleConfigurationSwapperFixture.java @@ -46,6 +46,6 @@ public String getRuleTagName() { @Override public int getOrder() { - return 3; + return -300; } } diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java index 4b963af32b73d..b492c6185c8a0 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ShardingSphereConnection.java @@ -265,28 +265,22 @@ public void setReadOnly(final boolean readOnly) throws SQLException { databaseConnectionManager.setReadOnly(readOnly); } - /** - * This is just to avoid the Warning in brettwooldridge/HikariCP#2196. - * ShardingSphere does not propagate this property to the real JDBC Driver. - * `0` is actually the default value of {@link java.net.Socket#getSoTimeout()}. + /* + * This is just to avoid the Warning in brettwooldridge/HikariCP#2196. ShardingSphere does not propagate this property to the + * real JDBC Driver. `0` is actually the default value of {@link java.net.Socket#getSoTimeout()}. */ @Override public int getNetworkTimeout() { return 0; } - /** - * This is just to avoid the Warning in brettwooldridge/HikariCP#2196. - * ShardingSphere does not propagate this property to the real JDBC Driver. - * - * @param executor Not used. - * @param milliseconds The time in milliseconds to wait for the database operation to complete. + /* + * This is just to avoid the Warning in brettwooldridge/HikariCP#2196. ShardingSphere does not propagate this property to the + * real JDBC Driver. */ @Override public void setNetworkTimeout(final Executor executor, final int milliseconds) throws SQLException { - if (0 > milliseconds) { - throw new SQLException("Network timeout must be a value greater than or equal to 0."); - } + ShardingSpherePreconditions.checkState(0 <= milliseconds, () -> new SQLException("Network timeout must be a value greater than or equal to 0.")); } @Override diff --git a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/it/AuthorityRuleConfigurationYamlUnmarshalIT.java b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/it/AuthorityRuleConfigurationYamlUnmarshalIT.java index 82a2ae56a1dde..4372e3c61c1e7 100644 --- a/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/it/AuthorityRuleConfigurationYamlUnmarshalIT.java +++ b/kernel/authority/core/src/test/java/org/apache/shardingsphere/authority/it/AuthorityRuleConfigurationYamlUnmarshalIT.java @@ -17,61 +17,24 @@ package org.apache.shardingsphere.authority.it; -import org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration; -import org.apache.shardingsphere.authority.yaml.config.YamlUserConfiguration; -import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration; -import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; +import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration; +import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.Arrays; +import java.util.Collections; +import java.util.Properties; class AuthorityRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { AuthorityRuleConfigurationYamlUnmarshalIT() { - super("yaml/authority-rule.yaml"); - } - - @Override - protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) { - assertAuthorityRule((YamlAuthorityRuleConfiguration) actual.getRules().iterator().next()); - } - - private void assertAuthorityRule(final YamlAuthorityRuleConfiguration actual) { - assertUsers(new ArrayList<>(actual.getUsers())); - assertPrivilege(actual.getPrivilege()); - assertAuthenticators(actual.getAuthenticators()); - assertDefaultAuthenticator(actual.getDefaultAuthenticator()); - } - - private void assertUsers(final List actual) { - assertThat(actual.size(), is(2)); - assertThat(actual.get(0).getUser(), is("root@%")); - assertThat(actual.get(0).getPassword(), is("root")); - assertTrue(actual.get(0).isAdmin()); - assertThat(actual.get(1).getUser(), is("sharding@")); - assertThat(actual.get(1).getPassword(), is("sharding")); - assertFalse(actual.get(1).isAdmin()); - } - - private void assertPrivilege(final YamlAlgorithmConfiguration actual) { - assertThat(actual.getType(), is("ALL_PERMITTED")); - assertTrue(actual.getProps().isEmpty()); - } - - private void assertAuthenticators(final Map actual) { - assertThat(actual.size(), is(1)); - assertThat(actual.get("fixture").getType(), is("FIXTURE")); - assertTrue(actual.get("fixture").getProps().isEmpty()); + super("yaml/authority-rule.yaml", getExpectedRuleConfiguration()); } - private void assertDefaultAuthenticator(final String actual) { - assertThat(actual, is("fixture")); + private static AuthorityRuleConfiguration getExpectedRuleConfiguration() { + return new AuthorityRuleConfiguration( + Arrays.asList(new ShardingSphereUser("root", "root", "%", null, true), new ShardingSphereUser("sharding", "sharding", "%", null, false)), + new AlgorithmConfiguration("ALL_PERMITTED", new Properties()), Collections.singletonMap("fixture", new AlgorithmConfiguration("FIXTURE", new Properties())), "fixture"); } } diff --git a/kernel/authority/core/src/test/resources/yaml/authority-rule.yaml b/kernel/authority/core/src/test/resources/yaml/authority-rule.yaml index c0afd337893be..e5472d6299a13 100644 --- a/kernel/authority/core/src/test/resources/yaml/authority-rule.yaml +++ b/kernel/authority/core/src/test/resources/yaml/authority-rule.yaml @@ -21,7 +21,7 @@ rules: - user: root@% password: root admin: true - - user: sharding@ + - user: sharding@% password: sharding admin: false privilege: diff --git a/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/it/GlobalClockRuleConfigurationYamlUnmarshalIT.java b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/it/GlobalClockRuleConfigurationYamlUnmarshalIT.java index 624ff57c60a2c..825c1211575df 100644 --- a/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/it/GlobalClockRuleConfigurationYamlUnmarshalIT.java +++ b/kernel/global-clock/core/src/test/java/org/apache/shardingsphere/globalclock/it/GlobalClockRuleConfigurationYamlUnmarshalIT.java @@ -17,31 +17,14 @@ package org.apache.shardingsphere.globalclock.it; -import org.apache.shardingsphere.globalclock.yaml.config.YamlGlobalClockRuleConfiguration; -import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; +import org.apache.shardingsphere.globalclock.config.GlobalClockRuleConfiguration; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.apache.shardingsphere.test.util.PropertiesBuilder; +import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; class GlobalClockRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { GlobalClockRuleConfigurationYamlUnmarshalIT() { - super("yaml/global-clock-rule.yaml"); - } - - @Override - protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) { - assertGlobalClockRule((YamlGlobalClockRuleConfiguration) actual.getRules().iterator().next()); - } - - private void assertGlobalClockRule(final YamlGlobalClockRuleConfiguration actual) { - assertThat(actual.getType(), is("FIXTURE")); - assertThat(actual.getProvider(), is("FIXTURE")); - assertTrue(actual.isEnabled()); - assertThat(actual.getProps().size(), is(2)); - assertThat(actual.getProps().getProperty("k0"), is("v0")); - assertThat(actual.getProps().getProperty("k1"), is("v1")); + super("yaml/global-clock-rule.yaml", new GlobalClockRuleConfiguration("FIXTURE", "FIXTURE", true, PropertiesBuilder.build(new Property("k0", "v0"), new Property("k1", "v1")))); } } diff --git a/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/it/LoggingRuleConfigurationYamlUnmarshalIT.java b/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/it/LoggingRuleConfigurationYamlUnmarshalIT.java index b722f96c1835c..83f0dc27fefe7 100644 --- a/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/it/LoggingRuleConfigurationYamlUnmarshalIT.java +++ b/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/it/LoggingRuleConfigurationYamlUnmarshalIT.java @@ -17,51 +17,25 @@ package org.apache.shardingsphere.logging.it; -import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; -import org.apache.shardingsphere.logging.yaml.config.YamlAppenderConfiguration; -import org.apache.shardingsphere.logging.yaml.config.YamlLoggerConfiguration; -import org.apache.shardingsphere.logging.yaml.config.YamlLoggingRuleConfiguration; +import org.apache.shardingsphere.logging.config.LoggingRuleConfiguration; +import org.apache.shardingsphere.logging.logger.ShardingSphereAppender; +import org.apache.shardingsphere.logging.logger.ShardingSphereLogger; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; -import java.util.ArrayList; -import java.util.Collection; - -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 LoggingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { LoggingRuleConfigurationYamlUnmarshalIT() { - super("yaml/logging-rule.yaml"); - } - - @Override - protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) { - assertLoggingRule((YamlLoggingRuleConfiguration) actual.getRules().iterator().next()); - } - - private void assertLoggingRule(final YamlLoggingRuleConfiguration actual) { - assertLoggers(actual.getLoggers()); - assertAppenders(actual.getAppenders()); - } - - private void assertLoggers(final Collection actual) { - assertThat(actual.size(), is(1)); - assertThat(new ArrayList<>(actual).get(0).getLoggerName(), is("foo_logger")); - assertThat(new ArrayList<>(actual).get(0).getLevel(), is("INFO")); - assertTrue(new ArrayList<>(actual).get(0).getAdditivity()); - assertThat(new ArrayList<>(actual).get(0).getAppenderName(), is("foo_appender")); - assertThat(new ArrayList<>(actual).get(0).getProps().size(), is(2)); - assertThat(new ArrayList<>(actual).get(0).getProps().getProperty("k0"), is("v0")); - assertThat(new ArrayList<>(actual).get(0).getProps().getProperty("k1"), is("v1")); + super("yaml/logging-rule.yaml", getExpectedRuleConfiguration()); } - private void assertAppenders(final Collection actual) { - assertThat(actual.size(), is(1)); - assertThat(new ArrayList<>(actual).get(0).getAppenderName(), is("foo_appender")); - assertThat(new ArrayList<>(actual).get(0).getAppenderClass(), is("foo_appender_class")); - assertThat(new ArrayList<>(actual).get(0).getPattern(), is("sss")); - assertThat(new ArrayList<>(actual).get(0).getFile(), is("foo_file")); + private static LoggingRuleConfiguration getExpectedRuleConfiguration() { + ShardingSphereLogger logger = new ShardingSphereLogger("foo_logger", "INFO", true, "foo_appender"); + logger.getProps().put("k0", "v0"); + logger.getProps().put("k1", "v1"); + ShardingSphereAppender appender = new ShardingSphereAppender("foo_appender", "foo_appender_class", "sss"); + appender.setFile("foo_file"); + return new LoggingRuleConfiguration(Collections.singletonList(logger), Collections.singletonList(appender)); } } diff --git a/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/YamlLoggingRuleConfigurationSwapperTest.java b/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/YamlLoggingRuleConfigurationSwapperTest.java deleted file mode 100644 index a15e73b75e830..0000000000000 --- a/kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/YamlLoggingRuleConfigurationSwapperTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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.logging.yaml.swapper; - -import org.apache.shardingsphere.logging.config.LoggingRuleConfiguration; -import org.apache.shardingsphere.logging.logger.ShardingSphereAppender; -import org.apache.shardingsphere.logging.logger.ShardingSphereLogger; -import org.apache.shardingsphere.logging.yaml.config.YamlLoggingRuleConfiguration; -import org.junit.jupiter.api.Test; - -import java.util.Collections; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -class YamlLoggingRuleConfigurationSwapperTest { - - private final YamlLoggingRuleConfigurationSwapper swapper = new YamlLoggingRuleConfigurationSwapper(); - - private final YamlLoggerSwapper loggerSwapper = new YamlLoggerSwapper(); - - private final YamlAppenderSwapper appenderSwapper = new YamlAppenderSwapper(); - - @Test - void assertSwapToYamlConfiguration() { - YamlLoggingRuleConfiguration yamlLoggingRuleConfig = swapper.swapToYamlConfiguration(createLoggingRuleConfiguration()); - assertThat(yamlLoggingRuleConfig.getLoggers().size(), is(1)); - assertThat(yamlLoggingRuleConfig.getAppenders().size(), is(1)); - } - - private LoggingRuleConfiguration createLoggingRuleConfiguration() { - return new LoggingRuleConfiguration(Collections.singleton(new ShardingSphereLogger("ROOT", "INFO", true, "console")), - Collections.singleton(new ShardingSphereAppender("console", "ch.qos.logback.core.ConsoleAppender", "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n"))); - } - - @Test - void assertSwapToObject() { - LoggingRuleConfiguration loggingRuleConfig = swapper.swapToObject(createYamlLoggingRuleConfiguration()); - assertThat(loggingRuleConfig.getLoggers().size(), is(1)); - assertThat(loggingRuleConfig.getAppenders().size(), is(1)); - } - - private YamlLoggingRuleConfiguration createYamlLoggingRuleConfiguration() { - YamlLoggingRuleConfiguration result = new YamlLoggingRuleConfiguration(); - result.setLoggers(Collections.singleton(loggerSwapper.swapToYamlConfiguration(new ShardingSphereLogger("ROOT", "INFO", true, "console")))); - result.setAppenders(Collections.singleton(appenderSwapper.swapToYamlConfiguration( - new ShardingSphereAppender("console", "ch.qos.logback.core.ConsoleAppender", "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n")))); - return result; - } -} diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/fixture/YamlRuleConfigurationSwapperFixture.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/fixture/YamlRuleConfigurationSwapperFixture.java index e9a1b168014e0..f13eb66b5a6d7 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/fixture/YamlRuleConfigurationSwapperFixture.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/fixture/YamlRuleConfigurationSwapperFixture.java @@ -47,6 +47,6 @@ public String getRuleTagName() { @Override public int getOrder() { - return 3; + return -400; } } diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/it/SingleRuleConfigurationYamlUnmarshalIT.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/it/SingleRuleConfigurationYamlUnmarshalIT.java index 44a0133eec848..e58f304160261 100644 --- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/it/SingleRuleConfigurationYamlUnmarshalIT.java +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/it/SingleRuleConfigurationYamlUnmarshalIT.java @@ -17,28 +17,19 @@ package org.apache.shardingsphere.single.it; -import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; -import org.apache.shardingsphere.single.yaml.config.pojo.YamlSingleRuleConfiguration; +import org.apache.shardingsphere.single.config.SingleRuleConfiguration; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; -import java.util.Collections; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - class SingleRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { SingleRuleConfigurationYamlUnmarshalIT() { - super("yaml/single-rule.yaml"); - } - - @Override - protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) { - assertSingleRule((YamlSingleRuleConfiguration) actual.getRules().iterator().next()); + super("yaml/single-rule.yaml", getExpectedRuleConfiguration()); } - private void assertSingleRule(final YamlSingleRuleConfiguration actual) { - assertThat(actual.getTables(), is(Collections.singletonList("foo_tbl"))); - assertThat(actual.getDefaultDataSource(), is("foo_ds")); + private static SingleRuleConfiguration getExpectedRuleConfiguration() { + SingleRuleConfiguration result = new SingleRuleConfiguration(); + result.getTables().add("foo_tbl"); + result.setDefaultDataSource("foo_ds"); + return result; } } diff --git a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/it/SQLFederationRuleConfigurationYamlUnmarshalIT.java b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/it/SQLFederationRuleConfigurationYamlUnmarshalIT.java index f5a6c8e921f80..08b9da410c154 100644 --- a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/it/SQLFederationRuleConfigurationYamlUnmarshalIT.java +++ b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/it/SQLFederationRuleConfigurationYamlUnmarshalIT.java @@ -17,34 +17,13 @@ package org.apache.shardingsphere.sqlfederation.it; -import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; -import org.apache.shardingsphere.sqlfederation.yaml.config.YamlSQLFederationExecutionPlanCacheRuleConfiguration; -import org.apache.shardingsphere.sqlfederation.yaml.config.YamlSQLFederationRuleConfiguration; +import org.apache.shardingsphere.sql.parser.api.CacheOption; +import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - class SQLFederationRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { SQLFederationRuleConfigurationYamlUnmarshalIT() { - super("yaml/sql-federation-rule.yaml"); - } - - @Override - protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) { - assertSQLFederationRule((YamlSQLFederationRuleConfiguration) actual.getRules().iterator().next()); - } - - private void assertSQLFederationRule(final YamlSQLFederationRuleConfiguration actual) { - assertTrue(actual.isSqlFederationEnabled()); - assertTrue(actual.isAllQueryUseSQLFederation()); - assertExecutionPlanCache(actual.getExecutionPlanCache()); - } - - private void assertExecutionPlanCache(final YamlSQLFederationExecutionPlanCacheRuleConfiguration actual) { - assertThat(actual.getInitialCapacity(), is(128)); - assertThat(actual.getMaximumSize(), is(1024L)); + super("yaml/sql-federation-rule.yaml", new SQLFederationRuleConfiguration(true, true, new CacheOption(128, 1024))); } } diff --git a/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/it/SQLParserRuleConfigurationYamlUnmarshalIT.java b/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/it/SQLParserRuleConfigurationYamlUnmarshalIT.java index 65f54296466be..909dc59eab66f 100644 --- a/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/it/SQLParserRuleConfigurationYamlUnmarshalIT.java +++ b/kernel/sql-parser/core/src/test/java/org/apache/shardingsphere/parser/it/SQLParserRuleConfigurationYamlUnmarshalIT.java @@ -17,37 +17,13 @@ package org.apache.shardingsphere.parser.it; -import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; -import org.apache.shardingsphere.parser.yaml.config.YamlSQLParserCacheOptionRuleConfiguration; -import org.apache.shardingsphere.parser.yaml.config.YamlSQLParserRuleConfiguration; +import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration; +import org.apache.shardingsphere.sql.parser.api.CacheOption; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - class SQLParserRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { SQLParserRuleConfigurationYamlUnmarshalIT() { - super("yaml/sql-parser-rule.yaml"); - } - - @Override - protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) { - assertSQLParserRule((YamlSQLParserRuleConfiguration) actual.getRules().iterator().next()); - } - - private void assertSQLParserRule(final YamlSQLParserRuleConfiguration actual) { - assertSQLStatementCache(actual.getSqlStatementCache()); - assertSQLParseTreeCache(actual.getParseTreeCache()); - } - - private void assertSQLStatementCache(final YamlSQLParserCacheOptionRuleConfiguration actual) { - assertThat(actual.getInitialCapacity(), is(256)); - assertThat(actual.getMaximumSize(), is(4096L)); - } - - private void assertSQLParseTreeCache(final YamlSQLParserCacheOptionRuleConfiguration actual) { - assertThat(actual.getInitialCapacity(), is(128)); - assertThat(actual.getMaximumSize(), is(1024L)); + super("yaml/sql-parser-rule.yaml", new SQLParserRuleConfiguration(new CacheOption(128, 1024), new CacheOption(256, 4096))); } } diff --git a/kernel/sql-parser/core/src/test/resources/yaml/sql-parser-rule.yaml b/kernel/sql-parser/core/src/test/resources/yaml/sql-parser-rule.yaml index 168ce49e551e5..9eace8f0797d1 100644 --- a/kernel/sql-parser/core/src/test/resources/yaml/sql-parser-rule.yaml +++ b/kernel/sql-parser/core/src/test/resources/yaml/sql-parser-rule.yaml @@ -17,9 +17,9 @@ rules: - !SQL_PARSER - sqlStatementCache: - initialCapacity: 256 - maximumSize: 4096 parseTreeCache: initialCapacity: 128 maximumSize: 1024 + sqlStatementCache: + initialCapacity: 256 + maximumSize: 4096 diff --git a/kernel/sql-translator/core/pom.xml b/kernel/sql-translator/core/pom.xml index 1519ed8a290e4..7877b996fcfe2 100644 --- a/kernel/sql-translator/core/pom.xml +++ b/kernel/sql-translator/core/pom.xml @@ -38,6 +38,12 @@ ${project.version} + + org.apache.shardingsphere + shardingsphere-test-util + ${project.version} + test + org.apache.shardingsphere shardingsphere-test-it-yaml diff --git a/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/it/SQLTranslatorRuleConfigurationYamlUnmarshalIT.java b/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/it/SQLTranslatorRuleConfigurationYamlUnmarshalIT.java index f5c10a3a6d3b0..c8639b29fa97a 100644 --- a/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/it/SQLTranslatorRuleConfigurationYamlUnmarshalIT.java +++ b/kernel/sql-translator/core/src/test/java/org/apache/shardingsphere/sqltranslator/it/SQLTranslatorRuleConfigurationYamlUnmarshalIT.java @@ -17,30 +17,14 @@ package org.apache.shardingsphere.sqltranslator.it; -import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; -import org.apache.shardingsphere.sqltranslator.yaml.config.YamlSQLTranslatorRuleConfiguration; +import org.apache.shardingsphere.sqltranslator.config.SQLTranslatorRuleConfiguration; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import org.apache.shardingsphere.test.util.PropertiesBuilder; +import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; class SQLTranslatorRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { SQLTranslatorRuleConfigurationYamlUnmarshalIT() { - super("yaml/sql-translator-rule.yaml"); - } - - @Override - protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) { - assertSQLTranslatorRule((YamlSQLTranslatorRuleConfiguration) actual.getRules().iterator().next()); - } - - private void assertSQLTranslatorRule(final YamlSQLTranslatorRuleConfiguration actual) { - assertThat(actual.getType(), is("FIXTURE")); - assertThat(actual.getProps().size(), is(2)); - assertThat(actual.getProps().getProperty("k0"), is("v0")); - assertThat(actual.getProps().getProperty("k1"), is("v1")); - assertTrue(actual.isUseOriginalSQLWhenTranslatingFailed()); + super("yaml/sql-translator-rule.yaml", new SQLTranslatorRuleConfiguration("FIXTURE", PropertiesBuilder.build(new Property("k0", "v0"), new Property("k1", "v1")), true)); } } diff --git a/kernel/sql-translator/core/src/test/resources/yaml/sql-translator-rule.yaml b/kernel/sql-translator/core/src/test/resources/yaml/sql-translator-rule.yaml index e91c78a05be76..e31adde7ce9bd 100644 --- a/kernel/sql-translator/core/src/test/resources/yaml/sql-translator-rule.yaml +++ b/kernel/sql-translator/core/src/test/resources/yaml/sql-translator-rule.yaml @@ -18,7 +18,7 @@ rules: - !SQL_TRANSLATOR type: FIXTURE + useOriginalSQLWhenTranslatingFailed: true props: k0: v0 k1: v1 - useOriginalSQLWhenTranslatingFailed: true diff --git a/kernel/transaction/core/pom.xml b/kernel/transaction/core/pom.xml index f8de584f35474..4cc7e3b090b21 100644 --- a/kernel/transaction/core/pom.xml +++ b/kernel/transaction/core/pom.xml @@ -38,6 +38,12 @@ ${project.parent.version} + + org.apache.shardingsphere + shardingsphere-test-util + ${project.version} + test + org.apache.shardingsphere shardingsphere-test-fixture-database diff --git a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/it/TransactionRuleConfigurationYamlUnmarshalIT.java b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/it/TransactionRuleConfigurationYamlUnmarshalIT.java index d51cc1b78b679..2c1244472a860 100644 --- a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/it/TransactionRuleConfigurationYamlUnmarshalIT.java +++ b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/it/TransactionRuleConfigurationYamlUnmarshalIT.java @@ -17,29 +17,14 @@ package org.apache.shardingsphere.transaction.it; -import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; -import org.apache.shardingsphere.transaction.yaml.config.YamlTransactionRuleConfiguration; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import org.apache.shardingsphere.test.util.PropertiesBuilder; +import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; +import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration; class TransactionRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { TransactionRuleConfigurationYamlUnmarshalIT() { - super("yaml/transaction-rule.yaml"); - } - - @Override - protected void assertYamlRootConfiguration(final YamlRootConfiguration actual) { - assertSQLTranslatorRule((YamlTransactionRuleConfiguration) actual.getRules().iterator().next()); - } - - private void assertSQLTranslatorRule(final YamlTransactionRuleConfiguration actual) { - assertThat(actual.getDefaultType(), is("XA")); - assertThat(actual.getProviderType(), is("FIXTURE")); - assertThat(actual.getProps().size(), is(2)); - assertThat(actual.getProps().getProperty("k0"), is("v0")); - assertThat(actual.getProps().getProperty("k1"), is("v1")); + super("yaml/transaction-rule.yaml", new TransactionRuleConfiguration("XA", "FIXTURE", PropertiesBuilder.build(new Property("k0", "v0"), new Property("k1", "v1")))); } } diff --git a/test/it/yaml/src/main/java/org/apache/shardingsphere/test/it/yaml/YamlRuleConfigurationUnmarshalIT.java b/test/it/yaml/src/main/java/org/apache/shardingsphere/test/it/yaml/YamlRuleConfigurationUnmarshalIT.java index 5881d7c3e1f1b..69d88d0a9ddea 100644 --- a/test/it/yaml/src/main/java/org/apache/shardingsphere/test/it/yaml/YamlRuleConfigurationUnmarshalIT.java +++ b/test/it/yaml/src/main/java/org/apache/shardingsphere/test/it/yaml/YamlRuleConfigurationUnmarshalIT.java @@ -17,52 +17,80 @@ package org.apache.shardingsphere.test.it.yaml; -import lombok.RequiredArgsConstructor; +import com.cedarsoftware.util.DeepEquals; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; +import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration; +import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper; import org.junit.jupiter.api.Test; -import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.Collections; +import java.util.stream.Collectors; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; -@RequiredArgsConstructor public abstract class YamlRuleConfigurationUnmarshalIT { private final String yamlFile; + private final RuleConfiguration expectedRuleConfig; + + @SuppressWarnings("rawtypes") + private final YamlRuleConfigurationSwapper swapper; + + public YamlRuleConfigurationUnmarshalIT(final String yamlFile, final RuleConfiguration expectedRuleConfig) { + this.yamlFile = yamlFile; + this.expectedRuleConfig = expectedRuleConfig; + swapper = OrderedSPILoader.getServices(YamlRuleConfigurationSwapper.class, Collections.singleton(expectedRuleConfig)).get(expectedRuleConfig); + } + + @Test + void assertUnmarshal() throws IOException, URISyntaxException { + URL url = Thread.currentThread().getContextClassLoader().getResource(yamlFile); + assertNotNull(url); + YamlRootConfiguration yamlRootConfigFromFile = YamlEngine.unmarshal(new File(url.getFile()), YamlRootConfiguration.class); + YamlRootConfiguration yamlRootConfigFromBytes = YamlEngine.unmarshal( + Files.readAllLines(Paths.get(url.toURI())).stream().collect(Collectors.joining(System.lineSeparator())).getBytes(), YamlRootConfiguration.class); + assertTrue(DeepEquals.deepEquals(yamlRootConfigFromFile, yamlRootConfigFromBytes)); + } + + @SuppressWarnings("unchecked") @Test - void assertUnmarshalWithYamlFile() throws IOException { + void assertSwapToYamlConfiguration() throws IOException { URL url = Thread.currentThread().getContextClassLoader().getResource(yamlFile); assertNotNull(url); YamlRootConfiguration actual = YamlEngine.unmarshal(new File(url.getFile()), YamlRootConfiguration.class); assertThat(actual.getRules().size(), is(1)); - assertYamlRootConfiguration(actual); + YamlRuleConfiguration actualYAMLRuleConfig = actual.getRules().iterator().next(); + YamlRuleConfiguration expectedYAMLRuleConfig = (YamlRuleConfiguration) swapper.swapToYamlConfiguration(expectedRuleConfig); + if (!assertYamlConfiguration(actualYAMLRuleConfig)) { + assertTrue(DeepEquals.deepEquals(actualYAMLRuleConfig, expectedYAMLRuleConfig)); + } } + @SuppressWarnings("unchecked") @Test - void assertUnmarshalWithYamlBytes() throws IOException, URISyntaxException { + void assertSwapToObject() throws IOException { URL url = Thread.currentThread().getContextClassLoader().getResource(yamlFile); assertNotNull(url); - StringBuilder yamlContent = new StringBuilder(); - try (BufferedReader reader = Files.newBufferedReader(Paths.get(url.toURI()))) { - String line; - while (null != (line = reader.readLine())) { - yamlContent.append(line).append(System.lineSeparator()); - } - } - YamlRootConfiguration actual = YamlEngine.unmarshal(yamlContent.toString().getBytes(), YamlRootConfiguration.class); - assertThat(actual.getRules().size(), is(1)); - assertYamlRootConfiguration(actual); + YamlRootConfiguration actual = YamlEngine.unmarshal(new File(url.getFile()), YamlRootConfiguration.class); + RuleConfiguration actualRuleConfig = (RuleConfiguration) swapper.swapToObject(actual.getRules().iterator().next()); + assertTrue(DeepEquals.deepEquals(actualRuleConfig, expectedRuleConfig)); } - protected abstract void assertYamlRootConfiguration(YamlRootConfiguration actual); + // TODO should remove the method when yaml rule swapper fixed by map's key + protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) { + return false; + } } diff --git a/test/util/src/main/java/org/apache/shardingsphere/test/util/PropertiesBuilder.java b/test/util/src/main/java/org/apache/shardingsphere/test/util/PropertiesBuilder.java index f289a827e4a85..81e8f6c042281 100644 --- a/test/util/src/main/java/org/apache/shardingsphere/test/util/PropertiesBuilder.java +++ b/test/util/src/main/java/org/apache/shardingsphere/test/util/PropertiesBuilder.java @@ -38,7 +38,12 @@ public final class PropertiesBuilder { public static Properties build(final Property... props) { Properties result = new Properties(); for (Property each : props) { - result.setProperty(each.key, each.value); + if (each.value instanceof String) { + result.setProperty(each.key, each.value.toString()); + } else { + result.put(each.key, each.value); + } + } return result; } @@ -51,6 +56,6 @@ public static class Property { private final String key; - private final String value; + private final Object value; } }