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/05/15 13:13:41 UTC

[GitHub] [shardingsphere] iisheng opened a new pull request, #17674: Add unit test for EncryptInsertOnUpdateTokenGenerator

iisheng opened a new pull request, #17674:
URL: https://github.com/apache/shardingsphere/pull/17674

   For #15820 .
   
   Changes proposed in this pull request:
   - Add unit test for EncryptInsertOnUpdateTokenGenerator#isGenerateSQLToken
   - Add unit test for EncryptInsertOnUpdateTokenGenerator#generateSQLTokens
   


-- 
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


[GitHub] [shardingsphere] terrymanu merged pull request #17674: Add unit test for EncryptInsertOnUpdateTokenGenerator

Posted by GitBox <gi...@apache.org>.
terrymanu merged PR #17674:
URL: https://github.com/apache/shardingsphere/pull/17674


-- 
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


[GitHub] [shardingsphere] iisheng commented on pull request #17674: Add unit test for EncryptInsertOnUpdateTokenGenerator

Posted by GitBox <gi...@apache.org>.
iisheng commented on PR #17674:
URL: https://github.com/apache/shardingsphere/pull/17674#issuecomment-1128830515

   > Please fix the CI
   
   
   
   > 
   
   


-- 
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


[GitHub] [shardingsphere] terrymanu commented on a diff in pull request #17674: Add unit test for EncryptInsertOnUpdateTokenGenerator

Posted by GitBox <gi...@apache.org>.
terrymanu commented on code in PR #17674:
URL: https://github.com/apache/shardingsphere/pull/17674#discussion_r874962231


##########
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 com.google.common.collect.Lists;
+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.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 class EncryptInsertOnUpdateTokenGeneratorTest {

Review Comment:
   Please add final if class does not design for extension



##########
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 com.google.common.collect.Lists;
+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.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 class EncryptInsertOnUpdateTokenGeneratorTest {
+    
+    private EncryptInsertOnUpdateTokenGenerator generator;
+    
+    @Before
+    public void setup() {
+        generator = new EncryptInsertOnUpdateTokenGenerator();
+        generator.setEncryptRule(mockEncryptRule());
+    }
+    
+    @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 Lists.newArrayList(assignmentSegment1, assignmentSegment2, assignmentSegment3);
+    }
+    
+    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;
+    }

Review Comment:
   Please move the method after method with the first call



-- 
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


[GitHub] [shardingsphere] iisheng closed pull request #17674: Add unit test for EncryptInsertOnUpdateTokenGenerator

Posted by GitBox <gi...@apache.org>.
iisheng closed pull request #17674: Add unit test for EncryptInsertOnUpdateTokenGenerator
URL: https://github.com/apache/shardingsphere/pull/17674


-- 
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