This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new f1dbeca9081 Add test cases on RuleItemConfigurationChangedProcessor's
impl (#33485)
f1dbeca9081 is described below
commit f1dbeca90814a990fbff58490d6895f20f7fb9bb
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Oct 31 22:52:16 2024 +0800
Add test cases on RuleItemConfigurationChangedProcessor's impl (#33485)
* Add test cases on RuleItemConfigurationChangedProcessor's impl
* Add test cases on RuleItemConfigurationChangedProcessor's impl
* Add test cases on RuleItemConfigurationChangedProcessor's impl
* Add test cases on RuleItemConfigurationChangedProcessor's impl
* Add test cases on RuleItemConfigurationChangedProcessor's impl
* Add test cases on RuleItemConfigurationChangedProcessor's impl
* Add test cases on RuleItemConfigurationChangedProcessor's impl
* Add test cases on RuleItemConfigurationChangedProcessor's impl
* Add test cases on RuleItemConfigurationChangedProcessor's impl
---
.../changed/EncryptorChangedProcessorTest.java | 92 +++++++++++++++++++++
.../changed/MaskAlgorithmChangedProcessor.java | 7 +-
.../rule/changed/MaskTableChangedProcessor.java | 7 +-
.../changed/MaskAlgorithmChangedProcessorTest.java | 49 +++++------
.../changed/MaskTableChangedProcessorTest.java | 57 +++++++------
...ultShadowAlgorithmNameChangedProcessorTest.java | 79 ++++++++++++++++++
.../ShadowAlgorithmChangedProcessorTest.java | 94 ++++++++++++++++++++++
.../DefaultShardingColumnChangedProcessorTest.java | 79 ++++++++++++++++++
.../changed/KeyGeneratorChangedProcessorTest.java | 94 ++++++++++++++++++++++
.../ShardingAlgorithmChangedProcessorTest.java | 94 ++++++++++++++++++++++
.../ShardingAuditorChangedProcessorTest.java | 94 ++++++++++++++++++++++
11 files changed, 684 insertions(+), 62 deletions(-)
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/changed/EncryptorChangedProcessorTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/changed/EncryptorChangedProcessorTest.java
new file mode 100644
index 00000000000..3484a0a79c2
--- /dev/null
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/changed/EncryptorChangedProcessorTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.encrypt.rule.changed;
+
+import org.apache.shardingsphere.encrypt.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Properties;
+
+import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class EncryptorChangedProcessorTest {
+
+ @SuppressWarnings("unchecked")
+ private final
RuleItemConfigurationChangedProcessor<EncryptRuleConfiguration,
AlgorithmConfiguration> processor = TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class, "encrypt.encryptors");
+
+ @Test
+ void assertSwapRuleItemConfiguration() {
+ AlgorithmConfiguration actual =
processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl",
"", "", ""), createYAMLContent());
+ assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo",
new Properties())));
+ }
+
+ private String createYAMLContent() {
+ YamlAlgorithmConfiguration yamlConfig = new
YamlAlgorithmConfiguration();
+ yamlConfig.setType("foo_algo");
+ return YamlEngine.marshal(yamlConfig);
+ }
+
+ @Test
+ void assertFindRuleConfiguration() {
+ EncryptRuleConfiguration ruleConfig =
mock(EncryptRuleConfiguration.class);
+ assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
+ }
+
+ private ShardingSphereDatabase mockDatabase(final EncryptRuleConfiguration
ruleConfig) {
+ EncryptRule rule = mock(EncryptRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ return result;
+ }
+
+ @Test
+ void assertChangeRuleItemConfiguration() {
+ EncryptRuleConfiguration currentRuleConfig = new
EncryptRuleConfiguration(Collections.emptyList(), new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(
+ new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""),
currentRuleConfig, toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getEncryptors().size(), is(1));
+
assertThat(currentRuleConfig.getEncryptors().get("foo_algo").getType(),
is("FIXTURE"));
+ }
+
+ @Test
+ void assertDropRuleItemConfiguration() {
+ EncryptRuleConfiguration currentRuleConfig = new
EncryptRuleConfiguration(Collections.emptyList(), new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_algo", ""), currentRuleConfig);
+ assertTrue(currentRuleConfig.getEncryptors().isEmpty());
+ }
+}
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessor.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessor.java
index 24e9d25a8e2..b4c57432a22 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessor.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessor.java
@@ -46,12 +46,7 @@ public final class MaskAlgorithmChangedProcessor implements
RuleItemConfiguratio
@Override
public MaskRuleConfiguration findRuleConfiguration(final
ShardingSphereDatabase database) {
- return database.getRuleMetaData().findSingleRule(MaskRule.class)
- .map(optional ->
getConfiguration(optional.getConfiguration())).orElseGet(() -> new
MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
- }
-
- private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration
ruleConfig) {
- return null == ruleConfig.getMaskAlgorithms() ? new
MaskRuleConfiguration(ruleConfig.getTables(), new LinkedHashMap<>()) :
ruleConfig;
+ return
database.getRuleMetaData().findSingleRule(MaskRule.class).map(MaskRule::getConfiguration).orElseGet(()
-> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
}
@Override
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessor.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessor.java
index 62eb502a4a1..8ee7950c8c1 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessor.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessor.java
@@ -45,12 +45,7 @@ public final class MaskTableChangedProcessor implements
RuleItemConfigurationCha
@Override
public MaskRuleConfiguration findRuleConfiguration(final
ShardingSphereDatabase database) {
- return database.getRuleMetaData().findSingleRule(MaskRule.class)
- .map(optional ->
getConfiguration(optional.getConfiguration())).orElseGet(() -> new
MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
- }
-
- private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration
config) {
- return null == config.getTables() ? new MaskRuleConfiguration(new
LinkedList<>(), config.getMaskAlgorithms()) : config;
+ return
database.getRuleMetaData().findSingleRule(MaskRule.class).map(MaskRule::getConfiguration).orElseGet(()
-> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
}
@Override
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java
index bc5a8057a6d..af27dadc863 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java
@@ -18,8 +18,11 @@
package org.apache.shardingsphere.mask.rule.changed;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.rule.MaskRule;
@@ -31,47 +34,45 @@ import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
-import java.util.Optional;
import java.util.Properties;
+import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class MaskAlgorithmChangedProcessorTest {
- private final MaskAlgorithmChangedProcessor processor =
(MaskAlgorithmChangedProcessor)
TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class,
"mask.mask_algorithms");
+ @SuppressWarnings("unchecked")
+ private final RuleItemConfigurationChangedProcessor<MaskRuleConfiguration,
AlgorithmConfiguration> processor = TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class,
"mask.mask_algorithms");
@Test
void assertSwapRuleItemConfiguration() {
-
assertThat(processor.swapRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class),
"type: TEST").getType(), is("TEST"));
+ AlgorithmConfiguration actual =
processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "",
"", ""), createYAMLContent());
+ assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo",
new Properties())));
}
- @Test
- void assertFindRuleConfigurationWhenRuleDoesNotExist() {
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
-
when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.empty());
-
assertTrue(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty());
+ private String createYAMLContent() {
+ YamlAlgorithmConfiguration yamlConfig = new
YamlAlgorithmConfiguration();
+ yamlConfig.setType("foo_algo");
+ return YamlEngine.marshal(yamlConfig);
}
@Test
- void assertFindRuleConfigurationWhenMaskAlgorithmDoesNotExist() {
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
-
when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(new
MaskRule(new MaskRuleConfiguration(Collections.emptyList(),
Collections.emptyMap()))));
-
assertTrue(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty());
+ void assertFindRuleConfiguration() {
+ MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class);
+ assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
}
- @Test
- void assertFindRuleConfigurationWhenRuleExists() {
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
- MaskRule maskRule = mock(MaskRule.class, RETURNS_DEEP_STUBS);
-
when(maskRule.getConfiguration().getMaskAlgorithms()).thenReturn(Collections.singletonMap("foo",
new AlgorithmConfiguration("FOO", new Properties())));
-
when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(maskRule));
-
assertFalse(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty());
+ private ShardingSphereDatabase mockDatabase(final MaskRuleConfiguration
ruleConfig) {
+ MaskRule rule = mock(MaskRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ return result;
}
@Test
@@ -80,15 +81,15 @@ class MaskAlgorithmChangedProcessorTest {
new LinkedList<>(Collections.singleton(new
MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))), new
HashMap<>());
AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("FIXTURE", new Properties());
processor.changeRuleItemConfiguration(
- new AlterNamedRuleItemEvent("foo_db", "foo_algo", "key", "0",
""), currentRuleConfig, toBeChangedItemConfig);
+ new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""),
currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(1));
assertThat(currentRuleConfig.getMaskAlgorithms().get("foo_algo").getType(),
is("FIXTURE"));
}
@Test
void assertDropRuleItemConfiguration() {
- MaskRuleConfiguration currentRuleConfig = new
MaskRuleConfiguration(Collections.emptyList(), new
HashMap<>(Collections.singletonMap("type: TEST",
mock(AlgorithmConfiguration.class))));
- processor.dropRuleItemConfiguration(new
DropNamedRuleItemEvent("foo_db", "type: TEST", ""), currentRuleConfig);
+ MaskRuleConfiguration currentRuleConfig = new
MaskRuleConfiguration(Collections.emptyList(), new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_algo", ""), currentRuleConfig);
assertTrue(currentRuleConfig.getMaskAlgorithms().isEmpty());
}
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java
index d9adee29c8d..ada208141a7 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java
@@ -18,59 +18,65 @@
package org.apache.shardingsphere.mask.rule.changed;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
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.rule.MaskRule;
+import
org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskColumnRuleConfiguration;
+import
org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
import
org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
-import
org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterRuleItemEvent;
import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
import org.junit.jupiter.api.Test;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
-import java.util.Optional;
+import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class MaskTableChangedProcessorTest {
- private final MaskTableChangedProcessor processor =
(MaskTableChangedProcessor)
TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class,
"mask.tables");
+ @SuppressWarnings("unchecked")
+ private final RuleItemConfigurationChangedProcessor<MaskRuleConfiguration,
MaskTableRuleConfiguration> processor = TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class, "mask.tables");
@Test
void assertSwapRuleItemConfiguration() {
-
assertThat(processor.swapRuleItemConfiguration(mock(AlterRuleItemEvent.class),
"name: test_table").getName(), is("test_table"));
+ MaskTableRuleConfiguration actual =
processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl",
"", "", ""), createYAMLContent());
+ assertThat(actual, deepEqual(new MaskTableRuleConfiguration("foo_tbl",
Collections.singletonList(new MaskColumnRuleConfiguration("foo_col",
"foo_algo")))));
}
- @Test
- void assertFindRuleConfigurationWhenRuleDoesNotExist() {
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
-
when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.empty());
-
assertTrue(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty());
+ private String createYAMLContent() {
+ YamlMaskTableRuleConfiguration yamlConfig = new
YamlMaskTableRuleConfiguration();
+ yamlConfig.setName("foo_tbl");
+ YamlMaskColumnRuleConfiguration yamlColumnRuleConfig = new
YamlMaskColumnRuleConfiguration();
+ yamlColumnRuleConfig.setLogicColumn("foo_col");
+ yamlColumnRuleConfig.setMaskAlgorithm("foo_algo");
+ yamlConfig.setColumns(Collections.singletonMap("foo_col",
yamlColumnRuleConfig));
+ return YamlEngine.marshal(yamlConfig);
}
@Test
- void assertFindRuleConfigurationWhenTableDoesNotExist() {
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
-
when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(new
MaskRule(new MaskRuleConfiguration(Collections.emptyList(),
Collections.emptyMap()))));
-
assertTrue(processor.findRuleConfiguration(database).getTables().isEmpty());
+ void assertFindRuleConfiguration() {
+ MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class);
+ assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
}
- @Test
- void assertFindRuleConfigurationWhenRuleExists() {
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
- MaskRule maskRule = mock(MaskRule.class, RETURNS_DEEP_STUBS);
-
when(maskRule.getConfiguration().getTables()).thenReturn(Collections.singleton(new
MaskTableRuleConfiguration("foo_tbl", Collections.emptyList())));
-
when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(maskRule));
-
assertFalse(processor.findRuleConfiguration(database).getTables().isEmpty());
+ private ShardingSphereDatabase mockDatabase(final MaskRuleConfiguration
ruleConfig) {
+ MaskRule rule = mock(MaskRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ return result;
}
@Test
@@ -78,17 +84,16 @@ class MaskTableChangedProcessorTest {
MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(
new LinkedList<>(Collections.singleton(new
MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))),
Collections.emptyMap());
MaskTableRuleConfiguration toBeChangedItemConfig = new
MaskTableRuleConfiguration("foo_tbl",
Collections.singleton(mock(MaskColumnRuleConfiguration.class)));
- processor.changeRuleItemConfiguration(
- new AlterNamedRuleItemEvent("foo_db", "foo_tbl", "key", "0",
""), currentRuleConfig, toBeChangedItemConfig);
+ processor.changeRuleItemConfiguration(new AlterNamedRuleItemEvent("",
"foo_tbl", "", "", ""), currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getTables().size(), is(1));
-
assertThat(currentRuleConfig.getTables().iterator().next().getColumns().size(),
is(1));
+ assertThat(new
ArrayList<>(currentRuleConfig.getTables()).get(0).getColumns().size(), is(1));
}
@Test
void assertDropRuleItemConfiguration() {
MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(
new LinkedList<>(Collections.singleton(new
MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))),
Collections.emptyMap());
- processor.dropRuleItemConfiguration(new
DropNamedRuleItemEvent("foo_db", "foo_tbl", ""), currentRuleConfig);
+ processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_tbl", ""), currentRuleConfig);
assertTrue(currentRuleConfig.getTables().isEmpty());
}
}
diff --git
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/DefaultShadowAlgorithmNameChangedProcessorTest.java
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/DefaultShadowAlgorithmNameChangedProcessorTest.java
new file mode 100644
index 00000000000..57981b4b09f
--- /dev/null
+++
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/DefaultShadowAlgorithmNameChangedProcessorTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.shadow.rule.changed;
+
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterRuleItemEvent;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent;
+import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultShadowAlgorithmNameChangedProcessorTest {
+
+ @SuppressWarnings("unchecked")
+ private final
RuleItemConfigurationChangedProcessor<ShadowRuleConfiguration, String>
processor = TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class,
"shadow.default_shadow_algorithm_name");
+
+ @Test
+ void assertSwapRuleItemConfiguration() {
+ String actual =
processor.swapRuleItemConfiguration(mock(AlterRuleItemEvent.class), "foo_algo");
+ assertThat(actual, is("foo_algo"));
+ }
+
+ @Test
+ void assertFindRuleConfiguration() {
+ ShadowRuleConfiguration ruleConfig =
mock(ShadowRuleConfiguration.class);
+ assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
+ }
+
+ private ShardingSphereDatabase mockDatabase(final ShadowRuleConfiguration
ruleConfig) {
+ ShadowRule rule = mock(ShadowRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ return result;
+ }
+
+ @Test
+ void assertChangeRuleItemConfiguration() {
+ ShadowRuleConfiguration currentRuleConfig = new
ShadowRuleConfiguration();
+ String toBeChangedItemConfig = "bar_algo";
+ processor.changeRuleItemConfiguration(mock(AlterRuleItemEvent.class),
currentRuleConfig, toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getDefaultShadowAlgorithmName(),
is("bar_algo"));
+ }
+
+ @Test
+ void assertDropRuleItemConfiguration() {
+ ShadowRuleConfiguration currentRuleConfig = new
ShadowRuleConfiguration();
+ currentRuleConfig.setDefaultShadowAlgorithmName("foo_algo");
+ processor.dropRuleItemConfiguration(mock(DropRuleItemEvent.class),
currentRuleConfig);
+ assertNull(currentRuleConfig.getDefaultShadowAlgorithmName());
+ }
+}
diff --git
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/ShadowAlgorithmChangedProcessorTest.java
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/ShadowAlgorithmChangedProcessorTest.java
new file mode 100644
index 00000000000..d0f9a8b56d6
--- /dev/null
+++
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/ShadowAlgorithmChangedProcessorTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.shadow.rule.changed;
+
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Properties;
+
+import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ShadowAlgorithmChangedProcessorTest {
+
+ @SuppressWarnings("unchecked")
+ private final
RuleItemConfigurationChangedProcessor<ShadowRuleConfiguration,
AlgorithmConfiguration> processor = TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class,
"shadow.shadow_algorithms");
+
+ @Test
+ void assertSwapRuleItemConfiguration() {
+ AlgorithmConfiguration actual =
processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl",
"", "", ""), createYAMLContent());
+ assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo",
new Properties())));
+ }
+
+ private String createYAMLContent() {
+ YamlAlgorithmConfiguration yamlConfig = new
YamlAlgorithmConfiguration();
+ yamlConfig.setType("foo_algo");
+ return YamlEngine.marshal(yamlConfig);
+ }
+
+ @Test
+ void assertFindRuleConfiguration() {
+ ShadowRuleConfiguration ruleConfig =
mock(ShadowRuleConfiguration.class);
+ assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
+ }
+
+ private ShardingSphereDatabase mockDatabase(final ShadowRuleConfiguration
ruleConfig) {
+ ShadowRule rule = mock(ShadowRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ return result;
+ }
+
+ @Test
+ void assertChangeRuleItemConfiguration() {
+ ShadowRuleConfiguration currentRuleConfig = new
ShadowRuleConfiguration();
+ currentRuleConfig.setShadowAlgorithms(new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(
+ new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""),
currentRuleConfig, toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getShadowAlgorithms().size(), is(1));
+
assertThat(currentRuleConfig.getShadowAlgorithms().get("foo_algo").getType(),
is("FIXTURE"));
+ }
+
+ @Test
+ void assertDropRuleItemConfiguration() {
+ ShadowRuleConfiguration currentRuleConfig = new
ShadowRuleConfiguration();
+ currentRuleConfig.setShadowAlgorithms(new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_algo", ""), currentRuleConfig);
+ assertTrue(currentRuleConfig.getShadowAlgorithms().isEmpty());
+ }
+}
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/DefaultShardingColumnChangedProcessorTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/DefaultShardingColumnChangedProcessorTest.java
new file mode 100644
index 00000000000..406ecf9b44c
--- /dev/null
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/DefaultShardingColumnChangedProcessorTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.sharding.rule.changed;
+
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterRuleItemEvent;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent;
+import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultShardingColumnChangedProcessorTest {
+
+ @SuppressWarnings("unchecked")
+ private final
RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration, String>
processor = TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class,
"sharding.default_sharding_column");
+
+ @Test
+ void assertSwapRuleItemConfiguration() {
+ String actual =
processor.swapRuleItemConfiguration(mock(AlterRuleItemEvent.class), "foo_col");
+ assertThat(actual, is("foo_col"));
+ }
+
+ @Test
+ void assertFindRuleConfiguration() {
+ ShardingRuleConfiguration ruleConfig =
mock(ShardingRuleConfiguration.class);
+ assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
+ }
+
+ private ShardingSphereDatabase mockDatabase(final
ShardingRuleConfiguration ruleConfig) {
+ ShardingRule rule = mock(ShardingRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ return result;
+ }
+
+ @Test
+ void assertChangeRuleItemConfiguration() {
+ ShardingRuleConfiguration currentRuleConfig = new
ShardingRuleConfiguration();
+ String toBeChangedItemConfig = "bar_col";
+ processor.changeRuleItemConfiguration(mock(AlterRuleItemEvent.class),
currentRuleConfig, toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getDefaultShardingColumn(),
is("bar_col"));
+ }
+
+ @Test
+ void assertDropRuleItemConfiguration() {
+ ShardingRuleConfiguration currentRuleConfig = new
ShardingRuleConfiguration();
+ currentRuleConfig.setDefaultShardingColumn("foo_col");
+ processor.dropRuleItemConfiguration(mock(DropRuleItemEvent.class),
currentRuleConfig);
+ assertNull(currentRuleConfig.getDefaultShardingColumn());
+ }
+}
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/KeyGeneratorChangedProcessorTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/KeyGeneratorChangedProcessorTest.java
new file mode 100644
index 00000000000..1c27dae0d21
--- /dev/null
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/KeyGeneratorChangedProcessorTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.sharding.rule.changed;
+
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Properties;
+
+import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class KeyGeneratorChangedProcessorTest {
+
+ @SuppressWarnings("unchecked")
+ private final
RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration,
AlgorithmConfiguration> processor = TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class,
"sharding.key_generators");
+
+ @Test
+ void assertSwapRuleItemConfiguration() {
+ AlgorithmConfiguration actual =
processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl",
"", "", ""), createYAMLContent());
+ assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo",
new Properties())));
+ }
+
+ private String createYAMLContent() {
+ YamlAlgorithmConfiguration yamlConfig = new
YamlAlgorithmConfiguration();
+ yamlConfig.setType("foo_algo");
+ return YamlEngine.marshal(yamlConfig);
+ }
+
+ @Test
+ void assertFindRuleConfiguration() {
+ ShardingRuleConfiguration ruleConfig =
mock(ShardingRuleConfiguration.class);
+ assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
+ }
+
+ private ShardingSphereDatabase mockDatabase(final
ShardingRuleConfiguration ruleConfig) {
+ ShardingRule rule = mock(ShardingRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ return result;
+ }
+
+ @Test
+ void assertChangeRuleItemConfiguration() {
+ ShardingRuleConfiguration currentRuleConfig = new
ShardingRuleConfiguration();
+ currentRuleConfig.setKeyGenerators(new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(
+ new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""),
currentRuleConfig, toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getKeyGenerators().size(), is(1));
+
assertThat(currentRuleConfig.getKeyGenerators().get("foo_algo").getType(),
is("FIXTURE"));
+ }
+
+ @Test
+ void assertDropRuleItemConfiguration() {
+ ShardingRuleConfiguration currentRuleConfig = new
ShardingRuleConfiguration();
+ currentRuleConfig.setKeyGenerators(new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_algo", ""), currentRuleConfig);
+ assertTrue(currentRuleConfig.getKeyGenerators().isEmpty());
+ }
+}
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessorTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessorTest.java
new file mode 100644
index 00000000000..11c9ac26a06
--- /dev/null
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessorTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.sharding.rule.changed;
+
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Properties;
+
+import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ShardingAlgorithmChangedProcessorTest {
+
+ @SuppressWarnings("unchecked")
+ private final
RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration,
AlgorithmConfiguration> processor = TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class,
"sharding.sharding_algorithms");
+
+ @Test
+ void assertSwapRuleItemConfiguration() {
+ AlgorithmConfiguration actual =
processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl",
"", "", ""), createYAMLContent());
+ assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo",
new Properties())));
+ }
+
+ private String createYAMLContent() {
+ YamlAlgorithmConfiguration yamlConfig = new
YamlAlgorithmConfiguration();
+ yamlConfig.setType("foo_algo");
+ return YamlEngine.marshal(yamlConfig);
+ }
+
+ @Test
+ void assertFindRuleConfiguration() {
+ ShardingRuleConfiguration ruleConfig =
mock(ShardingRuleConfiguration.class);
+ assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
+ }
+
+ private ShardingSphereDatabase mockDatabase(final
ShardingRuleConfiguration ruleConfig) {
+ ShardingRule rule = mock(ShardingRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ return result;
+ }
+
+ @Test
+ void assertChangeRuleItemConfiguration() {
+ ShardingRuleConfiguration currentRuleConfig = new
ShardingRuleConfiguration();
+ currentRuleConfig.setShardingAlgorithms(new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(
+ new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""),
currentRuleConfig, toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getShardingAlgorithms().size(), is(1));
+
assertThat(currentRuleConfig.getShardingAlgorithms().get("foo_algo").getType(),
is("FIXTURE"));
+ }
+
+ @Test
+ void assertDropRuleItemConfiguration() {
+ ShardingRuleConfiguration currentRuleConfig = new
ShardingRuleConfiguration();
+ currentRuleConfig.setShardingAlgorithms(new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_algo", ""), currentRuleConfig);
+ assertTrue(currentRuleConfig.getShardingAlgorithms().isEmpty());
+ }
+}
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessorTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessorTest.java
new file mode 100644
index 00000000000..17bd467df49
--- /dev/null
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessorTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.sharding.rule.changed;
+
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
+import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Properties;
+
+import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ShardingAuditorChangedProcessorTest {
+
+ @SuppressWarnings("unchecked")
+ private final
RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration,
AlgorithmConfiguration> processor = TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class, "sharding.auditors");
+
+ @Test
+ void assertSwapRuleItemConfiguration() {
+ AlgorithmConfiguration actual =
processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl",
"", "", ""), createYAMLContent());
+ assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo",
new Properties())));
+ }
+
+ private String createYAMLContent() {
+ YamlAlgorithmConfiguration yamlConfig = new
YamlAlgorithmConfiguration();
+ yamlConfig.setType("foo_algo");
+ return YamlEngine.marshal(yamlConfig);
+ }
+
+ @Test
+ void assertFindRuleConfiguration() {
+ ShardingRuleConfiguration ruleConfig =
mock(ShardingRuleConfiguration.class);
+ assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
+ }
+
+ private ShardingSphereDatabase mockDatabase(final
ShardingRuleConfiguration ruleConfig) {
+ ShardingRule rule = mock(ShardingRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ return result;
+ }
+
+ @Test
+ void assertChangeRuleItemConfiguration() {
+ ShardingRuleConfiguration currentRuleConfig = new
ShardingRuleConfiguration();
+ currentRuleConfig.setAuditors(new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(
+ new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""),
currentRuleConfig, toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getAuditors().size(), is(1));
+ assertThat(currentRuleConfig.getAuditors().get("foo_algo").getType(),
is("FIXTURE"));
+ }
+
+ @Test
+ void assertDropRuleItemConfiguration() {
+ ShardingRuleConfiguration currentRuleConfig = new
ShardingRuleConfiguration();
+ currentRuleConfig.setAuditors(new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_algo", ""), currentRuleConfig);
+ assertTrue(currentRuleConfig.getAuditors().isEmpty());
+ }
+}