You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/10/20 02:06:57 UTC

[shardingsphere] branch master updated: Adding the test cases for ShardingSQLRewriteContextDecorator and ShardingParameterRewriterBuilder (#13115)

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

duanzhengqiang 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 ea2117a  Adding the test cases for ShardingSQLRewriteContextDecorator and ShardingParameterRewriterBuilder (#13115)
ea2117a is described below

commit ea2117ae4111995d43ab234a6799e447f0510ff7
Author: Bhupendra Singh Gadwal <60...@users.noreply.github.com>
AuthorDate: Wed Oct 20 07:36:10 2021 +0530

    Adding the test cases for ShardingSQLRewriteContextDecorator and ShardingParameterRewriterBuilder (#13115)
    
    * Adding the test cases for ShardingSQLRewriteContextDecorator and ShardingParameterRewriterBuilder
    
    * Addressing the code review comments
    
    * Adding the assertion using assertThat
    
    Co-authored-by: bgadwal <bh...@intuit.com>
---
 .../ShardingSQLRewriteContextDecoratorTest.java    | 83 ++++++++++++++++++++++
 .../ShardingParameterRewriterBuilderTest.java      | 45 ++++++++++++
 2 files changed, 128 insertions(+)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
new file mode 100644
index 0000000..472ab8e
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.rewrite.context;
+
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.rewrite.context.SQLRewriteContext;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.sharding.constant.ShardingOrder;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class ShardingSQLRewriteContextDecoratorTest {
+
+    @Test
+    public void assertDecorateForRouteContextWhenIsFederated() {
+        ShardingSQLRewriteContextDecorator shardingSQLRewriteContextDecorator = new ShardingSQLRewriteContextDecorator();
+        ShardingRule shardingRule = mock(ShardingRule.class);
+        ConfigurationProperties configurationProperties = mock(ConfigurationProperties.class);
+        SQLRewriteContext sqlRewriteContext = mock(SQLRewriteContext.class);
+        RouteContext routeContext = mock(RouteContext.class);
+        when(routeContext.isFederated()).thenReturn(true);
+        shardingSQLRewriteContextDecorator.decorate(shardingRule, configurationProperties, sqlRewriteContext, routeContext);
+        assertTrue(Objects.isNull(sqlRewriteContext.getSchema()));
+        assertTrue(Objects.isNull(sqlRewriteContext.getSqlStatementContext()));
+        assertThat(sqlRewriteContext.getParameters().size(), is(0));
+        assertTrue(Objects.isNull(sqlRewriteContext.getParameterBuilder()));
+        assertThat(sqlRewriteContext.getSqlTokens().size(), is(0));
+    }
+
+    @Test
+    public void assertDecorateForRouteContextWhenNotFederated() {
+        List<Object> dummy = new ArrayList<>();
+        dummy.add(new Object());
+        ShardingSQLRewriteContextDecorator shardingSQLRewriteContextDecorator = new ShardingSQLRewriteContextDecorator();
+        ShardingRule shardingRule = mock(ShardingRule.class);
+        ConfigurationProperties configurationProperties = mock(ConfigurationProperties.class);
+        SQLRewriteContext sqlRewriteContext = mock(SQLRewriteContext.class);
+        when(sqlRewriteContext.getParameters()).thenReturn(dummy);
+        RouteContext routeContext = mock(RouteContext.class);
+        when(routeContext.isFederated()).thenReturn(false);
+        shardingSQLRewriteContextDecorator.decorate(shardingRule, configurationProperties, sqlRewriteContext, routeContext);
+        assertTrue(Objects.nonNull(sqlRewriteContext.getSqlTokens()));
+    }
+
+    @Test
+    public void assertGetOrder() {
+        ShardingSQLRewriteContextDecorator shardingSQLRewriteContextDecorator = new ShardingSQLRewriteContextDecorator();
+        int actual = shardingSQLRewriteContextDecorator.getOrder();
+        assertThat(actual, is(ShardingOrder.ORDER));
+    }
+
+    @Test
+    public void assertGetTypeClass() {
+        ShardingSQLRewriteContextDecorator shardingSQLRewriteContextDecorator = new ShardingSQLRewriteContextDecorator();
+        Class<ShardingRule> actual = shardingSQLRewriteContextDecorator.getTypeClass();
+        assertThat(actual.getName(), is(ShardingRule.class.getName()));
+    }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilderTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilderTest.java
new file mode 100644
index 0000000..73dfe61
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilderTest.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.sharding.rewrite.parameter;
+
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.rewrite.parameter.rewriter.ParameterRewriter;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.junit.Test;
+
+import java.util.Collection;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertFalse;
+import static org.mockito.Mockito.mock;
+
+public final class ShardingParameterRewriterBuilderTest {
+
+    @Test
+    public void assertGetParameterRewriters() {
+        ShardingSphereSchema shardingSphereSchema = mock(ShardingSphereSchema.class);
+        ShardingRule shardingRule = mock(ShardingRule.class);
+        RouteContext routeContext = mock(RouteContext.class);
+        ShardingParameterRewriterBuilder shardingParameterRewriterBuilder = new ShardingParameterRewriterBuilder(shardingRule, routeContext);
+        Collection<ParameterRewriter> result = shardingParameterRewriterBuilder.getParameterRewriters(shardingSphereSchema);
+        assertFalse(result.isEmpty());
+        assertThat(result.size(), is(2));
+    }
+}