gitaroktato commented on a change in pull request #13133:
URL: https://github.com/apache/shardingsphere/pull/13133#discussion_r731683588



##########
File path: 
shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/dml/ShadowUpdateStatementRoutingEngineTest.java
##########
@@ -0,0 +1,161 @@
+/*
+ * 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.UpdateStatementContext;
+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.MySQLUpdateStatement;
+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 ShadowUpdateStatementRoutingEngineTest {
+    
+    private ShadowUpdateStatementRoutingEngine shadowRouteEngine;
+    
+    @Before
+    public void init() {
+        shadowRouteEngine = new 
ShadowUpdateStatementRoutingEngine(createUpdateStatementContext(), 
Collections.emptyList());
+    }
+    
+    private UpdateStatementContext createUpdateStatementContext() {
+        UpdateStatementContext result = mock(UpdateStatementContext.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));
+        MySQLUpdateStatement updateStatement = new MySQLUpdateStatement();
+        Collection<CommentSegment> commentSegments = new LinkedList<>();
+        commentSegments.add(new CommentSegment("/*shadow:true,foo:bar*/", 0, 
20));
+        commentSegments.add(new CommentSegment("/*aaa:bbb*/", 21, 30));
+        updateStatement.setCommentSegments(commentSegments);
+        when(result.getSqlStatement()).thenReturn(updateStatement);
+        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);
+        shadowRouteEngine.route(routeContext, new 
ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()));
+        Optional<Collection<ShadowColumnCondition>> shadowColumnConditions = 
shadowRouteEngine.parseShadowColumnConditions();
+        assertThat(shadowColumnConditions.isPresent(), is(true));

Review comment:
       @TeslaCN - I've added the requested changes in a new commit. 




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to