|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shardingsphere.shadow.swapper; |
| 19 | + |
| 20 | +import org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlShardingSphereAlgorithmConfiguration; |
| 21 | +import org.apache.shardingsphere.shadow.algorithm.ColumnRegularMatchShadowAlgorithm; |
| 22 | +import org.apache.shardingsphere.shadow.algorithm.config.AlgorithmProvidedShadowRuleConfiguration; |
| 23 | +import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration; |
| 24 | +import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration; |
| 25 | +import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; |
| 26 | +import org.apache.shardingsphere.shadow.yaml.config.YamlShadowRuleConfiguration; |
| 27 | +import org.apache.shardingsphere.shadow.yaml.config.datasource.YamlShadowDataSourceConfiguration; |
| 28 | +import org.apache.shardingsphere.shadow.yaml.config.table.YamlShadowTableConfiguration; |
| 29 | +import org.apache.shardingsphere.shadow.yaml.swapper.ShadowRuleAlgorithmProviderConfigurationYamlSwapper; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import java.util.Arrays; |
| 33 | + |
| 34 | +import static org.hamcrest.CoreMatchers.is; |
| 35 | +import static org.junit.Assert.assertNotNull; |
| 36 | +import static org.junit.Assert.assertThat; |
| 37 | + |
| 38 | +public final class ShadowRuleAlgorithmProviderConfigurationYamlSwapperTest { |
| 39 | + |
| 40 | + private AlgorithmProvidedShadowRuleConfiguration algorithmProvidedShadowRuleConfiguration; |
| 41 | + |
| 42 | + private YamlShadowRuleConfiguration yamlShadowRuleConfiguration; |
| 43 | + |
| 44 | + private void buildAlgorithmProvidedShadowRuleConfiguration() { |
| 45 | + algorithmProvidedShadowRuleConfiguration = new AlgorithmProvidedShadowRuleConfiguration("id", Arrays.asList("ds"), Arrays.asList("ds-shadow")); |
| 46 | + algorithmProvidedShadowRuleConfiguration.getDataSources().put("shadow-data-source", new ShadowDataSourceConfiguration("ds", "ds-shadow")); |
| 47 | + algorithmProvidedShadowRuleConfiguration.getShadowTables().put("t_order", new ShadowTableConfiguration(Arrays.asList("user-id-match-algorithm", "note-algorithm"))); |
| 48 | + algorithmProvidedShadowRuleConfiguration.getShadowAlgorithms().put("user-id-match-algorithm", new ColumnRegularMatchShadowAlgorithm()); |
| 49 | + } |
| 50 | + |
| 51 | + private void buildYamlShadowRuleConfiguration() { |
| 52 | + yamlShadowRuleConfiguration = new YamlShadowRuleConfiguration(); |
| 53 | + yamlShadowRuleConfiguration.setColumn("id"); |
| 54 | + yamlShadowRuleConfiguration.setSourceDataSourceNames(Arrays.asList("ds")); |
| 55 | + yamlShadowRuleConfiguration.setShadowDataSourceNames(Arrays.asList("ds-shadow")); |
| 56 | + YamlShadowDataSourceConfiguration yamlShadowDataSourceConfiguration = new YamlShadowDataSourceConfiguration(); |
| 57 | + yamlShadowDataSourceConfiguration.setSourceDataSourceName("ds"); |
| 58 | + yamlShadowDataSourceConfiguration.setShadowDataSourceName("ds-shadow"); |
| 59 | + yamlShadowRuleConfiguration.getDataSources().put("shadow-data-source", yamlShadowDataSourceConfiguration); |
| 60 | + YamlShadowTableConfiguration yamlShadowTableConfiguration = new YamlShadowTableConfiguration(); |
| 61 | + yamlShadowTableConfiguration.setShadowAlgorithmNames(Arrays.asList("user-id-match-algorithm", "note-algorithm")); |
| 62 | + yamlShadowRuleConfiguration.getShadowTables().put("t_order", yamlShadowTableConfiguration); |
| 63 | + YamlShardingSphereAlgorithmConfiguration yamlShardingSphereAlgorithmConfiguration = new YamlShardingSphereAlgorithmConfiguration(); |
| 64 | + yamlShardingSphereAlgorithmConfiguration.setType("COLUMN-REGULAR-MATCH"); |
| 65 | + yamlShadowRuleConfiguration.getShadowAlgorithms().put("user-id-match-algorithm", yamlShardingSphereAlgorithmConfiguration); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void assertSwapToYamlConfiguration() { |
| 70 | + buildAlgorithmProvidedShadowRuleConfiguration(); |
| 71 | + ShadowRuleAlgorithmProviderConfigurationYamlSwapper swapper = new ShadowRuleAlgorithmProviderConfigurationYamlSwapper(); |
| 72 | + YamlShadowRuleConfiguration yamlShadowRuleConfiguration = swapper.swapToYamlConfiguration(algorithmProvidedShadowRuleConfiguration); |
| 73 | + assertThat(yamlShadowRuleConfiguration.getColumn(), is(algorithmProvidedShadowRuleConfiguration.getColumn())); |
| 74 | + assertThat(yamlShadowRuleConfiguration.getShadowDataSourceNames(), is(algorithmProvidedShadowRuleConfiguration.getShadowDataSourceNames())); |
| 75 | + assertThat(yamlShadowRuleConfiguration.getSourceDataSourceNames(), is(algorithmProvidedShadowRuleConfiguration.getSourceDataSourceNames())); |
| 76 | + yamlShadowRuleConfiguration.getDataSources().entrySet().forEach(each -> { |
| 77 | + ShadowDataSourceConfiguration dataSourceConfiguration = algorithmProvidedShadowRuleConfiguration.getDataSources().get(each.getKey()); |
| 78 | + assertNotNull(dataSourceConfiguration); |
| 79 | + assertThat(each.getValue().getShadowDataSourceName(), is(dataSourceConfiguration.getShadowDataSourceName())); |
| 80 | + assertThat(each.getValue().getSourceDataSourceName(), is(dataSourceConfiguration.getSourceDataSourceName())); |
| 81 | + }); |
| 82 | + yamlShadowRuleConfiguration.getShadowTables().entrySet().forEach(each -> { |
| 83 | + ShadowTableConfiguration shadowTableConfiguration = algorithmProvidedShadowRuleConfiguration.getShadowTables().get(each.getKey()); |
| 84 | + assertNotNull(shadowTableConfiguration); |
| 85 | + assertThat(each.getValue().getShadowAlgorithmNames(), is(shadowTableConfiguration.getShadowAlgorithmNames())); |
| 86 | + }); |
| 87 | + yamlShadowRuleConfiguration.getShadowAlgorithms().entrySet().forEach(each -> { |
| 88 | + ShadowAlgorithm shadowAlgorithm = algorithmProvidedShadowRuleConfiguration.getShadowAlgorithms().get(each.getKey()); |
| 89 | + assertNotNull(shadowAlgorithm); |
| 90 | + assertThat(each.getValue().getType(), is(shadowAlgorithm.getType())); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void assertSwapToObject() { |
| 96 | + buildYamlShadowRuleConfiguration(); |
| 97 | + ShadowRuleAlgorithmProviderConfigurationYamlSwapper swapper = new ShadowRuleAlgorithmProviderConfigurationYamlSwapper(); |
| 98 | + AlgorithmProvidedShadowRuleConfiguration targetConfiguration = swapper.swapToObject(yamlShadowRuleConfiguration); |
| 99 | + assertThat(targetConfiguration.getColumn(), is(yamlShadowRuleConfiguration.getColumn())); |
| 100 | + assertThat(targetConfiguration.getShadowDataSourceNames(), is(yamlShadowRuleConfiguration.getShadowDataSourceNames())); |
| 101 | + assertThat(targetConfiguration.getSourceDataSourceNames(), is(yamlShadowRuleConfiguration.getSourceDataSourceNames())); |
| 102 | + targetConfiguration.getDataSources().entrySet().forEach(each -> { |
| 103 | + YamlShadowDataSourceConfiguration yamlShadowDataSourceConfiguration = yamlShadowRuleConfiguration.getDataSources().get(each.getKey()); |
| 104 | + assertNotNull(yamlShadowDataSourceConfiguration); |
| 105 | + assertThat(each.getValue().getShadowDataSourceName(), is(yamlShadowDataSourceConfiguration.getShadowDataSourceName())); |
| 106 | + assertThat(each.getValue().getSourceDataSourceName(), is(yamlShadowDataSourceConfiguration.getSourceDataSourceName())); |
| 107 | + }); |
| 108 | + targetConfiguration.getShadowTables().entrySet().forEach(each -> { |
| 109 | + YamlShadowTableConfiguration yamlShadowTableConfiguration = yamlShadowRuleConfiguration.getShadowTables().get(each.getKey()); |
| 110 | + assertNotNull(yamlShadowTableConfiguration); |
| 111 | + assertThat(each.getValue().getShadowAlgorithmNames(), is(yamlShadowTableConfiguration.getShadowAlgorithmNames())); |
| 112 | + }); |
| 113 | + targetConfiguration.getShadowAlgorithms().entrySet().forEach(each -> { |
| 114 | + YamlShardingSphereAlgorithmConfiguration yamlShardingSphereAlgorithmConfiguration = yamlShadowRuleConfiguration.getShadowAlgorithms().get(each.getKey()); |
| 115 | + assertNotNull(yamlShardingSphereAlgorithmConfiguration); |
| 116 | + assertThat(each.getValue().getType(), is(yamlShardingSphereAlgorithmConfiguration.getType())); |
| 117 | + }); |
| 118 | + } |
| 119 | +} |
0 commit comments