Skip to content

Commit 3810f14

Browse files
committed
FIX apache#11687 issue add unit test
1 parent af37971 commit 3810f14

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.datasource;
19+
20+
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
21+
import org.apache.shardingsphere.shadow.yaml.config.datasource.YamlShadowDataSourceConfiguration;
22+
import org.apache.shardingsphere.shadow.yaml.swapper.datasource.ShadowDataSourceConfigurationYamlSwapper;
23+
import org.junit.Test;
24+
25+
import static org.hamcrest.CoreMatchers.is;
26+
import static org.junit.Assert.assertThat;
27+
28+
public final class ShadowDataSourceConfigurationYamlSwapperTest {
29+
30+
@Test
31+
public void assertSwapToYamlConfiguration() {
32+
ShadowDataSourceConfiguration shadowDataSourceConfiguration = new ShadowDataSourceConfiguration("ds", "ds-shadow");
33+
ShadowDataSourceConfigurationYamlSwapper swapper = new ShadowDataSourceConfigurationYamlSwapper();
34+
YamlShadowDataSourceConfiguration configuration = swapper.swapToYamlConfiguration(shadowDataSourceConfiguration);
35+
assertThat(shadowDataSourceConfiguration.getSourceDataSourceName(), is(configuration.getSourceDataSourceName()));
36+
assertThat(shadowDataSourceConfiguration.getShadowDataSourceName(), is(configuration.getShadowDataSourceName()));
37+
}
38+
39+
@Test
40+
public void assertSwapToObject() {
41+
YamlShadowDataSourceConfiguration yamlConfiguration = new YamlShadowDataSourceConfiguration();
42+
yamlConfiguration.setShadowDataSourceName("ds-shadow");
43+
yamlConfiguration.setSourceDataSourceName("ds");
44+
ShadowDataSourceConfigurationYamlSwapper swapper = new ShadowDataSourceConfigurationYamlSwapper();
45+
ShadowDataSourceConfiguration dataSourceConfiguration = swapper.swapToObject(yamlConfiguration);
46+
assertThat(yamlConfiguration.getSourceDataSourceName(), is(dataSourceConfiguration.getSourceDataSourceName()));
47+
assertThat(yamlConfiguration.getShadowDataSourceName(), is(dataSourceConfiguration.getShadowDataSourceName()));
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.table;
19+
20+
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
21+
import org.apache.shardingsphere.shadow.yaml.config.table.YamlShadowTableConfiguration;
22+
import org.apache.shardingsphere.shadow.yaml.swapper.table.ShadowTableConfigurationYamlSwapper;
23+
import org.junit.Test;
24+
25+
import java.util.Arrays;
26+
27+
import static org.hamcrest.CoreMatchers.is;
28+
import static org.junit.Assert.assertThat;
29+
30+
public final class ShadowTableConfigurationYamlSwapperTest {
31+
32+
@Test
33+
public void assertSwapToYamlConfiguration() {
34+
ShadowTableConfiguration shadowTableConfiguration = new ShadowTableConfiguration(Arrays.asList("t_order", "t_user"));
35+
ShadowTableConfigurationYamlSwapper swapper = new ShadowTableConfigurationYamlSwapper();
36+
YamlShadowTableConfiguration yamlShadowTableConfiguration = swapper.swapToYamlConfiguration(shadowTableConfiguration);
37+
assertThat(yamlShadowTableConfiguration.getShadowAlgorithmNames(), is(shadowTableConfiguration.getShadowAlgorithmNames()));
38+
}
39+
40+
@Test
41+
public void assertSwapToObject() {
42+
YamlShadowTableConfiguration yamlConfig = new YamlShadowTableConfiguration();
43+
yamlConfig.setShadowAlgorithmNames(Arrays.asList("user-id-match-algorithm", "note-algorithm"));
44+
ShadowTableConfigurationYamlSwapper swapper = new ShadowTableConfigurationYamlSwapper();
45+
ShadowTableConfiguration shadowTableConfiguration = swapper.swapToObject(yamlConfig);
46+
assertThat(shadowTableConfiguration.getShadowAlgorithmNames(), is(yamlConfig.getShadowAlgorithmNames()));
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.rule;
2+
3+
import org.junit.runner.RunWith;
4+
import org.mockito.junit.MockitoJUnitRunner;
5+
6+
@RunWith(MockitoJUnitRunner.class)
7+
public class RuleDefinitionBackendHandlerTest {
8+
9+
}

0 commit comments

Comments
 (0)