Skip to content

Commit

Permalink
Refactor BroadcastRouteEngineFactoryTest (#33506)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 1, 2024
1 parent f4001ba commit 715f884
Showing 1 changed file with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
@StaticMockSettings(SQLStatementContextExtractor.class)
class BroadcastRouteEngineFactoryTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");

@Mock
private BroadcastRule rule;

Expand Down Expand Up @@ -108,16 +106,14 @@ void assertNewInstanceWithCursorAvailableAndIsNotAllBroadcastTables() {
@Test
void assertNewInstanceWithCursorAvailableAndIsAllBroadcastTables() {
CloseStatementContext sqlStatementContext = mock(CloseStatementContext.class, RETURNS_DEEP_STUBS);
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
when(sqlStatementContext.getTablesContext()).thenReturn(createTablesContext("foo_tbl"));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastUnicastRouteEngine.class));
}

@Test
void assertNewInstanceWithCursorAvailableAndNotTableAvailable() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(CursorAvailable.class));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DDLStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastIgnoreRouteEngine.class));
}
Expand Down Expand Up @@ -148,17 +144,15 @@ void assertNewInstanceWithoutTableAvailableStatement() {
@Test
void assertNewInstanceWithEmptyTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(new TablesContext(Collections.emptyList(), databaseType, null));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(SQLStatement.class));
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(createTablesContext());
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastIgnoreRouteEngine.class));
}

@Test
void assertNewInstanceWithDALStatement() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(createTablesContext("foo_tbl"));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DALStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastTableBroadcastRouteEngine.class));
Expand All @@ -167,8 +161,7 @@ void assertNewInstanceWithDALStatement() {
@Test
void assertNewInstanceWithDCLStatementWithoutBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("bar_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(createTablesContext("bar_tbl"));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DCLStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastIgnoreRouteEngine.class));
Expand All @@ -177,8 +170,7 @@ void assertNewInstanceWithDCLStatementWithoutBroadcastTables() {
@Test
void assertNewInstanceWithDCLStatementWithBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(createTablesContext("foo_tbl"));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DCLStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastTableBroadcastRouteEngine.class));
Expand All @@ -187,8 +179,7 @@ void assertNewInstanceWithDCLStatementWithBroadcastTables() {
@Test
void assertNewInstanceWithDMLStatementAndIsNotAllBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("bar_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(createTablesContext("bar_tbl"));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(DMLStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastIgnoreRouteEngine.class));
Expand All @@ -197,8 +188,7 @@ void assertNewInstanceWithDMLStatementAndIsNotAllBroadcastTables() {
@Test
void assertNewInstanceWithSelectStatementAndIsAllBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(createTablesContext("foo_tbl"));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(SelectStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastUnicastRouteEngine.class));
Expand All @@ -207,10 +197,19 @@ void assertNewInstanceWithSelectStatementAndIsAllBroadcastTables() {
@Test
void assertNewInstanceWithUpdateStatementAndIsAllBroadcastTables() {
SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class));
TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl")))), databaseType, null);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(tablesContext);
when(((TableAvailable) sqlStatementContext).getTablesContext()).thenReturn(createTablesContext("foo_tbl"));
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(UpdateStatement.class));
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
assertThat(BroadcastRouteEngineFactory.newInstance(rule, database, queryContext), instanceOf(BroadcastDatabaseBroadcastRouteEngine.class));
}

private TablesContext createTablesContext() {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
return new TablesContext(Collections.emptyList(), databaseType, null);
}

private TablesContext createTablesContext(final String tableName) {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
return new TablesContext(Collections.singleton(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue(tableName)))), databaseType, null);
}
}

0 comments on commit 715f884

Please sign in to comment.