Trydamere commented on a change in pull request #16012: URL: https://github.com/apache/shardingsphere/pull/16012#discussion_r825211802
########## File path: 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 sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); + ColumnOrderByItemSegment columnOrderByItemSegment = new ColumnOrderByItemSegment(columnSegment, OrderDirection.ASC); + OrderByItem orderByItem = new OrderByItem(columnOrderByItemSegment); + when(sqlStatementContext.getOrderByContext().getItems()).thenReturn(Collections.singletonList(orderByItem)); + when(sqlStatementContext.getGroupByContext().getItems()).thenReturn(Collections.emptyList()); + when(sqlStatementContext.getSubqueryContexts().values()).thenReturn(Collections.emptyList()); + when(sqlStatementContext.getTablesContext()).thenReturn(new TablesContext(Collections.singletonList(simpleTableSegment))); + return sqlStatementContext; + } + + private EncryptRule buildEncryptRule() { + EncryptRule encryptRule = mock(EncryptRule.class); + EncryptTable encryptTable = mock(EncryptTable.class); + when(encryptRule.getCipherColumn("t_encrypt", "certificate_number")).thenReturn("cipher_certificate_number"); + when(encryptRule.findAssistedQueryColumn("t_encrypt", "certificate_number")).thenReturn(Optional.of("assisted_certificate_number")); + when(encryptRule.findPlainColumn("t_encrypt", "certificate_number")).thenReturn(Optional.of("certificate_number_plain")); + when(encryptTable.findEncryptorName("certificate_number")).thenReturn(Optional.of("encryptor_name")); + when(encryptRule.findEncryptor("t_encrypt", "certificate_number")).thenReturn(Optional.of(mock(EncryptAlgorithm.class))); + when(encryptRule.findEncryptTable("t_encrypt")).thenReturn(Optional.of(encryptTable)); + return encryptRule; + } + Review comment: Sure. I'll do it. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
