PrimaryReplicaReplicationSQLRouter implements SQLRouter<PrimaryReplicaReplicationRule> { @Override public RouteContext createRouteContext(final LogicSQL logicSQL, final PrimaryReplicaReplicationRule rule, final ConfigurationProperties props) { RouteContext result = new RouteContext(); String dataSourceName = new PrimaryReplicaReplicationDataSourceRouter(rule.getSingleDataSourceRule()).route(logicSQL.getSqlStatementContext().getSqlStatement()); result.getRouteUnits().add(new RouteUnit(new RouteMapper(DefaultSchema.LOGIC_NAME, dataSourceName), Collections.emptyList())); return result; }  @Override RouteContext createRouteContext(final LogicSQL logicSQL, final PrimaryReplicaReplicationRule rule, final ConfigurationProperties props); @Override void decorateRouteContext(final RouteContext routeContext, final LogicSQL logicSQL, final PrimaryReplicaReplicationRule rule, final ConfigurationProperties props); @Override int getOrder(); @Override Class<PrimaryReplicaReplicationRule> getTypeClass();  }
@Test public void assertCreateRouteContextToPrimaryWithoutRouteUnits() { ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(rule), Collections.emptyMap(), mock(ShardingSphereMetaData.class)); LogicSQL logicSQL = new LogicSQL(schema, mock(SQLStatementContext.class), "", Collections.emptyList()); RouteContext actual = sqlRouter.createRouteContext(logicSQL, rule, new ConfigurationProperties(new Properties())); Iterator<String> routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(PRIMARY_DATASOURCE)); }
@Test public void assertCreateRouteContextToReplicaDataSource() { MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement); when(selectStatement.getLock()).thenReturn(Optional.empty()); ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(rule), Collections.emptyMap(), mock(ShardingSphereMetaData.class)); LogicSQL logicSQL = new LogicSQL(schema, sqlStatementContext, "", Collections.emptyList()); RouteContext actual = sqlRouter.createRouteContext(logicSQL, rule, new ConfigurationProperties(new Properties())); Iterator<String> routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(REPLICA_DATASOURCE)); }
@Test public void assertCreateRouteContextToPrimaryDataSourceWithLock() { MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement); when(selectStatement.getLock()).thenReturn(Optional.of(mock(LockSegment.class))); ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(rule), Collections.emptyMap(), mock(ShardingSphereMetaData.class)); LogicSQL logicSQL = new LogicSQL(schema, sqlStatementContext, "", Collections.emptyList()); RouteContext actual = sqlRouter.createRouteContext(logicSQL, rule, new ConfigurationProperties(new Properties())); Iterator<String> routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(PRIMARY_DATASOURCE)); }
@Test public void assertCreateRouteContextToPrimaryDataSource() { when(sqlStatementContext.getSqlStatement()).thenReturn(mock(InsertStatement.class)); ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(rule), Collections.emptyMap(), mock(ShardingSphereMetaData.class)); LogicSQL logicSQL = new LogicSQL(schema, sqlStatementContext, "", Collections.emptyList()); RouteContext actual = sqlRouter.createRouteContext(logicSQL, rule, new ConfigurationProperties(new Properties())); Iterator<String> routedDataSourceNames = actual.getActualDataSourceNames().iterator(); assertThat(routedDataSourceNames.next(), is(PRIMARY_DATASOURCE)); }