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 aa70c6a7865 Refactor ReadwriteSplittingDataSourceChangedProcessorTest (#33491) aa70c6a7865 is described below commit aa70c6a78655a762396f4228b645798e83c52c94 Author: Liang Zhang <zhangli...@apache.org> AuthorDate: Fri Nov 1 11:58:13 2024 +0800 Refactor ReadwriteSplittingDataSourceChangedProcessorTest (#33491) * Refactor ReadwriteSplittingDataSourceChangedProcessorTest * Refactor ReadwriteSplittingDataSourceChangedProcessorTest --- ...iteSplittingDataSourceChangedProcessorTest.java | 29 ++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) 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 index 783994b48f9..1ed47ee1cf1 100644 --- 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 @@ -50,14 +50,17 @@ class ReadwriteSplittingDataSourceChangedProcessorTest { @Test void assertSwapRuleItemConfigurationWithoutTransactionalReadQueryStrategy() { - ReadwriteSplittingDataSourceGroupRuleConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "", "", ""), createYAMLContent(null)); + AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class); + when(event.getItemName()).thenReturn("foo"); + ReadwriteSplittingDataSourceGroupRuleConfiguration actual = processor.swapRuleItemConfiguration(event, 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)); + AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class); + when(event.getItemName()).thenReturn("foo"); + ReadwriteSplittingDataSourceGroupRuleConfiguration actual = processor.swapRuleItemConfiguration(event, createYAMLContent(TransactionalReadQueryStrategy.PRIMARY)); assertThat(actual, deepEqual(new ReadwriteSplittingDataSourceGroupRuleConfiguration( "foo", "write_ds", Collections.singletonList("read_ds"), TransactionalReadQueryStrategy.PRIMARY, "foo_balancer"))); } @@ -87,21 +90,27 @@ class ReadwriteSplittingDataSourceChangedProcessorTest { @Test void assertChangeRuleItemConfiguration() { - ReadwriteSplittingRuleConfiguration currentRuleConfig = new ReadwriteSplittingRuleConfiguration( - new LinkedList<>(Collections.singleton(new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo", "write_ds", Collections.singletonList("read_ds"), "foo_balancer"))), - Collections.emptyMap()); + AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class); + when(event.getItemName()).thenReturn("foo"); + ReadwriteSplittingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); ReadwriteSplittingDataSourceGroupRuleConfiguration toBeChangedItemConfig = new ReadwriteSplittingDataSourceGroupRuleConfiguration( "foo", "write_ds", Collections.singletonList("read_ds"), TransactionalReadQueryStrategy.FIXED, "foo_balancer"); - processor.changeRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class), currentRuleConfig, toBeChangedItemConfig); + processor.changeRuleItemConfiguration(event, currentRuleConfig, toBeChangedItemConfig); assertThat(new ArrayList<>(currentRuleConfig.getDataSourceGroups()).get(0).getTransactionalReadQueryStrategy(), is(TransactionalReadQueryStrategy.FIXED)); } @Test void assertDropRuleItemConfiguration() { - ReadwriteSplittingRuleConfiguration currentRuleConfig = new ReadwriteSplittingRuleConfiguration( + DropNamedRuleItemEvent event = mock(DropNamedRuleItemEvent.class); + when(event.getItemName()).thenReturn("foo"); + ReadwriteSplittingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); + processor.dropRuleItemConfiguration(event, currentRuleConfig); + assertTrue(currentRuleConfig.getDataSourceGroups().isEmpty()); + } + + private static ReadwriteSplittingRuleConfiguration createCurrentRuleConfiguration() { + return 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()); } }