Skip to content

Commit 300637d

Browse files
authored
Add test cases on EncryptRule.findQueryEncryptor (#32266)
* Add test cases on EncryptColumn * Add test cases on EncryptTable * Add test cases on EncryptRule
1 parent eba8773 commit 300637d

File tree

3 files changed

+77
-20
lines changed

3 files changed

+77
-20
lines changed

features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java

+14-18
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
import java.util.Collections;
3636
import java.util.HashMap;
3737
import java.util.Map;
38+
import java.util.Optional;
3839
import java.util.Properties;
3940
import java.util.stream.Stream;
4041

4142
import static org.hamcrest.CoreMatchers.is;
4243
import static org.hamcrest.MatcherAssert.assertThat;
44+
import static org.junit.jupiter.api.Assertions.assertFalse;
4345
import static org.junit.jupiter.api.Assertions.assertThrows;
4446
import static org.junit.jupiter.api.Assertions.assertTrue;
4547

@@ -73,24 +75,6 @@ private EncryptRuleConfiguration createEncryptRuleConfiguration() {
7375
new AlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties()), new AlgorithmConfiguration("CORE.QUERY_LIKE.FIXTURE", new Properties())));
7476
}
7577

76-
@Test
77-
void assertAssistedQueryEncryptorNameSpecified() {
78-
EncryptColumnRuleConfiguration pwdColumnConfig =
79-
new EncryptColumnRuleConfiguration("pwd", new EncryptColumnItemRuleConfiguration("pwd_cipher", "standard_encryptor"));
80-
pwdColumnConfig.setAssistedQuery(new EncryptColumnItemRuleConfiguration("pwd_assist", "assisted_query_test_encryptor"));
81-
assertTrue(pwdColumnConfig.getAssistedQuery().isPresent());
82-
assertThat(pwdColumnConfig.getAssistedQuery().get().getEncryptorName(), is("assisted_query_test_encryptor"));
83-
}
84-
85-
@Test
86-
void assertLikeQueryEncryptorNameSpecified() {
87-
EncryptColumnRuleConfiguration pwdColumnConfig =
88-
new EncryptColumnRuleConfiguration("pwd", new EncryptColumnItemRuleConfiguration("pwd_cipher", "standard_encryptor"));
89-
pwdColumnConfig.setLikeQuery(new EncryptColumnItemRuleConfiguration("pwd_like", "like_query_test_encryptor"));
90-
assertTrue(pwdColumnConfig.getLikeQuery().isPresent());
91-
assertThat(pwdColumnConfig.getLikeQuery().get().getEncryptorName(), is("like_query_test_encryptor"));
92-
}
93-
9478
private Map<String, AlgorithmConfiguration> getEncryptors(final AlgorithmConfiguration standardEncryptConfig, final AlgorithmConfiguration queryAssistedEncryptConfig,
9579
final AlgorithmConfiguration queryLikeEncryptConfig) {
9680
Map<String, AlgorithmConfiguration> result = new HashMap<>(3, 1F);
@@ -100,6 +84,18 @@ private Map<String, AlgorithmConfiguration> getEncryptors(final AlgorithmConfigu
10084
return result;
10185
}
10286

87+
@Test
88+
void assertFindQueryEncryptor() {
89+
EncryptRule encryptRule = new EncryptRule("foo_db", createEncryptRuleConfiguration());
90+
assertThat(encryptRule.findQueryEncryptor("t_encrypt", "credit_card"),
91+
is(Optional.of(encryptRule.getEncryptTable("t_encrypt").getEncryptColumn("credit_card").getCipher().getEncryptor())));
92+
}
93+
94+
@Test
95+
void assertNotFindQueryEncryptor() {
96+
assertFalse(new EncryptRule("foo_db", createEncryptRuleConfiguration()).findQueryEncryptor("t_encrypt", "invalid_col").isPresent());
97+
}
98+
10399
@SuppressWarnings("unused")
104100
@ParameterizedTest(name = "Wrong{0}")
105101
@ArgumentsSource(TestCaseArgumentsProvider.class)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.encrypt.rule.column;
19+
20+
import org.apache.shardingsphere.encrypt.rule.column.item.AssistedQueryColumnItem;
21+
import org.apache.shardingsphere.encrypt.rule.column.item.CipherColumnItem;
22+
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.hamcrest.CoreMatchers.is;
26+
import static org.hamcrest.MatcherAssert.assertThat;
27+
import static org.mockito.Mockito.mock;
28+
29+
class EncryptColumnTest {
30+
31+
@Test
32+
void assertGetQueryEncryptorWithoutAssistedQuery() {
33+
EncryptAlgorithm cipherAlgorithm = mock(EncryptAlgorithm.class);
34+
assertThat(new EncryptColumn("foo_tbl", new CipherColumnItem("foo_col", cipherAlgorithm)).getQueryEncryptor(), is(cipherAlgorithm));
35+
}
36+
37+
@Test
38+
void assertGetQueryEncryptorWithAssistedQuery() {
39+
EncryptColumn encryptColumn = new EncryptColumn("foo_tbl", new CipherColumnItem("foo_cipher_col", mock(EncryptAlgorithm.class)));
40+
EncryptAlgorithm assistedQueryAlgorithm = mock(EncryptAlgorithm.class);
41+
encryptColumn.setAssistedQuery(new AssistedQueryColumnItem("foo_assisted_query_col", assistedQueryAlgorithm));
42+
assertThat(encryptColumn.getQueryEncryptor(), is(assistedQueryAlgorithm));
43+
}
44+
}

features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/table/EncryptTableTest.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import org.junit.jupiter.api.Test;
2727

2828
import java.util.Collections;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
import java.util.Optional;
2932

3033
import static org.hamcrest.CoreMatchers.is;
3134
import static org.hamcrest.MatcherAssert.assertThat;
@@ -44,8 +47,11 @@ void setUp() {
4447
EncryptColumnRuleConfiguration columnRuleConfig = new EncryptColumnRuleConfiguration("logicColumn", new EncryptColumnItemRuleConfiguration("cipherColumn", "myEncryptor"));
4548
columnRuleConfig.setAssistedQuery(new EncryptColumnItemRuleConfiguration("assistedQueryColumn", "foo_assist_query_encryptor"));
4649
columnRuleConfig.setLikeQuery(new EncryptColumnItemRuleConfiguration("likeQueryColumn", "foo_like_encryptor"));
47-
encryptTable = new EncryptTable(new EncryptTableRuleConfiguration("t_encrypt",
48-
Collections.singleton(columnRuleConfig)), Collections.singletonMap("myEncryptor", mock(EncryptAlgorithm.class)));
50+
Map<String, EncryptAlgorithm> encryptors = new HashMap<>(3, 1F);
51+
encryptors.put("myEncryptor", mock(EncryptAlgorithm.class));
52+
encryptors.put("foo_assist_query_encryptor", mock(EncryptAlgorithm.class));
53+
encryptors.put("foo_like_encryptor", mock(EncryptAlgorithm.class));
54+
encryptTable = new EncryptTable(new EncryptTableRuleConfiguration("t_encrypt", Collections.singleton(columnRuleConfig)), encryptors);
4955
}
5056

5157
@Test
@@ -92,4 +98,15 @@ void assertGetEncryptColumn() {
9298
void assertGetLogicColumnByCipherColumnWhenNotFind() {
9399
assertThrows(EncryptLogicColumnNotFoundException.class, () -> encryptTable.getLogicColumnByCipherColumn("invalidColumn"));
94100
}
101+
102+
@Test
103+
void assertFindQueryEncryptor() {
104+
assertTrue(encryptTable.getEncryptColumn("logicColumn").getAssistedQuery().isPresent());
105+
assertThat(encryptTable.findQueryEncryptor("logicColumn"), is(Optional.of(encryptTable.getEncryptColumn("logicColumn").getAssistedQuery().get().getEncryptor())));
106+
}
107+
108+
@Test
109+
void assertFindQueryEncryptorWithNotEncryptColumn() {
110+
assertThat(encryptTable.findQueryEncryptor("invalidColumn"), is(Optional.empty()));
111+
}
95112
}

0 commit comments

Comments
 (0)