You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/07/05 02:58:12 UTC

[shardingsphere] branch master updated: add unit test for SQLHint (#18827)

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

zhangliang 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 093cacc6dad add unit test for SQLHint (#18827)
093cacc6dad is described below

commit 093cacc6dadc1f54c459cc94af34d60a1c401f1f
Author: natehuang <na...@tencent.com>
AuthorDate: Tue Jul 5 10:58:07 2022 +0800

    add unit test for SQLHint (#18827)
---
 .../ShardingRuleConfigurationYamlSwapperTest.java     | 16 ++++++++++++++++
 .../infra/hint/SQLHintExtractorTest.java              | 19 +++++++++++++++++++
 .../shardingsphere/infra/hint/SQLHintUtilsTest.java   |  9 +++++++++
 3 files changed, 44 insertions(+)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/ShardingRuleConfigurationYamlSwapperTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/ShardingRuleConfigurationYamlSwapperTest.java
index 973cd22a4a0..0f17ed1a6e1 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/ShardingRuleConfigurationYamlSwapperTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/ShardingRuleConfigurationYamlSwapperTest.java
@@ -21,15 +21,18 @@ import org.apache.shardingsphere.infra.yaml.config.swapper.YamlConfigurationSwap
 import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
 import org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
 import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.yaml.config.strategy.audit.YamlShardingAuditStrategyConfiguration;
 import org.apache.shardingsphere.sharding.yaml.config.strategy.keygen.YamlKeyGenerateStrategyConfiguration;
 import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration;
 import org.apache.shardingsphere.sharding.yaml.swapper.ShardingRuleConfigurationYamlSwapper;
 import org.apache.shardingsphere.sharding.yaml.swapper.rule.ShardingTableRuleConfigurationYamlSwapper;
 import org.apache.shardingsphere.sharding.yaml.swapper.strategy.KeyGenerateStrategyConfigurationYamlSwapper;
+import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingAuditStrategyConfigurationYamlSwapper;
 import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingStrategyConfigurationYamlSwapper;
 import org.junit.Before;
 import org.junit.Test;
@@ -62,6 +65,9 @@ public final class ShardingRuleConfigurationYamlSwapperTest {
     
     @Mock
     private KeyGenerateStrategyConfigurationYamlSwapper keyGenerateStrategyConfigurationYamlSwapper;
+
+    @Mock
+    private ShardingAuditStrategyConfigurationYamlSwapper shardingAuditStrategyConfigurationYamlSwapper;
     
     private final ShardingRuleConfigurationYamlSwapper shardingRuleConfigurationYamlSwapper = new ShardingRuleConfigurationYamlSwapper();
     
@@ -76,6 +82,9 @@ public final class ShardingRuleConfigurationYamlSwapperTest {
         setSwapper("keyGenerateStrategyYamlSwapper", keyGenerateStrategyConfigurationYamlSwapper);
         when(keyGenerateStrategyConfigurationYamlSwapper.swapToYamlConfiguration(ArgumentMatchers.any())).thenReturn(mock(YamlKeyGenerateStrategyConfiguration.class));
         when(keyGenerateStrategyConfigurationYamlSwapper.swapToObject(ArgumentMatchers.any())).thenReturn(mock(KeyGenerateStrategyConfiguration.class));
+        setSwapper("auditStrategyYamlSwapper", shardingAuditStrategyConfigurationYamlSwapper);
+        when(shardingAuditStrategyConfigurationYamlSwapper.swapToYamlConfiguration(ArgumentMatchers.any())).thenReturn(mock(YamlShardingAuditStrategyConfiguration.class));
+        when(shardingAuditStrategyConfigurationYamlSwapper.swapToObject(ArgumentMatchers.any())).thenReturn(mock(ShardingAuditStrategyConfiguration.class));
     }
     
     private void setSwapper(final String swapperFieldName, final YamlConfigurationSwapper swapperFieldValue) throws ReflectiveOperationException {
@@ -96,6 +105,7 @@ public final class ShardingRuleConfigurationYamlSwapperTest {
         assertNull(actual.getDefaultTableStrategy());
         assertNull(actual.getDefaultKeyGenerateStrategy());
         assertNull(actual.getDefaultShardingColumn());
+        assertNull(actual.getAuditStrategy());
         assertNull(actual.getScalingName());
         assertTrue(actual.getScaling().isEmpty());
     }
@@ -108,7 +118,9 @@ public final class ShardingRuleConfigurationYamlSwapperTest {
         shardingRuleConfig.getBroadcastTables().add("dict");
         shardingRuleConfig.setDefaultDatabaseShardingStrategy(mock(ShardingStrategyConfiguration.class));
         shardingRuleConfig.setDefaultTableShardingStrategy(mock(ShardingStrategyConfiguration.class));
+        shardingRuleConfig.setDefaultTableShardingStrategy(mock(ShardingStrategyConfiguration.class));
         shardingRuleConfig.setDefaultKeyGenerateStrategy(mock(KeyGenerateStrategyConfiguration.class));
+        shardingRuleConfig.setAuditStrategy(mock(ShardingAuditStrategyConfiguration.class));
         shardingRuleConfig.setDefaultShardingColumn("user_id");
         YamlShardingRuleConfiguration actual = shardingRuleConfigurationYamlSwapper.swapToYamlConfiguration(shardingRuleConfig);
         assertThat(actual.getTables().size(), is(1));
@@ -119,6 +131,7 @@ public final class ShardingRuleConfigurationYamlSwapperTest {
         assertNotNull(actual.getDefaultDatabaseStrategy());
         assertNotNull(actual.getDefaultTableStrategy());
         assertNotNull(actual.getDefaultKeyGenerateStrategy());
+        assertNotNull(actual.getAuditStrategy());
         assertThat(actual.getDefaultShardingColumn(), is("user_id"));
     }
     
@@ -133,6 +146,7 @@ public final class ShardingRuleConfigurationYamlSwapperTest {
         assertNull(actual.getDefaultDatabaseShardingStrategy());
         assertNull(actual.getDefaultTableShardingStrategy());
         assertNull(actual.getDefaultKeyGenerateStrategy());
+        assertNull(actual.getAuditStrategy());
         assertNull(actual.getDefaultShardingColumn());
     }
     
@@ -145,6 +159,7 @@ public final class ShardingRuleConfigurationYamlSwapperTest {
         yamlConfig.setDefaultDatabaseStrategy(mock(YamlShardingStrategyConfiguration.class));
         yamlConfig.setDefaultTableStrategy(mock(YamlShardingStrategyConfiguration.class));
         yamlConfig.setDefaultKeyGenerateStrategy(mock(YamlKeyGenerateStrategyConfiguration.class));
+        yamlConfig.setAuditStrategy(mock(YamlShardingAuditStrategyConfiguration.class));
         yamlConfig.setDefaultShardingColumn("user_id");
         ShardingRuleConfiguration actual = shardingRuleConfigurationYamlSwapper.swapToObject(yamlConfig);
         assertThat(actual.getTables().size(), is(1));
@@ -155,6 +170,7 @@ public final class ShardingRuleConfigurationYamlSwapperTest {
         assertNotNull(actual.getDefaultDatabaseShardingStrategy());
         assertNotNull(actual.getDefaultTableShardingStrategy());
         assertNotNull(actual.getDefaultKeyGenerateStrategy());
+        assertNotNull(actual.getAuditStrategy());
         assertThat(actual.getDefaultShardingColumn(), is("user_id"));
     }
     
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintExtractorTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintExtractorTest.java
index 5e27b72ff7b..b55ee54f485 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintExtractorTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintExtractorTest.java
@@ -21,11 +21,15 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSe
 import org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
 import org.junit.Test;
 
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.hamcrest.CoreMatchers.is;
 
 public final class SQLHintExtractorTest {
     
@@ -36,4 +40,19 @@ public final class SQLHintExtractorTest {
         assertTrue(new SQLHintExtractor(statement).isHintWriteRouteOnly());
     }
     
+    @Test
+    public void assertSQLHintSkipEncryptRewrite() {
+        AbstractSQLStatement statement = mock(AbstractSQLStatement.class);
+        when(statement.getCommentSegments()).thenReturn(Collections.singletonList(new CommentSegment("/* ShardingSphere hint: skipEncryptRewrite=true */", 0, 0)));
+        assertTrue(new SQLHintExtractor(statement).isHintSkipEncryptRewrite());
+    }
+    
+    @Test
+    public void assertSQLHintDisableAuditNames() {
+        AbstractSQLStatement statement = mock(AbstractSQLStatement.class);
+        when(statement.getCommentSegments()).thenReturn(Collections.singletonList(new CommentSegment("/* ShardingSphere hint: disableAuditNames=sharding_audit1 sharding_audit2 */", 0, 0)));
+        Collection<String> actual = new SQLHintExtractor(statement).findDisableAuditNames();
+        assertThat(actual.size(), is(2));
+        assertTrue(actual.containsAll(Arrays.asList("sharding_audit1", "sharding_audit2")));
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java
index 0e4ad5806bd..856f89f6daf 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.infra.hint;
 
 import org.junit.Test;
 
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -54,4 +56,11 @@ public final class SQLHintUtilsTest {
         assertThat(actual.get("tableName"), is("t_order"));
         assertThat(actual.get("columnName"), is("order_id"));
     }
+    
+    @Test
+    public void assertGetSplitterSQLHintValue() {
+        Collection<String> actual = SQLHintUtils.getSplitterSQLHintValue("  sharding_audit1    sharding_audit2 ");
+        assertThat(actual.size(), is(2));
+        assertTrue(actual.containsAll(Arrays.asList("sharding_audit1", "sharding_audit2")));
+    }
 }