From d03931f1b2a30c22664156d485eeffdba9c83ea7 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 27 Sep 2024 17:12:54 +0800 Subject: [PATCH 1/8] Revise #33024 --- .../core/connection/ShardingSphereConnection.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) 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..0de9142b55b23 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,7 +265,7 @@ 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()}. @@ -275,18 +275,13 @@ 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. */ @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 From 5558c622f7174999a4356c9b24675f7e4de78d79 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 27 Sep 2024 17:18:25 +0800 Subject: [PATCH 2/8] Revise #33024 --- .../jdbc/core/connection/ShardingSphereConnection.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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 0de9142b55b23..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 @@ -266,9 +266,8 @@ public void setReadOnly(final boolean readOnly) throws SQLException { } /* - * 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() { @@ -276,8 +275,8 @@ public int getNetworkTimeout() { } /* - * This is just to avoid the Warning in brettwooldridge/HikariCP#2196. - * ShardingSphere does not propagate this property to the real JDBC Driver. + * 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 { From bc40aa3b27b9a565b7ed91bbb574f4a1249dd84b Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 28 Sep 2024 00:03:37 +0800 Subject: [PATCH 3/8] Refactor YamlRuleConfigurationUnmarshalIT --- ...dcastRuleConfigurationYamlUnmarshalIT.java | 23 +--- ...cryptRuleConfigurationYamlUnmarshalIT.java | 32 ++++- .../MaskRuleConfigurationYamlUnmarshalIT.java | 32 ++++- ...ttingRuleConfigurationYamlUnmarshalIT.java | 30 ++++- .../ShadowDataSourceConfiguration.java | 2 +- ...hadowRuleConfigurationYamlUnmarshalIT.java | 119 +++++------------- ...rdingRuleConfigurationYamlUnmarshalIT.java | 88 ++++++++++--- .../test/resources/yaml/sharding-rule.yaml | 5 +- ...orityRuleConfigurationYamlUnmarshalIT.java | 61 ++------- .../test/resources/yaml/authority-rule.yaml | 2 +- ...ClockRuleConfigurationYamlUnmarshalIT.java | 27 +--- ...ggingRuleConfigurationYamlUnmarshalIT.java | 52 ++------ ...mlLoggingRuleConfigurationSwapperTest.java | 65 ---------- ...ingleRuleConfigurationYamlUnmarshalIT.java | 25 ++-- ...ationRuleConfigurationYamlUnmarshalIT.java | 29 +---- ...arserRuleConfigurationYamlUnmarshalIT.java | 32 +---- .../test/resources/yaml/sql-parser-rule.yaml | 6 +- kernel/sql-translator/core/pom.xml | 6 + ...latorRuleConfigurationYamlUnmarshalIT.java | 26 +--- .../resources/yaml/sql-translator-rule.yaml | 2 +- kernel/transaction/core/pom.xml | 6 + ...ctionRuleConfigurationYamlUnmarshalIT.java | 25 +--- .../YamlRuleConfigurationUnmarshalIT.java | 64 +++++++--- .../test/util/PropertiesBuilder.java | 9 +- 24 files changed, 310 insertions(+), 458 deletions(-) delete mode 100644 kernel/logging/core/src/test/java/org/apache/shardingsphere/logging/yaml/swapper/YamlLoggingRuleConfigurationSwapperTest.java 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..3ee311a5ed6dd 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 java.util.Arrays; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -class BroadcastRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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..d732b5769cc09 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,26 +17,48 @@ 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; import static org.hamcrest.MatcherAssert.assertThat; -class EncryptRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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(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..d99191b5a5678 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,27 +17,49 @@ 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; import static org.junit.jupiter.api.Assertions.assertTrue; -class MaskRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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(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..fedcffbe649fb 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 { +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(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..786419f0c7c92 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 java.util.Collections; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class ShadowRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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/ShardingRuleConfigurationYamlUnmarshalIT.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/it/ShardingRuleConfigurationYamlUnmarshalIT.java index fd332477bc29e..a46607e0ee706 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,38 +17,89 @@ 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; import static org.junit.jupiter.api.Assertions.assertTrue; -class ShardingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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(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.yaml b/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml index 5fc2064d4eb77..a7a325eaf9f5b 100644 --- a/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml +++ b/features/sharding/core/src/test/resources/yaml/sharding-rule.yaml @@ -52,7 +52,6 @@ rules: shardingColumn: order_id shardingAlgorithmName: core_standard_fixture bindingTables: - - t_order, t_order_item - foo:t_order, t_order_item defaultDatabaseStrategy: standard: @@ -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/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..1ed15b802516b 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 java.util.Arrays; +import java.util.Collections; +import java.util.Properties; -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; - -class AuthorityRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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..ea2e5013bcf96 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 org.apache.shardingsphere.test.util.PropertiesBuilder; +import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class GlobalClockRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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..ed2a51268c3dd 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 java.util.Collections; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class LoggingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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/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..77a7dfc5166ca 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 { +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..a2bc5a7ad3a0d 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 { +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..13fb77741b102 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 { +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..850f9871685ca 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 org.apache.shardingsphere.test.util.PropertiesBuilder; +import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; - -class SQLTranslatorRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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..c3a48f54b4d7f 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 org.apache.shardingsphere.test.util.PropertiesBuilder; +import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; +import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - -class TransactionRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +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..42b23248534d5 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 { +public abstract class YamlRuleConfigurationUnmarshalIT { private final String yamlFile; + private final T expectedRuleConfig; + + @SuppressWarnings("rawtypes") + private final YamlRuleConfigurationSwapper swapper; + + public YamlRuleConfigurationUnmarshalIT(final String yamlFile, final T 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); + T actualRuleConfig = (T) swapper.swapToObject(actual.getRules().iterator().next()); + assertTrue(DeepEquals.deepEquals(actualRuleConfig, expectedRuleConfig)); } - protected abstract void assertYamlRootConfiguration(YamlRootConfiguration actual); + // TODO should remove it when yaml rule swapper fixed by map's key + protected boolean assertYamlConfiguration(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; } } From 1a19ef9ce58b9214538f8478a6e364c7f609ae1b Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 28 Sep 2024 00:07:53 +0800 Subject: [PATCH 4/8] Refactor YamlRuleConfigurationUnmarshalIT --- .../it/BroadcastRuleConfigurationYamlUnmarshalIT.java | 2 +- .../it/EncryptRuleConfigurationYamlUnmarshalIT.java | 2 +- .../mask/it/MaskRuleConfigurationYamlUnmarshalIT.java | 2 +- ...eadwriteSplittingRuleConfigurationYamlUnmarshalIT.java | 2 +- .../shadow/it/ShadowRuleConfigurationYamlUnmarshalIT.java | 2 +- .../it/ShardingRuleConfigurationYamlUnmarshalIT.java | 2 +- .../fixture/YamlRuleConfigurationSwapperFixture.java | 2 +- .../it/AuthorityRuleConfigurationYamlUnmarshalIT.java | 2 +- .../it/GlobalClockRuleConfigurationYamlUnmarshalIT.java | 2 +- .../it/LoggingRuleConfigurationYamlUnmarshalIT.java | 2 +- .../fixture/YamlRuleConfigurationSwapperFixture.java | 2 +- .../single/it/SingleRuleConfigurationYamlUnmarshalIT.java | 2 +- .../it/SQLFederationRuleConfigurationYamlUnmarshalIT.java | 2 +- .../it/SQLParserRuleConfigurationYamlUnmarshalIT.java | 2 +- .../it/SQLTranslatorRuleConfigurationYamlUnmarshalIT.java | 2 +- .../it/TransactionRuleConfigurationYamlUnmarshalIT.java | 2 +- .../test/it/yaml/YamlRuleConfigurationUnmarshalIT.java | 8 ++++---- 17 files changed, 20 insertions(+), 20 deletions(-) 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 3ee311a5ed6dd..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 @@ -22,7 +22,7 @@ import java.util.Arrays; -class BroadcastRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class BroadcastRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { BroadcastRuleConfigurationYamlUnmarshalIT() { 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 d732b5769cc09..f217cf4cb8a8c 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 @@ -38,7 +38,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -class EncryptRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class EncryptRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { EncryptRuleConfigurationYamlUnmarshalIT() { super("yaml/encrypt-rule.yaml", getExpectedRuleConfiguration()); 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 d99191b5a5678..75f294a1bd04a 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 @@ -40,7 +40,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -class MaskRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class MaskRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { MaskRuleConfigurationYamlUnmarshalIT() { super("yaml/mask-rule.yaml", getExpectedRuleConfiguration()); 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 fedcffbe649fb..484d0a63743a9 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 @@ -33,7 +33,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -class ReadwriteSplittingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class ReadwriteSplittingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { ReadwriteSplittingRuleConfigurationYamlUnmarshalIT() { super("yaml/readwrite-splitting-rule.yaml", getExpectedRuleConfiguration()); 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 786419f0c7c92..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 @@ -28,7 +28,7 @@ import java.util.Arrays; import java.util.Collections; -class ShadowRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class ShadowRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { ShadowRuleConfigurationYamlUnmarshalIT() { super("yaml/shadow-rule.yaml", getExpectedRuleConfiguration()); 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 a46607e0ee706..cd044384588c6 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 @@ -45,7 +45,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -class ShardingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class ShardingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { ShardingRuleConfigurationYamlUnmarshalIT() { super("yaml/sharding-rule.yaml", getExpectedRuleConfiguration()); 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/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 1ed15b802516b..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 @@ -26,7 +26,7 @@ import java.util.Collections; import java.util.Properties; -class AuthorityRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class AuthorityRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { AuthorityRuleConfigurationYamlUnmarshalIT() { super("yaml/authority-rule.yaml", getExpectedRuleConfiguration()); 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 ea2e5013bcf96..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 @@ -22,7 +22,7 @@ import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -class GlobalClockRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class GlobalClockRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { GlobalClockRuleConfigurationYamlUnmarshalIT() { 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 ed2a51268c3dd..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 @@ -24,7 +24,7 @@ import java.util.Collections; -class LoggingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class LoggingRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { LoggingRuleConfigurationYamlUnmarshalIT() { super("yaml/logging-rule.yaml", getExpectedRuleConfiguration()); 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 77a7dfc5166ca..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 @@ -20,7 +20,7 @@ import org.apache.shardingsphere.single.config.SingleRuleConfiguration; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; -class SingleRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class SingleRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { SingleRuleConfigurationYamlUnmarshalIT() { super("yaml/single-rule.yaml", getExpectedRuleConfiguration()); 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 a2bc5a7ad3a0d..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 @@ -21,7 +21,7 @@ import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; -class SQLFederationRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class SQLFederationRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { SQLFederationRuleConfigurationYamlUnmarshalIT() { 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 13fb77741b102..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 @@ -21,7 +21,7 @@ import org.apache.shardingsphere.sql.parser.api.CacheOption; import org.apache.shardingsphere.test.it.yaml.YamlRuleConfigurationUnmarshalIT; -class SQLParserRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class SQLParserRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { SQLParserRuleConfigurationYamlUnmarshalIT() { super("yaml/sql-parser-rule.yaml", new SQLParserRuleConfiguration(new CacheOption(128, 1024), new CacheOption(256, 4096))); 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 850f9871685ca..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 @@ -22,7 +22,7 @@ import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -class SQLTranslatorRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class SQLTranslatorRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { SQLTranslatorRuleConfigurationYamlUnmarshalIT() { super("yaml/sql-translator-rule.yaml", new SQLTranslatorRuleConfiguration("FIXTURE", PropertiesBuilder.build(new Property("k0", "v0"), new Property("k1", "v1")), true)); 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 c3a48f54b4d7f..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 @@ -22,7 +22,7 @@ import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration; -class TransactionRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { +class TransactionRuleConfigurationYamlUnmarshalIT extends YamlRuleConfigurationUnmarshalIT { TransactionRuleConfigurationYamlUnmarshalIT() { 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 42b23248534d5..0b51f9d403933 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 @@ -40,16 +40,16 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -public abstract class YamlRuleConfigurationUnmarshalIT { +public abstract class YamlRuleConfigurationUnmarshalIT { private final String yamlFile; - private final T expectedRuleConfig; + private final RuleConfiguration expectedRuleConfig; @SuppressWarnings("rawtypes") private final YamlRuleConfigurationSwapper swapper; - public YamlRuleConfigurationUnmarshalIT(final String yamlFile, final T expectedRuleConfig) { + public YamlRuleConfigurationUnmarshalIT(final String yamlFile, final RuleConfiguration expectedRuleConfig) { this.yamlFile = yamlFile; this.expectedRuleConfig = expectedRuleConfig; swapper = OrderedSPILoader.getServices(YamlRuleConfigurationSwapper.class, Collections.singleton(expectedRuleConfig)).get(expectedRuleConfig); @@ -85,7 +85,7 @@ void assertSwapToObject() throws IOException { URL url = Thread.currentThread().getContextClassLoader().getResource(yamlFile); assertNotNull(url); YamlRootConfiguration actual = YamlEngine.unmarshal(new File(url.getFile()), YamlRootConfiguration.class); - T actualRuleConfig = (T) swapper.swapToObject(actual.getRules().iterator().next()); + RuleConfiguration actualRuleConfig = (RuleConfiguration) swapper.swapToObject(actual.getRules().iterator().next()); assertTrue(DeepEquals.deepEquals(actualRuleConfig, expectedRuleConfig)); } From 59a80173027201a5f19b43d0726cdb801305af53 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 28 Sep 2024 00:11:57 +0800 Subject: [PATCH 5/8] Refactor YamlRuleConfigurationUnmarshalIT --- .../test/it/yaml/YamlRuleConfigurationUnmarshalIT.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 0b51f9d403933..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 @@ -89,8 +89,8 @@ void assertSwapToObject() throws IOException { assertTrue(DeepEquals.deepEquals(actualRuleConfig, expectedRuleConfig)); } - // TODO should remove it when yaml rule swapper fixed by map's key - protected boolean assertYamlConfiguration(YamlRuleConfiguration actual) { + // TODO should remove the method when yaml rule swapper fixed by map's key + protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) { return false; } } From 8da590a6a54272d5ed49a3177cc65aacb1be0630 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 28 Sep 2024 00:18:16 +0800 Subject: [PATCH 6/8] Refactor YamlRuleConfigurationUnmarshalIT --- ...urationRepositoryTupleSwapperEngineIT.java | 2 +- ...rdingRuleConfigurationYamlUnmarshalIT.java | 1 + .../yaml/sharding-rule-for-tuple.yaml | 101 ++++++++++++++++++ .../test/resources/yaml/sharding-rule.yaml | 2 +- 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 features/sharding/core/src/test/resources/yaml/sharding-rule-for-tuple.yaml 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 cd044384588c6..f24cf6d5d4173 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 @@ -68,6 +68,7 @@ private static ShardingRuleConfiguration getExpectedRuleConfiguration() { 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(null, "t_order, t_order_item")); result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", "t_order, t_order_item")); result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "database_inline")); result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); 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 a7a325eaf9f5b..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,7 +52,7 @@ rules: shardingColumn: order_id shardingAlgorithmName: core_standard_fixture bindingTables: - - foo:t_order, t_order_item + - foo:t_order, t_order_item defaultDatabaseStrategy: standard: shardingColumn: order_id From 85c6756744971bd1600ebac3ff80c0870ba7a076 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 28 Sep 2024 00:19:07 +0800 Subject: [PATCH 7/8] Refactor YamlRuleConfigurationUnmarshalIT --- .../sharding/it/ShardingRuleConfigurationYamlUnmarshalIT.java | 1 - 1 file changed, 1 deletion(-) 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 f24cf6d5d4173..cd044384588c6 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 @@ -68,7 +68,6 @@ private static ShardingRuleConfiguration getExpectedRuleConfiguration() { 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(null, "t_order, t_order_item")); result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", "t_order, t_order_item")); result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "database_inline")); result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); From b7761028619b98e2226924ceeb39c12cf10a044c Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 28 Sep 2024 00:22:10 +0800 Subject: [PATCH 8/8] Refactor YamlRuleConfigurationUnmarshalIT --- .../encrypt/it/EncryptRuleConfigurationYamlUnmarshalIT.java | 2 +- .../mask/it/MaskRuleConfigurationYamlUnmarshalIT.java | 2 +- .../it/ReadwriteSplittingRuleConfigurationYamlUnmarshalIT.java | 2 +- .../sharding/it/ShardingRuleConfigurationYamlUnmarshalIT.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) 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 f217cf4cb8a8c..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 @@ -56,7 +56,7 @@ private static EncryptRuleConfiguration getExpectedRuleConfiguration() { } @Override - protected boolean assertYamlConfiguration(YamlRuleConfiguration actual) { + protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) { assertEncryptRule((YamlEncryptRuleConfiguration) actual); return true; } 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 75f294a1bd04a..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 @@ -57,7 +57,7 @@ private static MaskRuleConfiguration getExpectedRuleConfiguration() { } @Override - protected boolean assertYamlConfiguration(YamlRuleConfiguration actual) { + protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) { assertMaskRule((YamlMaskRuleConfiguration) actual); return true; } 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 484d0a63743a9..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 @@ -50,7 +50,7 @@ private static ReadwriteSplittingRuleConfiguration getExpectedRuleConfiguration( } @Override - protected boolean assertYamlConfiguration(YamlRuleConfiguration actual) { + protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) { assertReadwriteSplittingRule((YamlReadwriteSplittingRuleConfiguration) actual); return true; } 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 cd044384588c6..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 @@ -86,7 +86,7 @@ private static ShardingRuleConfiguration getExpectedRuleConfiguration() { } @Override - protected boolean assertYamlConfiguration(YamlRuleConfiguration actual) { + protected boolean assertYamlConfiguration(final YamlRuleConfiguration actual) { assertShardingRule((YamlShardingRuleConfiguration) actual); return true; }