MergeEngine { public MergedResult merge(final List<QueryResult> queryResults, final SQLStatementContext<?> sqlStatementContext) throws SQLException { Optional<MergedResult> mergedResult = executeMerge(queryResults, sqlStatementContext); Optional<MergedResult> result = mergedResult.isPresent() ? Optional.of(decorate(mergedResult.get(), sqlStatementContext)) : decorate(queryResults.get(0), sqlStatementContext); return result.orElseGet(() -> new TransparentMergedResult(queryResults.get(0))); } MergeEngine(final DatabaseType databaseType, final SchemaMetaData schemaMetaData, final ConfigurationProperties props, final Collection<ShardingSphereRule> rules); MergedResult merge(final List<QueryResult> queryResults, final SQLStatementContext<?> sqlStatementContext);  }
@Test public void assertMergeWithIndependentRule() throws SQLException { when(queryResult.getValue(1, String.class)).thenReturn("test"); MergeEngine mergeEngine = new MergeEngine(databaseType, schemaMetaData, props, Collections.singletonList(new IndependentRuleFixture())); MergedResult actual = mergeEngine.merge(Collections.singletonList(queryResult), sqlStatementContext); assertThat(actual.getValue(1, String.class), is("test")); }
@Test public void assertMergeWithMergerRuleOnly() throws SQLException { MergeEngine mergeEngine = new MergeEngine(databaseType, schemaMetaData, props, Collections.singletonList(new MergerRuleFixture())); MergedResult actual = mergeEngine.merge(Collections.singletonList(queryResult), sqlStatementContext); assertThat(actual.getValue(1, String.class), is("merged_value")); }
@Test public void assertMergeWithDecoratorRuleOnly() throws SQLException { MergeEngine mergeEngine = new MergeEngine(databaseType, schemaMetaData, props, Collections.singletonList(new DecoratorRuleFixture())); MergedResult actual = mergeEngine.merge(Collections.singletonList(queryResult), sqlStatementContext); assertThat(actual.getValue(1, String.class), is("decorated_value")); }
@Test public void assertMergeWithMergerRuleAndDecoratorRuleTogether() throws SQLException { MergeEngine mergeEngine = new MergeEngine(databaseType, schemaMetaData, props, Arrays.asList(new MergerRuleFixture(), new DecoratorRuleFixture())); MergedResult actual = mergeEngine.merge(Collections.singletonList(queryResult), sqlStatementContext); assertThat(actual.getValue(1, String.class), is("decorated_merged_value")); }