Skip to content

Commit

Permalink
Refactor ShowTransactionRuleExecutorTest (#33129)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 5, 2024
1 parent fd3ef3f commit 0c5f63a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 46 deletions.
2 changes: 1 addition & 1 deletion kernel/transaction/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 @@ -22,6 +22,7 @@
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
import org.apache.shardingsphere.transaction.distsql.statement.queryable.ShowTransactionRuleStatement;
import org.apache.shardingsphere.transaction.rule.TransactionRule;

Expand All @@ -44,7 +45,8 @@ public Collection<String> getColumnNames(final ShowTransactionRuleStatement sqlS

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShowTransactionRuleStatement sqlStatement, final ContextManager contextManager) {
return Collections.singleton(new LocalDataQueryResultRow(rule.getDefaultType().name(), rule.getProviderType(), rule.getProps()));
TransactionRuleConfiguration ruleConfig = rule.getConfiguration();
return Collections.singleton(new LocalDataQueryResultRow(ruleConfig.getDefaultType(), ruleConfig.getProviderType(), ruleConfig.getProps()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,66 +17,47 @@

package org.apache.shardingsphere.transaction.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.test.it.distsql.handler.engine.query.DistSQLGlobalRuleQueryExecutorTest;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
import org.apache.shardingsphere.transaction.distsql.statement.queryable.ShowTransactionRuleStatement;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.junit.jupiter.api.Test;
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.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ShowTransactionRuleExecutorTest {
class ShowTransactionRuleExecutorTest extends DistSQLGlobalRuleQueryExecutorTest {

@Test
void assertExecuteQueryWithXA() throws SQLException {
TransactionRule rule = new TransactionRule(new TransactionRuleConfiguration(TransactionType.XA.name(),
"Atomikos", PropertiesBuilder.build(new Property("host", "127.0.0.1"), new Property("databaseName", "jbossts"))), Collections.emptyMap());
ContextManager contextManager = mockContextManager(rule);
DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(new ShowTransactionRuleStatement(), null, contextManager, mock(DistSQLConnectionContext.class));
engine.executeQuery();
List<LocalDataQueryResultRow> actual = new ArrayList<>(engine.getRows());
assertThat(actual.size(), is(1));
assertThat(actual.get(0).getCell(1), is(TransactionType.XA.name()));
assertThat(actual.get(0).getCell(2), is("Atomikos"));
String props = (String) actual.get(0).getCell(3);
assertTrue(props.contains("\"databaseName\":\"jbossts\""));
assertTrue(props.contains("\"host\":\"127.0.0.1\""));
ShowTransactionRuleExecutorTest() {
super(mock(TransactionRule.class));
}

@Test
void assertExecuteQueryWithLocal() throws SQLException {
TransactionRule rule = new TransactionRule(new TransactionRuleConfiguration(TransactionType.LOCAL.name(), null, new Properties()), Collections.emptyMap());
ContextManager contextManager = mockContextManager(rule);
DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(new ShowTransactionRuleStatement(), null, contextManager, mock(DistSQLConnectionContext.class));
engine.executeQuery();
List<LocalDataQueryResultRow> actual = new ArrayList<>(engine.getRows());
assertThat(actual.size(), is(1));
assertThat(actual.get(0).getCell(1), is(TransactionType.LOCAL.name()));
assertThat(actual.get(0).getCell(2), is(""));
assertThat(actual.get(0).getCell(3), is(""));
@ParameterizedTest(name = "{0}")
@ArgumentsSource(TestCaseArgumentsProvider.class)
void assertExecuteQuery(final String name, final GlobalRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, final Collection<LocalDataQueryResultRow> expected) throws SQLException {
assertQueryResultRows(ruleConfig, sqlStatement, expected);
}

private ContextManager mockContextManager(final TransactionRule rule) {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
return result;
private static class TestCaseArgumentsProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
return Stream.of(
Arguments.arguments("normal", new TransactionRuleConfiguration("XA", "Atomikos", PropertiesBuilder.build(new Property("k", "v"))), new ShowTransactionRuleStatement(),
Collections.singleton(new LocalDataQueryResultRow("XA", "Atomikos", "{\"k\":\"v\"}"))));
}
}
}

0 comments on commit 0c5f63a

Please sign in to comment.