Skip to content

Commit

Permalink
Refactor ShowSQLTranslatorRuleExecutorTest (#33122)
Browse files Browse the repository at this point in the history
* Refactor ShowSQLTranslatorRuleExecutorTest

* Refactor ShowSQLTranslatorRuleExecutorTest
  • Loading branch information
terrymanu authored Oct 4, 2024
1 parent bd6943d commit 980228b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 63 deletions.
6 changes: 0 additions & 6 deletions kernel/sql-parser/distsql/handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-it-distsql</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.parser.distsql.handler.query;

import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration;
import org.apache.shardingsphere.parser.distsql.statement.queryable.ShowSQLParserRuleStatement;
Expand All @@ -31,45 +32,34 @@
import org.junit.jupiter.params.provider.ArgumentsSource;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

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

class ShowSQLParserRuleExecutorTest extends DistSQLGlobalRuleQueryExecutorTest<SQLParserRule> {
class ShowSQLParserRuleExecutorTest extends DistSQLGlobalRuleQueryExecutorTest {

ShowSQLParserRuleExecutorTest() {
super(SQLParserRule.class);
super(mock(SQLParserRule.class));
}

@ParameterizedTest(name = "{0}")
@ArgumentsSource(TestCaseArgumentsProvider.class)
void assertExecuteQuery(final String name, final SQLParserRule rule, final DistSQLStatement sqlStatement, final List<LocalDataQueryResultRow> expectedRows) throws SQLException {
assertQueryResultRows(rule, sqlStatement, expectedRows);
void assertExecuteQuery(final String name,
final GlobalRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, final Collection<LocalDataQueryResultRow> expectedRows) throws SQLException {
assertQueryResultRows(ruleConfig, sqlStatement, expectedRows);
}

private static class TestCaseArgumentsProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
return Stream.of(
Arguments.arguments("withCacheOption", mockRuleWithCacheOption(), new ShowSQLParserRuleStatement(),
Collections.singletonList(new LocalDataQueryResultRow("initialCapacity: 128, maximumSize: 1024", "initialCapacity: 2000, maximumSize: 65535"))),
Arguments.arguments("withoutCacheOption", mockRuleWithoutCacheOption(), new ShowSQLParserRuleStatement(), Collections.singletonList(new LocalDataQueryResultRow("", ""))));
}

private SQLParserRule mockRuleWithCacheOption() {
SQLParserRule result = mock(SQLParserRule.class);
when(result.getConfiguration()).thenReturn(new SQLParserRuleConfiguration(new CacheOption(128, 1024L), new CacheOption(2000, 65535L)));
return result;
}

private SQLParserRule mockRuleWithoutCacheOption() {
SQLParserRule result = mock(SQLParserRule.class);
when(result.getConfiguration()).thenReturn(new SQLParserRuleConfiguration(null, null));
return result;
Arguments.arguments("withCacheOption", new SQLParserRuleConfiguration(new CacheOption(128, 1024L), new CacheOption(2000, 65535L)), new ShowSQLParserRuleStatement(),
Collections.singleton(new LocalDataQueryResultRow("initialCapacity: 128, maximumSize: 1024", "initialCapacity: 2000, maximumSize: 65535"))),
Arguments.arguments("withoutCacheOption", new SQLParserRuleConfiguration(null, null), new ShowSQLParserRuleStatement(),
Collections.singleton(new LocalDataQueryResultRow("", ""))));
}
}
}
2 changes: 1 addition & 1 deletion kernel/sql-translator/distsql/handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
<artifactId>shardingsphere-test-it-distsql</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,46 @@

package org.apache.shardingsphere.sqltranslator.distsql.handler.query;

import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext;
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.sqltranslator.config.SQLTranslatorRuleConfiguration;
import org.apache.shardingsphere.sqltranslator.distsql.statement.queryable.ShowSQLTranslatorRuleStatement;
import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule;
import org.junit.jupiter.api.Test;
import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLGlobalRuleQueryExecutorTest;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.stream.Stream;

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 ShowSQLTranslatorRuleExecutorTest {
class ShowSQLTranslatorRuleExecutorTest extends DistSQLGlobalRuleQueryExecutorTest {

@Test
void assertExecute() throws SQLException {
DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(new ShowSQLTranslatorRuleStatement(), null, mockContextManager(), mock(DistSQLConnectionContext.class));
engine.executeQuery();
List<LocalDataQueryResultRow> actual = new ArrayList<>(engine.getRows());
assertThat(actual.size(), is(1));
assertThat(actual.get(0).getCell(1), is("NATIVE"));
assertThat(actual.get(0).getCell(2), is(""));
assertThat(actual.get(0).getCell(3), is("true"));
ShowSQLTranslatorRuleExecutorTest() {
super(mock(SQLTranslatorRule.class));
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
SQLTranslatorRule rule = mock(SQLTranslatorRule.class);
when(rule.getConfiguration()).thenReturn(new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true));
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
return result;
@ParameterizedTest(name = "{0}")
@ArgumentsSource(TestCaseArgumentsProvider.class)
void assertExecuteQuery(final String name,
final GlobalRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, final Collection<LocalDataQueryResultRow> expectedRows) throws SQLException {
assertQueryResultRows(ruleConfig, sqlStatement, expectedRows);
}

private static class TestCaseArgumentsProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
return Stream.of(Arguments.arguments("normal", new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true), new ShowSQLTranslatorRuleStatement(),
Collections.singleton(new LocalDataQueryResultRow("NATIVE", "", "true"))));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext;
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
Expand All @@ -30,7 +31,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -39,20 +39,21 @@
import static org.mockito.Mockito.when;

@RequiredArgsConstructor
public abstract class DistSQLGlobalRuleQueryExecutorTest<T extends GlobalRule> {
public abstract class DistSQLGlobalRuleQueryExecutorTest {

private final Class<T> ruleType;
private final GlobalRule mockedRule;

protected void assertQueryResultRows(final T rule, final DistSQLStatement sqlStatement, final List<LocalDataQueryResultRow> expectedRows) throws SQLException {
DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(sqlStatement, null, mockContextManager(rule), mock(DistSQLConnectionContext.class));
protected void assertQueryResultRows(final GlobalRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, final Collection<LocalDataQueryResultRow> expectedRows) throws SQLException {
DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(sqlStatement, null, mockContextManager(ruleConfig), mock(DistSQLConnectionContext.class));
engine.executeQuery();
Collection<LocalDataQueryResultRow> actualRows = new ArrayList<>(engine.getRows());
assertThat(actualRows, deepEqual(expectedRows));
assertThat(actualRows, deepEqual(new ArrayList<>(expectedRows)));
}

private ContextManager mockContextManager(final T rule) {
private ContextManager mockContextManager(final GlobalRuleConfiguration ruleConfig) {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
when(mockedRule.getConfiguration()).thenReturn(ruleConfig);
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(mockedRule)));
return result;
}
}

0 comments on commit 980228b

Please sign in to comment.