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 c72be71e44ca7..e26d1a73bebb4 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.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 { +public final class EncryptorChangedProcessor extends AlgorithmChangedProcessor { - @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 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 3484a0a79c2cc..9066f52dd57bc 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 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 b4c57432a2249..0ab1137a895c9 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 { +public final class MaskAlgorithmChangedProcessor extends AlgorithmChangedProcessor { - @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 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 af27dadc863e9..c0b12db3faf94 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 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 e5b530041da02..3216297fbac79 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 { +public final class ReadwriteSplittingLoadBalancerChangedProcessor extends AlgorithmChangedProcessor { - @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 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 b14c42383cd5e..08dd630ed39c9 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 ccebfcf6768d3..541c8f4ea89ea 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 { +public final class ShadowAlgorithmChangedProcessor extends AlgorithmChangedProcessor { - @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 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 d0f9a8b56d666..d79b2ca9b1b84 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 ae6e6a73c3241..2e6359c740a15 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 { +public final class KeyGeneratorChangedProcessor extends AlgorithmChangedProcessor { - @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 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 f7ece1f515fbb..c39ed28ba6ac4 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 { +public final class ShardingAlgorithmChangedProcessor extends AlgorithmChangedProcessor { - @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 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 1c27dae0d218f..2c97179aa0aff 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 11c9ac26a06e4..a8ede01df4313 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/mode/core/src/main/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessor.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessor.java new file mode 100644 index 0000000000000..580b2eb56b034 --- /dev/null +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessor.java @@ -0,0 +1,70 @@ +/* + * 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; + +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.mode.spi.RuleItemConfigurationChangedProcessor; + +import java.util.Map; + +/** + * Algorithm changed processor. + * + * @param type of rule configuration + */ +@RequiredArgsConstructor +public abstract class AlgorithmChangedProcessor implements RuleItemConfigurationChangedProcessor { + + private final Class ruleClass; + + @Override + public final AlgorithmConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); + } + + @SuppressWarnings("unchecked") + @Override + public final T findRuleConfiguration(final ShardingSphereDatabase database) { + return (T) database.getRuleMetaData().findSingleRule(ruleClass).map(ShardingSphereRule::getConfiguration).orElseGet(this::createEmptyRuleConfiguration); + } + + @Override + public final void changeRuleItemConfiguration(final AlterRuleItemEvent event, final T currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) { + getAlgorithmConfigurations(currentRuleConfig).put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + } + + @Override + public final void dropRuleItemConfiguration(final DropRuleItemEvent event, final T currentRuleConfig) { + getAlgorithmConfigurations(currentRuleConfig).remove(((DropNamedRuleItemEvent) event).getItemName()); + } + + protected abstract T createEmptyRuleConfiguration(); + + protected abstract Map getAlgorithmConfigurations(T currentRuleConfig); +} diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessorTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessorTest.java new file mode 100644 index 0000000000000..93ba50b3e617b --- /dev/null +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/processor/AlgorithmChangedProcessorTest.java @@ -0,0 +1,91 @@ +/* + * 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; + +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.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.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 AlgorithmChangedProcessorTest { + + @SuppressWarnings({"rawtypes", "unchecked"}) + private final AlgorithmChangedProcessor processor = (AlgorithmChangedProcessor) TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "FIXTURE"); + + @Test + void assertSwapRuleItemConfiguration() { + AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "", "", ""), 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() { + AlgorithmChangedProcessorFixtureRuleConfiguration ruleConfig = new AlgorithmChangedProcessorFixtureRuleConfiguration(); + assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig)); + } + + private ShardingSphereDatabase mockDatabase(final AlgorithmChangedProcessorFixtureRuleConfiguration ruleConfig) { + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(new AlgorithmChangedProcessorFixtureRule(ruleConfig)))); + return result; + } + + @Test + void assertChangeRuleItemConfiguration() { + 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() { + AlgorithmChangedProcessorFixtureRuleConfiguration currentRuleConfig = new AlgorithmChangedProcessorFixtureRuleConfiguration(); + currentRuleConfig.getAlgorithmConfigurations().put("foo_algo", new AlgorithmConfiguration("FOO_FIXTURE", new Properties())); + processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_algo", ""), currentRuleConfig); + 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 0000000000000..cb89211d9d3ce --- /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 0000000000000..60f7424a31796 --- /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 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 0000000000000..fe4e3520fa6aa --- /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 { + + public FixtureAlgorithmChangedProcessor() { + super(AlgorithmChangedProcessorFixtureRule.class); + } + + @Override + protected AlgorithmChangedProcessorFixtureRuleConfiguration createEmptyRuleConfiguration() { + return new AlgorithmChangedProcessorFixtureRuleConfiguration(); + } + + @Override + protected Map 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 0000000000000..3d370fa5414a7 --- /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