From 9e156ba1f95248dace930d1f496ca44f2476c41a Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 1 Nov 2024 01:49:57 +0800 Subject: [PATCH 1/2] Refactor AlgorithmChangedProcessorTest --- .../mode/processor/AlgorithmChangedProcessorTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index b1ee13754799b..cbbe1fe245e80 100644 --- 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 @@ -48,7 +48,8 @@ class AlgorithmChangedProcessorTest { @Test void assertSwapRuleItemConfiguration() { - AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "", "", ""), createYAMLContent()); + AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("", "foo", "", "", ""); + AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(event, createYAMLContent()); assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo", new Properties()))); } From e187133fd1b79e211a7b0fe69ea5c3749d274602 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 1 Nov 2024 02:42:31 +0800 Subject: [PATCH 2/2] Refactor ShardingAuditorChangedProcessor --- .../ShardingAuditorChangedProcessor.java | 32 ++++------- .../ShardingAuditorChangedProcessorTest.java | 53 +++++++------------ 2 files changed, 30 insertions(+), 55 deletions(-) diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessor.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessor.java index 62d33be71f650..cab3ad91a379e 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessor.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessor.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 auditor changed processor. */ -public final class ShardingAuditorChangedProcessor implements RuleItemConfigurationChangedProcessor { +public final class ShardingAuditorChangedProcessor 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 ShardingAuditorChangedProcessor() { + super(ShardingRule.class); } @Override - public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) { - currentRuleConfig.getAuditors().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + protected ShardingRuleConfiguration createEmptyRuleConfiguration() { + return new ShardingRuleConfiguration(); } @Override - public void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { - currentRuleConfig.getAuditors().remove(((DropNamedRuleItemEvent) event).getItemName()); + protected Map getAlgorithmConfigurations(final ShardingRuleConfiguration currentRuleConfig) { + return currentRuleConfig.getAuditors(); } @Override diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessorTest.java index 17bd467df492e..86c48fbc582ba 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessorTest.java @@ -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 ShardingAuditorChangedProcessorTest { RuleItemConfigurationChangedProcessor.class, "sharding.auditors"); @Test - void assertSwapRuleItemConfiguration() { - AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl", "", "", ""), createYAMLContent()); - assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo", new Properties()))); + 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.setAuditors(new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class)))); - AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("FIXTURE", new Properties()); - processor.changeRuleItemConfiguration( - new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""), currentRuleConfig, toBeChangedItemConfig); - assertThat(currentRuleConfig.getAuditors().size(), is(1)); - assertThat(currentRuleConfig.getAuditors().get("foo_algo").getType(), is("FIXTURE")); + AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("", "bar_algo", "", "", ""); + ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); + AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("BAR_FIXTURE", new Properties()); + processor.changeRuleItemConfiguration(event, currentRuleConfig, toBeChangedItemConfig); + assertThat(currentRuleConfig.getAuditors().size(), is(2)); + assertThat(currentRuleConfig.getAuditors().get("foo_algo").getType(), is("FOO_FIXTURE")); + assertThat(currentRuleConfig.getAuditors().get("bar_algo").getType(), is("BAR_FIXTURE")); } @Test void assertDropRuleItemConfiguration() { - ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); - currentRuleConfig.setAuditors(new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class)))); - processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_algo", ""), currentRuleConfig); + DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("", "foo_algo", ""); + ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); + processor.dropRuleItemConfiguration(event, currentRuleConfig); assertTrue(currentRuleConfig.getAuditors().isEmpty()); } + + private ShardingRuleConfiguration createCurrentRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getAuditors().put("foo_algo", new AlgorithmConfiguration("FOO_FIXTURE", new Properties())); + return result; + } }