Skip to content

Commit

Permalink
Finish add test cases for RuleSchemaMetaDataLoader (apache#6765)
Browse files Browse the repository at this point in the history
* Add RuleSchemaMetaDataLoaderTest.assertAsyncLoadFullDatabase

* Add RuleSchemaMetaDataLoaderTest.load by table name
  • Loading branch information
terrymanu authored Aug 10, 2020
1 parent 64d2481 commit 3b05715
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.
*/

package org.apache.shardingsphere.infra.metadata.fixture.decorator;

import org.apache.shardingsphere.infra.metadata.fixture.rule.DataNodeRoutedFixtureRule;
import org.apache.shardingsphere.infra.metadata.schema.spi.RuleMetaDataDecorator;
import org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaData;
import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;

import java.util.Collections;

public final class DataNodeRoutedFixtureRuleMetaDataDecorator implements RuleMetaDataDecorator<DataNodeRoutedFixtureRule> {

@Override
public TableMetaData decorate(final String tableName, final TableMetaData tableMetaData, final DataNodeRoutedFixtureRule rule) {
ColumnMetaData columnMetaData = new ColumnMetaData("id", 1, "INT", true, true, false);
return new TableMetaData(Collections.singletonList(columnMetaData), Collections.emptyList());
}

@Override
public int getOrder() {
return 1;
}

@Override
public Class<DataNodeRoutedFixtureRule> getTypeClass() {
return DataNodeRoutedFixtureRule.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public SchemaMetaData load(final DatabaseType databaseType, final Map<String, Da
@Override
public Optional<TableMetaData> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final DataNodes dataNodes, final String tableName, final DataNodeRoutedFixtureRule rule, final ConfigurationProperties props) {
return Optional.empty();
return ("data_node_routed_table_0".equals(tableName) || "data_node_routed_table_1".equals(tableName))
? Optional.of(new TableMetaData(Collections.emptyList(), Collections.emptyList())) : Optional.empty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.infra.metadata.schema;

import com.google.common.util.concurrent.MoreExecutors;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
Expand All @@ -33,8 +34,10 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.concurrent.Executors;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -53,6 +56,8 @@ public final class RuleSchemaMetaDataLoaderTest {
@Mock
private ConfigurationProperties props;

private final RuleSchemaMetaDataLoader loader = new RuleSchemaMetaDataLoader(Arrays.asList(new CommonFixtureRule(), new DataNodeRoutedFixtureRule()));

@Before
public void setUp() throws SQLException {
ResultSet resultSet = mockResultSet();
Expand All @@ -69,22 +74,35 @@ private ResultSet mockResultSet() throws SQLException {

@Test
public void assertSyncLoadFullDatabase() throws SQLException {
RuleSchemaMetaDataLoader loader = new RuleSchemaMetaDataLoader(Arrays.asList(new CommonFixtureRule(), new DataNodeRoutedFixtureRule()));
RuleSchemaMetaData actual = loader.load(databaseType, dataSource, props, null);
assertRuleSchemaMetaData(loader.load(databaseType, dataSource, props, null));
}

@Test
public void assertAsyncLoadFullDatabase() throws SQLException {
assertRuleSchemaMetaData(loader.load(databaseType, dataSource, props, MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1))));
}

private void assertRuleSchemaMetaData(final RuleSchemaMetaData actual) {
assertThat(actual.getConfiguredSchemaMetaData().getAllTableNames().size(), is(4));
assertTrue(actual.getConfiguredSchemaMetaData().containsTable("common_table_0"));
assertTrue(actual.getConfiguredSchemaMetaData().containsTable("common_table_1"));
assertTrue(actual.getConfiguredSchemaMetaData().containsTable("data_node_routed_table_0"));
assertTrue(actual.getConfiguredSchemaMetaData().get("data_node_routed_table_0").getColumns().containsKey("id"));
assertTrue(actual.getConfiguredSchemaMetaData().containsTable("data_node_routed_table_1"));
assertTrue(actual.getConfiguredSchemaMetaData().get("data_node_routed_table_1").getColumns().containsKey("id"));
assertThat(actual.getUnconfiguredSchemaMetaDataMap().size(), is(1));
assertTrue(actual.getUnconfiguredSchemaMetaDataMap().containsKey("logic_db"));
assertTrue(actual.getUnconfiguredSchemaMetaDataMap().get("logic_db").containsTable("unconfigured_table_0"));
assertTrue(actual.getUnconfiguredSchemaMetaDataMap().get("logic_db").containsTable("unconfigured_table_1"));
}

// @Test
// @Ignore
// public void assertLoadWithTableName() {
//
// }
@Test
public void assertLoadWithExistedTableName() throws SQLException {
assertTrue(loader.load(databaseType, dataSource, "data_node_routed_table_0", props).isPresent());
}

@Test
public void assertLoadWithNotExistedTableName() throws SQLException {
assertFalse(loader.load(databaseType, dataSource, "invalid_table", props).isPresent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.shardingsphere.infra.metadata.fixture.decorator.DataNodeRoutedFixtureRuleMetaDataDecorator

0 comments on commit 3b05715

Please sign in to comment.