You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/07/25 01:59:14 UTC

[GitHub] [shardingsphere] strongduanmu commented on a diff in pull request #19502: Add Encrypt test

strongduanmu commented on code in PR #19502:
URL: https://github.com/apache/shardingsphere/pull/19502#discussion_r928355873


##########
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptGeneratorBaseTest.java:
##########
@@ -30,44 +30,57 @@
 import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
 import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
 import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptInsertValuesToken;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementContext;
 import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import org.apache.shardingsphere.infra.database.DefaultDatabase;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
 import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.InsertValue;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.ColumnAssignmentSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.SetAssignmentSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.InsertColumnsSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
+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.segment.dml.predicate.WhereSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLUpdateStatement;
 
 public abstract class EncryptGeneratorBaseTest {
     
-    protected static EncryptRuleConfiguration createEncryptRuleConfiguration() {
+    private static final String TABLE_NAME = "t_user";
+    
+    protected static EncryptRule createEncryptRule() {
         EncryptColumnRuleConfiguration pwdColumnConfig = new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "pwd_assist", "pwd_plain", "test_encryptor", "test_encryptor", false);
-        return new EncryptRuleConfiguration(Collections.singleton(new EncryptTableRuleConfiguration("tbl", Collections.singletonList(pwdColumnConfig), null)),
-                Collections.singletonMap("test_encryptor", new ShardingSphereAlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties())));
+        return new EncryptRule(new EncryptRuleConfiguration(Collections.singleton(new EncryptTableRuleConfiguration(TABLE_NAME, Collections.singletonList(pwdColumnConfig), null)),
+                Collections.singletonMap("test_encryptor", new ShardingSphereAlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties()))));
     }
     
+    /**
+     * SQL: INSERT INTO t_user (id, name, status, pwd) VALUES (?, ?, ?, ?).

Review Comment:
   Please remove useless javadoc in unit test.



##########
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptGeneratorBaseTest.java:
##########
@@ -76,6 +89,40 @@ protected static InsertStatement createInsertStatement() {
         return result;
     }
     
+    /**
+     * SQL: UPDATE t_user SET pwd = '654321' WHERE name = 'LiLei' AND pwd = '123456'.

Review Comment:
   Please remove useless javadoc in unit test.



##########
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGeneratorTest.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.generator;
+
+import org.apache.shardingsphere.infra.database.DefaultDatabase;
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.SubstitutableColumnNameToken;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class EncryptPredicateColumnTokenGeneratorTest extends EncryptGeneratorBaseTest {
+    
+    @Test
+    public void assertIsGenerateSQLToken() {
+        EncryptPredicateColumnTokenGenerator tokenGenerator = new EncryptPredicateColumnTokenGenerator();
+        tokenGenerator.setDatabaseName(DefaultDatabase.LOGIC_NAME);
+        tokenGenerator.setEncryptRule(createEncryptRule());
+        tokenGenerator.setSchemas(Collections.emptyMap());
+        assertTrue(tokenGenerator.isGenerateSQLToken(createUpdatesStatementContext()));
+    }
+    
+    @Test
+    public void assertGenerateSQLTokenFromGenerateNewSQLToken() {
+        EncryptPredicateColumnTokenGenerator tokenGenerator = new EncryptPredicateColumnTokenGenerator();
+        tokenGenerator.setDatabaseName(DefaultDatabase.LOGIC_NAME);
+        tokenGenerator.setEncryptRule(createEncryptRule());
+        tokenGenerator.setSchemas(Collections.emptyMap());
+        Collection<SubstitutableColumnNameToken> substitutableColumnNameTokens = tokenGenerator.generateSQLTokens(createUpdatesStatementContext());
+        assertTrue(substitutableColumnNameTokens.stream().findFirst().isPresent());

Review Comment:
   Please use iterator instead of stream api.



##########
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptGeneratorBaseTest.java:
##########
@@ -76,6 +89,40 @@ protected static InsertStatement createInsertStatement() {
         return result;
     }
     
+    /**
+     * SQL: UPDATE t_user SET pwd = '654321' WHERE name = 'LiLei' AND pwd = '123456'.
+     */
+    protected static UpdateStatementContext createUpdatesStatementContext() {
+        MySQLUpdateStatement mySQLUpdateStatement = new MySQLUpdateStatement();
+        mySQLUpdateStatement.setTableSegment(createTableSegment(TABLE_NAME));
+        mySQLUpdateStatement.setWhere(createWhereSegment());
+        mySQLUpdateStatement.setSetAssignment(createSetAssignmentSegment());
+        return new UpdateStatementContext(mySQLUpdateStatement);
+    }
+    
+    /**
+     * SQL-template: WHERE name = 'LiLei' AND pwd = '123456'.
+     */
+    private static WhereSegment createWhereSegment() {
+        BinaryOperationExpression nameExpression = new BinaryOperationExpression(10, 24,
+                new ColumnSegment(10, 13, new IdentifierValue("name")), new LiteralExpressionSegment(18, 22, "LiLei"), "=", "name = 'LiLei'");
+        BinaryOperationExpression pwdExpression = new BinaryOperationExpression(30, 44,
+                new ColumnSegment(30, 32, new IdentifierValue("pwd")), new LiteralExpressionSegment(40, 45, "123456"), "=", "pwd = '123456'");
+        return new WhereSegment(0, 0, new BinaryOperationExpression(0, 0, nameExpression, pwdExpression, "AND", "name = 'LiLei' AND pwd = '123456'"));
+    }
+    
+    /**
+     * SQL-template: SET pwd = '654321'.

Review Comment:
   Please remove useless javadoc in unit test.



##########
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptGeneratorBaseTest.java:
##########
@@ -76,6 +89,40 @@ protected static InsertStatement createInsertStatement() {
         return result;
     }
     
+    /**
+     * SQL: UPDATE t_user SET pwd = '654321' WHERE name = 'LiLei' AND pwd = '123456'.
+     */
+    protected static UpdateStatementContext createUpdatesStatementContext() {
+        MySQLUpdateStatement mySQLUpdateStatement = new MySQLUpdateStatement();
+        mySQLUpdateStatement.setTableSegment(createTableSegment(TABLE_NAME));
+        mySQLUpdateStatement.setWhere(createWhereSegment());
+        mySQLUpdateStatement.setSetAssignment(createSetAssignmentSegment());
+        return new UpdateStatementContext(mySQLUpdateStatement);
+    }
+    
+    /**
+     * SQL-template: WHERE name = 'LiLei' AND pwd = '123456'.

Review Comment:
   Please remove useless javadoc in unit test.



##########
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateRightValueTokenGeneratorTest.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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.generator;
+
+import org.apache.shardingsphere.encrypt.rewrite.condition.EncryptCondition;
+import org.apache.shardingsphere.encrypt.rewrite.condition.EncryptConditionEngine;
+import org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementContext;
+import org.apache.shardingsphere.infra.database.DefaultDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class EncryptPredicateRightValueTokenGeneratorTest extends EncryptGeneratorBaseTest {
+    
+    @Test
+    public void assertIsGenerateSQLToken() {
+        EncryptPredicateRightValueTokenGenerator tokenGenerator = new EncryptPredicateRightValueTokenGenerator();
+        tokenGenerator.setDatabaseName(DefaultDatabase.LOGIC_NAME);
+        tokenGenerator.setEncryptRule(createEncryptRule());
+        assertTrue(tokenGenerator.isGenerateSQLToken(createUpdatesStatementContext()));
+    }
+    
+    @Test
+    public void assertGenerateSQLTokenFromGenerateNewSQLToken() {
+        UpdateStatementContext updatesStatementContext = createUpdatesStatementContext();
+        EncryptPredicateRightValueTokenGenerator tokenGenerator = new EncryptPredicateRightValueTokenGenerator();
+        tokenGenerator.setDatabaseName(DefaultDatabase.LOGIC_NAME);
+        tokenGenerator.setEncryptRule(createEncryptRule());
+        tokenGenerator.setEncryptConditions(getEncryptConditions(updatesStatementContext));
+        Collection<SQLToken> sqlTokens = tokenGenerator.generateSQLTokens(updatesStatementContext);
+        assertTrue(sqlTokens.stream().findFirst().isPresent());

Review Comment:
   Please use iterator instead of stream api.



##########
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGeneratorTest.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.generator;
+
+import org.apache.shardingsphere.infra.database.DefaultDatabase;
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.SubstitutableColumnNameToken;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class EncryptPredicateColumnTokenGeneratorTest extends EncryptGeneratorBaseTest {

Review Comment:
   Please add final for EncryptPredicateColumnTokenGeneratorTest.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org