From 68c935b50efb2c2da75e2bebf104dae274118027 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 31 Oct 2024 21:53:45 +0800 Subject: [PATCH 1/9] Add test cases on RuleItemConfigurationChangedProcessor's impl --- .../MaskAlgorithmChangedProcessor.java | 7 +-- .../MaskAlgorithmChangedProcessorTest.java | 49 ++++++++++--------- 2 files changed, 26 insertions(+), 30 deletions(-) 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 24e9d25a8e2bd..b4c57432a2249 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 @@ -46,12 +46,7 @@ public AlgorithmConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent @Override public MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { - return database.getRuleMetaData().findSingleRule(MaskRule.class) - .map(optional -> getConfiguration(optional.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); - } - - private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration ruleConfig) { - return null == ruleConfig.getMaskAlgorithms() ? new MaskRuleConfiguration(ruleConfig.getTables(), new LinkedHashMap<>()) : ruleConfig; + return database.getRuleMetaData().findSingleRule(MaskRule.class).map(MaskRule::getConfiguration).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); } @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 bc5a8057a6db7..af27dadc863e9 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,8 +18,11 @@ 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; @@ -31,47 +34,45 @@ import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; -import java.util.Optional; 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.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; class MaskAlgorithmChangedProcessorTest { - private final MaskAlgorithmChangedProcessor processor = (MaskAlgorithmChangedProcessor) TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, "mask.mask_algorithms"); + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "mask.mask_algorithms"); @Test void assertSwapRuleItemConfiguration() { - assertThat(processor.swapRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class), "type: TEST").getType(), is("TEST")); + AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "", "", ""), createYAMLContent()); + assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo", new Properties()))); } - @Test - void assertFindRuleConfigurationWhenRuleDoesNotExist() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.empty()); - assertTrue(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty()); + private String createYAMLContent() { + YamlAlgorithmConfiguration yamlConfig = new YamlAlgorithmConfiguration(); + yamlConfig.setType("foo_algo"); + return YamlEngine.marshal(yamlConfig); } @Test - void assertFindRuleConfigurationWhenMaskAlgorithmDoesNotExist() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(new MaskRule(new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap())))); - assertTrue(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty()); + void assertFindRuleConfiguration() { + MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class); + assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig)); } - @Test - void assertFindRuleConfigurationWhenRuleExists() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - MaskRule maskRule = mock(MaskRule.class, RETURNS_DEEP_STUBS); - when(maskRule.getConfiguration().getMaskAlgorithms()).thenReturn(Collections.singletonMap("foo", new AlgorithmConfiguration("FOO", new Properties()))); - when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(maskRule)); - assertFalse(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty()); + private ShardingSphereDatabase mockDatabase(final MaskRuleConfiguration ruleConfig) { + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(ruleConfig); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + return result; } @Test @@ -80,15 +81,15 @@ void assertChangeRuleItemConfiguration() { 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", "key", "0", ""), currentRuleConfig, toBeChangedItemConfig); + new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""), currentRuleConfig, toBeChangedItemConfig); assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(1)); assertThat(currentRuleConfig.getMaskAlgorithms().get("foo_algo").getType(), is("FIXTURE")); } @Test void assertDropRuleItemConfiguration() { - MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("type: TEST", mock(AlgorithmConfiguration.class)))); - processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("foo_db", "type: TEST", ""), currentRuleConfig); + MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class)))); + processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_algo", ""), currentRuleConfig); assertTrue(currentRuleConfig.getMaskAlgorithms().isEmpty()); } } From c0063d8e1f8d3afe9f01371fa7b663419569aaed Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 31 Oct 2024 22:01:03 +0800 Subject: [PATCH 2/9] Add test cases on RuleItemConfigurationChangedProcessor's impl --- .../changed/MaskTableChangedProcessor.java | 7 +-- .../MaskTableChangedProcessorTest.java | 57 ++++++++++--------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessor.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessor.java index 62eb502a4a159..8ee7950c8c106 100644 --- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessor.java +++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessor.java @@ -45,12 +45,7 @@ public MaskTableRuleConfiguration swapRuleItemConfiguration(final AlterRuleItemE @Override public MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { - return database.getRuleMetaData().findSingleRule(MaskRule.class) - .map(optional -> getConfiguration(optional.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); - } - - private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration config) { - return null == config.getTables() ? new MaskRuleConfiguration(new LinkedList<>(), config.getMaskAlgorithms()) : config; + return database.getRuleMetaData().findSingleRule(MaskRule.class).map(MaskRule::getConfiguration).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); } @Override diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java index d9adee29c8dfe..ada208141a707 100644 --- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java +++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java @@ -18,59 +18,65 @@ package org.apache.shardingsphere.mask.rule.changed; 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.MaskColumnRuleConfiguration; import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration; import org.apache.shardingsphere.mask.rule.MaskRule; +import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskColumnRuleConfiguration; +import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration; 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.spi.RuleItemConfigurationChangedProcessor; import org.junit.jupiter.api.Test; +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; -import java.util.Optional; +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.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; class MaskTableChangedProcessorTest { - private final MaskTableChangedProcessor processor = (MaskTableChangedProcessor) TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, "mask.tables"); + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "mask.tables"); @Test void assertSwapRuleItemConfiguration() { - assertThat(processor.swapRuleItemConfiguration(mock(AlterRuleItemEvent.class), "name: test_table").getName(), is("test_table")); + MaskTableRuleConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl", "", "", ""), createYAMLContent()); + assertThat(actual, deepEqual(new MaskTableRuleConfiguration("foo_tbl", Collections.singletonList(new MaskColumnRuleConfiguration("foo_col", "foo_algo"))))); } - @Test - void assertFindRuleConfigurationWhenRuleDoesNotExist() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.empty()); - assertTrue(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty()); + private String createYAMLContent() { + YamlMaskTableRuleConfiguration yamlConfig = new YamlMaskTableRuleConfiguration(); + yamlConfig.setName("foo_tbl"); + YamlMaskColumnRuleConfiguration yamlColumnRuleConfig = new YamlMaskColumnRuleConfiguration(); + yamlColumnRuleConfig.setLogicColumn("foo_col"); + yamlColumnRuleConfig.setMaskAlgorithm("foo_algo"); + yamlConfig.setColumns(Collections.singletonMap("foo_col", yamlColumnRuleConfig)); + return YamlEngine.marshal(yamlConfig); } @Test - void assertFindRuleConfigurationWhenTableDoesNotExist() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(new MaskRule(new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap())))); - assertTrue(processor.findRuleConfiguration(database).getTables().isEmpty()); + void assertFindRuleConfiguration() { + MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class); + assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig)); } - @Test - void assertFindRuleConfigurationWhenRuleExists() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - MaskRule maskRule = mock(MaskRule.class, RETURNS_DEEP_STUBS); - when(maskRule.getConfiguration().getTables()).thenReturn(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))); - when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(maskRule)); - assertFalse(processor.findRuleConfiguration(database).getTables().isEmpty()); + private ShardingSphereDatabase mockDatabase(final MaskRuleConfiguration ruleConfig) { + MaskRule rule = mock(MaskRule.class); + when(rule.getConfiguration()).thenReturn(ruleConfig); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + return result; } @Test @@ -78,17 +84,16 @@ void assertChangeRuleItemConfiguration() { MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration( new LinkedList<>(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))), Collections.emptyMap()); MaskTableRuleConfiguration toBeChangedItemConfig = new MaskTableRuleConfiguration("foo_tbl", Collections.singleton(mock(MaskColumnRuleConfiguration.class))); - processor.changeRuleItemConfiguration( - new AlterNamedRuleItemEvent("foo_db", "foo_tbl", "key", "0", ""), currentRuleConfig, toBeChangedItemConfig); + processor.changeRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl", "", "", ""), currentRuleConfig, toBeChangedItemConfig); assertThat(currentRuleConfig.getTables().size(), is(1)); - assertThat(currentRuleConfig.getTables().iterator().next().getColumns().size(), is(1)); + assertThat(new ArrayList<>(currentRuleConfig.getTables()).get(0).getColumns().size(), is(1)); } @Test void assertDropRuleItemConfiguration() { MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration( new LinkedList<>(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))), Collections.emptyMap()); - processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("foo_db", "foo_tbl", ""), currentRuleConfig); + processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_tbl", ""), currentRuleConfig); assertTrue(currentRuleConfig.getTables().isEmpty()); } } From e69f09e9ece057b3ec4a4fa01fa7ab99609929c8 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 31 Oct 2024 22:22:13 +0800 Subject: [PATCH 3/9] Add test cases on RuleItemConfigurationChangedProcessor's impl --- .../ShardingAuditorChangedProcessorTest.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessorTest.java 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 new file mode 100644 index 0000000000000..17bd467df492e --- /dev/null +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAuditorChangedProcessorTest.java @@ -0,0 +1,94 @@ +/* + * 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.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; +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 ShardingAuditorChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + 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()))); + } + + 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); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + 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")); + } + + @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); + assertTrue(currentRuleConfig.getAuditors().isEmpty()); + } +} From b3904e0969ace95d4b83d304c8863c82ee97adfd Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 31 Oct 2024 22:27:53 +0800 Subject: [PATCH 4/9] Add test cases on RuleItemConfigurationChangedProcessor's impl --- ...ShardingAlgorithmChangedProcessorTest.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessorTest.java 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 new file mode 100644 index 0000000000000..ee9da553cf2e8 --- /dev/null +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/ShardingAlgorithmChangedProcessorTest.java @@ -0,0 +1,94 @@ +/* + * 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.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; +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 ShardingAlgorithmChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + 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()))); + } + + 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); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + 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.getShardingAlgorithms().size(), is(1)); + assertThat(currentRuleConfig.getShardingAlgorithms().get("foo_algo").getType(), is("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); + assertTrue(currentRuleConfig.getShardingAlgorithms().isEmpty()); + } +} From 8e16894490a61e50f956a4c1d578b6c000ed860e Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 31 Oct 2024 22:30:19 +0800 Subject: [PATCH 5/9] Add test cases on RuleItemConfigurationChangedProcessor's impl --- .../ShadowAlgorithmChangedProcessorTest.java | 94 +++++++++++++++++++ ...ShardingAlgorithmChangedProcessorTest.java | 4 +- 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/ShadowAlgorithmChangedProcessorTest.java 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 new file mode 100644 index 0000000000000..d0f9a8b56d666 --- /dev/null +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/ShadowAlgorithmChangedProcessorTest.java @@ -0,0 +1,94 @@ +/* + * 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.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; +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 ShadowAlgorithmChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + 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()))); + } + + 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); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + 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")); + } + + @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); + assertTrue(currentRuleConfig.getShadowAlgorithms().isEmpty()); + } +} 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 ee9da553cf2e8..11c9ac26a06e4 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 @@ -76,7 +76,7 @@ private ShardingSphereDatabase mockDatabase(final ShardingRuleConfiguration rule @Test void assertChangeRuleItemConfiguration() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); - currentRuleConfig.setAuditors(new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class)))); + 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); @@ -87,7 +87,7 @@ void assertChangeRuleItemConfiguration() { @Test void assertDropRuleItemConfiguration() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); - currentRuleConfig.setAuditors(new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class)))); + currentRuleConfig.setShardingAlgorithms(new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class)))); processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_algo", ""), currentRuleConfig); assertTrue(currentRuleConfig.getShardingAlgorithms().isEmpty()); } From c386c4f5e1c9b63bf9c1e915e53ab356f53a0c61 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 31 Oct 2024 22:32:10 +0800 Subject: [PATCH 6/9] Add test cases on RuleItemConfigurationChangedProcessor's impl --- .../KeyGeneratorChangedProcessorTest.java | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/KeyGeneratorChangedProcessorTest.java 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 new file mode 100644 index 0000000000000..1c27dae0d218f --- /dev/null +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/KeyGeneratorChangedProcessorTest.java @@ -0,0 +1,94 @@ +/* + * 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.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; +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 KeyGeneratorChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + 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()))); + } + + 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); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + 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")); + } + + @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); + assertTrue(currentRuleConfig.getKeyGenerators().isEmpty()); + } +} From 0347bcd44e0f66e61e3a1c3c71d6be2f594f51d8 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 31 Oct 2024 22:35:11 +0800 Subject: [PATCH 7/9] Add test cases on RuleItemConfigurationChangedProcessor's impl --- .../EncryptorChangedProcessorTest.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/changed/EncryptorChangedProcessorTest.java 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 new file mode 100644 index 0000000000000..3484a0a79c2cc --- /dev/null +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/changed/EncryptorChangedProcessorTest.java @@ -0,0 +1,92 @@ +/* + * 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.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; +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; +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 EncryptorChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + 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()))); + } + + 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); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + 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")); + } + + @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); + assertTrue(currentRuleConfig.getEncryptors().isEmpty()); + } +} From dadfd0ffbd0abb6a0c537085a5fde42bf55a36b5 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 31 Oct 2024 22:40:27 +0800 Subject: [PATCH 8/9] Add test cases on RuleItemConfigurationChangedProcessor's impl --- ...adowAlgorithmNameChangedProcessorTest.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/DefaultShadowAlgorithmNameChangedProcessorTest.java diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/DefaultShadowAlgorithmNameChangedProcessorTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/DefaultShadowAlgorithmNameChangedProcessorTest.java new file mode 100644 index 0000000000000..57981b4b09f9a --- /dev/null +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/rule/changed/DefaultShadowAlgorithmNameChangedProcessorTest.java @@ -0,0 +1,79 @@ +/* + * 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.shadow.rule.changed; + +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.mode.event.dispatch.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent; +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 static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class DefaultShadowAlgorithmNameChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "shadow.default_shadow_algorithm_name"); + + @Test + void assertSwapRuleItemConfiguration() { + String actual = processor.swapRuleItemConfiguration(mock(AlterRuleItemEvent.class), "foo_algo"); + assertThat(actual, is("foo_algo")); + } + + @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); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + return result; + } + + @Test + void assertChangeRuleItemConfiguration() { + ShadowRuleConfiguration currentRuleConfig = new ShadowRuleConfiguration(); + String toBeChangedItemConfig = "bar_algo"; + processor.changeRuleItemConfiguration(mock(AlterRuleItemEvent.class), currentRuleConfig, toBeChangedItemConfig); + assertThat(currentRuleConfig.getDefaultShadowAlgorithmName(), is("bar_algo")); + } + + @Test + void assertDropRuleItemConfiguration() { + ShadowRuleConfiguration currentRuleConfig = new ShadowRuleConfiguration(); + currentRuleConfig.setDefaultShadowAlgorithmName("foo_algo"); + processor.dropRuleItemConfiguration(mock(DropRuleItemEvent.class), currentRuleConfig); + assertNull(currentRuleConfig.getDefaultShadowAlgorithmName()); + } +} From 8c3f34d0d13bf1bc78048ca02a48c07e15835709 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Thu, 31 Oct 2024 22:42:50 +0800 Subject: [PATCH 9/9] Add test cases on RuleItemConfigurationChangedProcessor's impl --- ...ultShardingColumnChangedProcessorTest.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/DefaultShardingColumnChangedProcessorTest.java diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/DefaultShardingColumnChangedProcessorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/DefaultShardingColumnChangedProcessorTest.java new file mode 100644 index 0000000000000..406ecf9b44ca4 --- /dev/null +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/changed/DefaultShardingColumnChangedProcessorTest.java @@ -0,0 +1,79 @@ +/* + * 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.sharding.rule.changed; + +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.mode.event.dispatch.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent; +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 static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class DefaultShardingColumnChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "sharding.default_sharding_column"); + + @Test + void assertSwapRuleItemConfiguration() { + String actual = processor.swapRuleItemConfiguration(mock(AlterRuleItemEvent.class), "foo_col"); + assertThat(actual, is("foo_col")); + } + + @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); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + return result; + } + + @Test + void assertChangeRuleItemConfiguration() { + ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); + String toBeChangedItemConfig = "bar_col"; + processor.changeRuleItemConfiguration(mock(AlterRuleItemEvent.class), currentRuleConfig, toBeChangedItemConfig); + assertThat(currentRuleConfig.getDefaultShardingColumn(), is("bar_col")); + } + + @Test + void assertDropRuleItemConfiguration() { + ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); + currentRuleConfig.setDefaultShardingColumn("foo_col"); + processor.dropRuleItemConfiguration(mock(DropRuleItemEvent.class), currentRuleConfig); + assertNull(currentRuleConfig.getDefaultShardingColumn()); + } +}