You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/05/07 12:49:58 UTC
[shardingsphere] branch master updated: Add assert for ShadowNonDMLStatementRoutingEngineTest (#17425)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 4938b6db182 Add assert for ShadowNonDMLStatementRoutingEngineTest (#17425)
4938b6db182 is described below
commit 4938b6db1820c399e167f535fa005c54744a788a
Author: gin <ja...@163.com>
AuthorDate: Sat May 7 20:49:40 2022 +0800
Add assert for ShadowNonDMLStatementRoutingEngineTest (#17425)
---
.../impl/ShadowNonDMLStatementRoutingEngine.java | 5 ++---
.../ShadowNonDMLStatementRoutingEngineTest.java | 21 ++++++++++++++++-----
2 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngine.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngine.java
index 083dd0df234..82dbf193544 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngine.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngine.java
@@ -49,15 +49,14 @@ public final class ShadowNonDMLStatementRoutingEngine implements ShadowRouteEngi
}
private Map<String, String> findShadowDataSourceMappings(final ShadowRule shadowRule) {
- Map<String, String> result = new LinkedHashMap<>();
Optional<Collection<String>> sqlComments = parseSQLComments();
if (!sqlComments.isPresent()) {
- return result;
+ return new LinkedHashMap<>();
}
if (isMatchAnyNoteShadowAlgorithms(shadowRule, createShadowDetermineCondition(sqlComments.get()))) {
return shadowRule.getAllShadowDataSourceMappings();
}
- return result;
+ return new LinkedHashMap<>();
}
private Optional<Collection<String>> parseSQLComments() {
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngineTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngineTest.java
index 8a53adb4913..e28cfc522b5 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngineTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/test/java/org/apache/shardingsphere/shadow/route/engine/impl/ShadowNonDMLStatementRoutingEngineTest.java
@@ -34,10 +34,13 @@ import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQ
import org.junit.Before;
import org.junit.Test;
+import java.util.Collection;
import java.util.Collections;
import java.util.Map;
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;
@@ -60,17 +63,25 @@ public final class ShadowNonDMLStatementRoutingEngineTest {
@Test
public void assertRoute() {
- shadowRouteEngine.route(createRouteContext(), new ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()));
- // TODO finish assert
+ RouteContext routeContext = createRouteContext();
+ shadowRouteEngine.route(routeContext, new ShadowRule(createAlgorithmProvidedShadowRuleConfiguration()));
+ Collection<RouteUnit> routeUnits = routeContext.getRouteUnits();
+ RouteMapper dataSourceMapper = routeUnits.iterator().next().getDataSourceMapper();
+ assertThat(dataSourceMapper.getLogicName(), is("logic_db"));
+ assertThat(dataSourceMapper.getActualName(), is("ds_shadow"));
}
private RouteContext createRouteContext() {
- RouteContext result = mock(RouteContext.class);
- when(result.getRouteUnits()).thenReturn(
- Collections.singleton(new RouteUnit(new RouteMapper("ds", "ds"), Collections.singleton(new RouteMapper("t_order", "t_order")))));
+ RouteContext result = new RouteContext();
+ Collection<RouteUnit> routeUnits = result.getRouteUnits();
+ routeUnits.add(createRouteUnit());
return result;
}
+ private RouteUnit createRouteUnit() {
+ return new RouteUnit(new RouteMapper("logic_db", "shadow-data-source"), Collections.singleton(new RouteMapper("t_order", "t_order")));
+ }
+
private AlgorithmProvidedShadowRuleConfiguration createAlgorithmProvidedShadowRuleConfiguration() {
AlgorithmProvidedShadowRuleConfiguration result = new AlgorithmProvidedShadowRuleConfiguration();
result.setDataSources(Collections.singletonMap("shadow-data-source", new ShadowDataSourceConfiguration("ds", "ds_shadow")));