diff --git a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/rule/EncryptRuleTest.java b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/rule/EncryptRuleTest.java index 18905b06153473535ff9fc19b9a51db5a65dc326..0efb2c3e499e91ce7b13f5537be65f9de8121022 100644 --- a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/rule/EncryptRuleTest.java +++ b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/rule/EncryptRuleTest.java @@ -28,7 +28,11 @@ import java.util.Collections; import java.util.List; import java.util.Properties; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertThat; public final class EncryptRuleTest { @@ -64,4 +68,64 @@ public final class EncryptRuleTest { assertNull(value); } } + + @Test + public void assertFindEncryptTable() { + assertTrue(new EncryptRule(encryptRuleConfig).findEncryptTable(table).isPresent()); + } + + @Test + public void assertGetLogicColumn() { + assertThat(new EncryptRule(encryptRuleConfig).getLogicColumn(table, "cipher_pwd"), is(column)); + } + + @Test + public void assertFindPlainColumn() { + assertFalse(new EncryptRule(encryptRuleConfig).findPlainColumn(table, "cipher_pwd").isPresent()); + } + + @Test(expected = NullPointerException.class) + public void assertGetCipherColumnWhenNoEncryptColumn() { + new EncryptRule(encryptRuleConfig).getCipherColumn(table, "cipher_pwd"); + } + + @Test + public void assertGetCipherColumnWhenEncryptColumnExist() { + assertThat(new EncryptRule(encryptRuleConfig).getCipherColumn(table, column), is("cipher_pwd")); + } + + @Test + public void assertIsCipherColumn() { + assertTrue(new EncryptRule(encryptRuleConfig).isCipherColumn(table, "cipher_pwd")); + } + + @Test + public void assertFindAssistedQueryColumn() { + assertFalse(new EncryptRule(encryptRuleConfig).findAssistedQueryColumn(table, "cipher_pwd").isPresent()); + } + + @Test + public void assertGetAssistedQueryColumns() { + assertTrue(new EncryptRule(encryptRuleConfig).getAssistedQueryColumns(table).isEmpty()); + } + + @Test + public void assertGetAssistedQueryAndPlainColumns() { + assertFalse(new EncryptRule(encryptRuleConfig).getAssistedQueryAndPlainColumns(table).isEmpty()); + } + + @Test + public void assertGetLogicAndCipherColumns() { + assertFalse(new EncryptRule(encryptRuleConfig).getLogicAndCipherColumns(table).isEmpty()); + } + + @Test + public void assertGetLogicAndPlainColumns() { + assertFalse(new EncryptRule(encryptRuleConfig).getLogicAndPlainColumns(table).isEmpty()); + } + + @Test + public void assertGetEncryptTableNames() { + assertFalse(new EncryptRule(encryptRuleConfig).getEncryptTableNames().isEmpty()); + } } diff --git a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/strategy/encrypt/impl/EncryptColumnTest.java b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/strategy/encrypt/impl/EncryptColumnTest.java new file mode 100644 index 0000000000000000000000000000000000000000..15a3875c19d24befe3ff865dc19e3b29cb36f02a --- /dev/null +++ b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/strategy/encrypt/impl/EncryptColumnTest.java @@ -0,0 +1,36 @@ +/* + * 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.core.strategy.encrypt.impl; + +import org.apache.shardingsphere.core.strategy.encrypt.EncryptColumn; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public final class EncryptColumnTest { + + @Test + public void assertGetAssistedQueryColumn() { + assertTrue(new EncryptColumn("cipherColumn", "assistedQueryColumn", "plainColumn", "encryptor").getAssistedQueryColumn().isPresent()); + } + + @Test + public void assertGetPlainColumn() { + assertTrue(new EncryptColumn("cipherColumn", "assistedQueryColumn", "plainColumn", "encryptor").getPlainColumn().isPresent()); + } +} diff --git a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/strategy/encrypt/impl/EncryptTableTest.java b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/strategy/encrypt/impl/EncryptTableTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7b1746f18aea167d14c74895ccaa970d01675f9f --- /dev/null +++ b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/strategy/encrypt/impl/EncryptTableTest.java @@ -0,0 +1,78 @@ +/* + * 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.core.strategy.encrypt.impl; + +import com.google.common.collect.ImmutableMap; +import org.apache.shardingsphere.api.config.encrypt.EncryptColumnRuleConfiguration; +import org.apache.shardingsphere.api.config.encrypt.EncryptTableRuleConfiguration; +import org.apache.shardingsphere.core.exception.ShardingException; +import org.apache.shardingsphere.core.strategy.encrypt.EncryptTable; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public final class EncryptTableTest { + + private EncryptTable encryptTable; + + @Before + public void setUp() { + EncryptTableRuleConfiguration config = mock(EncryptTableRuleConfiguration.class); + EncryptColumnRuleConfiguration encryptColumnRuleConfiguration = mock(EncryptColumnRuleConfiguration.class); + when(config.getColumns()).thenReturn(ImmutableMap.of("key", encryptColumnRuleConfiguration)); + when(encryptColumnRuleConfiguration.getCipherColumn()).thenReturn("cipherColumn"); + when(encryptColumnRuleConfiguration.getAssistedQueryColumn()).thenReturn("assistedQueryColumn"); + when(encryptColumnRuleConfiguration.getPlainColumn()).thenReturn("plainColumn"); + when(encryptColumnRuleConfiguration.getEncryptor()).thenReturn("encryptor"); + encryptTable = new EncryptTable(config); + } + + @Test + public void assertGetLogicColumn() { + assertNotNull(encryptTable.getLogicColumn("cipherColumn")); + } + + @Test(expected = ShardingException.class) + public void assertGetLogicColumnShardingExceptionThrownWhenCipherColumnAbsent() { + encryptTable.getLogicColumn("___cipherColumn"); + } + + @Test + public void assertGetLogicColumns() { + assertFalse(encryptTable.getLogicColumns().isEmpty()); + } + + @Test + public void assertFindPlainColumn() { + assertFalse(encryptTable.findPlainColumn("logicColumn").isPresent()); + } + + @Test + public void assertGetLogicAndCipherColumns() { + assertFalse(encryptTable.getLogicAndCipherColumns().isEmpty()); + } + + @Test + public void assertGetLogicAndPlainColumns() { + assertFalse(encryptTable.getLogicAndPlainColumns().isEmpty()); + } +}