diff --git a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/changed/BroadcastTableChangedProcessorTest.java b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/changed/BroadcastTableChangedProcessorTest.java new file mode 100644 index 0000000000000..373d2991ce5b7 --- /dev/null +++ b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/changed/BroadcastTableChangedProcessorTest.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.broadcast.rule.changed; + +import org.apache.shardingsphere.broadcast.config.BroadcastRuleConfiguration; +import org.apache.shardingsphere.broadcast.rule.BroadcastRule; +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.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.LinkedList; + +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 BroadcastTableChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "broadcast.tables"); + + @Test + void assertSwapRuleItemConfiguration() { + BroadcastRuleConfiguration actual = processor.swapRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class), "- foo_tbl"); + assertThat(actual.getTables(), is(Collections.singleton("foo_tbl"))); + } + + @Test + void assertFindRuleConfiguration() { + BroadcastRuleConfiguration ruleConfig = mock(BroadcastRuleConfiguration.class); + assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig)); + } + + private ShardingSphereDatabase mockDatabase(final BroadcastRuleConfiguration ruleConfig) { + BroadcastRule rule = mock(BroadcastRule.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() { + BroadcastRuleConfiguration currentRuleConfig = new BroadcastRuleConfiguration(new LinkedList<>(Collections.singleton("foo_tbl"))); + BroadcastRuleConfiguration toBeChangedItemConfig = new BroadcastRuleConfiguration(new LinkedList<>(Collections.singleton("bar_tbl"))); + processor.changeRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class), currentRuleConfig, toBeChangedItemConfig); + assertThat(currentRuleConfig.getTables(), is(Collections.singletonList("bar_tbl"))); + } + + @Test + void assertDropRuleItemConfiguration() { + BroadcastRuleConfiguration currentRuleConfig = new BroadcastRuleConfiguration(new LinkedList<>(Collections.singleton("foo_tbl"))); + processor.dropRuleItemConfiguration(mock(DropRuleItemEvent.class), currentRuleConfig); + assertTrue(currentRuleConfig.getTables().isEmpty()); + } +} diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingDataSourceChangedProcessor.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingDataSourceChangedProcessor.java index e17378be9c647..a159c04261ef1 100644 --- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingDataSourceChangedProcessor.java +++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingDataSourceChangedProcessor.java @@ -59,8 +59,7 @@ private TransactionalReadQueryStrategy getTransactionalReadQueryStrategy(final Y @Override public ReadwriteSplittingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { Optional rule = database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class); - return rule.map(ReadwriteSplittingRule::getConfiguration) - .orElseGet(() -> new ReadwriteSplittingRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); + return rule.map(ReadwriteSplittingRule::getConfiguration).orElseGet(() -> new ReadwriteSplittingRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); } @Override diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingDataSourceChangedProcessorTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingDataSourceChangedProcessorTest.java new file mode 100644 index 0000000000000..783994b48f9fa --- /dev/null +++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingDataSourceChangedProcessorTest.java @@ -0,0 +1,107 @@ +/* + * 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.readwritesplitting.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.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor; +import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration; +import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration; +import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; +import org.apache.shardingsphere.readwritesplitting.transaction.TransactionalReadQueryStrategy; +import org.apache.shardingsphere.readwritesplitting.yaml.config.rule.YamlReadwriteSplittingDataSourceGroupRuleConfiguration; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedList; + +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 ReadwriteSplittingDataSourceChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "readwrite_splitting.data_source_groups"); + + @Test + void assertSwapRuleItemConfigurationWithoutTransactionalReadQueryStrategy() { + ReadwriteSplittingDataSourceGroupRuleConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "", "", ""), createYAMLContent(null)); + assertThat(actual, deepEqual(new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo", "write_ds", Collections.singletonList("read_ds"), "foo_balancer"))); + } + + @Test + void assertSwapRuleItemConfigurationWithTransactionalReadQueryStrategy() { + ReadwriteSplittingDataSourceGroupRuleConfiguration actual = processor.swapRuleItemConfiguration( + new AlterNamedRuleItemEvent("", "foo", "", "", ""), createYAMLContent(TransactionalReadQueryStrategy.PRIMARY)); + assertThat(actual, deepEqual(new ReadwriteSplittingDataSourceGroupRuleConfiguration( + "foo", "write_ds", Collections.singletonList("read_ds"), TransactionalReadQueryStrategy.PRIMARY, "foo_balancer"))); + } + + private String createYAMLContent(final TransactionalReadQueryStrategy strategy) { + YamlReadwriteSplittingDataSourceGroupRuleConfiguration yamlConfig = new YamlReadwriteSplittingDataSourceGroupRuleConfiguration(); + yamlConfig.setWriteDataSourceName("write_ds"); + yamlConfig.setReadDataSourceNames(Collections.singletonList("read_ds")); + yamlConfig.setTransactionalReadQueryStrategy(null == strategy ? null : strategy.name()); + yamlConfig.setLoadBalancerName("foo_balancer"); + return YamlEngine.marshal(yamlConfig); + } + + @Test + void assertFindRuleConfiguration() { + ReadwriteSplittingRuleConfiguration ruleConfig = mock(ReadwriteSplittingRuleConfiguration.class); + assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig)); + } + + private ShardingSphereDatabase mockDatabase(final ReadwriteSplittingRuleConfiguration ruleConfig) { + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(ruleConfig); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + return result; + } + + @Test + void assertChangeRuleItemConfiguration() { + ReadwriteSplittingRuleConfiguration currentRuleConfig = new ReadwriteSplittingRuleConfiguration( + new LinkedList<>(Collections.singleton(new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo", "write_ds", Collections.singletonList("read_ds"), "foo_balancer"))), + Collections.emptyMap()); + ReadwriteSplittingDataSourceGroupRuleConfiguration toBeChangedItemConfig = new ReadwriteSplittingDataSourceGroupRuleConfiguration( + "foo", "write_ds", Collections.singletonList("read_ds"), TransactionalReadQueryStrategy.FIXED, "foo_balancer"); + processor.changeRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class), currentRuleConfig, toBeChangedItemConfig); + assertThat(new ArrayList<>(currentRuleConfig.getDataSourceGroups()).get(0).getTransactionalReadQueryStrategy(), is(TransactionalReadQueryStrategy.FIXED)); + } + + @Test + void assertDropRuleItemConfiguration() { + ReadwriteSplittingRuleConfiguration currentRuleConfig = new ReadwriteSplittingRuleConfiguration( + new LinkedList<>(Collections.singleton(new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo", "write_ds", Collections.singletonList("read_ds"), "foo_balancer"))), + Collections.emptyMap()); + processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo", ""), currentRuleConfig); + assertTrue(currentRuleConfig.getDataSourceGroups().isEmpty()); + } +} diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessorTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessorTest.java new file mode 100644 index 0000000000000..b14c42383cd5e --- /dev/null +++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/rule/changed/ReadwriteSplittingLoadBalancerChangedProcessorTest.java @@ -0,0 +1,98 @@ +/* + * 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.readwritesplitting.rule.changed; + +import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor; +import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration; +import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration; +import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +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 ReadwriteSplittingLoadBalancerChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "readwrite_splitting.load_balancers"); + + @Test + void assertSwapRuleItemConfiguration() { + AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "", "", ""), createYAMLContent()); + assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_balancer", new Properties()))); + } + + private String createYAMLContent() { + YamlAlgorithmConfiguration yamlConfig = new YamlAlgorithmConfiguration(); + yamlConfig.setType("foo_balancer"); + return YamlEngine.marshal(yamlConfig); + } + + @Test + void assertFindRuleConfiguration() { + ReadwriteSplittingRuleConfiguration ruleConfig = mock(ReadwriteSplittingRuleConfiguration.class); + assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig)); + } + + private ShardingSphereDatabase mockDatabase(final ReadwriteSplittingRuleConfiguration ruleConfig) { + ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + when(rule.getConfiguration()).thenReturn(ruleConfig); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); + return result; + } + + @Test + void assertChangeRuleItemConfiguration() { + ReadwriteSplittingRuleConfiguration currentRuleConfig = new ReadwriteSplittingRuleConfiguration( + new LinkedList<>(Collections.singleton(new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo", "write_ds", Collections.singletonList("read_ds"), "foo_balancer"))), + new HashMap<>(Collections.singletonMap("foo_balancer", new AlgorithmConfiguration("FOO_BALANCER", new Properties())))); + AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("BAR_BALANCER", new Properties()); + processor.changeRuleItemConfiguration(new AlterNamedRuleItemEvent("", "bar_balancer", "", "", ""), currentRuleConfig, toBeChangedItemConfig); + assertThat(currentRuleConfig.getLoadBalancers().size(), is(2)); + assertThat(currentRuleConfig.getLoadBalancers().get("foo_balancer").getType(), is("FOO_BALANCER")); + assertThat(currentRuleConfig.getLoadBalancers().get("bar_balancer").getType(), is("BAR_BALANCER")); + } + + @Test + void assertDropRuleItemConfiguration() { + ReadwriteSplittingRuleConfiguration currentRuleConfig = new ReadwriteSplittingRuleConfiguration( + new LinkedList<>(Collections.singleton(new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo", "write_ds", Collections.singletonList("read_ds"), "foo_balancer"))), + new HashMap<>(Collections.singletonMap("foo_balancer", new AlgorithmConfiguration("FOO_BALANCER", new Properties())))); + processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_balancer", ""), currentRuleConfig); + assertTrue(currentRuleConfig.getLoadBalancers().isEmpty()); + } +} diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/changed/DefaultDataSourceChangedProcessorTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/changed/DefaultDataSourceChangedProcessorTest.java new file mode 100644 index 0000000000000..c43d38ecc63ca --- /dev/null +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/changed/DefaultDataSourceChangedProcessorTest.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.single.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.single.config.SingleRuleConfiguration; +import org.apache.shardingsphere.single.rule.SingleRule; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.Optional; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class DefaultDataSourceChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "single.default_data_source"); + + @Test + void assertSwapRuleItemConfiguration() { + String actual = processor.swapRuleItemConfiguration(mock(AlterRuleItemEvent.class), "foo_ds"); + assertThat(actual, is("foo_ds")); + } + + @Test + void assertFindRuleConfiguration() { + SingleRuleConfiguration ruleConfig = mock(SingleRuleConfiguration.class); + assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig)); + } + + private ShardingSphereDatabase mockDatabase(final SingleRuleConfiguration ruleConfig) { + SingleRule rule = mock(SingleRule.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() { + SingleRuleConfiguration currentRuleConfig = new SingleRuleConfiguration(Collections.emptyList(), "foo_ds"); + String toBeChangedItemConfig = "bar_ds"; + processor.changeRuleItemConfiguration(mock(AlterRuleItemEvent.class), currentRuleConfig, toBeChangedItemConfig); + assertThat(currentRuleConfig.getDefaultDataSource(), is(Optional.of("bar_ds"))); + } + + @Test + void assertDropRuleItemConfiguration() { + SingleRuleConfiguration currentRuleConfig = new SingleRuleConfiguration(Collections.emptyList(), "foo_ds"); + processor.dropRuleItemConfiguration(mock(DropRuleItemEvent.class), currentRuleConfig); + assertFalse(currentRuleConfig.getDefaultDataSource().isPresent()); + } +} diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/changed/SingleTableChangedProcessorTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/changed/SingleTableChangedProcessorTest.java new file mode 100644 index 0000000000000..040f8f5902671 --- /dev/null +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/changed/SingleTableChangedProcessorTest.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.single.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.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor; +import org.apache.shardingsphere.single.config.SingleRuleConfiguration; +import org.apache.shardingsphere.single.rule.SingleRule; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.LinkedList; + +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 SingleTableChangedProcessorTest { + + @SuppressWarnings("unchecked") + private final RuleItemConfigurationChangedProcessor processor = TypedSPILoader.getService( + RuleItemConfigurationChangedProcessor.class, "single.tables"); + + @Test + void assertSwapRuleItemConfiguration() { + SingleRuleConfiguration actual = processor.swapRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class), "- foo_tbl"); + assertThat(actual.getTables(), is(Collections.singleton("foo_tbl"))); + } + + @Test + void assertFindRuleConfiguration() { + SingleRuleConfiguration ruleConfig = mock(SingleRuleConfiguration.class); + assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig)); + } + + private ShardingSphereDatabase mockDatabase(final SingleRuleConfiguration ruleConfig) { + SingleRule rule = mock(SingleRule.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() { + SingleRuleConfiguration currentRuleConfig = new SingleRuleConfiguration(new LinkedList<>(Collections.singleton("foo_tbl")), null); + SingleRuleConfiguration toBeChangedItemConfig = new SingleRuleConfiguration(new LinkedList<>(Collections.singleton("bar_tbl")), null); + processor.changeRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class), currentRuleConfig, toBeChangedItemConfig); + assertThat(currentRuleConfig.getTables(), is(Collections.singletonList("bar_tbl"))); + } + + @Test + void assertDropRuleItemConfiguration() { + SingleRuleConfiguration currentRuleConfig = new SingleRuleConfiguration(new LinkedList<>(Collections.singleton("foo_tbl")), null); + processor.dropRuleItemConfiguration(mock(DropRuleItemEvent.class), currentRuleConfig); + assertTrue(currentRuleConfig.getTables().isEmpty()); + } +}