This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 c18b9c8cd68 Add AlgorithmChangedProcessor (#33486)
c18b9c8cd68 is described below
commit c18b9c8cd684909ba4e4b402ecf791f51c4c6c8a
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Nov 1 01:31:41 2024 +0800
Add AlgorithmChangedProcessor (#33486)
* Add AlgorithmChangedProcessor
* Add AlgorithmChangedProcessor
---
.../rule/changed/EncryptorChangedProcessor.java | 37 ++++-----------
.../changed/EncryptorChangedProcessorTest.java | 50 ++++++++------------
.../changed/MaskAlgorithmChangedProcessor.java | 31 ++++---------
.../changed/MaskAlgorithmChangedProcessorTest.java | 51 ++++++++-------------
...writeSplittingLoadBalancerChangedProcessor.java | 32 ++++---------
...eSplittingLoadBalancerChangedProcessorTest.java | 52 ++++++++-------------
.../changed/ShadowAlgorithmChangedProcessor.java | 32 ++++---------
.../ShadowAlgorithmChangedProcessorTest.java | 53 ++++++++--------------
.../rule/changed/KeyGeneratorChangedProcessor.java | 32 ++++---------
.../changed/ShardingAlgorithmChangedProcessor.java | 32 ++++---------
.../changed/KeyGeneratorChangedProcessorTest.java | 53 ++++++++--------------
.../ShardingAlgorithmChangedProcessorTest.java | 53 ++++++++--------------
.../mode/processor/AlgorithmChangedProcessor.java | 47 +++++++++++--------
.../processor/AlgorithmChangedProcessorTest.java | 44 ++++++++----------
.../AlgorithmChangedProcessorFixtureRule.java | 29 ++++++++++++
...hmChangedProcessorFixtureRuleConfiguration.java | 31 +++++++++++++
.../fixture/FixtureAlgorithmChangedProcessor.java | 45 ++++++++++++++++++
....mode.spi.RuleItemConfigurationChangedProcessor | 18 ++++++++
18 files changed, 344 insertions(+), 378 deletions(-)
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/changed/EncryptorChangedProcessor.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/changed/EncryptorChangedProcessor.java
index c72be71e44c..e26d1a73beb 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/changed/EncryptorChangedProcessor.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/changed/EncryptorChangedProcessor.java
@@ -21,48 +21,29 @@ import
org.apache.shardingsphere.encrypt.config.EncryptRuleConfiguration;
import
org.apache.shardingsphere.encrypt.metadata.nodepath.EncryptRuleNodePathProvider;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-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.event.dispatch.rule.drop.DropRuleItemEvent;
-import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
-import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;
import java.util.LinkedHashMap;
import java.util.LinkedList;
+import java.util.Map;
/**
* Encryptor changed processor.
*/
-public final class EncryptorChangedProcessor implements
RuleItemConfigurationChangedProcessor<EncryptRuleConfiguration,
AlgorithmConfiguration> {
+public final class EncryptorChangedProcessor extends
AlgorithmChangedProcessor<EncryptRuleConfiguration> {
- @Override
- public AlgorithmConfiguration swapRuleItemConfiguration(final
AlterRuleItemEvent event, final String yamlContent) {
- return new
YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent,
YamlAlgorithmConfiguration.class));
- }
-
- @Override
- public EncryptRuleConfiguration findRuleConfiguration(final
ShardingSphereDatabase database) {
- return database.getRuleMetaData().findSingleRule(EncryptRule.class)
- .map(optional ->
getEncryptRuleConfiguration(optional.getConfiguration()))
- .orElseGet(() -> new EncryptRuleConfiguration(new
LinkedList<>(), new LinkedHashMap<>()));
- }
-
- private EncryptRuleConfiguration getEncryptRuleConfiguration(final
EncryptRuleConfiguration config) {
- return null == config.getTables() ? new EncryptRuleConfiguration(new
LinkedList<>(), config.getEncryptors()) : config;
+ public EncryptorChangedProcessor() {
+ super(EncryptRule.class);
}
@Override
- public void changeRuleItemConfiguration(final AlterRuleItemEvent event,
final EncryptRuleConfiguration currentRuleConfig, final AlgorithmConfiguration
toBeChangedItemConfig) {
- currentRuleConfig.getEncryptors().put(((AlterNamedRuleItemEvent)
event).getItemName(), toBeChangedItemConfig);
+ protected EncryptRuleConfiguration createEmptyRuleConfiguration() {
+ return new EncryptRuleConfiguration(new LinkedList<>(), new
LinkedHashMap<>());
}
@Override
- public void dropRuleItemConfiguration(final DropRuleItemEvent event, final
EncryptRuleConfiguration currentRuleConfig) {
- currentRuleConfig.getEncryptors().remove(((DropNamedRuleItemEvent)
event).getItemName());
+ protected Map<String, AlgorithmConfiguration>
getAlgorithmConfigurations(final EncryptRuleConfiguration currentRuleConfig) {
+ return currentRuleConfig.getEncryptors();
}
@Override
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
index 3484a0a79c2..9066f52dd57 100644
---
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
@@ -18,13 +18,10 @@
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;
@@ -32,6 +29,8 @@ import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
import java.util.Properties;
import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
@@ -48,45 +47,36 @@ class EncryptorChangedProcessorTest {
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())));
+ void assertFindRuleConfigurationWhenAbsent() {
+ assertThat(processor.findRuleConfiguration(mockDatabase()),
deepEqual(new EncryptRuleConfiguration(new LinkedList<>(), new
LinkedHashMap<>())));
}
- 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);
+ private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
- when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.emptyList()));
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"));
+ AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("",
"bar_algo", "", "", "");
+ EncryptRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("BAR_FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(event, currentRuleConfig,
toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getEncryptors().size(), is(2));
+
assertThat(currentRuleConfig.getEncryptors().get("foo_algo").getType(),
is("FOO_FIXTURE"));
+
assertThat(currentRuleConfig.getEncryptors().get("bar_algo").getType(),
is("BAR_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);
+ DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("",
"foo_algo", "");
+ EncryptRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ processor.dropRuleItemConfiguration(event, currentRuleConfig);
assertTrue(currentRuleConfig.getEncryptors().isEmpty());
}
+
+ private EncryptRuleConfiguration createCurrentRuleConfiguration() {
+ return new EncryptRuleConfiguration(Collections.emptyList(), new
HashMap<>(Collections.singletonMap("foo_algo", new
AlgorithmConfiguration("FOO_FIXTURE", new Properties()))));
+ }
}
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 b4c57432a22..0ab1137a895 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
@@ -18,45 +18,32 @@
package org.apache.shardingsphere.mask.rule.changed;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-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.event.dispatch.rule.drop.DropRuleItemEvent;
-import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
import
org.apache.shardingsphere.mask.metadata.nodepath.MaskRuleNodePathProvider;
import org.apache.shardingsphere.mask.rule.MaskRule;
-import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;
import java.util.LinkedHashMap;
import java.util.LinkedList;
+import java.util.Map;
/**
* Mask algorithm changed processor.
*/
-public final class MaskAlgorithmChangedProcessor implements
RuleItemConfigurationChangedProcessor<MaskRuleConfiguration,
AlgorithmConfiguration> {
+public final class MaskAlgorithmChangedProcessor extends
AlgorithmChangedProcessor<MaskRuleConfiguration> {
- @Override
- public AlgorithmConfiguration swapRuleItemConfiguration(final
AlterRuleItemEvent event, final String yamlContent) {
- return new
YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent,
YamlAlgorithmConfiguration.class));
- }
-
- @Override
- public MaskRuleConfiguration findRuleConfiguration(final
ShardingSphereDatabase database) {
- return
database.getRuleMetaData().findSingleRule(MaskRule.class).map(MaskRule::getConfiguration).orElseGet(()
-> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
+ public MaskAlgorithmChangedProcessor() {
+ super(MaskRule.class);
}
@Override
- public void changeRuleItemConfiguration(final AlterRuleItemEvent event,
final MaskRuleConfiguration currentRuleConfig, final AlgorithmConfiguration
toBeChangedItemConfig) {
- currentRuleConfig.getMaskAlgorithms().put(((AlterNamedRuleItemEvent)
event).getItemName(), toBeChangedItemConfig);
+ protected MaskRuleConfiguration createEmptyRuleConfiguration() {
+ return new MaskRuleConfiguration(new LinkedList<>(), new
LinkedHashMap<>());
}
@Override
- public void dropRuleItemConfiguration(final DropRuleItemEvent event, final
MaskRuleConfiguration currentRuleConfig) {
- currentRuleConfig.getMaskAlgorithms().remove(((DropNamedRuleItemEvent)
event).getItemName());
+ protected Map<String, AlgorithmConfiguration>
getAlgorithmConfigurations(final MaskRuleConfiguration currentRuleConfig) {
+ return currentRuleConfig.getMaskAlgorithms();
}
@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 af27dadc863..c0b12db3faf 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,14 +18,10 @@
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;
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;
@@ -33,6 +29,7 @@ import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Properties;
@@ -50,46 +47,36 @@ class MaskAlgorithmChangedProcessorTest {
RuleItemConfigurationChangedProcessor.class,
"mask.mask_algorithms");
@Test
- void assertSwapRuleItemConfiguration() {
- AlgorithmConfiguration actual =
processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "",
"", ""), createYAMLContent());
- assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo",
new Properties())));
+ void assertFindRuleConfigurationWhenAbsent() {
+ assertThat(processor.findRuleConfiguration(mockDatabase()),
deepEqual(new MaskRuleConfiguration(new LinkedList<>(), new
LinkedHashMap<>())));
}
- private String createYAMLContent() {
- YamlAlgorithmConfiguration yamlConfig = new
YamlAlgorithmConfiguration();
- yamlConfig.setType("foo_algo");
- return YamlEngine.marshal(yamlConfig);
- }
-
- @Test
- void assertFindRuleConfiguration() {
- MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class);
- assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
- }
-
- private ShardingSphereDatabase mockDatabase(final MaskRuleConfiguration
ruleConfig) {
- MaskRule rule = mock(MaskRule.class);
- when(rule.getConfiguration()).thenReturn(ruleConfig);
+ private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
- when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.emptyList()));
return result;
}
@Test
void assertChangeRuleItemConfiguration() {
- MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(
- 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", "", "", ""),
currentRuleConfig, toBeChangedItemConfig);
- assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(1));
-
assertThat(currentRuleConfig.getMaskAlgorithms().get("foo_algo").getType(),
is("FIXTURE"));
+ AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("",
"bar_algo", "", "", "");
+ MaskRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("BAR_FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(event, currentRuleConfig,
toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(2));
+
assertThat(currentRuleConfig.getMaskAlgorithms().get("foo_algo").getType(),
is("FOO_FIXTURE"));
+
assertThat(currentRuleConfig.getMaskAlgorithms().get("bar_algo").getType(),
is("BAR_FIXTURE"));
}
@Test
void assertDropRuleItemConfiguration() {
- MaskRuleConfiguration currentRuleConfig = new
MaskRuleConfiguration(Collections.emptyList(), new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
- processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_algo", ""), currentRuleConfig);
+ DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("",
"foo_algo", "");
+ MaskRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ processor.dropRuleItemConfiguration(event, currentRuleConfig);
assertTrue(currentRuleConfig.getMaskAlgorithms().isEmpty());
}
+
+ private MaskRuleConfiguration createCurrentRuleConfiguration() {
+ return new MaskRuleConfiguration(Collections.emptyList(), new
HashMap<>(Collections.singletonMap("foo_algo", new
AlgorithmConfiguration("FOO_FIXTURE", new Properties()))));
+ }
}
diff --git
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessor.java
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessor.java
index e5b530041da..3216297fbac 100644
---
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessor.java
+++
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessor.java
@@ -18,46 +18,32 @@
package org.apache.shardingsphere.readwritesplitting.rule.changed;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-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.event.dispatch.rule.drop.DropRuleItemEvent;
-import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
-import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;
import
org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.metadata.nodepath.ReadwriteSplittingRuleNodePathProvider;
import
org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
import java.util.LinkedHashMap;
import java.util.LinkedList;
+import java.util.Map;
/**
* Readwrite-splitting load-balancer changed processor.
*/
-public final class ReadwriteSplittingLoadBalancerChangedProcessor implements
RuleItemConfigurationChangedProcessor<ReadwriteSplittingRuleConfiguration,
AlgorithmConfiguration> {
+public final class ReadwriteSplittingLoadBalancerChangedProcessor extends
AlgorithmChangedProcessor<ReadwriteSplittingRuleConfiguration> {
- @Override
- public AlgorithmConfiguration swapRuleItemConfiguration(final
AlterRuleItemEvent event, final String yamlContent) {
- return new
YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent,
YamlAlgorithmConfiguration.class));
- }
-
- @Override
- public ReadwriteSplittingRuleConfiguration findRuleConfiguration(final
ShardingSphereDatabase database) {
- return
database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class).map(ReadwriteSplittingRule::getConfiguration)
- .orElseGet(() -> new ReadwriteSplittingRuleConfiguration(new
LinkedList<>(), new LinkedHashMap<>()));
+ public ReadwriteSplittingLoadBalancerChangedProcessor() {
+ super(ReadwriteSplittingRule.class);
}
@Override
- public void changeRuleItemConfiguration(final AlterRuleItemEvent event,
final ReadwriteSplittingRuleConfiguration currentRuleConfig, final
AlgorithmConfiguration toBeChangedItemConfig) {
- currentRuleConfig.getLoadBalancers().put(((AlterNamedRuleItemEvent)
event).getItemName(), toBeChangedItemConfig);
+ protected ReadwriteSplittingRuleConfiguration
createEmptyRuleConfiguration() {
+ return new ReadwriteSplittingRuleConfiguration(new LinkedList<>(), new
LinkedHashMap<>());
}
@Override
- public void dropRuleItemConfiguration(final DropRuleItemEvent event, final
ReadwriteSplittingRuleConfiguration currentRuleConfig) {
- currentRuleConfig.getLoadBalancers().remove(((DropNamedRuleItemEvent)
event).getItemName());
+ protected Map<String, AlgorithmConfiguration>
getAlgorithmConfigurations(final ReadwriteSplittingRuleConfiguration
currentRuleConfig) {
+ return currentRuleConfig.getLoadBalancers();
}
@Override
diff --git
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessorTest.java
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessorTest.java
index b14c42383cd..08dd630ed39 100644
---
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessorTest.java
+++
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessorTest.java
@@ -18,21 +18,18 @@
package org.apache.shardingsphere.readwritesplitting.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.readwritesplitting.config.ReadwriteSplittingRuleConfiguration;
-import
org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
-import
org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Properties;
@@ -50,49 +47,36 @@ class ReadwriteSplittingLoadBalancerChangedProcessorTest {
RuleItemConfigurationChangedProcessor.class,
"readwrite_splitting.load_balancers");
@Test
- void assertSwapRuleItemConfiguration() {
- AlgorithmConfiguration actual =
processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "",
"", ""), createYAMLContent());
- assertThat(actual, deepEqual(new
AlgorithmConfiguration("foo_balancer", new Properties())));
+ void assertFindRuleConfigurationWhenAbsent() {
+ assertThat(processor.findRuleConfiguration(mockDatabase()),
deepEqual(new ReadwriteSplittingRuleConfiguration(new LinkedList<>(), new
LinkedHashMap<>())));
}
- private String createYAMLContent() {
- YamlAlgorithmConfiguration yamlConfig = new
YamlAlgorithmConfiguration();
- yamlConfig.setType("foo_balancer");
- return YamlEngine.marshal(yamlConfig);
- }
-
- @Test
- void assertFindRuleConfiguration() {
- ReadwriteSplittingRuleConfiguration ruleConfig =
mock(ReadwriteSplittingRuleConfiguration.class);
- assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
- }
-
- private ShardingSphereDatabase mockDatabase(final
ReadwriteSplittingRuleConfiguration ruleConfig) {
- ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class);
- when(rule.getConfiguration()).thenReturn(ruleConfig);
+ private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
- when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.emptyList()));
return result;
}
@Test
void assertChangeRuleItemConfiguration() {
- ReadwriteSplittingRuleConfiguration currentRuleConfig = new
ReadwriteSplittingRuleConfiguration(
- new LinkedList<>(Collections.singleton(new
ReadwriteSplittingDataSourceGroupRuleConfiguration("foo", "write_ds",
Collections.singletonList("read_ds"), "foo_balancer"))),
- new HashMap<>(Collections.singletonMap("foo_balancer", new
AlgorithmConfiguration("FOO_BALANCER", new Properties()))));
- AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("BAR_BALANCER", new Properties());
- processor.changeRuleItemConfiguration(new AlterNamedRuleItemEvent("",
"bar_balancer", "", "", ""), currentRuleConfig, toBeChangedItemConfig);
+ AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("",
"bar_algo", "", "", "");
+ ReadwriteSplittingRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("BAR_FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(event, currentRuleConfig,
toBeChangedItemConfig);
assertThat(currentRuleConfig.getLoadBalancers().size(), is(2));
-
assertThat(currentRuleConfig.getLoadBalancers().get("foo_balancer").getType(),
is("FOO_BALANCER"));
-
assertThat(currentRuleConfig.getLoadBalancers().get("bar_balancer").getType(),
is("BAR_BALANCER"));
+
assertThat(currentRuleConfig.getLoadBalancers().get("foo_algo").getType(),
is("FOO_FIXTURE"));
+
assertThat(currentRuleConfig.getLoadBalancers().get("bar_algo").getType(),
is("BAR_FIXTURE"));
}
@Test
void assertDropRuleItemConfiguration() {
- ReadwriteSplittingRuleConfiguration currentRuleConfig = new
ReadwriteSplittingRuleConfiguration(
- new LinkedList<>(Collections.singleton(new
ReadwriteSplittingDataSourceGroupRuleConfiguration("foo", "write_ds",
Collections.singletonList("read_ds"), "foo_balancer"))),
- new HashMap<>(Collections.singletonMap("foo_balancer", new
AlgorithmConfiguration("FOO_BALANCER", new Properties()))));
- processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_balancer", ""), currentRuleConfig);
+ DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("",
"foo_algo", "");
+ ReadwriteSplittingRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ processor.dropRuleItemConfiguration(event, currentRuleConfig);
assertTrue(currentRuleConfig.getLoadBalancers().isEmpty());
}
+
+ private ReadwriteSplittingRuleConfiguration
createCurrentRuleConfiguration() {
+ return new
ReadwriteSplittingRuleConfiguration(Collections.emptyList(), new
HashMap<>(Collections.singletonMap("foo_algo", new
AlgorithmConfiguration("FOO_FIXTURE", new Properties()))));
+ }
}
diff --git
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/changed/ShadowAlgorithmChangedProcessor.java
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/changed/ShadowAlgorithmChangedProcessor.java
index ccebfcf6768..541c8f4ea89 100644
---
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/changed/ShadowAlgorithmChangedProcessor.java
+++
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/changed/ShadowAlgorithmChangedProcessor.java
@@ -18,42 +18,30 @@
package org.apache.shardingsphere.shadow.rule.changed;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-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.event.dispatch.rule.drop.DropRuleItemEvent;
-import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
-import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;
import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration;
import
org.apache.shardingsphere.shadow.metadata.nodepath.ShadowRuleNodePathProvider;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import java.util.Map;
+
/**
* Shadow algorithm changed processor.
*/
-public final class ShadowAlgorithmChangedProcessor implements
RuleItemConfigurationChangedProcessor<ShadowRuleConfiguration,
AlgorithmConfiguration> {
+public final class ShadowAlgorithmChangedProcessor extends
AlgorithmChangedProcessor<ShadowRuleConfiguration> {
- @Override
- public AlgorithmConfiguration swapRuleItemConfiguration(final
AlterRuleItemEvent event, final String yamlContent) {
- return new
YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent,
YamlAlgorithmConfiguration.class));
- }
-
- @Override
- public ShadowRuleConfiguration findRuleConfiguration(final
ShardingSphereDatabase database) {
- return
database.getRuleMetaData().findSingleRule(ShadowRule.class).map(ShadowRule::getConfiguration).orElseGet(ShadowRuleConfiguration::new);
+ public ShadowAlgorithmChangedProcessor() {
+ super(ShadowRule.class);
}
@Override
- public void changeRuleItemConfiguration(final AlterRuleItemEvent event,
final ShadowRuleConfiguration currentRuleConfig, final AlgorithmConfiguration
toBeChangedItemConfig) {
- currentRuleConfig.getShadowAlgorithms().put(((AlterNamedRuleItemEvent)
event).getItemName(), toBeChangedItemConfig);
+ protected ShadowRuleConfiguration createEmptyRuleConfiguration() {
+ return new ShadowRuleConfiguration();
}
@Override
- public void dropRuleItemConfiguration(final DropRuleItemEvent event, final
ShadowRuleConfiguration currentRuleConfig) {
-
currentRuleConfig.getShadowAlgorithms().remove(((DropNamedRuleItemEvent)
event).getItemName());
+ protected Map<String, AlgorithmConfiguration>
getAlgorithmConfigurations(final ShadowRuleConfiguration currentRuleConfig) {
+ return currentRuleConfig.getShadowAlgorithms();
}
@Override
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
index d0f9a8b56d6..d79b2ca9b1b 100644
---
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
@@ -18,20 +18,16 @@
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;
@@ -48,47 +44,38 @@ class ShadowAlgorithmChangedProcessorTest {
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())));
+ void assertFindRuleConfigurationWhenAbsent() {
+ assertThat(processor.findRuleConfiguration(mockDatabase()),
deepEqual(new ShadowRuleConfiguration()));
}
- 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);
+ private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
- when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.emptyList()));
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"));
+ AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("",
"bar_algo", "", "", "");
+ ShadowRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("BAR_FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(event, currentRuleConfig,
toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getShadowAlgorithms().size(), is(2));
+
assertThat(currentRuleConfig.getShadowAlgorithms().get("foo_algo").getType(),
is("FOO_FIXTURE"));
+
assertThat(currentRuleConfig.getShadowAlgorithms().get("bar_algo").getType(),
is("BAR_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);
+ DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("",
"foo_algo", "");
+ ShadowRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ processor.dropRuleItemConfiguration(event, currentRuleConfig);
assertTrue(currentRuleConfig.getShadowAlgorithms().isEmpty());
}
+
+ private ShadowRuleConfiguration createCurrentRuleConfiguration() {
+ ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+ result.getShadowAlgorithms().put("foo_algo", new
AlgorithmConfiguration("FOO_FIXTURE", new Properties()));
+ return result;
+ }
}
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/KeyGeneratorChangedProcessor.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/KeyGeneratorChangedProcessor.java
index ae6e6a73c32..2e6359c740a 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/KeyGeneratorChangedProcessor.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/KeyGeneratorChangedProcessor.java
@@ -18,42 +18,30 @@
package org.apache.shardingsphere.sharding.rule.changed;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-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.event.dispatch.rule.drop.DropRuleItemEvent;
-import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
-import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.metadata.nodepath.ShardingRuleNodePathProvider;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import java.util.Map;
+
/**
* Key generator changed processor.
*/
-public final class KeyGeneratorChangedProcessor implements
RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration,
AlgorithmConfiguration> {
+public final class KeyGeneratorChangedProcessor extends
AlgorithmChangedProcessor<ShardingRuleConfiguration> {
- @Override
- public AlgorithmConfiguration swapRuleItemConfiguration(final
AlterRuleItemEvent event, final String yamlContent) {
- return new
YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent,
YamlAlgorithmConfiguration.class));
- }
-
- @Override
- public ShardingRuleConfiguration findRuleConfiguration(final
ShardingSphereDatabase database) {
- return
database.getRuleMetaData().findSingleRule(ShardingRule.class).map(ShardingRule::getConfiguration).orElseGet(ShardingRuleConfiguration::new);
+ public KeyGeneratorChangedProcessor() {
+ super(ShardingRule.class);
}
@Override
- public void changeRuleItemConfiguration(final AlterRuleItemEvent event,
final ShardingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration
toBeChangedItemConfig) {
- currentRuleConfig.getKeyGenerators().put(((AlterNamedRuleItemEvent)
event).getItemName(), toBeChangedItemConfig);
+ protected ShardingRuleConfiguration createEmptyRuleConfiguration() {
+ return new ShardingRuleConfiguration();
}
@Override
- public void dropRuleItemConfiguration(final DropRuleItemEvent event, final
ShardingRuleConfiguration currentRuleConfig) {
- currentRuleConfig.getKeyGenerators().remove(((DropNamedRuleItemEvent)
event).getItemName());
+ protected Map<String, AlgorithmConfiguration>
getAlgorithmConfigurations(final ShardingRuleConfiguration currentRuleConfig) {
+ return currentRuleConfig.getKeyGenerators();
}
@Override
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessor.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessor.java
index f7ece1f515f..c39ed28ba6a 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessor.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessor.java
@@ -18,42 +18,30 @@
package org.apache.shardingsphere.sharding.rule.changed;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-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.event.dispatch.rule.drop.DropRuleItemEvent;
-import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
-import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
+import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.metadata.nodepath.ShardingRuleNodePathProvider;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import java.util.Map;
+
/**
* Sharding algorithm changed processor.
*/
-public final class ShardingAlgorithmChangedProcessor implements
RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration,
AlgorithmConfiguration> {
+public final class ShardingAlgorithmChangedProcessor extends
AlgorithmChangedProcessor<ShardingRuleConfiguration> {
- @Override
- public AlgorithmConfiguration swapRuleItemConfiguration(final
AlterRuleItemEvent event, final String yamlContent) {
- return new
YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent,
YamlAlgorithmConfiguration.class));
- }
-
- @Override
- public ShardingRuleConfiguration findRuleConfiguration(final
ShardingSphereDatabase database) {
- return
database.getRuleMetaData().findSingleRule(ShardingRule.class).map(ShardingRule::getConfiguration).orElseGet(ShardingRuleConfiguration::new);
+ public ShardingAlgorithmChangedProcessor() {
+ super(ShardingRule.class);
}
@Override
- public void changeRuleItemConfiguration(final AlterRuleItemEvent event,
final ShardingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration
toBeChangedItemConfig) {
-
currentRuleConfig.getShardingAlgorithms().put(((AlterNamedRuleItemEvent)
event).getItemName(), toBeChangedItemConfig);
+ protected ShardingRuleConfiguration createEmptyRuleConfiguration() {
+ return new ShardingRuleConfiguration();
}
@Override
- public void dropRuleItemConfiguration(final DropRuleItemEvent event, final
ShardingRuleConfiguration currentRuleConfig) {
-
currentRuleConfig.getShardingAlgorithms().remove(((DropNamedRuleItemEvent)
event).getItemName());
+ protected Map<String, AlgorithmConfiguration>
getAlgorithmConfigurations(final ShardingRuleConfiguration currentRuleConfig) {
+ return currentRuleConfig.getShardingAlgorithms();
}
@Override
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
index 1c27dae0d21..2c97179aa0a 100644
---
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
@@ -18,20 +18,16 @@
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;
@@ -48,47 +44,38 @@ class KeyGeneratorChangedProcessorTest {
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())));
+ void assertFindRuleConfigurationWhenAbsent() {
+ assertThat(processor.findRuleConfiguration(mockDatabase()),
deepEqual(new ShardingRuleConfiguration()));
}
- 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);
+ private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
- when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.emptyList()));
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"));
+ AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("",
"bar_algo", "", "", "");
+ ShardingRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("BAR_FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(event, currentRuleConfig,
toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getKeyGenerators().size(), is(2));
+
assertThat(currentRuleConfig.getKeyGenerators().get("foo_algo").getType(),
is("FOO_FIXTURE"));
+
assertThat(currentRuleConfig.getKeyGenerators().get("bar_algo").getType(),
is("BAR_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);
+ DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("",
"foo_algo", "");
+ ShardingRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ processor.dropRuleItemConfiguration(event, currentRuleConfig);
assertTrue(currentRuleConfig.getKeyGenerators().isEmpty());
}
+
+ private ShardingRuleConfiguration createCurrentRuleConfiguration() {
+ ShardingRuleConfiguration result = new ShardingRuleConfiguration();
+ result.getKeyGenerators().put("foo_algo", new
AlgorithmConfiguration("FOO_FIXTURE", new Properties()));
+ return result;
+ }
}
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
index 11c9ac26a06..a8ede01df43 100644
---
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
@@ -18,20 +18,16 @@
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;
@@ -48,47 +44,38 @@ class ShardingAlgorithmChangedProcessorTest {
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())));
+ void assertFindRuleConfigurationWhenAbsent() {
+ assertThat(processor.findRuleConfiguration(mockDatabase()),
deepEqual(new ShardingRuleConfiguration()));
}
- 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);
+ private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
- when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.emptyList()));
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"));
+ AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("",
"bar_algo", "", "", "");
+ ShardingRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("BAR_FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(event, currentRuleConfig,
toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getShardingAlgorithms().size(), is(2));
+
assertThat(currentRuleConfig.getShardingAlgorithms().get("foo_algo").getType(),
is("FOO_FIXTURE"));
+
assertThat(currentRuleConfig.getShardingAlgorithms().get("bar_algo").getType(),
is("BAR_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);
+ DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("",
"foo_algo", "");
+ ShardingRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
+ processor.dropRuleItemConfiguration(event, currentRuleConfig);
assertTrue(currentRuleConfig.getShardingAlgorithms().isEmpty());
}
+
+ private ShardingRuleConfiguration createCurrentRuleConfiguration() {
+ ShardingRuleConfiguration result = new ShardingRuleConfiguration();
+ result.getShardingAlgorithms().put("foo_algo", new
AlgorithmConfiguration("FOO_FIXTURE", new Properties()));
+ return result;
+ }
}
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessor.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessor.java
similarity index 54%
copy from
features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessor.java
copy to
mode/core/src/main/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessor.java
index f7ece1f515f..580b2eb56b0 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessor.java
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessor.java
@@ -15,49 +15,56 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sharding.rule.changed;
+package org.apache.shardingsphere.mode.processor;
+import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+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.alter.AlterRuleItemEvent;
import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
import
org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent;
-import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
-import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
-import
org.apache.shardingsphere.sharding.metadata.nodepath.ShardingRuleNodePathProvider;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
+
+import java.util.Map;
/**
- * Sharding algorithm changed processor.
+ * Algorithm changed processor.
+ *
+ * @param <T> type of rule configuration
*/
-public final class ShardingAlgorithmChangedProcessor implements
RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration,
AlgorithmConfiguration> {
+@RequiredArgsConstructor
+public abstract class AlgorithmChangedProcessor<T extends RuleConfiguration>
implements RuleItemConfigurationChangedProcessor<T, AlgorithmConfiguration> {
+
+ private final Class<? extends ShardingSphereRule> ruleClass;
@Override
- public AlgorithmConfiguration swapRuleItemConfiguration(final
AlterRuleItemEvent event, final String yamlContent) {
+ public final AlgorithmConfiguration swapRuleItemConfiguration(final
AlterRuleItemEvent event, final String yamlContent) {
return new
YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent,
YamlAlgorithmConfiguration.class));
}
+ @SuppressWarnings("unchecked")
@Override
- public ShardingRuleConfiguration findRuleConfiguration(final
ShardingSphereDatabase database) {
- return
database.getRuleMetaData().findSingleRule(ShardingRule.class).map(ShardingRule::getConfiguration).orElseGet(ShardingRuleConfiguration::new);
+ public final T findRuleConfiguration(final ShardingSphereDatabase
database) {
+ return (T)
database.getRuleMetaData().findSingleRule(ruleClass).map(ShardingSphereRule::getConfiguration).orElseGet(this::createEmptyRuleConfiguration);
}
@Override
- public void changeRuleItemConfiguration(final AlterRuleItemEvent event,
final ShardingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration
toBeChangedItemConfig) {
-
currentRuleConfig.getShardingAlgorithms().put(((AlterNamedRuleItemEvent)
event).getItemName(), toBeChangedItemConfig);
+ public final void changeRuleItemConfiguration(final AlterRuleItemEvent
event, final T currentRuleConfig, final AlgorithmConfiguration
toBeChangedItemConfig) {
+
getAlgorithmConfigurations(currentRuleConfig).put(((AlterNamedRuleItemEvent)
event).getItemName(), toBeChangedItemConfig);
}
@Override
- public void dropRuleItemConfiguration(final DropRuleItemEvent event, final
ShardingRuleConfiguration currentRuleConfig) {
-
currentRuleConfig.getShardingAlgorithms().remove(((DropNamedRuleItemEvent)
event).getItemName());
+ public final void dropRuleItemConfiguration(final DropRuleItemEvent event,
final T currentRuleConfig) {
+
getAlgorithmConfigurations(currentRuleConfig).remove(((DropNamedRuleItemEvent)
event).getItemName());
}
- @Override
- public String getType() {
- return ShardingRuleNodePathProvider.RULE_TYPE + "." +
ShardingRuleNodePathProvider.SHARDING_ALGORITHMS;
- }
+ protected abstract T createEmptyRuleConfiguration();
+
+ protected abstract Map<String, AlgorithmConfiguration>
getAlgorithmConfigurations(T currentRuleConfig);
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java
b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessorTest.java
similarity index 60%
copy from
features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java
copy to
mode/core/src/test/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessorTest.java
index af27dadc863..93ba50b3e61 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java
+++
b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessorTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mask.rule.changed;
+package org.apache.shardingsphere.mode.processor;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import
org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
@@ -23,17 +23,14 @@ 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;
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.processor.fixture.AlgorithmChangedProcessorFixtureRule;
+import
org.apache.shardingsphere.mode.processor.fixture.AlgorithmChangedProcessorFixtureRuleConfiguration;
import
org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
import org.junit.jupiter.api.Test;
import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedList;
import java.util.Properties;
import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
@@ -43,11 +40,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-class MaskAlgorithmChangedProcessorTest {
+class AlgorithmChangedProcessorTest {
- @SuppressWarnings("unchecked")
- private final RuleItemConfigurationChangedProcessor<MaskRuleConfiguration,
AlgorithmConfiguration> processor = TypedSPILoader.getService(
- RuleItemConfigurationChangedProcessor.class,
"mask.mask_algorithms");
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private final
AlgorithmChangedProcessor<AlgorithmChangedProcessorFixtureRuleConfiguration>
processor = (AlgorithmChangedProcessor) TypedSPILoader.getService(
+ RuleItemConfigurationChangedProcessor.class, "FIXTURE");
@Test
void assertSwapRuleItemConfiguration() {
@@ -63,33 +60,32 @@ class MaskAlgorithmChangedProcessorTest {
@Test
void assertFindRuleConfiguration() {
- MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class);
+ AlgorithmChangedProcessorFixtureRuleConfiguration ruleConfig = new
AlgorithmChangedProcessorFixtureRuleConfiguration();
assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)),
is(ruleConfig));
}
- private ShardingSphereDatabase mockDatabase(final MaskRuleConfiguration
ruleConfig) {
- MaskRule rule = mock(MaskRule.class);
- when(rule.getConfiguration()).thenReturn(ruleConfig);
+ private ShardingSphereDatabase mockDatabase(final
AlgorithmChangedProcessorFixtureRuleConfiguration ruleConfig) {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
- when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
+ when(result.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(new
AlgorithmChangedProcessorFixtureRule(ruleConfig))));
return result;
}
@Test
void assertChangeRuleItemConfiguration() {
- MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(
- 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", "", "", ""),
currentRuleConfig, toBeChangedItemConfig);
- assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(1));
-
assertThat(currentRuleConfig.getMaskAlgorithms().get("foo_algo").getType(),
is("FIXTURE"));
+ AlgorithmChangedProcessorFixtureRuleConfiguration currentRuleConfig =
new AlgorithmChangedProcessorFixtureRuleConfiguration();
+ currentRuleConfig.getAlgorithmConfigurations().put("foo_algo", new
AlgorithmConfiguration("FOO_FIXTURE", new Properties()));
+ AlgorithmConfiguration toBeChangedItemConfig = new
AlgorithmConfiguration("BAR_FIXTURE", new Properties());
+ processor.changeRuleItemConfiguration(new AlterNamedRuleItemEvent("",
"bar_algo", "", "", ""), currentRuleConfig, toBeChangedItemConfig);
+ assertThat(currentRuleConfig.getAlgorithmConfigurations().size(),
is(2));
+
assertThat(currentRuleConfig.getAlgorithmConfigurations().get("foo_algo").getType(),
is("FOO_FIXTURE"));
+
assertThat(currentRuleConfig.getAlgorithmConfigurations().get("bar_algo").getType(),
is("BAR_FIXTURE"));
}
@Test
void assertDropRuleItemConfiguration() {
- MaskRuleConfiguration currentRuleConfig = new
MaskRuleConfiguration(Collections.emptyList(), new
HashMap<>(Collections.singletonMap("foo_algo",
mock(AlgorithmConfiguration.class))));
+ AlgorithmChangedProcessorFixtureRuleConfiguration currentRuleConfig =
new AlgorithmChangedProcessorFixtureRuleConfiguration();
+ currentRuleConfig.getAlgorithmConfigurations().put("foo_algo", new
AlgorithmConfiguration("FOO_FIXTURE", new Properties()));
processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("",
"foo_algo", ""), currentRuleConfig);
- assertTrue(currentRuleConfig.getMaskAlgorithms().isEmpty());
+ assertTrue(currentRuleConfig.getAlgorithmConfigurations().isEmpty());
}
}
diff --git
a/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/fixture/AlgorithmChangedProcessorFixtureRule.java
b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/fixture/AlgorithmChangedProcessorFixtureRule.java
new file mode 100644
index 00000000000..cb89211d9d3
--- /dev/null
+++
b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/fixture/AlgorithmChangedProcessorFixtureRule.java
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mode.processor.fixture;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+
+@RequiredArgsConstructor
+@Getter
+public final class AlgorithmChangedProcessorFixtureRule implements
ShardingSphereRule {
+
+ private final AlgorithmChangedProcessorFixtureRuleConfiguration
configuration;
+}
diff --git
a/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/fixture/AlgorithmChangedProcessorFixtureRuleConfiguration.java
b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/fixture/AlgorithmChangedProcessorFixtureRuleConfiguration.java
new file mode 100644
index 00000000000..60f7424a317
--- /dev/null
+++
b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/fixture/AlgorithmChangedProcessorFixtureRuleConfiguration.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mode.processor.fixture;
+
+import lombok.Getter;
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Getter
+public final class AlgorithmChangedProcessorFixtureRuleConfiguration
implements RuleConfiguration {
+
+ private final Map<String, AlgorithmConfiguration> algorithmConfigurations
= new HashMap<>();
+}
diff --git
a/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/fixture/FixtureAlgorithmChangedProcessor.java
b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/fixture/FixtureAlgorithmChangedProcessor.java
new file mode 100644
index 00000000000..fe4e3520fa6
--- /dev/null
+++
b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/fixture/FixtureAlgorithmChangedProcessor.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mode.processor.fixture;
+
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;
+
+import java.util.Map;
+
+public final class FixtureAlgorithmChangedProcessor extends
AlgorithmChangedProcessor<AlgorithmChangedProcessorFixtureRuleConfiguration> {
+
+ public FixtureAlgorithmChangedProcessor() {
+ super(AlgorithmChangedProcessorFixtureRule.class);
+ }
+
+ @Override
+ protected AlgorithmChangedProcessorFixtureRuleConfiguration
createEmptyRuleConfiguration() {
+ return new AlgorithmChangedProcessorFixtureRuleConfiguration();
+ }
+
+ @Override
+ protected Map<String, AlgorithmConfiguration>
getAlgorithmConfigurations(final
AlgorithmChangedProcessorFixtureRuleConfiguration currentRuleConfig) {
+ return currentRuleConfig.getAlgorithmConfigurations();
+ }
+
+ @Override
+ public Object getType() {
+ return "FIXTURE";
+ }
+}
diff --git
a/mode/core/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor
b/mode/core/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor
new file mode 100644
index 00000000000..3d370fa5414
--- /dev/null
+++
b/mode/core/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.mode.processor.fixture.FixtureAlgorithmChangedProcessor