Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test cases on RuleItemConfigurationChangedProcessor's impl #33484

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<BroadcastRuleConfiguration, BroadcastRuleConfiguration> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ private TransactionalReadQueryStrategy getTransactionalReadQueryStrategy(final Y
@Override
public ReadwriteSplittingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
Optional<ReadwriteSplittingRule> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ReadwriteSplittingRuleConfiguration, ReadwriteSplittingDataSourceGroupRuleConfiguration> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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<ReadwriteSplittingRuleConfiguration, AlgorithmConfiguration> 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());
}
}
Loading
Loading