Skip to content

Commit

Permalink
Add test cases on CountResultRowBuilder's impl (#33472)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 30, 2024
1 parent 3fed8d5 commit 68755bd
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.broadcast.distsql.handler.query;

import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
import org.apache.shardingsphere.distsql.handler.executor.rql.rule.CountResultRowBuilder;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class BroadcastCountResultRowBuilderTest {

@SuppressWarnings("unchecked")
private final CountResultRowBuilder<BroadcastRule> builder = TypedSPILoader.getService(CountResultRowBuilder.class, "BROADCAST");

@Test
void assertGenerateRows() {
List<LocalDataQueryResultRow> actual = new ArrayList<>(builder.generateRows(mockRule(), "foo_db"));
assertThat(actual.size(), is(1));
assertThat(actual.get(0).getCell(1), is("broadcast_table"));
assertThat(actual.get(0).getCell(2), is("foo_db"));
assertThat(actual.get(0).getCell(3), is("1"));
}

private BroadcastRule mockRule() {
BroadcastRule result = mock(BroadcastRule.class, RETURNS_DEEP_STUBS);
when(result.getConfiguration().getTables().size()).thenReturn(1);
return result;
}

@Test
void assertGetRuleClass() {
assertThat(builder.getRuleClass(), is(BroadcastRule.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.encrypt.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.executor.rql.rule.CountResultRowBuilder;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class EncryptCountResultRowBuilderTest {

@SuppressWarnings("unchecked")
private final CountResultRowBuilder<EncryptRule> builder = TypedSPILoader.getService(CountResultRowBuilder.class, "ENCRYPT");

@Test
void assertGenerateRows() {
List<LocalDataQueryResultRow> actual = new ArrayList<>(builder.generateRows(mockRule(), "foo_db"));
assertThat(actual.size(), is(1));
assertThat(actual.get(0).getCell(1), is("encrypt"));
assertThat(actual.get(0).getCell(2), is("foo_db"));
assertThat(actual.get(0).getCell(3), is("1"));
}

private EncryptRule mockRule() {
TableMapperRuleAttribute tableMapperRuleAttribute = mock(TableMapperRuleAttribute.class, RETURNS_DEEP_STUBS);
when(tableMapperRuleAttribute.getLogicTableNames().size()).thenReturn(1);
EncryptRule result = mock(EncryptRule.class);
when(result.getAttributes()).thenReturn(new RuleAttributes(tableMapperRuleAttribute));
return result;
}

@Test
void assertGetRuleClass() {
assertThat(builder.getRuleClass(), is(EncryptRule.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.mask.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.executor.rql.rule.CountResultRowBuilder;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.mask.rule.MaskRule;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class MaskCountResultRowBuilderTest {

@SuppressWarnings("unchecked")
private final CountResultRowBuilder<MaskRule> builder = TypedSPILoader.getService(CountResultRowBuilder.class, "MASK");

@Test
void assertGenerateRows() {
List<LocalDataQueryResultRow> actual = new ArrayList<>(builder.generateRows(mockRule(), "foo_db"));
assertThat(actual.size(), is(1));
assertThat(actual.get(0).getCell(1), is("mask"));
assertThat(actual.get(0).getCell(2), is("foo_db"));
assertThat(actual.get(0).getCell(3), is("1"));
}

private MaskRule mockRule() {
TableMapperRuleAttribute tableMapperRuleAttribute = mock(TableMapperRuleAttribute.class, RETURNS_DEEP_STUBS);
when(tableMapperRuleAttribute.getLogicTableNames().size()).thenReturn(1);
MaskRule result = mock(MaskRule.class);
when(result.getAttributes()).thenReturn(new RuleAttributes(tableMapperRuleAttribute));
return result;
}

@Test
void assertGetRuleClass() {
assertThat(builder.getRuleClass(), is(MaskRule.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.readwritesplitting.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.executor.rql.rule.CountResultRowBuilder;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ReadwriteSplittingCountResultRowBuilderTest {

@SuppressWarnings("unchecked")
private final CountResultRowBuilder<ReadwriteSplittingRule> builder = TypedSPILoader.getService(CountResultRowBuilder.class, "READWRITE_SPLITTING");

@Test
void assertGenerateRows() {
List<LocalDataQueryResultRow> actual = new ArrayList<>(builder.generateRows(mockRule(), "foo_db"));
assertThat(actual.size(), is(1));
assertThat(actual.get(0).getCell(1), is("readwrite_splitting"));
assertThat(actual.get(0).getCell(2), is("foo_db"));
assertThat(actual.get(0).getCell(3), is("1"));
}

private ReadwriteSplittingRule mockRule() {
DataSourceMapperRuleAttribute dataSourceMapperRuleAttribute = mock(DataSourceMapperRuleAttribute.class, RETURNS_DEEP_STUBS);
when(dataSourceMapperRuleAttribute.getDataSourceMapper().size()).thenReturn(1);
ReadwriteSplittingRule result = mock(ReadwriteSplittingRule.class);
when(result.getAttributes()).thenReturn(new RuleAttributes(dataSourceMapperRuleAttribute));
return result;
}

@Test
void assertGetRuleClass() {
assertThat(builder.getRuleClass(), is(ReadwriteSplittingRule.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.shadow.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.executor.rql.rule.CountResultRowBuilder;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ShadowCountResultRowBuilderTest {

@SuppressWarnings("unchecked")
private final CountResultRowBuilder<ShadowRule> builder = TypedSPILoader.getService(CountResultRowBuilder.class, "SHADOW");

@Test
void assertGenerateRows() {
List<LocalDataQueryResultRow> actual = new ArrayList<>(builder.generateRows(mockRule(), "foo_db"));
assertThat(actual.size(), is(1));
assertThat(actual.get(0).getCell(1), is("shadow"));
assertThat(actual.get(0).getCell(2), is("foo_db"));
assertThat(actual.get(0).getCell(3), is("1"));
}

private ShadowRule mockRule() {
DataSourceMapperRuleAttribute dataSourceMapperRuleAttribute = mock(DataSourceMapperRuleAttribute.class, RETURNS_DEEP_STUBS);
when(dataSourceMapperRuleAttribute.getDataSourceMapper().size()).thenReturn(1);
ShadowRule result = mock(ShadowRule.class);
when(result.getAttributes()).thenReturn(new RuleAttributes(dataSourceMapperRuleAttribute));
return result;
}

@Test
void assertGetRuleClass() {
assertThat(builder.getRuleClass(), is(ShadowRule.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.sharding.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.executor.rql.rule.CountResultRowBuilder;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ShardingCountResultRowBuilderTest {

@SuppressWarnings("unchecked")
private final CountResultRowBuilder<ShardingRule> builder = TypedSPILoader.getService(CountResultRowBuilder.class, "SHARDING");

@Test
void assertGenerateRows() {
List<LocalDataQueryResultRow> actual = new ArrayList<>(builder.generateRows(mockRule(), "foo_db"));
assertThat(actual.size(), is(2));
assertThat(actual.get(0).getCell(1), is("sharding_table"));
assertThat(actual.get(0).getCell(2), is("foo_db"));
assertThat(actual.get(0).getCell(3), is("1"));
assertThat(actual.get(1).getCell(1), is("sharding_table_reference"));
assertThat(actual.get(1).getCell(2), is("foo_db"));
assertThat(actual.get(1).getCell(3), is("1"));
}

private ShardingRule mockRule() {
ShardingRule result = mock(ShardingRule.class, RETURNS_DEEP_STUBS);
when(result.getShardingTables().size()).thenReturn(1);
when(result.getConfiguration().getBindingTableGroups().size()).thenReturn(1);
return result;
}

@Test
void assertGetRuleClass() {
assertThat(builder.getRuleClass(), is(ShardingRule.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public final class SingleCountResultRowBuilder implements CountResultRowBuilder<

@Override
public Collection<LocalDataQueryResultRow> generateRows(final SingleRule rule, final String databaseName) {
return Collections.singleton(
new LocalDataQueryResultRow("single", databaseName, rule.getAttributes().getAttribute(TableMapperRuleAttribute.class).getLogicTableNames().size()));
return Collections.singleton(new LocalDataQueryResultRow("single", databaseName, rule.getAttributes().getAttribute(TableMapperRuleAttribute.class).getLogicTableNames().size()));
}

@Override
Expand Down
Loading

0 comments on commit 68755bd

Please sign in to comment.