This is an automated email from the ASF dual-hosted git repository.

zhaojinchao 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 2754dce8b0e Add unit test for `ImportDatabaseConfigurationUpdater` 
(#24290)
2754dce8b0e is described below

commit 2754dce8b0e1779b811ccbf628733acc44b83570
Author: Zichao <[email protected]>
AuthorDate: Wed Feb 22 10:45:15 2023 +0800

    Add unit test for `ImportDatabaseConfigurationUpdater` (#24290)
---
 .../ImportDatabaseConfigurationUpdaterTest.java    | 54 +++++++++++++
 .../test/resources/conf/import/config-encrypt.yaml | 59 ++++++++++++++
 .../test/resources/conf/import/config-mask.yaml    | 66 ++++++++++++++++
 .../test/resources/conf/import/config-shadow.yaml  | 90 ++++++++++++++++++++++
 4 files changed, 269 insertions(+)

diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java
index b0ea8958e91..9d21f25e143 100644
--- 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationUpdaterTest.java
@@ -28,7 +28,10 @@ import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSp
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.DatabaseDiscoveryRuleConfigurationImportChecker;
+import 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.EncryptRuleConfigurationImportChecker;
+import 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.MaskRuleConfigurationImportChecker;
 import 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.ReadwriteSplittingRuleConfigurationImportChecker;
+import 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.ShadowRuleConfigurationImportChecker;
 import 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.ShardingRuleConfigurationImportChecker;
 import org.apache.shardingsphere.proxy.backend.util.ProxyContextRestorer;
 import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
@@ -62,6 +65,12 @@ public final class ImportDatabaseConfigurationUpdaterTest 
extends ProxyContextRe
     
     private final String databaseDiscovery = "database_discovery_db";
     
+    private final String encrypt = "encrypt_db";
+    
+    private final String shadow = "shadow_db";
+    
+    private final String mask = "mask_db";
+    
     @Mock
     private DataSourcePropertiesValidateHandler validateHandler;
     
@@ -74,6 +83,15 @@ public final class ImportDatabaseConfigurationUpdaterTest 
extends ProxyContextRe
     @Mock
     private DatabaseDiscoveryRuleConfigurationImportChecker 
databaseDiscoveryRuleConfigurationImportChecker;
     
+    @Mock
+    private EncryptRuleConfigurationImportChecker 
encryptRuleConfigurationImportChecker;
+    
+    @Mock
+    private ShadowRuleConfigurationImportChecker 
shadowRuleConfigurationImportChecker;
+    
+    @Mock
+    private MaskRuleConfigurationImportChecker 
maskRuleConfigurationImportChecker;
+    
     private ImportDatabaseConfigurationUpdater 
importDatabaseConfigurationUpdater;
     
     private final Map<String, String> featureMap = new HashMap<>(3, 1);
@@ -83,6 +101,9 @@ public final class ImportDatabaseConfigurationUpdaterTest 
extends ProxyContextRe
         featureMap.put(sharding, "/conf/import/config-sharding.yaml");
         featureMap.put(readwriteSplitting, 
"/conf/import/config-readwrite-splitting.yaml");
         featureMap.put(databaseDiscovery, 
"/conf/import/config-database-discovery.yaml");
+        featureMap.put(encrypt, "/conf/import/config-encrypt.yaml");
+        featureMap.put(shadow, "/conf/import/config-shadow.yaml");
+        featureMap.put(mask, "/conf/import/config-mask.yaml");
     }
     
     @Test(expected = IllegalStateException.class)
@@ -118,6 +139,39 @@ public final class ImportDatabaseConfigurationUpdaterTest 
extends ProxyContextRe
                 new 
ImportDatabaseConfigurationStatement(Objects.requireNonNull(ImportDatabaseConfigurationUpdaterTest.class.getResource(featureMap.get(databaseDiscovery))).getPath()));
     }
     
+    @Test(expected = IllegalStateException.class)
+    public void assertImportDatabaseExecutorForEncrypt() throws 
ReflectiveOperationException, SQLException {
+        init(encrypt);
+        
Plugins.getMemberAccessor().set(importDatabaseConfigurationUpdater.getClass().getDeclaredField("encryptRuleConfigurationImportChecker"),
+                importDatabaseConfigurationUpdater, 
encryptRuleConfigurationImportChecker);
+        
assertNotNull(ProxyContext.getInstance().getContextManager().getDataSourceMap(encrypt));
+        
assertNotNull(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseDiscovery).getRuleMetaData().getConfigurations());
+        importDatabaseConfigurationUpdater.executeUpdate(encrypt,
+                new 
ImportDatabaseConfigurationStatement(Objects.requireNonNull(ImportDatabaseConfigurationUpdaterTest.class.getResource(featureMap.get(encrypt))).getPath()));
+    }
+    
+    @Test(expected = IllegalStateException.class)
+    public void assertImportDatabaseExecutorForShadow() throws 
ReflectiveOperationException, SQLException {
+        init(shadow);
+        
Plugins.getMemberAccessor().set(importDatabaseConfigurationUpdater.getClass().getDeclaredField("shadowRuleConfigurationImportChecker"),
+                importDatabaseConfigurationUpdater, 
shadowRuleConfigurationImportChecker);
+        
assertNotNull(ProxyContext.getInstance().getContextManager().getDataSourceMap(shadow));
+        
assertNotNull(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(shadow).getRuleMetaData().getConfigurations());
+        importDatabaseConfigurationUpdater.executeUpdate(shadow,
+                new 
ImportDatabaseConfigurationStatement(Objects.requireNonNull(ImportDatabaseConfigurationUpdaterTest.class.getResource(featureMap.get(shadow))).getPath()));
+    }
+    
+    @Test(expected = IllegalStateException.class)
+    public void assertImportDatabaseExecutorForMask() throws 
ReflectiveOperationException, SQLException {
+        init(mask);
+        
Plugins.getMemberAccessor().set(importDatabaseConfigurationUpdater.getClass().getDeclaredField("maskRuleConfigurationImportChecker"),
+                importDatabaseConfigurationUpdater, 
maskRuleConfigurationImportChecker);
+        
assertNotNull(ProxyContext.getInstance().getContextManager().getDataSourceMap(mask));
+        
assertNotNull(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(mask).getRuleMetaData().getConfigurations());
+        importDatabaseConfigurationUpdater.executeUpdate(mask,
+                new 
ImportDatabaseConfigurationStatement(Objects.requireNonNull(ImportDatabaseConfigurationUpdaterTest.class.getResource(featureMap.get(mask))).getPath()));
+    }
+    
     private void init(final String feature) throws 
ReflectiveOperationException {
         importDatabaseConfigurationUpdater = new 
ImportDatabaseConfigurationUpdater();
         
Plugins.getMemberAccessor().set(importDatabaseConfigurationUpdater.getClass().getDeclaredField("validateHandler"),
 importDatabaseConfigurationUpdater, validateHandler);
diff --git 
a/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml 
b/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml
new file mode 100644
index 00000000000..19df047b882
--- /dev/null
+++ b/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml
@@ -0,0 +1,59 @@
+#
+# 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.
+#
+
+databaseName: encrypt_db
+
+dataSources:
+  ds_1:
+    url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
+    username: root
+    password:
+    connectionTimeoutMilliseconds: 30000
+    idleTimeoutMilliseconds: 60000
+    maxLifetimeMilliseconds: 1800000
+    maxPoolSize: 50
+    minPoolSize: 1
+  ds_2:
+    url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
+    username: root
+    password:
+    connectionTimeoutMilliseconds: 30000
+    idleTimeoutMilliseconds: 60000
+    maxLifetimeMilliseconds: 1800000
+    maxPoolSize: 50
+    minPoolSize: 1
+
+rules:
+  - !ENCRYPT
+    encryptors:
+      aes_encryptor:
+        type: AES
+        props:
+          aes-key-value: 123456abc
+      md5_encryptor:
+        type: MD5
+    tables:
+      t_encrypt:
+        columns:
+          user_id:
+            plainColumn: user_plain
+            cipherColumn: user_cipher
+            encryptorName: aes_encryptor
+          order_id:
+            cipherColumn: order_cipher
+            encryptorName: md5_encryptor
+
diff --git a/proxy/backend/core/src/test/resources/conf/import/config-mask.yaml 
b/proxy/backend/core/src/test/resources/conf/import/config-mask.yaml
new file mode 100644
index 00000000000..1bdf594a1c1
--- /dev/null
+++ b/proxy/backend/core/src/test/resources/conf/import/config-mask.yaml
@@ -0,0 +1,66 @@
+#
+# 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.
+#
+
+databaseName: mask_db
+
+dataSources:
+  ds_1:
+    url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
+    username: root
+    password:
+    connectionTimeoutMilliseconds: 30000
+    idleTimeoutMilliseconds: 60000
+    maxLifetimeMilliseconds: 1800000
+    maxPoolSize: 50
+    minPoolSize: 1
+  ds_2:
+    url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
+    username: root
+    password:
+    connectionTimeoutMilliseconds: 30000
+    idleTimeoutMilliseconds: 60000
+    maxLifetimeMilliseconds: 1800000
+    maxPoolSize: 50
+    minPoolSize: 1
+
+rules:
+  - !MASK
+    tables:
+      t_user:
+        columns:
+          password:
+            maskAlgorithm: md5_mask
+          email:
+            maskAlgorithm: mask_before_special_chars_mask
+          telephone:
+            maskAlgorithm: keep_first_n_last_m_mask
+    
+    maskAlgorithms:
+      md5_mask:
+        type: MD5
+      mask_before_special_chars_mask:
+        type: MASK_BEFORE_SPECIAL_CHARS
+        props:
+          special-chars: '@'
+          replace-char: '*'
+      keep_first_n_last_m_mask:
+        type: KEEP_FIRST_N_LAST_M
+        props:
+          first-n: 3
+          last-m: 4
+          replace-char: '*'
+
diff --git 
a/proxy/backend/core/src/test/resources/conf/import/config-shadow.yaml 
b/proxy/backend/core/src/test/resources/conf/import/config-shadow.yaml
new file mode 100644
index 00000000000..203a2128439
--- /dev/null
+++ b/proxy/backend/core/src/test/resources/conf/import/config-shadow.yaml
@@ -0,0 +1,90 @@
+#
+# 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.
+#
+
+databaseName: shadow_db
+
+dataSources:
+  ds_1:
+    url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
+    username: root
+    password:
+    connectionTimeoutMilliseconds: 30000
+    idleTimeoutMilliseconds: 60000
+    maxLifetimeMilliseconds: 1800000
+    maxPoolSize: 50
+    minPoolSize: 1
+  ds_2:
+    url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
+    username: root
+    password:
+    connectionTimeoutMilliseconds: 30000
+    idleTimeoutMilliseconds: 60000
+    maxLifetimeMilliseconds: 1800000
+    maxPoolSize: 50
+    minPoolSize: 1
+
+rules:
+  - !SHADOW
+    dataSources:
+      shadowDataSource:
+        productionDataSourceName: ds
+        shadowDataSourceName: shadow_ds
+    tables:
+      t_order:
+        dataSourceNames:
+          - shadowDataSource
+        shadowAlgorithmNames:
+          - user_id_insert_match_algorithm
+          - user_id_select_match_algorithm
+      t_order_item:
+        dataSourceNames:
+          - shadowDataSource
+        shadowAlgorithmNames:
+          - user_id_insert_match_algorithm
+          - user_id_update_match_algorithm
+          - user_id_select_match_algorithm
+      t_address:
+        dataSourceNames:
+          - shadowDataSource
+        shadowAlgorithmNames:
+          - user_id_insert_match_algorithm
+          - user_id_select_match_algorithm
+          - sql_hint_algorithm
+    shadowAlgorithms:
+      user_id_insert_match_algorithm:
+        type: REGEX_MATCH
+        props:
+          operation: insert
+          column: user_id
+          regex: "[1]"
+      user_id_update_match_algorithm:
+        type: REGEX_MATCH
+        props:
+          operation: update
+          column: user_id
+          regex: "[1]"
+      user_id_select_match_algorithm:
+        type: REGEX_MATCH
+        props:
+          operation: select
+          column: user_id
+          regex: "[1]"
+      sql_hint_algorithm:
+        type: SQL_HINT
+        props:
+          foo: bar
+

Reply via email to