未验证 提交 59e93705 编写于 作者: A Andrew 提交者: GitHub

add test case for MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper (#6891)

* add test case for MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper

* refactor MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapperTest
上级 46c20adb
/*
* 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.masterslave.yaml.swapper;
import com.google.common.collect.ImmutableMap;
import org.apache.shardingsphere.masterslave.algorithm.RandomMasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.masterslave.algorithm.config.AlgorithmProvidedMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.masterslave.api.config.rule.MasterSlaveDataSourceRuleConfiguration;
import org.apache.shardingsphere.masterslave.constant.MasterSlaveOrder;
import org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveRuleConfiguration;
import org.junit.Test;
import java.util.Collections;
import java.util.LinkedHashMap;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public final class MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapperTest {
private final MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper swapper = new MasterSlaveRuleAlgorithmProviderConfigurationYamlSwapper();
@Test
public void assertSwapToYamlConfiguration() {
YamlMasterSlaveRuleConfiguration actual = createYamlMasterSlaveRuleConfiguration();
assertNotNull(actual);
assertNotNull(actual.getDataSources());
assertThat(actual.getDataSources().keySet(), is(Collections.singleton("name")));
assertThat(actual.getDataSources().get("name").getName(), is("name"));
assertThat(actual.getDataSources().get("name").getMasterDataSourceName(), is("masterDataSourceName"));
assertThat(actual.getDataSources().get("name").getLoadBalancerName(), is("loadBalancerName"));
assertThat(actual.getDataSources().get("name").getSlaveDataSourceNames(), is(Collections.singletonList("slaveDataSourceName")));
assertNotNull(actual.getLoadBalancers());
assertThat(actual.getLoadBalancers().keySet(), is(Collections.singleton("name")));
assertNotNull(actual.getLoadBalancers().get("name"));
assertThat(actual.getLoadBalancers().get("name").getType(), is("RANDOM"));
}
@Test
public void assertSwapToObject() {
AlgorithmProvidedMasterSlaveRuleConfiguration actual = swapper.swapToObject(createYamlMasterSlaveRuleConfiguration());
assertNotNull(actual);
assertNotNull(actual.getDataSources());
assertTrue(actual.getDataSources().iterator().hasNext());
MasterSlaveDataSourceRuleConfiguration ruleConfiguration = actual.getDataSources().iterator().next();
assertNotNull(ruleConfiguration);
assertThat(ruleConfiguration.getName(), is("name"));
assertThat(ruleConfiguration.getMasterDataSourceName(), is("masterDataSourceName"));
assertThat(ruleConfiguration.getLoadBalancerName(), is("loadBalancerName"));
assertThat(ruleConfiguration.getSlaveDataSourceNames(), is(Collections.singletonList("slaveDataSourceName")));
assertThat(actual.getLoadBalanceAlgorithms(), is(new LinkedHashMap<>()));
}
@Test
public void assertGetTypeClass() {
assertThat(swapper.getTypeClass(), equalTo(AlgorithmProvidedMasterSlaveRuleConfiguration.class));
}
@Test
public void assertGetRuleTagName() {
assertThat(swapper.getRuleTagName(), is("MASTER_SLAVE"));
}
@Test
public void assertGetOrder() {
assertThat(swapper.getOrder(), is(MasterSlaveOrder.ALGORITHM_PROVIDER_MASTER_SLAVE_ORDER));
}
private YamlMasterSlaveRuleConfiguration createYamlMasterSlaveRuleConfiguration() {
MasterSlaveDataSourceRuleConfiguration ruleConfiguration = new MasterSlaveDataSourceRuleConfiguration("name", "masterDataSourceName",
Collections.singletonList("slaveDataSourceName"), "loadBalancerName");
YamlMasterSlaveRuleConfiguration result = swapper.swapToYamlConfiguration(
new AlgorithmProvidedMasterSlaveRuleConfiguration(Collections.singletonList(ruleConfiguration), ImmutableMap.of("name", new RandomMasterSlaveLoadBalanceAlgorithm())));
return result;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册