From 7ba53c81c46ee84551097023224561d7c36e1049 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 1 Nov 2024 01:28:07 +0800 Subject: [PATCH] Add AlgorithmChangedProcessor --- .../changed/EncryptorChangedProcessor.java | 37 ++++--------- .../EncryptorChangedProcessorTest.java | 50 +++++++---------- .../MaskAlgorithmChangedProcessor.java | 31 ++++------- .../MaskAlgorithmChangedProcessorTest.java | 51 +++++++----------- .../ShadowAlgorithmChangedProcessor.java | 32 ++++------- .../ShadowAlgorithmChangedProcessorTest.java | 53 +++++++------------ .../changed/KeyGeneratorChangedProcessor.java | 32 ++++------- .../ShardingAlgorithmChangedProcessor.java | 32 ++++------- .../KeyGeneratorChangedProcessorTest.java | 53 +++++++------------ ...ShardingAlgorithmChangedProcessorTest.java | 53 +++++++------------ 10 files changed, 147 insertions(+), 277 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 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/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; + } }