|
| 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.infra.binder.engine.statement.dcl; |
| 19 | + |
| 20 | +import org.apache.shardingsphere.database.connector.core.type.DatabaseType; |
| 21 | +import org.apache.shardingsphere.infra.binder.engine.statement.SQLStatementBinderContext; |
| 22 | +import org.apache.shardingsphere.infra.hint.HintValueContext; |
| 23 | +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; |
| 24 | +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; |
| 25 | +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; |
| 26 | +import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; |
| 27 | +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; |
| 28 | +import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment; |
| 29 | +import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment; |
| 30 | +import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dcl.GrantStatement; |
| 31 | +import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 36 | + |
| 37 | +import java.util.Collection; |
| 38 | +import java.util.Collections; |
| 39 | + |
| 40 | +import static org.hamcrest.CoreMatchers.is; |
| 41 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 42 | +import static org.mockito.Mockito.when; |
| 43 | + |
| 44 | +@ExtendWith(MockitoExtension.class) |
| 45 | +class GrantStatementBinderTest { |
| 46 | + |
| 47 | + private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE"); |
| 48 | + |
| 49 | + @Mock |
| 50 | + private ShardingSphereMetaData metaData; |
| 51 | + |
| 52 | + @Mock |
| 53 | + private ShardingSphereDatabase database; |
| 54 | + |
| 55 | + @Mock |
| 56 | + private ShardingSphereSchema schema; |
| 57 | + |
| 58 | + @Mock |
| 59 | + private ShardingSphereTable table; |
| 60 | + |
| 61 | + @Test |
| 62 | + void assertBind() { |
| 63 | + when(metaData.containsDatabase("foo_db")).thenReturn(true); |
| 64 | + when(metaData.getDatabase("foo_db")).thenReturn(database); |
| 65 | + when(database.containsSchema("foo_db")).thenReturn(true); |
| 66 | + when(database.getSchema("foo_db")).thenReturn(schema); |
| 67 | + when(schema.containsTable("test_table")).thenReturn(true); |
| 68 | + when(schema.getTable("test_table")).thenReturn(table); |
| 69 | + when(table.getAllColumns()).thenReturn(Collections.emptyList()); |
| 70 | + HintValueContext hintValueContext = new HintValueContext(); |
| 71 | + hintValueContext.setSkipMetadataValidate(true); |
| 72 | + SimpleTableSegment tableSegment = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("test_table"))); |
| 73 | + GrantStatement original = new GrantStatement(databaseType); |
| 74 | + original.getTables().add(tableSegment); |
| 75 | + SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, "foo_db", hintValueContext, original); |
| 76 | + GrantStatement actual = new GrantStatementBinder().bind(original, binderContext); |
| 77 | + Collection<SimpleTableSegment> actualTables = actual.getTables(); |
| 78 | + assertThat(actualTables.size(), is(1)); |
| 79 | + assertThat(actualTables.iterator().next().getTableName().getIdentifier().getValue(), is("test_table")); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void assertBindWithEmptyTables() { |
| 84 | + GrantStatement original = new GrantStatement(databaseType); |
| 85 | + HintValueContext hintValueContext = new HintValueContext(); |
| 86 | + hintValueContext.setSkipMetadataValidate(true); |
| 87 | + SQLStatementBinderContext binderContext = new SQLStatementBinderContext(metaData, "foo_db", hintValueContext, original); |
| 88 | + GrantStatement actual = new GrantStatementBinder().bind(original, binderContext); |
| 89 | + Collection<SimpleTableSegment> actualTables = actual.getTables(); |
| 90 | + assertThat(actualTables.size(), is(0)); |
| 91 | + } |
| 92 | +} |
0 commit comments