OrchestrationSpringBootRegistryShardingTest.java 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18
package org.apache.shardingsphere.spring.boot.orchestration.type;
19

20
import lombok.SneakyThrows;
21
import org.apache.commons.dbcp2.BasicDataSource;
22 23
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.driver.orchestration.internal.datasource.OrchestrationShardingSphereDataSource;
24 25
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.datanode.DataNode;
26
import org.apache.shardingsphere.kernel.context.SchemaContexts;
27 28
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.TableRule;
29
import org.apache.shardingsphere.sharding.strategy.standard.StandardShardingStrategy;
30
import org.apache.shardingsphere.spring.boot.orchestration.registry.TestOrchestrationRepository;
L
Liang Zhang 已提交
31
import org.apache.shardingsphere.spring.boot.orchestration.util.EmbedTestingServer;
32 33 34 35 36 37 38 39
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

40 41 42
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.lang.reflect.Field;
43 44 45
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
46

47 48 49 50
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
51 52 53 54 55 56 57

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = OrchestrationSpringBootRegistryShardingTest.class)
@SpringBootApplication
@ActiveProfiles("registry")
public class OrchestrationSpringBootRegistryShardingTest {
    
58 59 60 61
    private static final String SHARDING_DATABASES_FILE = "yaml/sharding-databases.yaml";
    
    private static final String SHARDING_RULE_FILE = "yaml/sharding-rule.yaml";
    
62 63 64 65 66 67
    @Resource
    private DataSource dataSource;
    
    @BeforeClass
    public static void init() {
        EmbedTestingServer.start();
68 69
        String shardingDatabases = readYAML(SHARDING_DATABASES_FILE);
        String shardingRule = readYAML(SHARDING_RULE_FILE);
70
        TestOrchestrationRepository repository = new TestOrchestrationRepository();
M
menghaoranss 已提交
71 72 73
        repository.persist("/config/schema/logic_db/datasource", shardingDatabases);
        repository.persist("/config/schema/logic_db/rule", shardingRule);
        repository.persist("/config/props", ""
74 75
                + "executor.size: '100'\n"
                + "sql.show: 'true'\n");
M
menghaoranss 已提交
76
        repository.persist("/registry/datasources", "");
77 78 79
    }
    
    @Test
80 81 82
    public void assertWithShardingSphereDataSource() {
        assertTrue(dataSource instanceof OrchestrationShardingSphereDataSource);
        ShardingSphereDataSource shardingSphereDataSource = getFieldValue("dataSource", OrchestrationShardingSphereDataSource.class, dataSource);
J
Juan Pan(Trista) 已提交
83
        SchemaContexts schemaContexts = shardingSphereDataSource.getSchemaContexts();
84
        for (DataSource each : shardingSphereDataSource.getDataSourceMap().values()) {
85 86
            assertThat(((BasicDataSource) each).getMaxTotal(), is(16));
        }
L
Liang Zhang 已提交
87 88
        assertTrue(schemaContexts.getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW));
        assertTrue(schemaContexts.getProps().getValue(ConfigurationPropertyKey.SQL_SHOW));
89 90 91
    }
    
    @Test
92 93
    public void assertWithShardingSphereDataSourceNames() {
        ShardingSphereDataSource shardingSphereDataSource = getFieldValue("dataSource", OrchestrationShardingSphereDataSource.class, dataSource);
J
Juan Pan(Trista) 已提交
94 95
        SchemaContexts schemaContexts = shardingSphereDataSource.getSchemaContexts();
        ShardingRule shardingRule = (ShardingRule) schemaContexts.getDefaultSchemaContext().getSchema().getRules().iterator().next();
96 97 98
        assertThat(shardingRule.getDataSourceNames().size(), is(2));
        assertTrue(shardingRule.getDataSourceNames().contains("ds_0"));
        assertTrue(shardingRule.getDataSourceNames().contains("ds_1"));
99 100 101 102
    }
    
    @Test
    public void assertWithTableRules() {
103
        ShardingSphereDataSource shardingSphereDataSource = getFieldValue("dataSource", OrchestrationShardingSphereDataSource.class, dataSource);
J
Juan Pan(Trista) 已提交
104 105
        SchemaContexts schemaContexts = shardingSphereDataSource.getSchemaContexts();
        ShardingRule shardingRule = (ShardingRule) schemaContexts.getDefaultSchemaContext().getSchema().getRules().iterator().next();
106 107 108 109 110 111 112 113
        assertThat(shardingRule.getTableRules().size(), is(2));
        TableRule orderRule = shardingRule.getTableRule("t_order");
        assertThat(orderRule.getLogicTable(), is("t_order"));
        assertThat(orderRule.getActualDataNodes().size(), is(4));
        assertTrue(orderRule.getActualDataNodes().contains(new DataNode("ds_0", "t_order_0")));
        assertTrue(orderRule.getActualDataNodes().contains(new DataNode("ds_0", "t_order_1")));
        assertTrue(orderRule.getActualDataNodes().contains(new DataNode("ds_1", "t_order_0")));
        assertTrue(orderRule.getActualDataNodes().contains(new DataNode("ds_1", "t_order_1")));
114 115
        assertTrue(orderRule.getGenerateKeyColumn().isPresent());
        assertThat(orderRule.getGenerateKeyColumn().get(), is("order_id"));
116 117 118 119 120 121 122
        TableRule itemRule = shardingRule.getTableRule("t_order_item");
        assertThat(itemRule.getLogicTable(), is("t_order_item"));
        assertThat(itemRule.getActualDataNodes().size(), is(4));
        assertTrue(itemRule.getActualDataNodes().contains(new DataNode("ds_0", "t_order_item_0")));
        assertTrue(itemRule.getActualDataNodes().contains(new DataNode("ds_0", "t_order_item_1")));
        assertTrue(itemRule.getActualDataNodes().contains(new DataNode("ds_1", "t_order_item_0")));
        assertTrue(itemRule.getActualDataNodes().contains(new DataNode("ds_1", "t_order_item_1")));
123
        assertThat(itemRule.getTableShardingStrategy(), instanceOf(StandardShardingStrategy.class));
124
        assertThat(itemRule.getTableShardingStrategy().getShardingColumns().iterator().next(), is("order_id"));
125 126
        assertTrue(itemRule.getGenerateKeyColumn().isPresent());
        assertThat(itemRule.getGenerateKeyColumn().get(), is("order_item_id"));
127
        assertThat(itemRule.getTableShardingStrategy(), instanceOf(StandardShardingStrategy.class));
128 129 130 131 132
        assertThat(itemRule.getTableShardingStrategy().getShardingColumns().iterator().next(), is("order_id"));
    }
    
    @Test
    public void assertWithBindingTableRules() {
133
        ShardingSphereDataSource shardingSphereDataSource = getFieldValue("dataSource", OrchestrationShardingSphereDataSource.class, dataSource);
J
Juan Pan(Trista) 已提交
134 135
        SchemaContexts schemaContexts = shardingSphereDataSource.getSchemaContexts();
        ShardingRule shardingRule = (ShardingRule) schemaContexts.getDefaultSchemaContext().getSchema().getRules().iterator().next();
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        assertThat(shardingRule.getBindingTableRules().size(), is(2));
        TableRule orderRule = shardingRule.getTableRule("t_order");
        assertThat(orderRule.getLogicTable(), is("t_order"));
        assertThat(orderRule.getActualDataNodes().size(), is(4));
        assertTrue(orderRule.getActualDataNodes().contains(new DataNode("ds_0", "t_order_0")));
        assertTrue(orderRule.getActualDataNodes().contains(new DataNode("ds_0", "t_order_1")));
        assertTrue(orderRule.getActualDataNodes().contains(new DataNode("ds_1", "t_order_0")));
        assertTrue(orderRule.getActualDataNodes().contains(new DataNode("ds_1", "t_order_1")));
        TableRule itemRule = shardingRule.getTableRule("t_order_item");
        assertThat(itemRule.getLogicTable(), is("t_order_item"));
        assertThat(itemRule.getActualDataNodes().size(), is(4));
        assertTrue(itemRule.getActualDataNodes().contains(new DataNode("ds_0", "t_order_item_0")));
        assertTrue(itemRule.getActualDataNodes().contains(new DataNode("ds_0", "t_order_item_1")));
        assertTrue(itemRule.getActualDataNodes().contains(new DataNode("ds_1", "t_order_item_0")));
        assertTrue(itemRule.getActualDataNodes().contains(new DataNode("ds_1", "t_order_item_1")));
151
        assertThat(itemRule.getTableShardingStrategy(), instanceOf(StandardShardingStrategy.class));
152
        assertThat(itemRule.getTableShardingStrategy().getShardingColumns().iterator().next(), is("order_id"));
153 154
        assertTrue(itemRule.getGenerateKeyColumn().isPresent());
        assertThat(itemRule.getGenerateKeyColumn().get(), is("order_item_id"));
155
        assertThat(itemRule.getTableShardingStrategy(), instanceOf(StandardShardingStrategy.class));
156
        assertThat(itemRule.getTableShardingStrategy().getShardingColumns().iterator().next(), is("order_id"));
157 158
        assertTrue(orderRule.getGenerateKeyColumn().isPresent());
        assertThat(orderRule.getGenerateKeyColumn().get(), is("order_id"));
159 160 161 162 163
        
    }
    
    @Test
    public void assertWithBroadcastTables() {
164
        ShardingSphereDataSource shardingSphereDataSource = getFieldValue("dataSource", OrchestrationShardingSphereDataSource.class, dataSource);
J
Juan Pan(Trista) 已提交
165 166
        SchemaContexts schemaContexts = shardingSphereDataSource.getSchemaContexts();
        ShardingRule shardingRule = (ShardingRule) schemaContexts.getDefaultSchemaContext().getSchema().getRules().iterator().next();
167 168 169 170 171
        assertThat(shardingRule.getBroadcastTables().size(), is(1));
        assertThat(shardingRule.getBroadcastTables().iterator().next(), is("t_config"));
    }
    
    @SuppressWarnings("unchecked")
172
    @SneakyThrows(ReflectiveOperationException.class)
173 174 175 176 177
    private <T> T getFieldValue(final String fieldName, final Class<?> fieldClass, final Object target) {
        Field field = fieldClass.getDeclaredField(fieldName);
        field.setAccessible(true);
        return (T) field.get(target);
    }
178 179 180 181 182
    
    @SneakyThrows
    private static String readYAML(final String yamlFile) {
        return Files.readAllLines(Paths.get(ClassLoader.getSystemResource(yamlFile).toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining());
    }
183
}