You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/10/12 02:19:22 UTC

[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #12932: Add ut for ShadowDeleteStatementRoutingEngine in shadow project

strongduanmu commented on a change in pull request #12932:
URL: https://github.com/apache/shardingsphere/pull/12932#discussion_r726709224



##########
File path: shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowDeleteStatementRoutingEngineTest.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.route.engine.dml;
+
+import org.apache.shardingsphere.infra.binder.statement.dml.DeleteStatementContext;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.shadow.algorithm.config.AlgorithmProvidedShadowRuleConfiguration;
+import org.apache.shardingsphere.shadow.algorithm.shadow.column.ColumnRegexMatchShadowAlgorithm;
+import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.api.shadow.ShadowOperationType;
+import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.CommentSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLDeleteStatement;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class ShadowDeleteStatementRoutingEngineTest {
+
+    private ShadowDeleteStatementRoutingEngine shadowDeleteStatementRoutingEngine;
+
+    @Before
+    public void init() {
+        shadowDeleteStatementRoutingEngine = new ShadowDeleteStatementRoutingEngine(createDeleteStatementContext(), Collections.emptyList());
+    }
+
+    private DeleteStatementContext createDeleteStatementContext() {
+        DeleteStatementContext result = mock(DeleteStatementContext.class);
+        Collection<SimpleTableSegment> allTables = new LinkedList<>();
+        allTables.add(new SimpleTableSegment(new TableNameSegment(20, 25, new IdentifierValue("t_order"))));
+        when(result.getAllTables()).thenReturn(allTables);
+        BinaryOperationExpression binaryOperationExpression = mock(BinaryOperationExpression.class);
+        when(binaryOperationExpression.getLeft()).thenReturn(new ColumnSegment(0, 0, new IdentifierValue("user_id")));
+        when(binaryOperationExpression.getRight()).thenReturn(new LiteralExpressionSegment(0, 0, "1"));
+        WhereSegment whereSegment = new WhereSegment(0, 0, binaryOperationExpression);
+        when(result.getWhere()).thenReturn(Optional.of(whereSegment));
+        MySQLDeleteStatement mySQLDeleteStatement = new MySQLDeleteStatement();
+        Collection<CommentSegment> commentSegments = new LinkedList<>();
+        commentSegments.add(new CommentSegment("/*shadow:true,foo:bar*/", 0, 20));
+        commentSegments.add(new CommentSegment("/*aaa:bbb*/", 21, 30));
+        mySQLDeleteStatement.setCommentSegments(commentSegments);
+        when(result.getSqlStatement()).thenReturn(mySQLDeleteStatement);
+        return result;
+    }
+
+    @Test
+    public void assertRouteAndParseShadowColumnConditions() {
+        RouteContext routeContext = mock(RouteContext.class);
+        Collection<RouteUnit> routeUnits = new LinkedList<>();
+        routeUnits.add(new RouteUnit(new RouteMapper("ds", "ds_shadow"), new LinkedList<>()));
+        when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+        shadowDeleteStatementRoutingEngine.route(routeContext, new ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()));
+        Optional<Collection<ShadowColumnCondition>> shadowColumnConditions = shadowDeleteStatementRoutingEngine.parseShadowColumnConditions();
+        assertThat(shadowColumnConditions.isPresent(), is(true));
+        Collection<ShadowColumnCondition> shadowColumns = shadowColumnConditions.get();
+        assertThat(shadowColumns.size(), is(1));
+        Iterator<ShadowColumnCondition> iterator = shadowColumns.iterator();
+        ShadowColumnCondition userId = iterator.next();
+        assertThat(userId.getColumn(), is("user_id"));
+        assertThat(userId.getTable(), is("t_order"));
+        assertThat(userId.getValues().iterator().next(), is("1"));
+        Optional<Collection<String>> sqlNotes = shadowDeleteStatementRoutingEngine.parseSqlNotes();
+        assertThat(sqlNotes.isPresent(), is(true));
+        assertThat(sqlNotes.get().size(), is(2));
+        Iterator<String> sqlNotesIt = sqlNotes.get().iterator();
+        assertThat(sqlNotesIt.next(), is("/*shadow:true,foo:bar*/"));
+        assertThat(sqlNotesIt.next(), is("/*aaa:bbb*/"));
+    }
+
+    private AlgorithmProvidedShadowRuleConfiguration createAlgorithmProvidedShadowRuleConfiguration() {
+        AlgorithmProvidedShadowRuleConfiguration result = new AlgorithmProvidedShadowRuleConfiguration();
+        result.setEnable(true);
+        result.setDataSources(createDataSources());
+        result.setTables(createTables());
+        result.setShadowAlgorithms(createShadowAlgorithms());
+        return result;
+    }
+
+    private Map<String, ShadowAlgorithm> createShadowAlgorithms() {
+        Map<String, ShadowAlgorithm> result = new LinkedHashMap<>();
+        result.put("user-id-delete-regex-algorithm", createColumnShadowAlgorithm());
+        return result;
+    }
+
+    private ShadowAlgorithm createColumnShadowAlgorithm() {
+        final ColumnRegexMatchShadowAlgorithm columnRegexMatchShadowAlgorithm = new ColumnRegexMatchShadowAlgorithm();

Review comment:
       @LeeGuoPing Can we remove this final modifier?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org