ShadowSQLRouter implements SQLRouter<ShadowRule> { @Override public RouteContext createRouteContext(final LogicSQL logicSQL, final ShadowRule rule, final ConfigurationProperties props) { RouteContext result = new RouteContext(); if (!(logicSQL.getSqlStatementContext().getSqlStatement() instanceof DMLStatement)) { rule.getShadowMappings().forEach((key, value) -> { result.getRouteUnits().add(new RouteUnit(new RouteMapper(key, key), Collections.emptyList())); result.getRouteUnits().add(new RouteUnit(new RouteMapper(value, value), Collections.emptyList())); }); return result; } if (isShadow(logicSQL.getSqlStatementContext(), logicSQL.getParameters(), rule)) { rule.getShadowMappings().values().forEach(each -> result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList()))); } else { rule.getShadowMappings().keySet().forEach(each -> result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList()))); } return result; }  @Override RouteContext createRouteContext(final LogicSQL logicSQL, final ShadowRule rule, final ConfigurationProperties props); @Override void decorateRouteContext(final RouteContext routeContext, final LogicSQL logicSQL, final ShadowRule rule, final ConfigurationProperties props); @Override int getOrder(); @Override Class<ShadowRule> getTypeClass();  }
@Test public void assertCreateRouteContextToShadowDataSource() { ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(rule), Collections.emptyMap(), mock(ShardingSphereMetaData.class)); LogicSQL logicSQL = new LogicSQL(schema, mockSQLStatementContextForShadow(), "", Collections.emptyList()); RouteContext actual = sqlRouter.createRouteContext(logicSQL, rule, new ConfigurationProperties(new Properties())); Iterator<String> routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(SHADOW_DATASOURCE)); }
@Test public void assertCreateRouteContextToActualDataSource() { ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(rule), Collections.emptyMap(), mock(ShardingSphereMetaData.class)); LogicSQL logicSQL = new LogicSQL(schema, mockSQLStatementContext(), "", Collections.emptyList()); RouteContext actual = sqlRouter.createRouteContext(logicSQL, rule, new ConfigurationProperties(new Properties())); Iterator<String> routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(ACTUAL_DATASOURCE)); }
@Test public void assertCreateRouteContextForNonDMLStatement() { ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(rule), Collections.emptyMap(), mock(ShardingSphereMetaData.class)); LogicSQL logicSQL = new LogicSQL(schema, mockNonDMLSQLStatementContext(), "", Collections.emptyList()); RouteContext actual = sqlRouter.createRouteContext(logicSQL, rule, new ConfigurationProperties(new Properties())); assertThat(actual.getRouteUnits().size(), is(2)); assertTrue(actual.getActualDataSourceNames().contains(SHADOW_DATASOURCE)); assertTrue(actual.getActualDataSourceNames().contains(ACTUAL_DATASOURCE)); }