ShardingCreateFunctionStatementValidatorTest.java 7.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.route.engine.validator.ddl;
19

20 21
import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
22
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
23
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
24
import org.apache.shardingsphere.sharding.route.engine.exception.NoSuchTableException;
25
import org.apache.shardingsphere.sharding.route.engine.exception.TableExistsException;
26
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateFunctionStatementValidator;
27 28 29 30 31 32 33 34 35 36 37 38 39 40
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.routine.RoutineBodySegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.routine.ValidStatementSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateFunctionStatement;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateFunctionStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
import org.junit.Test;
import org.mockito.Mock;

import java.util.Collections;

41
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
42 43 44 45 46 47 48 49 50 51 52 53 54 55
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public final class ShardingCreateFunctionStatementValidatorTest {
    
    @Mock
    private ShardingRule shardingRule;
    
    @Test
    public void assertValidateCreateFunctionForMySQL() {
        MySQLSelectStatement selectStatement = new MySQLSelectStatement();
        selectStatement.setFrom(new SimpleTableSegment(0, 0, new IdentifierValue("t_order_item")));
        MySQLCreateTableStatement createTableStatement = new MySQLCreateTableStatement();
        createTableStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("t_order")));
56 57
        ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
        validStatementSegment.setSqlStatement(createTableStatement);
58 59 60
        ValidStatementSegment selectValidStatementSegment = new ValidStatementSegment(0, 0);
        selectValidStatementSegment.setSqlStatement(selectStatement);
        RoutineBodySegment routineBody = new RoutineBodySegment(0, 0);
61
        routineBody.getValidStatements().add(validStatementSegment);
62 63 64 65
        routineBody.getValidStatements().add(selectValidStatementSegment);
        MySQLCreateFunctionStatement sqlStatement = new MySQLCreateFunctionStatement();
        sqlStatement.setRoutineBody(routineBody);
        SQLStatementContext<CreateFunctionStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
66
        ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
67
        when(schema.containsTable("t_order_item")).thenReturn(true);
68
        new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), schema);
69 70 71 72 73 74 75 76 77 78 79 80 81
    }
    
    @Test(expected = ShardingSphereException.class)
    public void assertValidateCreateFunctionWithShardingTableForMySQL() {
        MySQLSelectStatement selectStatement = new MySQLSelectStatement();
        selectStatement.setFrom(new SimpleTableSegment(0, 0, new IdentifierValue("t_order")));
        ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
        validStatementSegment.setSqlStatement(selectStatement);
        RoutineBodySegment routineBody = new RoutineBodySegment(0, 0);
        routineBody.getValidStatements().add(validStatementSegment);
        MySQLCreateFunctionStatement sqlStatement = new MySQLCreateFunctionStatement();
        sqlStatement.setRoutineBody(routineBody);
        SQLStatementContext<CreateFunctionStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
82
        new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereSchema.class, RETURNS_DEEP_STUBS));
83 84
    }
    
85 86 87 88 89 90 91 92 93 94 95
    @Test(expected = NoSuchTableException.class)
    public void assertValidateCreateFunctionWithNoSuchTableForMySQL() {
        MySQLSelectStatement selectStatement = new MySQLSelectStatement();
        selectStatement.setFrom(new SimpleTableSegment(0, 0, new IdentifierValue("t_order")));
        ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
        validStatementSegment.setSqlStatement(selectStatement);
        RoutineBodySegment routineBody = new RoutineBodySegment(0, 0);
        routineBody.getValidStatements().add(validStatementSegment);
        MySQLCreateFunctionStatement sqlStatement = new MySQLCreateFunctionStatement();
        sqlStatement.setRoutineBody(routineBody);
        SQLStatementContext<CreateFunctionStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
96
        new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereSchema.class, RETURNS_DEEP_STUBS));
97 98
    }
    
99 100 101 102 103 104 105 106 107 108 109
    @Test(expected = TableExistsException.class)
    public void assertValidateCreateFunctionWithTableExistsForMySQL() {
        MySQLCreateTableStatement createTableStatement = new MySQLCreateTableStatement();
        createTableStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("t_order")));
        ValidStatementSegment validStatementSegment = new ValidStatementSegment(0, 0);
        validStatementSegment.setSqlStatement(createTableStatement);
        RoutineBodySegment routineBody = new RoutineBodySegment(0, 0);
        routineBody.getValidStatements().add(validStatementSegment);
        MySQLCreateFunctionStatement sqlStatement = new MySQLCreateFunctionStatement();
        sqlStatement.setRoutineBody(routineBody);
        SQLStatementContext<CreateFunctionStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
110
        ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
111
        when(schema.containsTable("t_order")).thenReturn(true);
112
        new ShardingCreateFunctionStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), schema);
113 114
    }
}