You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/05/17 16:07:44 UTC
[shardingsphere] branch master updated: Add unit test for EncryptInsertOnUpdateTokenGenerator (#17674)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 1097db7db68 Add unit test for EncryptInsertOnUpdateTokenGenerator (#17674)
1097db7db68 is described below
commit 1097db7db68fd310c09dcb847cddd6ea77533fe8
Author: iisheng <op...@gmail.com>
AuthorDate: Wed May 18 00:07:39 2022 +0800
Add unit test for EncryptInsertOnUpdateTokenGenerator (#17674)
* Add unit test for EncryptInsertOnUpdateTokenGenerator
* Remove usage of Lists.newArrayList
---
.../EncryptInsertOnUpdateTokenGeneratorTest.java | 132 +++++++++++++++++++++
...ngGeneratedKeyInsertValueParameterRewriter.java | 4 +-
.../ConditionValueCompareOperatorGenerator.java | 4 +-
3 files changed, 136 insertions(+), 4 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptInsertOnUpdateTokenGeneratorTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptInsertOnUpdateTokenGeneratorTest.java
new file mode 100644
index 00000000000..7a7858d723f
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptInsertOnUpdateTokenGeneratorTest.java
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.encrypt.rewrite.token;
+
+import org.apache.shardingsphere.encrypt.rewrite.token.generator.EncryptInsertOnUpdateTokenGenerator;
+import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptAssignmentToken;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import org.apache.shardingsphere.encrypt.rule.EncryptTable;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.ColumnAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.FunctionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.InsertStatementHandler;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class EncryptInsertOnUpdateTokenGeneratorTest {
+
+ private EncryptInsertOnUpdateTokenGenerator generator;
+
+ @Before
+ public void setup() {
+ generator = new EncryptInsertOnUpdateTokenGenerator();
+ generator.setEncryptRule(mockEncryptRule());
+ }
+
+ private EncryptRule mockEncryptRule() {
+ EncryptRule result = mock(EncryptRule.class);
+ when(result.getCipherColumn("t_user", "mobile")).thenReturn("cipher_mobile");
+ EncryptTable encryptTable = mock(EncryptTable.class);
+ when(encryptTable.getLogicColumns()).thenReturn(Collections.emptyList());
+ EncryptAlgorithm<?, ?> encryptAlgorithm = mock(EncryptAlgorithm.class);
+ when(result.findEncryptor("t_user", "mobile")).thenReturn(Optional.of(encryptAlgorithm));
+ when(result.findEncryptor("t_user", "cipher_mobile")).thenReturn(Optional.of(encryptAlgorithm));
+ when(result.getEncryptValues(null, "db_test", "t_user", "mobile",
+ Collections.singletonList(0))).thenReturn(Collections.singletonList("encryptValue"));
+ return result;
+ }
+
+ @Test
+ public void assertIsGenerateSQLToken() {
+ SelectStatementContext selectStatementContext = mock(SelectStatementContext.class);
+ assertFalse(generator.isGenerateSQLToken(selectStatementContext));
+ InsertStatementContext insertStatementContext = mock(InsertStatementContext.class);
+ assertFalse(generator.isGenerateSQLToken(insertStatementContext));
+ MySQLInsertStatement insertStatement = mock(MySQLInsertStatement.class);
+ when(insertStatementContext.getSqlStatement()).thenReturn(insertStatement);
+ when(InsertStatementHandler.getOnDuplicateKeyColumnsSegment(insertStatementContext.getSqlStatement()))
+ .thenReturn(Optional.of(new OnDuplicateKeyColumnsSegment(0, 0, Collections.emptyList())));
+ assertTrue(generator.isGenerateSQLToken(insertStatementContext));
+ }
+
+ @Test
+ public void assertGenerateSQLTokens() {
+ InsertStatementContext insertStatementContext = mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
+ when(insertStatementContext.getTablesContext().getSchemaName()).thenReturn(Optional.of("db_test"));
+ MySQLInsertStatement insertStatement = mock(MySQLInsertStatement.class, RETURNS_DEEP_STUBS);
+ when(insertStatement.getTable().getTableName().getIdentifier().getValue()).thenReturn("t_user");
+ OnDuplicateKeyColumnsSegment onDuplicateKeyColumnsSegment = mock(OnDuplicateKeyColumnsSegment.class);
+ when(onDuplicateKeyColumnsSegment.getColumns()).thenReturn(buildAssignmentSegment());
+ when(insertStatement.getOnDuplicateKeyColumns()).thenReturn(Optional.ofNullable(onDuplicateKeyColumnsSegment));
+ when(insertStatementContext.getSqlStatement()).thenReturn(insertStatement);
+ when(InsertStatementHandler.getOnDuplicateKeyColumnsSegment(insertStatementContext.getSqlStatement()))
+ .thenReturn(Optional.of(onDuplicateKeyColumnsSegment));
+ Collection<EncryptAssignmentToken> encryptAssignmentTokens = generator.generateSQLTokens(insertStatementContext);
+ assertThat(encryptAssignmentTokens.size(), is(3));
+ Iterator<EncryptAssignmentToken> iterator = encryptAssignmentTokens.iterator();
+ EncryptAssignmentToken assignmentToken1 = iterator.next();
+ assertThat(assignmentToken1.toString(), is("cipher_mobile = ?"));
+ assertThat(assignmentToken1.getStartIndex(), is(0));
+ assertThat(assignmentToken1.getStopIndex(), is(0));
+ EncryptAssignmentToken assignmentToken2 = iterator.next();
+ assertThat(assignmentToken2.toString(), is("cipher_mobile = VALUES(cipher_mobile)"));
+ assertThat(assignmentToken2.getStartIndex(), is(0));
+ assertThat(assignmentToken2.getStopIndex(), is(0));
+ EncryptAssignmentToken assignmentToken3 = iterator.next();
+ assertThat(assignmentToken3.toString(), is("cipher_mobile = 'encryptValue'"));
+ assertThat(assignmentToken3.getStartIndex(), is(0));
+ assertThat(assignmentToken3.getStopIndex(), is(0));
+ }
+
+ private Collection<AssignmentSegment> buildAssignmentSegment() {
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("mobile"));
+ List<ColumnSegment> columnSegments = Collections.singletonList(columnSegment);
+ AssignmentSegment assignmentSegment1 = new ColumnAssignmentSegment(0, 0, columnSegments,
+ new ParameterMarkerExpressionSegment(0, 0, 0));
+ FunctionSegment functionSegment = new FunctionSegment(0, 0, "VALUES", "VALUES (0)");
+ functionSegment.getParameters().add(columnSegment);
+ AssignmentSegment assignmentSegment2 = new ColumnAssignmentSegment(0, 0, columnSegments, functionSegment);
+ AssignmentSegment assignmentSegment3 = new ColumnAssignmentSegment(0, 0, columnSegments,
+ new LiteralExpressionSegment(0, 0, 0));
+ return Arrays.asList(assignmentSegment1, assignmentSegment2, assignmentSegment3);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/parameter/impl/ShardingGeneratedKeyInsertValueParameterRewriter.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/parameter/impl/ShardingGeneratedKeyInsertValueParameterRewriter.java
index df4b3922bfe..03bc6f213db 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/parameter/impl/ShardingGeneratedKeyInsertValueParameterRewriter.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/parameter/impl/ShardingGeneratedKeyInsertValueParameterRewriter.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.sharding.rewrite.parameter.impl;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
import lombok.Setter;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
@@ -26,6 +25,7 @@ import org.apache.shardingsphere.infra.rewrite.parameter.builder.ParameterBuilde
import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
import org.apache.shardingsphere.infra.rewrite.parameter.rewriter.ParameterRewriter;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -54,7 +54,7 @@ public final class ShardingGeneratedKeyInsertValueParameterRewriter implements P
parameterCount += insertStatementContext.getInsertValueContexts().get(count).getParameterCount();
Comparable<?> generatedValue = generatedValues.next();
if (!each.isEmpty()) {
- ((GroupedParameterBuilder) parameterBuilder).getParameterBuilders().get(count).addAddedParameters(parameterCount, Lists.newArrayList(generatedValue));
+ ((GroupedParameterBuilder) parameterBuilder).getParameterBuilders().get(count).addAddedParameters(parameterCount, Collections.singletonList(generatedValue));
}
count++;
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
index 4497df3f1e6..d4efe5c276c 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/generator/impl/ConditionValueCompareOperatorGenerator.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.sharding.route.engine.condition.generator.impl;
-import com.google.common.collect.Lists;
import com.google.common.collect.Range;
import org.apache.shardingsphere.infra.datetime.DatetimeServiceFactory;
import org.apache.shardingsphere.sharding.route.engine.condition.Column;
@@ -33,6 +32,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.Expressi
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
@@ -76,7 +76,7 @@ public final class ConditionValueCompareOperatorGenerator implements ConditionVa
String tableName = column.getTableName();
switch (operator) {
case EQUAL:
- return Optional.of(new ListShardingConditionValue<>(columnName, tableName, Lists.newArrayList(comparable)));
+ return Optional.of(new ListShardingConditionValue<>(columnName, tableName, Collections.singletonList(comparable)));
case GREATER_THAN:
return Optional.of(new RangeShardingConditionValue<>(columnName, tableName, Range.greaterThan(comparable)));
case LESS_THAN: