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 b777e8d Add more test cases for root rule configuration YAML swapper
#6848 (#6874)
b777e8d is described below
commit b777e8d746247da9287702da915be5d6bd5d2759
Author: zhang jian <[email protected]>
AuthorDate: Sun Aug 16 22:48:05 2020 +0800
Add more test cases for root rule configuration YAML swapper #6848 (#6874)
* Add more test cases for root rule configuration YAML swapper #6848
---
...orithmProviderConfigurationYamlSwapperTest.java | 87 ++++++++++++++++++++
.../ReplicaRuleConfigurationYamlSwapperTest.java | 92 ++++++++++++++++++++++
2 files changed, 179 insertions(+)
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/EncryptRuleAlgorithmProviderConfigurationYamlSwapperTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/EncryptRuleAlgorithmProviderConfigurationYamlSwapperTest.java
new file mode 100644
index 0000000..f162043
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/yaml/swapper/EncryptRuleAlgorithmProviderConfigurationYamlSwapperTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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.yaml.swapper;
+
+import
org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import
org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry;
+import
org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class EncryptRuleAlgorithmProviderConfigurationYamlSwapperTest {
+
+ static {
+
ShardingSphereServiceLoader.register(YamlRuleConfigurationSwapper.class);
+ }
+
+ @Mock
+ private AlgorithmProvidedEncryptRuleConfiguration ruleConfig;
+
+ @Test
+ public void assertSwapToYamlConfiguration() {
+ YamlEncryptRuleConfiguration actual =
getSwapper().swapToYamlConfiguration(createAlgorithmProvidedEncryptRuleConfiguration());
+ assertThat(actual.getTables().size(), is(1));
+ assertThat(actual.getEncryptors().size(), is(0));
+ }
+
+ private AlgorithmProvidedEncryptRuleConfiguration
createAlgorithmProvidedEncryptRuleConfiguration() {
+ Collection<EncryptTableRuleConfiguration> tables =
Collections.singletonList(new EncryptTableRuleConfiguration("tbl",
Collections.emptyList()));
+ Map<String, EncryptAlgorithm> encryptors = new LinkedHashMap<>();
+ return new AlgorithmProvidedEncryptRuleConfiguration(tables,
encryptors);
+ }
+
+ @Test
+ public void assertSwapToObject() {
+ AlgorithmProvidedEncryptRuleConfiguration actual =
getSwapper().swapToObject(createYamlEncryptRuleConfiguration());
+ assertThat(actual.getTables().size(), is(1));
+ assertThat(actual.getEncryptors().size(), is(0));
+ }
+
+ private YamlEncryptRuleConfiguration createYamlEncryptRuleConfiguration() {
+ YamlEncryptRuleConfiguration result = new
YamlEncryptRuleConfiguration();
+ YamlEncryptTableRuleConfiguration tableRuleConfig = new
YamlEncryptTableRuleConfiguration();
+ tableRuleConfig.setName("t_encrypt");
+ result.getTables().put("t_encrypt", tableRuleConfig);
+ YamlShardingSphereAlgorithmConfiguration algorithmConfig = new
YamlShardingSphereAlgorithmConfiguration();
+ algorithmConfig.setType("TEST");
+ result.getEncryptors().put("test", algorithmConfig);
+ return result;
+ }
+
+ private EncryptRuleAlgorithmProviderConfigurationYamlSwapper getSwapper() {
+ return (EncryptRuleAlgorithmProviderConfigurationYamlSwapper)
+
OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig),
YamlRuleConfigurationSwapper.class).get(ruleConfig);
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/test/java/org/apache/shardingsphere/replica/yaml/swapper/ReplicaRuleConfigurationYamlSwapperTest.java
b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/test/java/org/apache/shardingsphere/replica/yaml/swapper/ReplicaRuleConfigurationYamlSwapperTest.java
new file mode 100644
index 0000000..abdb50d
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-replica/shardingsphere-replica-common/src/test/java/org/apache/shardingsphere/replica/yaml/swapper/ReplicaRuleConfigurationYamlSwapperTest.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.replica.yaml.swapper;
+
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry;
+import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper;
+import
org.apache.shardingsphere.replica.api.config.ReplicaDataSourceConfiguration;
+import org.apache.shardingsphere.replica.api.config.ReplicaRuleConfiguration;
+import
org.apache.shardingsphere.replica.yaml.config.YamlReplicaDataSourceConfiguration;
+import
org.apache.shardingsphere.replica.yaml.config.YamlReplicaRuleConfiguration;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class ReplicaRuleConfigurationYamlSwapperTest {
+
+ static {
+
ShardingSphereServiceLoader.register(YamlRuleConfigurationSwapper.class);
+ }
+
+ @Mock
+ private ReplicaRuleConfiguration ruleConfig;
+
+ @Test
+ public void assertSwapToYamlConfiguration() {
+ YamlReplicaRuleConfiguration configuration =
getSwapper().swapToYamlConfiguration(createReplicaRuleConfiguration());
+ assertThat(configuration.getDataSources().size(), is(1));
+ assertTrue(configuration.getDataSources().containsKey("name"));
+
assertTrue(configuration.getDataSources().get("name").getReplicaDataSourceNames().contains("replicaSourceNames"));
+ }
+
+ private ReplicaRuleConfiguration createReplicaRuleConfiguration() {
+ ReplicaDataSourceConfiguration configuration = new
ReplicaDataSourceConfiguration("name", Arrays.asList("replicaSourceNames"));
+ ReplicaRuleConfiguration replicaRuleConfiguration = new
ReplicaRuleConfiguration(Arrays.asList(configuration));
+ return replicaRuleConfiguration;
+ }
+
+ @Test
+ public void assertSwapToObject() {
+ ReplicaRuleConfiguration configuration =
getSwapper().swapToObject(createYamlReplicaRuleConfiguration());
+ assertThat(configuration.getDataSources().size(), is(1));
+ Collection<ReplicaDataSourceConfiguration> dataSources =
configuration.getDataSources();
+ ReplicaDataSourceConfiguration sourceConfiguration =
dataSources.stream().findFirst().orElse(null);
+ assertNotNull(sourceConfiguration);
+ assertThat(sourceConfiguration.getName(), is("dataSources"));
+
assertTrue(sourceConfiguration.getReplicaSourceNames().contains("replicaDataSourceNames"));
+ }
+
+ private YamlReplicaRuleConfiguration createYamlReplicaRuleConfiguration() {
+ YamlReplicaDataSourceConfiguration configuration = new
YamlReplicaDataSourceConfiguration();
+ configuration.setName("name");
+
configuration.setReplicaDataSourceNames(Arrays.asList("replicaDataSourceNames"));
+ Map<String, YamlReplicaDataSourceConfiguration> dataSources = new
LinkedHashMap<>();
+ dataSources.put("dataSources", configuration);
+ YamlReplicaRuleConfiguration yamlReplicaRuleConfiguration = new
YamlReplicaRuleConfiguration();
+ yamlReplicaRuleConfiguration.setDataSources(dataSources);
+ return yamlReplicaRuleConfiguration;
+ }
+
+ private ReplicaRuleConfigurationYamlSwapper getSwapper() {
+ return (ReplicaRuleConfigurationYamlSwapper)
OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig),
YamlRuleConfigurationSwapper.class).get(ruleConfig);
+ }
+}