This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 0893abe795c Refactor ShardingAuditorChangedProcessor (#33489)
0893abe795c is described below

commit 0893abe795cc8d73feaa595623ad3107ce4e4faa
Author: Liang Zhang <zhangli...@apache.org>
AuthorDate: Fri Nov 1 02:44:24 2024 +0800

    Refactor ShardingAuditorChangedProcessor (#33489)
    
    * Refactor AlgorithmChangedProcessorTest
    
    * Refactor ShardingAuditorChangedProcessor
---
 .../changed/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 62d33be71f6..cab3ad91a37 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<ShardingRuleConfiguration, 
AlgorithmConfiguration> {
+public final class ShardingAuditorChangedProcessor extends 
AlgorithmChangedProcessor<ShardingRuleConfiguration> {
     
-    @Override
-    public AlgorithmConfiguration swapRuleItemConfiguration(final 
AlterRuleItemEvent event, final String yamlContent) {
-        return new 
YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent,
 YamlAlgorithmConfiguration.class));
-    }
-    
-    @Override
-    public ShardingRuleConfiguration findRuleConfiguration(final 
ShardingSphereDatabase database) {
-        return 
database.getRuleMetaData().findSingleRule(ShardingRule.class).map(ShardingRule::getConfiguration).orElseGet(ShardingRuleConfiguration::new);
+    public 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<String, AlgorithmConfiguration> 
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 17bd467df49..86c48fbc582 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;
+    }
 }

Reply via email to