Skip to content

Commit

Permalink
Merge pull request #2157 from codefairy08/dev
Browse files Browse the repository at this point in the history
#2134 MySQL sharding encrypt test
  • Loading branch information
terrymanu authored Apr 8, 2019
2 parents 9ff03e2 + 4d6afa6 commit 1b66810
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Encrypt or condition filler.
Expand Down Expand Up @@ -76,11 +78,17 @@ private void fillColumnTableMap(final SQLStatement sqlStatement, final ShardingT

private OrCondition filterCondition(final ShardingTableMetaData shardingTableMetaData, final SQLStatement sqlStatement, final OrConditionSegment orCondition, final EncryptRule encryptRule) {
OrCondition result = new OrCondition();
Set<Integer> filledConditionStopIndexes = new HashSet<>();
for (AndConditionSegment each : orCondition.getAndConditions()) {
for (ConditionSegment condition : each.getConditions()) {
if (null == condition.getColumn()) {
continue;
}
if(filledConditionStopIndexes.contains(condition.getStopIndex())) {
continue;
}else {
filledConditionStopIndexes.add(condition.getStopIndex());
}
Column column = new Column(condition.getColumn().getName(), getTableName(shardingTableMetaData, sqlStatement, condition));
fillEncryptCondition(column, condition, encryptRule, sqlStatement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@

package org.apache.shardingsphere.core.parse.antlr.filler.sharding.dml;

import com.google.common.base.Optional;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.shardingsphere.core.constant.ShardingOperator;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.metadata.table.TableMetaData;
Expand All @@ -39,11 +46,7 @@
import org.apache.shardingsphere.core.parse.parser.token.TableToken;
import org.apache.shardingsphere.core.rule.ShardingRule;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.google.common.base.Optional;

/**
* Or condition filler.
Expand Down Expand Up @@ -122,6 +125,7 @@ private OrCondition filterCondition(final ShardingTableMetaData shardingTableMet
break;
}
}
Set<Integer> filledConditionStopIndexes = new HashSet<>();
for (AndConditionSegment each : orCondition.getAndConditions()) {
for (ConditionSegment condition : each.getConditions()) {
if (null == condition.getColumn()) {
Expand All @@ -130,6 +134,11 @@ private OrCondition filterCondition(final ShardingTableMetaData shardingTableMet
if (condition.getExpression() instanceof ColumnSegment) {
continue;
}
if(filledConditionStopIndexes.contains(condition.getStopIndex())) {
continue;
}else {
filledConditionStopIndexes.add(condition.getStopIndex());
}
Column column = new Column(condition.getColumn().getName(), getTableName(shardingTableMetaData, shardingRule, sqlStatement, condition));
fillEncryptCondition(column, condition, shardingRule, sqlStatement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void assertSupportedSQL() {
execDatabaseType = DatabaseType.MySQL;
}
new SQLStatementAssert(new AntlrParsingEngine(execDatabaseType, sql, AbstractBaseIntegrateSQLParsingTest.getShardingRule(),
AbstractBaseIntegrateSQLParsingTest.getShardingTableMetaData()).parse(), sqlCaseId, sqlCaseType, sqlCasesLoader, parserResultSetLoader).assertSQLStatement();
AbstractBaseIntegrateSQLParsingTest.getShardingTableMetaData()).parse(), sqlCaseId, sqlCaseType, sqlCasesLoader, parserResultSetLoader, execDatabaseType).assertSQLStatement();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ public void assertSupportedSQL() {
}
new SQLStatementAssert(new AntlrParsingEngine(
execDatabaseType, sql, AbstractBaseIntegrateSQLParsingTest.getShardingRule(),
AbstractBaseIntegrateSQLParsingTest.getShardingTableMetaData()).parse(), sqlCaseId, sqlCaseType, sqlCasesLoader, parserResultSetLoader).assertSQLStatement();
AbstractBaseIntegrateSQLParsingTest.getShardingTableMetaData()).parse(), sqlCaseId, sqlCaseType, sqlCasesLoader, parserResultSetLoader, execDatabaseType).assertSQLStatement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package org.apache.shardingsphere.core.parse.integrate.asserts;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parse.antlr.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.antlr.sql.statement.ddl.AlterTableStatement;
import org.apache.shardingsphere.core.parse.antlr.sql.statement.ddl.CreateTableStatement;
Expand All @@ -36,9 +40,6 @@
import org.apache.shardingsphere.test.sql.SQLCaseType;
import org.apache.shardingsphere.test.sql.SQLCasesLoader;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
* SQL statement assert.
*
Expand Down Expand Up @@ -70,24 +71,27 @@ public final class SQLStatementAssert {

private final AlterTableAssert alterTableAssert;

public SQLStatementAssert(final SQLStatement actual, final String sqlCaseId, final SQLCaseType sqlCaseType) {
this(actual, sqlCaseId, sqlCaseType, SQLCasesLoader.getInstance(), ParserResultSetLoader.getInstance());
private final DatabaseType databaseType;

public SQLStatementAssert(final SQLStatement actual, final String sqlCaseId, final SQLCaseType sqlCaseType, final DatabaseType databaseType) {
this(actual, sqlCaseId, sqlCaseType, SQLCasesLoader.getInstance(), ParserResultSetLoader.getInstance(), databaseType);
}

public SQLStatementAssert(final SQLStatement actual, final String sqlCaseId, final SQLCaseType sqlCaseType, final SQLCasesLoader sqlLoader, final ParserResultSetLoader parserResultSetLoader) {
public SQLStatementAssert(final SQLStatement actual, final String sqlCaseId, final SQLCaseType sqlCaseType, final SQLCasesLoader sqlLoader, final ParserResultSetLoader parserResultSetLoader, final DatabaseType databaseType) {
SQLStatementAssertMessage assertMessage = new SQLStatementAssertMessage(sqlLoader, parserResultSetLoader, sqlCaseId, sqlCaseType);
this.actual = actual;
expected = parserResultSetLoader.getParserResult(sqlCaseId);
tableAssert = new TableAssert(assertMessage);
conditionAssert = new ConditionAssert(assertMessage);
tokenAssert = new TokenAssert(sqlCaseType, assertMessage);
tokenAssert = new TokenAssert(sqlCaseType, assertMessage, databaseType);
indexAssert = new IndexAssert(sqlCaseType, assertMessage);
itemAssert = new ItemAssert(assertMessage);
groupByAssert = new GroupByAssert(assertMessage);
orderByAssert = new OrderByAssert(assertMessage);
limitAssert = new LimitAssert(sqlCaseType, assertMessage);
metaAssert = new TableMetaDataAssert(assertMessage);
alterTableAssert = new AlterTableAssert(assertMessage);
this.databaseType = databaseType;
}

/**
Expand All @@ -96,7 +100,9 @@ public SQLStatementAssert(final SQLStatement actual, final String sqlCaseId, fin
public void assertSQLStatement() {
tableAssert.assertTables(actual.getTables(), expected.getTables());
conditionAssert.assertOrCondition(actual.getRouteConditions().getOrCondition(), expected.getOrCondition());
//conditionAssert.assertOrCondition(actual.getEncryptConditions().getOrCondition(), expected.getEncryptCondition());
if(DatabaseType.MySQL == databaseType) {
conditionAssert.assertOrCondition(actual.getEncryptConditions().getOrCondition(), expected.getEncryptCondition());
}
tokenAssert.assertTokens(actual.getSQLTokens(), expected.getTokens());
indexAssert.assertParametersIndex(actual.getParametersIndex(), expected.getParameters().size());
if (actual instanceof SelectStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@
import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedTokens;
import org.apache.shardingsphere.core.parse.parser.token.EncryptColumnToken;
import org.apache.shardingsphere.core.parse.parser.token.SQLToken;
import org.apache.shardingsphere.test.sql.SQLCaseType;

import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public final class EncryptColumnTokenAssert {

private final SQLCaseType sqlCaseType;

private final SQLStatementAssertMessage assertMessage;

void assertIndexToken(final Collection<SQLToken> actual, final ExpectedTokens expected) {
void assertEncryptColumnsToken(final Collection<SQLToken> actual, final ExpectedTokens expected) {
List<EncryptColumnToken> encryptColumnTokens = getEncryptColumnTokens(actual);
assertThat(assertMessage.getFullAssertMessage("Encrypt column tokens size error: "), encryptColumnTokens.size(), is(expected.getEncryptColumnTokens().size()));
int count = 0;
Expand All @@ -49,8 +52,13 @@ void assertIndexToken(final Collection<SQLToken> actual, final ExpectedTokens ex
}

private void assertEncryptColumnToken(final EncryptColumnToken actual, final ExpectedEncryptColumnToken expected) {
assertThat(assertMessage.getFullAssertMessage("Encrypt column start index assertion error: "), actual.getStartIndex(), is(expected.getStartIndex()));
assertThat(assertMessage.getFullAssertMessage("Encrypt column stop index assertion error: "), actual.getStopIndex(), is(expected.getStopIndex()));
if(SQLCaseType.Placeholder == sqlCaseType){
assertThat(assertMessage.getFullAssertMessage("Encrypt column start index for placeholder assertion error: "), actual.getStartIndex(), is(expected.getStartIndexForPlaceholder()));
assertThat(assertMessage.getFullAssertMessage("Encrypt column stop index for placeholder assertion error: "), actual.getStopIndex(), is(expected.getStopIndexForPlaceholder()));
}else {
assertThat(assertMessage.getFullAssertMessage("Encrypt column start index for literal assertion error: "), actual.getStartIndex(), is(expected.getStartIndexForLiteral()));
assertThat(assertMessage.getFullAssertMessage("Encrypt column stop index for literal assertion error: "), actual.getStopIndex(), is(expected.getStopIndexForLiteral()));
}
assertNotNull(assertMessage.getFullAssertMessage("Encrypt column does not exist assertion error: "), expected.getColumn());
assertTrue(assertMessage.getFullAssertMessage("Missing encrypt column assertion error: "), actual.getColumn() != null);
assertThat(assertMessage.getFullAssertMessage("Encrypt column name assertion error: "), actual.getColumn().getName(), is(expected.getColumn().getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.core.parse.integrate.asserts.token;

import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.parse.integrate.asserts.SQLStatementAssertMessage;
import org.apache.shardingsphere.core.parse.integrate.jaxb.token.ExpectedTokens;
import org.apache.shardingsphere.core.parse.parser.token.SQLToken;
Expand Down Expand Up @@ -51,7 +52,11 @@ public final class TokenAssert {

private final AggregationDistinctTokenAssert aggregationDistinctTokenAssert;

public TokenAssert(final SQLCaseType sqlCaseType, final SQLStatementAssertMessage assertMessage) {
private final EncryptColumnTokenAssert encryptColumnTokenAssert;

private final DatabaseType databaseType;

public TokenAssert(final SQLCaseType sqlCaseType, final SQLStatementAssertMessage assertMessage, final DatabaseType databaseType) {
tableTokenAssert = new TableTokenAssert(assertMessage);
schemaTokenAssert = new SchemaTokenAssert(assertMessage);
indexTokenAssert = new IndexTokenAssert(assertMessage);
Expand All @@ -62,6 +67,8 @@ public TokenAssert(final SQLCaseType sqlCaseType, final SQLStatementAssertMessag
offsetTokenAssert = new OffsetTokenAssert(sqlCaseType, assertMessage);
rowCountTokenAssert = new RowCountTokenAssert(sqlCaseType, assertMessage);
aggregationDistinctTokenAssert = new AggregationDistinctTokenAssert(assertMessage);
encryptColumnTokenAssert = new EncryptColumnTokenAssert(sqlCaseType, assertMessage);
this.databaseType = databaseType;
}

/**
Expand All @@ -81,5 +88,8 @@ public void assertTokens(final Collection<SQLToken> actual, final ExpectedTokens
offsetTokenAssert.assertOffsetToken(actual, expected);
rowCountTokenAssert.assertRowCountToken(actual, expected);
aggregationDistinctTokenAssert.assertAggregationDistinctTokens(actual, expected);
if(DatabaseType.MySQL == databaseType) {
encryptColumnTokenAssert.assertEncryptColumnsToken(actual, expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public void assertSupportedSQL() {
return;
}
new SQLStatementAssert(new SQLParsingEngine(
databaseType, sql, getShardingRule(), getShardingTableMetaData(), new ParsingResultCache()).parse(false), sqlCaseId, sqlCaseType).assertSQLStatement();
databaseType, sql, getShardingRule(), getShardingTableMetaData(), new ParsingResultCache()).parse(false), sqlCaseId, sqlCaseType, databaseType).assertSQLStatement();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,18 @@
@XmlAccessorType(XmlAccessType.FIELD)
public final class ExpectedEncryptColumnToken {

@XmlAttribute(name = "start-index")
private int startIndex;
@XmlAttribute(name = "start-index-for-placeholder")
private int startIndexForPlaceholder;

@XmlAttribute(name = "stop-index")
private int stopIndex;
@XmlAttribute(name = "stop-index-for-placeholder")
private int stopIndexForPlaceholder;


@XmlAttribute(name = "start-index-for-literal")
private int startIndexForLiteral;

@XmlAttribute(name = "stop-index-for-literal")
private int stopIndexForLiteral;

private ExpectedColumn column;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
</tables>
<tokens>
<table-token start-index="12" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="59" stop-index-for-placeholder="66" start-index-for-literal="65" stop-index-for-literal="77" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<or-condition>
<and-condition>
Expand All @@ -34,6 +37,13 @@
</condition>
</and-condition>
</or-condition>
<encrypt-condition>
<and-condition>
<condition column-name="status" table-name="t_order" operator="EQUAL">
<value index="2" literal="init" type="varchar" />
</condition>
</and-condition>
</encrypt-condition>
</parser-result>

<parser-result sql-case-id="delete_without_sharding_value" parameters="'init'">
Expand All @@ -42,7 +52,17 @@
</tables>
<tokens>
<table-token start-index="12" table-name="t_order" length="7" />
<encrypt-column-token start-index-for-placeholder="26" stop-index-for-placeholder="33" start-index-for-literal="26" stop-index-for-literal="38" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="status" table-name="t_order" operator="EQUAL">
<value index="0" literal="init" type="varchar" />
</condition>
</and-condition>
</encrypt-condition>
</parser-result>

<parser-result sql-case-id="delete_with_special_character_without_sharding_value">
Expand All @@ -51,7 +71,17 @@
</tables>
<tokens>
<table-token start-index="12" table-name="t_order" left-delimiter="`" right-delimiter="`" length="9" />
<encrypt-column-token start-index-for-placeholder="28" stop-index-for-placeholder="42" start-index-for-literal="28" stop-index-for-literal="42" is-in-where="true">
<column table-name="t_order" name="status"/>
</encrypt-column-token>
</tokens>
<encrypt-condition>
<and-condition>
<condition column-name="status" table-name="t_order" operator="EQUAL">
<value index="0" literal="init" type="varchar" />
</condition>
</and-condition>
</encrypt-condition>
</parser-result>

<parser-result sql-case-id="delete_with_special_comments_return_without_sharding_value">
Expand Down
Loading

0 comments on commit 1b66810

Please sign in to comment.