You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2024/10/30 14:17:31 UTC

(shardingsphere) branch master updated: Add test cases on InUsedStorageUnitRetriever's impl (#33470)

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

jianglongtao 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 f08c29cfaff Add test cases on InUsedStorageUnitRetriever's impl (#33470)
f08c29cfaff is described below

commit f08c29cfaffc1759d2ca90670e9c15f025e607f3
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Oct 30 22:17:24 2024 +0800

    Add test cases on InUsedStorageUnitRetriever's impl (#33470)
---
 .../InUsedEncryptStorageUnitRetrieverTest.java     | 45 +++++++++++++++
 .../query/InUsedMaskStorageUnitRetrieverTest.java  | 47 +++++++++++++++
 ...ReadwriteSplittingStorageUnitRetrieverTest.java | 66 ++++++++++++++++++++++
 .../InUsedShadowStorageUnitRetrieverTest.java      | 65 +++++++++++++++++++++
 .../InUsedShardingStorageUnitRetrieverTest.java    | 54 ++++++++++++++++++
 5 files changed, 277 insertions(+)

diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/InUsedEncryptStorageUnitRetrieverTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/InUsedEncryptStorageUnitRetrieverTest.java
new file mode 100644
index 00000000000..64c8b33de46
--- /dev/null
+++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/InUsedEncryptStorageUnitRetrieverTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.distsql.handler.query;
+
+import org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever;
+import org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class InUsedEncryptStorageUnitRetrieverTest {
+    
+    @SuppressWarnings("unchecked")
+    private final InUsedStorageUnitRetriever<EncryptRule> retriever = TypedSPILoader.getService(InUsedStorageUnitRetriever.class, EncryptRule.class);
+    
+    @Test
+    void assertGetInUsedResources() {
+        ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement(null, null);
+        EncryptRule rule = mock(EncryptRule.class);
+        when(rule.getAllTableNames()).thenReturn(Collections.singleton("foo_tbl"));
+        assertThat(retriever.getInUsedResources(sqlStatement, rule), is(Collections.singleton("foo_tbl")));
+    }
+}
diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/InUsedMaskStorageUnitRetrieverTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/InUsedMaskStorageUnitRetrieverTest.java
new file mode 100644
index 00000000000..e1b0e65bc9f
--- /dev/null
+++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/InUsedMaskStorageUnitRetrieverTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.mask.distsql.handler.query;
+
+import org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever;
+import org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.rule.MaskRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class InUsedMaskStorageUnitRetrieverTest {
+    
+    @SuppressWarnings("unchecked")
+    private final InUsedStorageUnitRetriever<MaskRule> retriever = TypedSPILoader.getService(InUsedStorageUnitRetriever.class, MaskRule.class);
+    
+    @Test
+    void assertGetInUsedResources() {
+        ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement(null, null);
+        MaskRule rule = mock(MaskRule.class, RETURNS_DEEP_STUBS);
+        when(rule.getConfiguration().getTables()).thenReturn(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList())));
+        assertThat(retriever.getInUsedResources(sqlStatement, rule), is(Collections.singletonList("foo_tbl")));
+    }
+}
diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetrieverTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetrieverTest.java
new file mode 100644
index 00000000000..1302fdebcf5
--- /dev/null
+++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/InUsedReadwriteSplittingStorageUnitRetrieverTest.java
@@ -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.
+ */
+
+package org.apache.shardingsphere.readwritesplitting.distsql.handler.query;
+
+import org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever;
+import org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
+import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class InUsedReadwriteSplittingStorageUnitRetrieverTest {
+    
+    @SuppressWarnings("unchecked")
+    private final InUsedStorageUnitRetriever<ReadwriteSplittingRule> retriever = TypedSPILoader.getService(InUsedStorageUnitRetriever.class, ReadwriteSplittingRule.class);
+    
+    @Test
+    void assertGetInUsedResourcesWithoutStorageUnit() {
+        ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement(null, null);
+        assertTrue(retriever.getInUsedResources(sqlStatement, mock(ReadwriteSplittingRule.class)).isEmpty());
+    }
+    
+    @Test
+    void assertGetInUsedResourcesWithWriteDataSource() {
+        ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement("foo_unit_write", null);
+        assertThat(retriever.getInUsedResources(sqlStatement, createReadwriteSplittingRule()), is(Collections.singletonList("foo_ds")));
+    }
+    
+    @Test
+    void assertGetInUsedResourcesWithReadDataSource() {
+        ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement("foo_unit_read", null);
+        assertThat(retriever.getInUsedResources(sqlStatement, createReadwriteSplittingRule()), is(Collections.singletonList("foo_ds")));
+    }
+    
+    private static ReadwriteSplittingRule createReadwriteSplittingRule() {
+        ReadwriteSplittingRule result = mock(ReadwriteSplittingRule.class, RETURNS_DEEP_STUBS);
+        ReadwriteSplittingDataSourceGroupRuleConfiguration dataSourceGroupRuleConfig = new ReadwriteSplittingDataSourceGroupRuleConfiguration(
+                "foo_ds", "foo_unit_write", Collections.singletonList("foo_unit_read"), "");
+        when(result.getConfiguration().getDataSourceGroups()).thenReturn(Collections.singleton(dataSourceGroupRuleConfig));
+        return result;
+    }
+}
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java
new file mode 100644
index 00000000000..aec1a3b24b2
--- /dev/null
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/handler/query/InUsedShadowStorageUnitRetrieverTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.shadow.distsql.handler.query;
+
+import org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever;
+import org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class InUsedShadowStorageUnitRetrieverTest {
+    
+    @SuppressWarnings("unchecked")
+    private final InUsedStorageUnitRetriever<ShadowRule> retriever = TypedSPILoader.getService(InUsedStorageUnitRetriever.class, ShadowRule.class);
+    
+    @Test
+    void assertGetInUsedResourcesWithoutStorageUnit() {
+        ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement(null, null);
+        assertTrue(retriever.getInUsedResources(sqlStatement, mock(ShadowRule.class)).isEmpty());
+    }
+    
+    @Test
+    void assertGetInUsedResourcesWithShadowDataSource() {
+        ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement("prod_ds", null);
+        assertThat(retriever.getInUsedResources(sqlStatement, createShadowRule()), is(Collections.singletonList("foo_ds")));
+    }
+    
+    @Test
+    void assertGetInUsedResourcesWithProductionDataSource() {
+        ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement("shadow_ds", null);
+        assertThat(retriever.getInUsedResources(sqlStatement, createShadowRule()), is(Collections.singletonList("foo_ds")));
+    }
+    
+    private static ShadowRule createShadowRule() {
+        ShadowRule result = mock(ShadowRule.class, RETURNS_DEEP_STUBS);
+        ShadowDataSourceConfiguration dataSourceConfig = new ShadowDataSourceConfiguration("foo_ds", "prod_ds", "shadow_ds");
+        when(result.getConfiguration().getDataSources()).thenReturn(Collections.singleton(dataSourceConfig));
+        return result;
+    }
+}
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/query/InUsedShardingStorageUnitRetrieverTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/query/InUsedShardingStorageUnitRetrieverTest.java
new file mode 100644
index 00000000000..6fc96351710
--- /dev/null
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/handler/query/InUsedShardingStorageUnitRetrieverTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.sharding.distsql.handler.query;
+
+import org.apache.shardingsphere.distsql.handler.executor.rql.resource.InUsedStorageUnitRetriever;
+import org.apache.shardingsphere.distsql.statement.rql.rule.database.ShowRulesUsedStorageUnitStatement;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class InUsedShardingStorageUnitRetrieverTest {
+    
+    @SuppressWarnings("unchecked")
+    private final InUsedStorageUnitRetriever<ShardingRule> retriever = TypedSPILoader.getService(InUsedStorageUnitRetriever.class, ShardingRule.class);
+    
+    @Test
+    void assertGetInUsedResources() {
+        ShowRulesUsedStorageUnitStatement sqlStatement = new ShowRulesUsedStorageUnitStatement("prod_ds", null);
+        assertThat(retriever.getInUsedResources(sqlStatement, createShardingRule()), is(Arrays.asList("foo_auto_tbl", "foo_tbl")));
+    }
+    
+    private static ShardingRule createShardingRule() {
+        ShardingRule result = mock(ShardingRule.class, RETURNS_DEEP_STUBS);
+        when(result.getConfiguration().getAutoTables()).thenReturn(Collections.singleton(new ShardingAutoTableRuleConfiguration("foo_auto_tbl", "")));
+        when(result.getConfiguration().getTables()).thenReturn(Collections.singleton(new ShardingTableRuleConfiguration("foo_tbl", "")));
+        return result;
+    }
+}