You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ki...@apache.org on 2020/08/15 15:12:04 UTC
[shardingsphere] branch master updated: Add test cases for
EncryptResultDecoratorEngine and EncryptMergedResult (#6862)
This is an automated email from the ASF dual-hosted git repository.
kimmking 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 c66bbc4 Add test cases for EncryptResultDecoratorEngine and EncryptMergedResult (#6862)
c66bbc4 is described below
commit c66bbc4674408075a55d5f269d72924e986e2662
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Aug 15 23:11:48 2020 +0800
Add test cases for EncryptResultDecoratorEngine and EncryptMergedResult (#6862)
* Add EncryptResultDecoratorEngineTest
* Add EncryptMergedResultTest
---
.../merge/EncryptResultDecoratorEngineTest.java | 96 ++++++++++++++++++++
.../encrypt/merge/dql/EncryptMergedResultTest.java | 101 +++++++++++++++++++++
2 files changed, 197 insertions(+)
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-merge/src/test/java/org/apache/shardingsphere/encrypt/merge/EncryptResultDecoratorEngineTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-merge/src/test/java/org/apache/shardingsphere/encrypt/merge/EncryptResultDecoratorEngineTest.java
new file mode 100644
index 0000000..dbc92a6
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-merge/src/test/java/org/apache/shardingsphere/encrypt/merge/EncryptResultDecoratorEngineTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.merge;
+
+import org.apache.shardingsphere.encrypt.merge.dal.EncryptDALResultDecorator;
+import org.apache.shardingsphere.encrypt.merge.dql.EncryptDQLResultDecorator;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.merge.engine.ResultProcessEngine;
+import org.apache.shardingsphere.infra.merge.engine.decorator.ResultDecorator;
+import org.apache.shardingsphere.infra.merge.engine.decorator.impl.TransparentResultDecorator;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry;
+import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.dal.DescribeStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.sql.parser.sql.statement.dal.dialect.mysql.DescribeStatement;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class EncryptResultDecoratorEngineTest {
+
+ static {
+ ShardingSphereServiceLoader.register(ResultProcessEngine.class);
+ }
+
+ @Mock
+ private EncryptRule rule;
+
+ @Mock
+ private DatabaseType databaseType;
+
+ @Mock
+ private SchemaMetaData schemaMetaData;
+
+ @Mock
+ private ConfigurationProperties props;
+
+ @Before
+ public void setUp() {
+ when(props.getValue(ConfigurationPropertyKey.QUERY_WITH_CIPHER_COLUMN)).thenReturn(true);
+ }
+
+ @Test
+ public void assertNewInstanceWithSelectStatement() {
+ EncryptResultDecoratorEngine engine = (EncryptResultDecoratorEngine) OrderedSPIRegistry.getRegisteredServices(Collections.singleton(rule), ResultProcessEngine.class).get(rule);
+ ResultDecorator actual = engine.newInstance(databaseType, schemaMetaData, rule, props, mock(SelectStatementContext.class));
+ assertThat(actual, instanceOf(EncryptDQLResultDecorator.class));
+ }
+
+ @Test
+ public void assertNewInstanceWithDALStatement() {
+ SQLStatementContext<DescribeStatement> sqlStatementContext = mock(DescribeStatementContext.class);
+ when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DescribeStatement.class));
+ EncryptResultDecoratorEngine engine = (EncryptResultDecoratorEngine) OrderedSPIRegistry.getRegisteredServices(Collections.singleton(rule), ResultProcessEngine.class).get(rule);
+ ResultDecorator actual = engine.newInstance(databaseType, schemaMetaData, rule, props, sqlStatementContext);
+ assertThat(actual, instanceOf(EncryptDALResultDecorator.class));
+ }
+
+ @Test
+ public void assertNewInstanceWithOtherStatement() {
+ EncryptResultDecoratorEngine engine = (EncryptResultDecoratorEngine) OrderedSPIRegistry.getRegisteredServices(Collections.singleton(rule), ResultProcessEngine.class).get(rule);
+ ResultDecorator actual = engine.newInstance(databaseType, schemaMetaData, rule, props, mock(InsertStatementContext.class));
+ assertThat(actual, instanceOf(TransparentResultDecorator.class));
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-merge/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-merge/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java
new file mode 100644
index 0000000..57bb741
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-merge/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.merge.dql;
+
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.io.InputStream;
+import java.sql.SQLException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class EncryptMergedResultTest {
+
+ @Mock
+ private EncryptAlgorithmMetaData metaData;
+
+ @Mock
+ private MergedResult mergedResult;
+
+ @Test
+ public void assertNext() throws SQLException {
+ assertFalse(new EncryptMergedResult(metaData, mergedResult, true).next());
+ }
+
+ @Test
+ public void assertGetValueWithQueryWithPlainColumn() throws SQLException {
+ when(mergedResult.getValue(1, String.class)).thenReturn("VALUE");
+ assertThat(new EncryptMergedResult(metaData, mergedResult, false).getValue(1, String.class), is("VALUE"));
+ }
+
+ @Test
+ public void assertGetValueWithQueryWithCipherColumnAndMismatchedEncryptor() throws SQLException {
+ when(mergedResult.getValue(1, String.class)).thenReturn("VALUE");
+ when(metaData.findEncryptor(1)).thenReturn(Optional.empty());
+ assertThat(new EncryptMergedResult(metaData, mergedResult, true).getValue(1, String.class), is("VALUE"));
+ }
+
+ @Test
+ public void assertGetValueWithQueryWithCipherColumnAndMatchedEncryptorWithNotNullCiphertext() throws SQLException {
+ when(mergedResult.getValue(1, String.class)).thenReturn("VALUE");
+ EncryptAlgorithm encryptAlgorithm = mock(EncryptAlgorithm.class);
+ when(encryptAlgorithm.decrypt("VALUE")).thenReturn("ORIGINAL_VALUE");
+ when(metaData.findEncryptor(1)).thenReturn(Optional.of(encryptAlgorithm));
+ assertThat(new EncryptMergedResult(metaData, mergedResult, true).getValue(1, String.class), is("ORIGINAL_VALUE"));
+ }
+
+ @Test
+ public void assertGetValueWithQueryWithCipherColumnAndMatchedEncryptorWithNullCiphertext() throws SQLException {
+ EncryptAlgorithm encryptAlgorithm = mock(EncryptAlgorithm.class);
+ when(metaData.findEncryptor(1)).thenReturn(Optional.of(encryptAlgorithm));
+ assertNull(new EncryptMergedResult(metaData, mergedResult, true).getValue(1, String.class));
+ }
+
+ @Test
+ public void assertGetCalendarValue() throws SQLException {
+ Calendar calendar = Calendar.getInstance();
+ when(mergedResult.getCalendarValue(1, Date.class, calendar)).thenReturn(new Date(0L));
+ assertThat(new EncryptMergedResult(metaData, mergedResult, true).getCalendarValue(1, Date.class, calendar), is(new Date(0L)));
+ }
+
+ @Test
+ public void assertGetInputStream() throws SQLException {
+ InputStream inputStream = mock(InputStream.class);
+ when(mergedResult.getInputStream(1, "asc")).thenReturn(inputStream);
+ assertThat(new EncryptMergedResult(metaData, mergedResult, true).getInputStream(1, "asc"), is(inputStream));
+ }
+
+ @Test
+ public void assertWasNull() throws SQLException {
+ assertFalse(new EncryptMergedResult(metaData, mergedResult, true).wasNull());
+ }
+}