MixSQLRewriterParameterizedTest.java 8.2 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.sharding.rewrite.parameterized;
19 20

import com.google.common.base.Preconditions;
21 22 23
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
24
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
L
Liang Zhang 已提交
25 26
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
27
import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
28 29
import org.apache.shardingsphere.infra.metadata.schema.model.physical.PhysicalColumnMetaData;
import org.apache.shardingsphere.infra.metadata.schema.model.physical.PhysicalIndexMetaData;
30
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
31
import org.apache.shardingsphere.infra.metadata.schema.model.physical.PhysicalTableMetaData;
32
import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine;
33 34 35 36 37 38 39 40 41
import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry;
import org.apache.shardingsphere.infra.rewrite.engine.result.GenericSQLRewriteResult;
import org.apache.shardingsphere.infra.rewrite.engine.result.RouteSQLRewriteResult;
import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteResult;
import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteUnit;
import org.apache.shardingsphere.infra.rewrite.parameterized.engine.AbstractSQLRewriterParameterizedTest;
import org.apache.shardingsphere.infra.rewrite.parameterized.engine.parameter.SQLRewriteEngineTestParameters;
import org.apache.shardingsphere.infra.rewrite.parameterized.engine.parameter.SQLRewriteEngineTestParametersBuilder;
import org.apache.shardingsphere.infra.route.context.RouteContext;
42
import org.apache.shardingsphere.infra.route.engine.SQLRouteEngine;
43
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
L
Liang Zhang 已提交
44
import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRulesBuilder;
45 46 47
import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
48 49 50 51 52
import org.junit.runners.Parameterized.Parameters;

import java.io.File;
import java.io.IOException;
import java.net.URL;
53
import java.sql.Types;
54 55
import java.util.Arrays;
import java.util.Collection;
56
import java.util.Collections;
L
Liang Zhang 已提交
57
import java.util.HashMap;
58
import java.util.LinkedHashMap;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
import java.util.Map;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public final class MixSQLRewriterParameterizedTest extends AbstractSQLRewriterParameterizedTest {
    
    private static final String PATH = "mix";
    
    public MixSQLRewriterParameterizedTest(final String type, final String name, final String fileName, final SQLRewriteEngineTestParameters testParameters) {
        super(testParameters);
    }
    
    @Parameters(name = "{0}: {1} -> {2}")
    public static Collection<Object[]> loadTestParameters() {
        return SQLRewriteEngineTestParametersBuilder.loadTestParameters(PATH.toUpperCase(), PATH, MixSQLRewriterParameterizedTest.class);
    }
    
    @Override
78
    protected Collection<SQLRewriteUnit> createSQLRewriteUnits() throws IOException {
79 80 81
        YamlRootRuleConfigurations ruleConfigurations = createRuleConfigurations();
        Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(
                new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(ruleConfigurations.getRules()), ruleConfigurations.getDataSources().keySet());
82
        SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType());
83
        ShardingSphereSchema schema = mockSchema();
L
Liang Zhang 已提交
84
        ConfigurationProperties props = new ConfigurationProperties(ruleConfigurations.getProps());
85 86
        SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(
                schema, getTestParameters().getInputParameters(), sqlStatementParserEngine.parse(getTestParameters().getInputSQL(), false));
L
Liang Zhang 已提交
87
        LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
88
        ShardingSphereMetaData metaData = new ShardingSphereMetaData("sharding_db", mock(ShardingSphereResource.class), new ShardingSphereRuleMetaData(Collections.emptyList(), rules), schema);
89
        RouteContext routeContext = new SQLRouteEngine(rules, props).route(logicSQL, metaData);
90
        SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(
91
                schema, props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext);
92 93
        return sqlRewriteResult instanceof GenericSQLRewriteResult
                ? Collections.singletonList(((GenericSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnit()) : (((RouteSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnits()).values();
94 95
    }
    
96
    private YamlRootRuleConfigurations createRuleConfigurations() throws IOException {
97
        URL url = MixSQLRewriterParameterizedTest.class.getClassLoader().getResource(getTestParameters().getRuleFile());
98
        Preconditions.checkNotNull(url, "Cannot found rewrite rule yaml configurations.");
99
        return YamlEngine.unmarshal(new File(url.getFile()), YamlRootRuleConfigurations.class);
100 101
    }
    
102 103
    private ShardingSphereSchema mockSchema() {
        ShardingSphereSchema result = mock(ShardingSphereSchema.class);
104
        when(result.getAllTableNames()).thenReturn(Arrays.asList("t_account", "t_account_bak", "t_account_detail"));
105
        PhysicalTableMetaData accountTableMetaData = mock(PhysicalTableMetaData.class);
106
        when(accountTableMetaData.getColumns()).thenReturn(createColumnMetaDataMap());
107 108
        Map<String, PhysicalIndexMetaData> indexMetaDataMap = new HashMap<>(1, 1);
        indexMetaDataMap.put("index_name", new PhysicalIndexMetaData("index_name"));
L
Liang Zhang 已提交
109
        when(accountTableMetaData.getIndexes()).thenReturn(indexMetaDataMap);
110 111
        when(result.containsTable("t_account")).thenReturn(true);
        when(result.get("t_account")).thenReturn(accountTableMetaData);
112
        PhysicalTableMetaData accountBakTableMetaData = mock(PhysicalTableMetaData.class);
113
        when(accountBakTableMetaData.getColumns()).thenReturn(createColumnMetaDataMap());
114 115 116 117 118 119
        when(result.containsTable("t_account_bak")).thenReturn(true);
        when(result.get("t_account_bak")).thenReturn(accountBakTableMetaData);
        when(result.get("t_account_detail")).thenReturn(mock(PhysicalTableMetaData.class));
        when(result.getAllColumnNames("t_account")).thenReturn(Arrays.asList("account_id", "password", "amount", "status"));
        when(result.getAllColumnNames("t_account_bak")).thenReturn(Arrays.asList("account_id", "password", "amount", "status"));
        return result;
120 121
    }
    
122
    private Map<String, PhysicalColumnMetaData> createColumnMetaDataMap() {
L
Liang Zhang 已提交
123
        Map<String, PhysicalColumnMetaData> result = new LinkedHashMap<>(4, 1);
124 125 126 127
        result.put("account_id", new PhysicalColumnMetaData("account_id", Types.INTEGER, "INT", true, true, false));
        result.put("password", mock(PhysicalColumnMetaData.class));
        result.put("amount", mock(PhysicalColumnMetaData.class));
        result.put("status", mock(PhysicalColumnMetaData.class));
128 129 130
        return result;
    }
}