This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 ba0f2fc Add unit test for EncryptOrderByItemTokenGenerator (#16012)
ba0f2fc is described below
commit ba0f2fceb81d1bf475d7057f91501603396fcfb9
Author: Trydamere <[email protected]>
AuthorDate: Sun Mar 13 12:09:02 2022 +0800
Add unit test for EncryptOrderByItemTokenGenerator (#16012)
---
.../impl/EncryptOrderByItemTokenGeneratorTest.java | 92 ++++++++++++++++++++++
1 file changed, 92 insertions(+)
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java
new file mode 100644
index 0000000..27fdee3
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.impl;
+
+import
org.apache.shardingsphere.encrypt.rewrite.token.generator.EncryptOrderByItemTokenGenerator;
+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.segment.select.orderby.OrderByItem;
+import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
+import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import
org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.SubstitutableColumnNameToken;
+import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
+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.value.identifier.IdentifierValue;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class EncryptOrderByItemTokenGeneratorTest {
+
+ private EncryptOrderByItemTokenGenerator generator;
+
+ @Before
+ public void setup() {
+ generator = new EncryptOrderByItemTokenGenerator();
+ generator.setEncryptRule(buildEncryptRule());
+ }
+
+ @Test
+ public void assertGenerateSQLTokens() {
+ Collection<SubstitutableColumnNameToken> sqlTokens =
generator.generateSQLTokens(buildSelectStatementContext());
+ assertThat(sqlTokens.size(), is(1));
+ }
+
+ private SelectStatementContext buildSelectStatementContext() {
+ SimpleTableSegment simpleTableSegment = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("t_encrypt")));
+ simpleTableSegment.setAlias(new AliasSegment(0, 0, new
IdentifierValue("a")));
+ ColumnSegment columnSegment = new ColumnSegment(0, 0, new
IdentifierValue("certificate_number"));
+ columnSegment.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("a")));
+ SelectStatementContext result = mock(SelectStatementContext.class,
RETURNS_DEEP_STUBS);
+ ColumnOrderByItemSegment columnOrderByItemSegment = new
ColumnOrderByItemSegment(columnSegment, OrderDirection.ASC);
+ OrderByItem orderByItem = new OrderByItem(columnOrderByItemSegment);
+
when(result.getOrderByContext().getItems()).thenReturn(Collections.singletonList(orderByItem));
+
when(result.getGroupByContext().getItems()).thenReturn(Collections.emptyList());
+
when(result.getSubqueryContexts().values()).thenReturn(Collections.emptyList());
+ when(result.getTablesContext()).thenReturn(new
TablesContext(Collections.singletonList(simpleTableSegment)));
+ return result;
+ }
+
+ private EncryptRule buildEncryptRule() {
+ EncryptRule result = mock(EncryptRule.class);
+ EncryptTable encryptTable = mock(EncryptTable.class);
+ when(result.getCipherColumn("t_encrypt",
"certificate_number")).thenReturn("cipher_certificate_number");
+ when(result.findAssistedQueryColumn("t_encrypt",
"certificate_number")).thenReturn(Optional.of("assisted_certificate_number"));
+ when(result.findPlainColumn("t_encrypt",
"certificate_number")).thenReturn(Optional.of("certificate_number_plain"));
+
when(encryptTable.findEncryptorName("certificate_number")).thenReturn(Optional.of("encryptor_name"));
+ when(result.findEncryptor("t_encrypt",
"certificate_number")).thenReturn(Optional.of(mock(EncryptAlgorithm.class)));
+
when(result.findEncryptTable("t_encrypt")).thenReturn(Optional.of(encryptTable));
+ return result;
+ }
+
+}