This is an automated email from the ASF dual-hosted git repository.
zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new ad11838b885 Add more test cases on
EncryptPredicateColumnSupportedChecker (#33642)
ad11838b885 is described below
commit ad11838b885656ba387413b7f6faa6732f817a18
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Nov 13 19:39:37 2024 +0800
Add more test cases on EncryptPredicateColumnSupportedChecker (#33642)
* Add more test cases on EncryptPredicateColumnSupportedChecker
* Add more test cases on EncryptPredicateColumnSupportedChecker
---
.../EncryptPredicateColumnSupportedChecker.java | 10 +--
...EncryptPredicateColumnSupportedCheckerTest.java | 85 +++++++++++++++++++++-
.../fixture/EncryptGeneratorFixtureBuilder.java | 19 -----
3 files changed, 87 insertions(+), 27 deletions(-)
diff --git
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptPredicateColumnSupportedChecker.java
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptPredicateColumnSupportedChecker.java
index a76c6816a7f..601fb68a6a2 100644
---
a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptPredicateColumnSupportedChecker.java
+++
b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptPredicateColumnSupportedChecker.java
@@ -52,16 +52,16 @@ public final class EncryptPredicateColumnSupportedChecker
implements SupportedSQ
}
@Override
- public void check(final EncryptRule encryptRule, final
ShardingSphereSchema schema, final SQLStatementContext sqlStatementContext) {
-
ShardingSpherePreconditions.checkState(JoinConditionsEncryptorComparator.isSame(((WhereAvailable)
sqlStatementContext).getJoinConditions(), encryptRule),
+ public void check(final EncryptRule rule, final ShardingSphereSchema
schema, final SQLStatementContext sqlStatementContext) {
+
ShardingSpherePreconditions.checkState(JoinConditionsEncryptorComparator.isSame(((WhereAvailable)
sqlStatementContext).getJoinConditions(), rule),
() -> new UnsupportedSQLOperationException("Can not use
different encryptor in join condition"));
- check(encryptRule, schema, (WhereAvailable) sqlStatementContext);
+ check(rule, schema, (WhereAvailable) sqlStatementContext);
}
- private void check(final EncryptRule encryptRule, final
ShardingSphereSchema schema, final WhereAvailable sqlStatementContext) {
+ private void check(final EncryptRule rule, final ShardingSphereSchema
schema, final WhereAvailable sqlStatementContext) {
Map<String, String> columnExpressionTableNames = ((TableAvailable)
sqlStatementContext).getTablesContext().findTableNames(sqlStatementContext.getColumnSegments(),
schema);
for (ColumnSegment each : sqlStatementContext.getColumnSegments()) {
- Optional<EncryptTable> encryptTable =
encryptRule.findEncryptTable(columnExpressionTableNames.getOrDefault(each.getExpression(),
""));
+ Optional<EncryptTable> encryptTable =
rule.findEncryptTable(columnExpressionTableNames.getOrDefault(each.getExpression(),
""));
String columnName = each.getIdentifier().getValue();
if (encryptTable.isPresent() &&
encryptTable.get().isEncryptColumn(columnName) &&
includesLike(sqlStatementContext.getWhereSegments(), each)) {
String tableName = encryptTable.get().getTable();
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptPredicateColumnSupportedCheckerTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptPredicateColumnSupportedCheckerTest.java
index 322a8142cde..dac61bc7ba9 100644
---
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptPredicateColumnSupportedCheckerTest.java
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/sql/EncryptPredicateColumnSupportedCheckerTest.java
@@ -17,17 +17,96 @@
package org.apache.shardingsphere.encrypt.checker.sql;
+import
org.apache.shardingsphere.encrypt.exception.metadata.MissingMatchedEncryptQueryAlgorithmException;
import
org.apache.shardingsphere.encrypt.rewrite.token.generator.fixture.EncryptGeneratorFixtureBuilder;
+import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import org.junit.jupiter.api.Test;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
+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 EncryptPredicateColumnSupportedCheckerTest {
@Test
- void assertGenerateSQLTokensWhenJoinConditionUseDifferentEncryptor() {
- assertThrows(UnsupportedSQLOperationException.class,
- () -> new
EncryptPredicateColumnSupportedChecker().check(EncryptGeneratorFixtureBuilder.createEncryptRule(),
null, EncryptGeneratorFixtureBuilder.createSelectStatementContext()));
+ void assertIsCheckWithNotWhereAvailable() {
+ assertFalse(new
EncryptPredicateColumnSupportedChecker().isCheck(mock(SQLStatementContext.class)));
+ }
+
+ @Test
+ void assertIsCheckWithEmptyWhereSegment() {
+ assertFalse(new
EncryptPredicateColumnSupportedChecker().isCheck(mock(SelectStatementContext.class)));
+ }
+
+ @Test
+ void assertIsCheckWithNotEmptyWhereSegments() {
+ SelectStatementContext sqlStatementContext =
mock(SelectStatementContext.class);
+
when(sqlStatementContext.getWhereSegments()).thenReturn(Collections.singleton(mock(WhereSegment.class)));
+ assertTrue(new
EncryptPredicateColumnSupportedChecker().isCheck(sqlStatementContext));
+ }
+
+ @Test
+ void assertCheckWithDifferentEncryptorsInJoinCondition() {
+ assertThrows(UnsupportedSQLOperationException.class, () -> new
EncryptPredicateColumnSupportedChecker()
+ .check(EncryptGeneratorFixtureBuilder.createEncryptRule(),
null, mockSelectStatementContextWithDifferentEncryptorsInJoinCondition()));
+ }
+
+ private SQLStatementContext
mockSelectStatementContextWithDifferentEncryptorsInJoinCondition() {
+ ColumnSegment leftColumn = new ColumnSegment(0, 0, new
IdentifierValue("user_name"));
+ leftColumn.setColumnBoundInfo(new ColumnSegmentBoundInfo(
+ new IdentifierValue(DefaultDatabase.LOGIC_NAME), new
IdentifierValue(DefaultDatabase.LOGIC_NAME), new IdentifierValue("t_user"), new
IdentifierValue("user_name")));
+ ColumnSegment rightColumn = new ColumnSegment(0, 0, new
IdentifierValue("user_id"));
+ rightColumn.setColumnBoundInfo(new ColumnSegmentBoundInfo(
+ new IdentifierValue(DefaultDatabase.LOGIC_NAME), new
IdentifierValue(DefaultDatabase.LOGIC_NAME), new IdentifierValue("t_user"), new
IdentifierValue("user_id")));
+ SelectStatementContext result = mock(SelectStatementContext.class);
+ when(result.getJoinConditions()).thenReturn(Collections.singleton(new
BinaryOperationExpression(0, 0, leftColumn, rightColumn, "=", "")));
+ return result;
+ }
+
+ @Test
+ void assertCheckWithNotMatchedLikeQueryEncryptor() {
+ assertThrows(MissingMatchedEncryptQueryAlgorithmException.class, () ->
new EncryptPredicateColumnSupportedChecker()
+ .check(EncryptGeneratorFixtureBuilder.createEncryptRule(),
null, mockSelectStatementContextWithLike()));
+ }
+
+ private SQLStatementContext mockSelectStatementContextWithLike() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("user_name"));
+ columnSegment.setColumnBoundInfo(new ColumnSegmentBoundInfo(
+ new IdentifierValue(DefaultDatabase.LOGIC_NAME), new
IdentifierValue(DefaultDatabase.LOGIC_NAME), new IdentifierValue("t_user"), new
IdentifierValue("user_name")));
+ SelectStatementContext result = mock(SelectStatementContext.class,
RETURNS_DEEP_STUBS);
+
when(result.getTablesContext().findTableNames(Collections.singleton(columnSegment),
null)).thenReturn(Collections.singletonMap("user_name", "t_user"));
+
when(result.getColumnSegments()).thenReturn(Collections.singleton(columnSegment));
+ when(result.getWhereSegments()).thenReturn(Collections.singleton(new
WhereSegment(0, 0, new BinaryOperationExpression(0, 0, columnSegment,
columnSegment, "LIKE", ""))));
+ return result;
+ }
+
+ @Test
+ void assertCheckSuccess() {
+ assertDoesNotThrow(() -> new
EncryptPredicateColumnSupportedChecker().check(EncryptGeneratorFixtureBuilder.createEncryptRule(),
null, mockSelectStatementContextWithEqual()));
+ }
+
+ private SQLStatementContext mockSelectStatementContextWithEqual() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("user_name"));
+ columnSegment.setColumnBoundInfo(new ColumnSegmentBoundInfo(
+ new IdentifierValue(DefaultDatabase.LOGIC_NAME), new
IdentifierValue(DefaultDatabase.LOGIC_NAME), new IdentifierValue("t_user"), new
IdentifierValue("user_name")));
+ SelectStatementContext result = mock(SelectStatementContext.class,
RETURNS_DEEP_STUBS);
+
when(result.getTablesContext().findTableNames(Collections.singleton(columnSegment),
null)).thenReturn(Collections.singletonMap("user_name", "t_user"));
+
when(result.getColumnSegments()).thenReturn(Collections.singleton(columnSegment));
+ when(result.getWhereSegments()).thenReturn(Collections.singleton(new
WhereSegment(0, 0, new BinaryOperationExpression(0, 0, columnSegment,
columnSegment, "=", ""))));
+ return result;
}
}
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java
index 7d49491a9cb..08bdf7448db 100644
---
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java
@@ -26,9 +26,7 @@ import
org.apache.shardingsphere.encrypt.config.rule.EncryptTableRuleConfigurati
import
org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptInsertValuesToken;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext;
-import
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
@@ -205,23 +203,6 @@ public final class EncryptGeneratorFixtureBuilder {
return result;
}
- /**
- * Create select statement context.
- *
- * @return select statement context
- */
- public static SQLStatementContext createSelectStatementContext() {
- ColumnSegment leftColumn = new ColumnSegment(0, 0, new
IdentifierValue("user_name"));
- leftColumn.setColumnBoundInfo(new ColumnSegmentBoundInfo(new
IdentifierValue(DefaultDatabase.LOGIC_NAME), new
IdentifierValue(DefaultDatabase.LOGIC_NAME), new IdentifierValue("t_user"),
- new IdentifierValue("user_name")));
- ColumnSegment rightColumn = new ColumnSegment(0, 0, new
IdentifierValue("user_id"));
- rightColumn.setColumnBoundInfo(new ColumnSegmentBoundInfo(new
IdentifierValue(DefaultDatabase.LOGIC_NAME), new
IdentifierValue(DefaultDatabase.LOGIC_NAME), new IdentifierValue("t_user"),
- new IdentifierValue("user_id")));
- SelectStatementContext result = mock(SelectStatementContext.class);
- when(result.getJoinConditions()).thenReturn(Collections.singleton(new
BinaryOperationExpression(0, 0, leftColumn, rightColumn, "=", "")));
- return result;
- }
-
/**
* Create insert select statement context.
*