TableScanStrategy extends ScanStrategy { @Override public DataContainer process(DataSource dataSource, Map<String, String> tableAliases) { return fetchData(dataSource, tableAliases); }  @Override DataContainer process(DataSource dataSource, Map<String, String> tableAliases);  }
@Test public void testSimpleFetchFromTable() throws Exception { DataSource dataSource = new DataSource(INSTANCES_ALIAS); DataSourceStrategy strategy = getStrategy(); DataContainer dataContainer = strategy.process(dataSource, Collections.singletonMap(INSTANCES_ALIAS, INSTANCES_TABLE)); assertThat(dataContainer.getColumnsMap(), equalTo(INSTANCES_COLUMNS_MAP)); List<DataRow> rows = dataContainer.getRows(); assertThat(rows.size(), equalTo(5)); assertThat(rowsAsValues(rows), equalTo(INSTANCES_DATA)); }
@Test(expected = IllegalArgumentException.class) public void testMissingTable() throws Exception { DataSource dataSource = new DataSource("missing"); DataSourceStrategy strategy = getStrategy(); strategy.process(dataSource, Collections.emptyMap()); }
@Test public void testCrossJoin() throws Exception { DataSource dataSource = prepareJoinDataSource(ds -> { ds.setJoinType(JoinType.INNER); return ds; }); DataSourceStrategy strategy = getStrategy(); DataContainer dataContainer = strategy.process(dataSource, TWO_TABLE_ALIASES); doCommonAssertions(dataContainer, 15); List<DataRow> rows = dataContainer.getRows(); List<List<Object>> correctData = Arrays.asList( Arrays.asList("1", "first", "2", "1", "first_project"), Arrays.asList("2", "second", "1", "1", "first_project"), Arrays.asList("3", "third", "2", "1", "first_project"), Arrays.asList("4", "third", "3", "1", "first_project"), Arrays.asList("5", "fifth", "2", "1", "first_project"), Arrays.asList("1", "first", "2", "2", "second_project"), Arrays.asList("2", "second", "1", "2", "second_project"), Arrays.asList("3", "third", "2", "2", "second_project"), Arrays.asList("4", "third", "3", "2", "second_project"), Arrays.asList("5", "fifth", "2", "2", "second_project"), Arrays.asList("1", "first", "2", "3", "third_project"), Arrays.asList("2", "second", "1", "3", "third_project"), Arrays.asList("3", "third", "2", "3", "third_project"), Arrays.asList("4", "third", "3", "3", "third_project"), Arrays.asList("5", "fifth", "2", "3", "third_project") ); assertThat(rowsAsValues(rows), equalTo(correctData)); }
@Test public void testInnerJoinByCondition() throws Exception { DataSource dataSource = prepareJoinDataSource(ds -> { ds.setJoinType(JoinType.INNER); ds.setCondition(prepareJoinCondition()); return ds; }); DataSourceStrategy strategy = getStrategy(); DataContainer dataContainer = strategy.process(dataSource, TWO_TABLE_ALIASES); doCommonAssertions(dataContainer, 5); List<DataRow> rows = dataContainer.getRows(); List<List<Object>> correctData = Arrays.asList( Arrays.asList("2", "second", "1", "1", "first_project"), Arrays.asList("1", "first", "2", "2", "second_project"), Arrays.asList("3", "third", "2", "2", "second_project"), Arrays.asList("5", "fifth", "2", "2", "second_project"), Arrays.asList("4", "third", "3", "3", "third_project") ); assertThat(rowsAsValues(rows), equalTo(correctData)); }
@Test public void testInnerJoinByColumns() throws Exception { DataSource dataSource = prepareJoinDataSource(ds -> { ds.setJoinType(JoinType.INNER); ds.getColumns().addAll(Collections.singletonList(ID)); return ds; }); DataSourceStrategy strategy = getStrategy(); DataContainer dataContainer = strategy.process(dataSource, TWO_TABLE_ALIASES); doCommonAssertions(dataContainer, 3); List<DataRow> rows = dataContainer.getRows(); List<List<Object>> correctData = Arrays.asList( Arrays.asList("1", "first", "2", "1", "first_project"), Arrays.asList("2", "second", "1", "2", "second_project"), Arrays.asList("3", "third", "2", "3", "third_project") ); assertThat(rowsAsValues(rows), equalTo(correctData)); }
@Test public void testNaturalInnerJoin() throws Exception { DataSource dataSource = prepareJoinDataSource(ds -> { ds.setJoinType(JoinType.INNER); ds.setNaturalJoin(true); return ds; }); DataSourceStrategy strategy = getStrategy(); DataContainer dataContainer = strategy.process(dataSource, TWO_TABLE_ALIASES); doCommonAssertions(dataContainer, 3); List<DataRow> rows = dataContainer.getRows(); List<List<Object>> correctData = Arrays.asList( Arrays.asList("1", "first", "2", "1", "first_project"), Arrays.asList("2", "second", "1", "2", "second_project"), Arrays.asList("3", "third", "2", "3", "third_project") ); assertThat(rowsAsValues(rows), equalTo(correctData)); }
@Test public void testLeftJoinByCondition() throws Exception { dataFetcher.addDataRow( INSTANCES_TABLE, Arrays.asList("6", "sixth", "4") ); DataSource dataSource = prepareJoinDataSource(ds -> { ds.setJoinType(JoinType.LEFT); ds.setCondition(prepareJoinCondition()); return ds; }); DataSourceStrategy strategy = getStrategy(); DataContainer dataContainer = strategy.process(dataSource, TWO_TABLE_ALIASES); doCommonAssertions(dataContainer, 6); List<DataRow> rows = dataContainer.getRows(); List<List<Object>> correctData = Arrays.asList( Arrays.asList("2", "second", "1", "1", "first_project"), Arrays.asList("1", "first", "2", "2", "second_project"), Arrays.asList("3", "third", "2", "2", "second_project"), Arrays.asList("5", "fifth", "2", "2", "second_project"), Arrays.asList("4", "third", "3", "3", "third_project"), Arrays.asList("6", "sixth", "4", null, null) ); assertThat(rowsAsValues(rows), equalTo(correctData)); }
@Test public void testLeftJoinByColumns() throws Exception { DataSource dataSource = prepareJoinDataSource(ds -> { ds.setJoinType(JoinType.LEFT); ds.getColumns().addAll(Collections.singletonList(ID)); return ds; }); DataSourceStrategy strategy = getStrategy(); DataContainer dataContainer = strategy.process(dataSource, TWO_TABLE_ALIASES); assertNaturalLeftJoinResults(dataContainer); }
@Test public void testRightJoinByCondition() throws Exception { dataFetcher.addDataRow( PROJECTS_TABLE, Arrays.asList("5", "fifth_project") ); DataSource dataSource = prepareJoinDataSource(ds -> { ds.setJoinType(JoinType.RIGHT); ds.setCondition(prepareJoinCondition()); return ds; }); DataSourceStrategy strategy = getStrategy(); DataContainer dataContainer = strategy.process(dataSource, TWO_TABLE_ALIASES); doCommonAssertions(dataContainer, 6); List<DataRow> rows = dataContainer.getRows(); List<List<Object>> correctData = Arrays.asList( Arrays.asList("2", "second", "1", "1", "first_project"), Arrays.asList("1", "first", "2", "2", "second_project"), Arrays.asList("3", "third", "2", "2", "second_project"), Arrays.asList("5", "fifth", "2", "2", "second_project"), Arrays.asList("4", "third", "3", "3", "third_project"), Arrays.asList(null, null, null, "5", "fifth_project") ); assertThat(rowsAsValues(rows), equalTo(correctData)); }
@Test public void testNaturalLeftJoin() throws Exception { DataSource dataSource = prepareJoinDataSource(ds -> { ds.setJoinType(JoinType.LEFT); ds.setNaturalJoin(true); return ds; }); DataSourceStrategy strategy = getStrategy(); DataContainer dataContainer = strategy.process(dataSource, TWO_TABLE_ALIASES); assertNaturalLeftJoinResults(dataContainer); }