ProjectionEngine { public Optional<Projection> createProjection(final Collection<SimpleTableSegment> tableSegments, final ProjectionSegment projectionSegment) { if (projectionSegment instanceof ShorthandProjectionSegment) { return Optional.of(createProjection(tableSegments, (ShorthandProjectionSegment) projectionSegment)); } if (projectionSegment instanceof ColumnProjectionSegment) { return Optional.of(createProjection((ColumnProjectionSegment) projectionSegment)); } if (projectionSegment instanceof ExpressionProjectionSegment) { return Optional.of(createProjection((ExpressionProjectionSegment) projectionSegment)); } if (projectionSegment instanceof AggregationDistinctProjectionSegment) { return Optional.of(createProjection((AggregationDistinctProjectionSegment) projectionSegment)); } if (projectionSegment instanceof AggregationProjectionSegment) { return Optional.of(createProjection((AggregationProjectionSegment) projectionSegment)); } return Optional.empty(); }  Optional<Projection> createProjection(final Collection<SimpleTableSegment> tableSegments, final ProjectionSegment projectionSegment);  }
@Test public void assertCreateProjectionWhenProjectionSegmentNotMatched() { assertFalse(new ProjectionEngine(mock(SchemaMetaData.class)).createProjection(Collections.emptyList(), null).isPresent()); }
@Test public void assertCreateProjectionWhenProjectionSegmentInstanceOfShorthandProjectionSegment() { ShorthandProjectionSegment shorthandProjectionSegment = new ShorthandProjectionSegment(0, 0); shorthandProjectionSegment.setOwner(new OwnerSegment(0, 0, new IdentifierValue("tbl"))); Optional<Projection> actual = new ProjectionEngine(mock(SchemaMetaData.class)).createProjection(Collections.emptyList(), shorthandProjectionSegment); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(ShorthandProjection.class)); }
@Test public void assertCreateProjectionWhenProjectionSegmentInstanceOfColumnProjectionSegment() { ColumnProjectionSegment columnProjectionSegment = new ColumnProjectionSegment(new ColumnSegment(0, 10, new IdentifierValue("name"))); columnProjectionSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("alias"))); Optional<Projection> actual = new ProjectionEngine(mock(SchemaMetaData.class)).createProjection(Collections.emptyList(), columnProjectionSegment); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(ColumnProjection.class)); }
@Test public void assertCreateProjectionWhenProjectionSegmentInstanceOfExpressionProjectionSegment() { ExpressionProjectionSegment expressionProjectionSegment = new ExpressionProjectionSegment(0, 10, "text"); Optional<Projection> actual = new ProjectionEngine(mock(SchemaMetaData.class)).createProjection(Collections.emptyList(), expressionProjectionSegment); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(ExpressionProjection.class)); }
@Test public void assertCreateProjectionWhenProjectionSegmentInstanceOfAggregationDistinctProjectionSegment() { AggregationDistinctProjectionSegment aggregationDistinctProjectionSegment = new AggregationDistinctProjectionSegment(0, 10, AggregationType.COUNT, "(1)", "distinctExpression"); Optional<Projection> actual = new ProjectionEngine(mock(SchemaMetaData.class)).createProjection(Collections.emptyList(), aggregationDistinctProjectionSegment); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(AggregationDistinctProjection.class)); }
@Test public void assertCreateProjectionWhenProjectionSegmentInstanceOfAggregationProjectionSegment() { AggregationProjectionSegment aggregationProjectionSegment = new AggregationProjectionSegment(0, 10, AggregationType.COUNT, "(1)"); Optional<Projection> actual = new ProjectionEngine(mock(SchemaMetaData.class)).createProjection(Collections.emptyList(), aggregationProjectionSegment); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(AggregationProjection.class)); }
@Test public void assertCreateProjectionWhenProjectionSegmentInstanceOfAggregationDistinctProjectionSegmentAndAggregationTypeIsAvg() { AggregationDistinctProjectionSegment aggregationDistinctProjectionSegment = new AggregationDistinctProjectionSegment(0, 10, AggregationType.AVG, "(1)", "distinctExpression"); Optional<Projection> actual = new ProjectionEngine(mock(SchemaMetaData.class)).createProjection(Collections.emptyList(), aggregationDistinctProjectionSegment); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(AggregationDistinctProjection.class)); }
@Test public void assertCreateProjectionWhenProjectionSegmentInstanceOfAggregationProjectionSegmentAndAggregationTypeIsAvg() { AggregationProjectionSegment aggregationProjectionSegment = new AggregationProjectionSegment(0, 10, AggregationType.AVG, "(1)"); Optional<Projection> actual = new ProjectionEngine(mock(SchemaMetaData.class)).createProjection(Collections.emptyList(), aggregationProjectionSegment); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(AggregationProjection.class)); }