Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RDLE2EIT #28672

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions test/e2e/sql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,5 @@
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ private Stream<Arguments> getBatchTestCaseArguments(final SQLCommandType type) {
private Stream<Arguments> getSingleTestCaseArguments(final SQLCommandType type) {
Collection<AssertionTestParameter> result = E2ETestParameterFactory.getAssertionTestParameters(type);
// TODO make sure test case can not be null
return result.isEmpty() ? Stream.of(Arguments.of(new AssertionTestParameter(null, null, null, null, null, null, null, null, null))) : result.stream().map(Arguments::of);
return result.isEmpty() ? Stream.of(Arguments.of(new AssertionTestParameter(null, null, null, null, null, null, null, null))) : result.stream().map(Arguments::of);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.apache.shardingsphere.test.e2e.cases.value.SQLValue;
import org.apache.shardingsphere.test.e2e.framework.param.model.AssertionTestParameter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -43,10 +41,6 @@ public final class SingleE2EContainerComposer extends E2EContainerComposer {

private final IntegrationTestCaseAssertion assertion;

private final Collection<IntegrationTestCaseAssertion> assertions;

private final List<DataSet> dataSets = new ArrayList<>();

private final DataSet dataSet;

private final DataSet generatedKeyDataSet;
Expand All @@ -55,18 +49,7 @@ public SingleE2EContainerComposer(final AssertionTestParameter testParam) {
super(testParam);
sql = testParam.getTestCaseContext().getTestCase().getSql();
sqlExecuteType = testParam.getSqlExecuteType();
assertions = testParam.getAssertions();
assertion = testParam.getAssertion();

if ("RDL".equals(testParam.getSqlCommandType().name())) {
for (IntegrationTestCaseAssertion caseAssertion : assertions) {
dataSets.add(null == caseAssertion || null == caseAssertion.getExpectedDataFile()
? null
: DataSetLoader.load(testParam.getTestCaseContext().getParentPath(), testParam.getScenario(), testParam.getDatabaseType(), testParam.getMode(),
caseAssertion.getExpectedDataFile()));
}
}

dataSet = null == assertion || null == assertion.getExpectedDataFile()
? null
: DataSetLoader.load(testParam.getTestCaseContext().getParentPath(), testParam.getScenario(), testParam.getDatabaseType(), testParam.getMode(), assertion.getExpectedDataFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package org.apache.shardingsphere.test.e2e.engine.type;

import com.google.common.base.Splitter;
import org.apache.shardingsphere.test.e2e.cases.SQLCommandType;
import org.apache.shardingsphere.test.e2e.cases.assertion.IntegrationTestCaseAssertion;
import org.apache.shardingsphere.test.e2e.cases.dataset.metadata.DataSetColumn;
import org.apache.shardingsphere.test.e2e.cases.dataset.metadata.DataSetMetaData;
import org.apache.shardingsphere.test.e2e.cases.dataset.row.DataSetRow;
Expand All @@ -27,12 +27,13 @@
import org.apache.shardingsphere.test.e2e.engine.composer.SingleE2EContainerComposer;
import org.apache.shardingsphere.test.e2e.framework.param.array.E2ETestParameterFactory;
import org.apache.shardingsphere.test.e2e.framework.param.model.AssertionTestParameter;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.testcontainers.shaded.org.awaitility.Awaitility;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
Expand All @@ -44,6 +45,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@E2ETestCaseSettings(SQLCommandType.RDL)
Expand All @@ -58,108 +60,79 @@ void assertExecute(final AssertionTestParameter testParam) throws SQLException {
return;
}
SingleE2EContainerComposer containerComposer = new SingleE2EContainerComposer(testParam);

if (null == containerComposer.getAssertions()) {
return;
}
int dataSetIndex = 0;
String mode = testParam.getMode();

for (IntegrationTestCaseAssertion each : containerComposer.getAssertions()) {
if (null != each.getInitialSQL() && null != each.getAssertionSQL()) {
init(containerComposer, each, dataSetIndex, mode);
}
if (null != each.getAssertionSQL() && null == each.getDestroySQL() && null == each.getInitialSQL()) {
executeSQLCase(containerComposer, each, dataSetIndex, mode);
}
if (null != each.getDestroySQL() && null != each.getAssertionSQL()) {
tearDown(containerComposer, each, dataSetIndex, mode);
}
dataSetIndex++;
}
init(containerComposer);
assertExecute(testParam, containerComposer);
tearDown(containerComposer);
}

private void executeSQLCase(final SingleE2EContainerComposer containerComposer, final IntegrationTestCaseAssertion testCaseExecuteSql,
final int dataSetIndex, final String mode) throws SQLException {
private void assertExecute(final AssertionTestParameter testParam, final SingleE2EContainerComposer containerComposer) throws SQLException {
assertNotNull(testParam.getAssertion().getAssertionSQL(), "Assertion SQL is required");
try (Connection connection = containerComposer.getTargetDataSource().getConnection()) {
try (Statement statement = connection.createStatement()) {
statement.execute(containerComposer.getSQL());

executeAssertionSQL(containerComposer, statement, testCaseExecuteSql, dataSetIndex, mode);
executeSQLCase(containerComposer, statement);
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
assertResultSet(containerComposer, statement);
}
}
}

private void init(final SingleE2EContainerComposer containerComposer, final IntegrationTestCaseAssertion testCaseInitSql,
final int dataSetIndex, final String mode) throws SQLException {
try (Connection connection = containerComposer.getTargetDataSource().getConnection()) {
executeInitSQLs(containerComposer, connection, testCaseInitSql, dataSetIndex, mode);
}
private void executeSQLCase(final SingleE2EContainerComposer containerComposer, final Statement statement) throws SQLException {
statement.execute(containerComposer.getSQL());
}

private void tearDown(final SingleE2EContainerComposer containerComposer, final IntegrationTestCaseAssertion testCaseDestroySQL,
final int dataSetIndex, final String mode) throws SQLException {
private void init(final SingleE2EContainerComposer containerComposer) throws SQLException {
try (Connection connection = containerComposer.getTargetDataSource().getConnection()) {
executeDestroySQLs(containerComposer, connection, testCaseDestroySQL, dataSetIndex, mode);
executeInitSQLs(containerComposer, connection);
}
}

private void executeInitSQLs(final SingleE2EContainerComposer containerComposer, final Connection connection,
final IntegrationTestCaseAssertion testCaseInitSql, final int dataSetIndex, final String mode) throws SQLException {
if (null == testCaseInitSql.getInitialSQL() || null == testCaseInitSql.getInitialSQL().getSql()) {
return;
}
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(testCaseInitSql.getInitialSQL().getSql());

executeAssertionSQL(containerComposer, statement, testCaseInitSql, dataSetIndex, mode);
private void tearDown(final SingleE2EContainerComposer containerComposer) throws SQLException {
if (null != containerComposer.getAssertion().getDestroySQL()) {
try (Connection connection = containerComposer.getTargetDataSource().getConnection()) {
executeDestroySQLs(containerComposer, connection);
}
}
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
}

private void executeAssertionSQL(final SingleE2EContainerComposer containerComposer, final Statement statement,
final IntegrationTestCaseAssertion testCaseAssertionSQL,
final int dataSetIndex, final String mode) {
if ("Cluster".equals(mode)) {
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> assertResultSet(containerComposer, statement, testCaseAssertionSQL, dataSetIndex));
} else if ("Standalone".equals(mode)) {
assertResultSet(containerComposer, statement, testCaseAssertionSQL, dataSetIndex);
private void executeInitSQLs(final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
if (null == containerComposer.getAssertion().getInitialSQL() || null == containerComposer.getAssertion().getInitialSQL().getSql()) {
return;
}
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getInitialSQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
}
}
}

private void executeDestroySQLs(final SingleE2EContainerComposer containerComposer, final Connection connection,
final IntegrationTestCaseAssertion testCaseDestroySQL,
final int dataSetIndex, final String mode) throws SQLException {
if (null == testCaseDestroySQL.getDestroySQL() || null == testCaseDestroySQL.getDestroySQL().getSql()) {
private void executeDestroySQLs(final SingleE2EContainerComposer containerComposer, final Connection connection) throws SQLException {
if (null == containerComposer.getAssertion().getDestroySQL().getSql()) {
return;
}
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(testCaseDestroySQL.getDestroySQL().getSql());

executeAssertionSQL(containerComposer, statement, testCaseDestroySQL, dataSetIndex, mode);
for (String each : Splitter.on(";").trimResults().splitToList(containerComposer.getAssertion().getDestroySQL().getSql())) {
try (PreparedStatement preparedStatement = connection.prepareStatement(each)) {
preparedStatement.executeUpdate();
Awaitility.await().pollDelay(2L, TimeUnit.SECONDS).until(() -> true);
}
}
}

private boolean assertResultSet(final SingleE2EContainerComposer containerComposer, final Statement statement,
final IntegrationTestCaseAssertion testCaseAssertionSql,
final int dataSetIndex) {
try (ResultSet resultSet = statement.executeQuery(testCaseAssertionSql.getAssertionSQL().getSql())) {
assertResultSet(containerComposer, resultSet, dataSetIndex);
return true;
} catch (final SQLException ignored) {
return false;
private void assertResultSet(final SingleE2EContainerComposer containerComposer, final Statement statement) throws SQLException {
try (ResultSet resultSet = statement.executeQuery(containerComposer.getAssertion().getAssertionSQL().getSql())) {
assertResultSet(containerComposer, resultSet);
}
}

private void assertResultSet(final SingleE2EContainerComposer containerComposer, final ResultSet resultSet,
final int dataSetIndex) throws SQLException {
assertMetaData(resultSet.getMetaData(), getExpectedColumns(containerComposer, dataSetIndex));
assertRows(resultSet, containerComposer.getDataSets().get(dataSetIndex).getRows());
private void assertResultSet(final SingleE2EContainerComposer containerComposer, final ResultSet resultSet) throws SQLException {
assertMetaData(resultSet.getMetaData(), getExpectedColumns(containerComposer));
assertRows(resultSet, containerComposer.getDataSet().getRows());
}

private Collection<DataSetColumn> getExpectedColumns(final SingleE2EContainerComposer containerComposer, final int dataSetIndex) {
private Collection<DataSetColumn> getExpectedColumns(final SingleE2EContainerComposer containerComposer) {
Collection<DataSetColumn> result = new LinkedList<>();
List<DataSetMetaData> dataSetMetaDataList = containerComposer.getDataSets().get(dataSetIndex).getMetaDataList();
for (DataSetMetaData each : dataSetMetaDataList) {
for (DataSetMetaData each : containerComposer.getDataSet().getMetaDataList()) {
result.addAll(each.getColumns());
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,30 @@ private Collection<AssertionTestParameter> getAssertionTestParameter(final Integ
private Collection<AssertionTestParameter> getAssertionTestParameter(final IntegrationTestCaseContext testCaseContext,
final DatabaseType databaseType, final SQLExecuteType sqlExecuteType, final SQLCommandType sqlCommandType) {
Collection<AssertionTestParameter> result = new LinkedList<>();

if ("RDL".equals(sqlCommandType.name())) {
IntegrationTestCaseAssertion eachAssertions = testCaseContext.getTestCase().getAssertions().iterator().next();
result.addAll(getAssertionTestParameter(testCaseContext, databaseType, sqlExecuteType, eachAssertions, testCaseContext.getTestCase().getAssertions(), sqlCommandType));
} else {
for (IntegrationTestCaseAssertion each : testCaseContext.getTestCase().getAssertions()) {
result.addAll(getAssertionTestParameter(testCaseContext, databaseType, sqlExecuteType, each, testCaseContext.getTestCase().getAssertions(), sqlCommandType));
}
for (IntegrationTestCaseAssertion each : testCaseContext.getTestCase().getAssertions()) {
result.addAll(getAssertionTestParameter(testCaseContext, databaseType, sqlExecuteType, each, sqlCommandType));
}
return result;
}

private Collection<AssertionTestParameter> getAssertionTestParameter(final IntegrationTestCaseContext testCaseContext,
final DatabaseType databaseType, final SQLExecuteType sqlExecuteType,
final IntegrationTestCaseAssertion assertion,
final Collection<IntegrationTestCaseAssertion> assertions,
final SQLCommandType sqlCommandType) {
final IntegrationTestCaseAssertion assertion, final SQLCommandType sqlCommandType) {
Collection<AssertionTestParameter> result = new LinkedList<>();
for (String each : getEnvAdapters(testCaseContext.getTestCase().getAdapters())) {
if (sqlCommandType.getRunningAdaptors().contains(each) && envAdapters.contains(each)) {
result.addAll(getAssertionTestParameter(testCaseContext, assertion, assertions, each, databaseType, sqlExecuteType, sqlCommandType));
result.addAll(getAssertionTestParameter(testCaseContext, assertion, each, databaseType, sqlExecuteType, sqlCommandType));
}
}
return result;
}

private Collection<AssertionTestParameter> getAssertionTestParameter(final IntegrationTestCaseContext testCaseContext, final IntegrationTestCaseAssertion assertion,
final Collection<IntegrationTestCaseAssertion> assertions,
final String adapter, final DatabaseType databaseType,
final SQLExecuteType sqlExecuteType, final SQLCommandType sqlCommandType) {
Collection<String> scenarios = null == testCaseContext.getTestCase().getScenarioTypes() ? Collections.emptyList() : Arrays.asList(testCaseContext.getTestCase().getScenarioTypes().split(","));
return envScenarios.stream().filter(each -> filterScenarios(each, scenarios, sqlCommandType.getSqlStatementClass()))
.map(each -> new AssertionTestParameter(testCaseContext, assertion, assertions, adapter, each, envMode, databaseType, sqlExecuteType, sqlCommandType)).collect(Collectors.toList());
.map(each -> new AssertionTestParameter(testCaseContext, assertion, adapter, each, envMode, databaseType, sqlExecuteType, sqlCommandType)).collect(Collectors.toList());
}

private Collection<String> getEnvAdapters(final String envAdapters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
import org.apache.shardingsphere.test.e2e.cases.SQLExecuteType;
import org.apache.shardingsphere.test.e2e.cases.assertion.IntegrationTestCaseAssertion;

import java.util.Collection;
/**
* Assertion test parameter.
*/

@RequiredArgsConstructor
@Getter
public final class AssertionTestParameter implements E2ETestParameter {
Expand All @@ -38,8 +36,6 @@ public final class AssertionTestParameter implements E2ETestParameter {

private final IntegrationTestCaseAssertion assertion;

private final Collection<IntegrationTestCaseAssertion> assertions;

private final String adapter;

private final String scenario;
Expand Down

This file was deleted.

Loading