This is an automated email from the ASF dual-hosted git repository. zehnder pushed a commit to branch SP-1336 in repository https://gitbox.apache.org/repos/asf/streampipes.git
commit 7b9c4dcb63ecf5cadbdafe8550e0f4a79d6acce6 Author: Philipp Zehnder <[email protected]> AuthorDate: Tue Feb 21 11:40:43 2023 +0100 [#1336] Add a builder to create SpQueryResult objects --- .../streampipes/model/datalake/DataSeries.java | 15 +++ .../{DataSeries.java => DataSeriesBuilder.java} | 52 ++++----- .../model/datalake/SpQueryResultBuilder.java | 73 +++++++++++++ .../model/datalake/DataSeriesBuilderTest.java | 90 ++++++++++++++++ .../model/datalake/SpQueryResultBuilderTest.java | 116 +++++++++++++++++++++ 5 files changed, 320 insertions(+), 26 deletions(-) diff --git a/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeries.java b/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeries.java index 13693a633..8ddb5c99d 100644 --- a/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeries.java +++ b/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeries.java @@ -60,4 +60,19 @@ public class DataSeries { return rows; } + public void setTotal(int total) { + this.total = total; + } + + public void setRows(List<List<Object>> rows) { + this.rows = rows; + } + + public void setTags(Map<String, String> tags) { + this.tags = tags; + } + + public void setHeaders(List<String> headers) { + this.headers = headers; + } } diff --git a/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeries.java b/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeriesBuilder.java similarity index 51% copy from streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeries.java copy to streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeriesBuilder.java index 13693a633..66a3d6108 100644 --- a/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeries.java +++ b/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/DataSeriesBuilder.java @@ -18,46 +18,46 @@ package org.apache.streampipes.model.datalake; -import org.apache.streampipes.model.shared.annotation.TsModel; - -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; import java.util.Map; -@TsModel -public class DataSeries { - - private int total; - private List<List<Object>> rows; - private Map<String, String> tags; - private List<String> headers; +public class DataSeriesBuilder { + private final DataSeries dataSeries; + private final List<List<Object>> rows; - public DataSeries() { - this.total = 0; - this.tags = new HashMap<>(); + private DataSeriesBuilder() { + this.dataSeries = new DataSeries(); + this.rows = new ArrayList<>(); } - public DataSeries(int total, List<List<Object>> rows, List<String> headers, Map<String, String> tags) { - this.total = total; - this.rows = rows; - this.headers = headers; - this.tags = tags; + public static DataSeriesBuilder create() { + return new DataSeriesBuilder(); } - public int getTotal() { - return total; + public DataSeries build() { + dataSeries.setRows(rows); + dataSeries.setTotal(rows.size()); + return dataSeries; } - public Map<String, String> getTags() { - return tags; + public DataSeriesBuilder withRow(List<Object> row) { + this.rows.add(row); + return this; } - public List<String> getHeaders() { - return headers; + public DataSeriesBuilder withRows(List<List<Object>> rows) { + this.rows.addAll(rows); + return this; } - public List<List<Object>> getRows() { - return rows; + public DataSeriesBuilder withHeaders(List<String> headers) { + dataSeries.setHeaders(headers); + return this; } + public DataSeriesBuilder withTags(Map<String, String> tags) { + dataSeries.setTags(tags); + return this; + } } diff --git a/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/SpQueryResultBuilder.java b/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/SpQueryResultBuilder.java new file mode 100644 index 000000000..2f9adf3d9 --- /dev/null +++ b/streampipes-model/src/main/java/org/apache/streampipes/model/datalake/SpQueryResultBuilder.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.streampipes.model.datalake; + +import java.util.ArrayList; +import java.util.List; + +public class SpQueryResultBuilder { + + private final SpQueryResult spQueryResult; + private final List<DataSeries> allDataSeries; + + private SpQueryResultBuilder(List<String> headers) { + spQueryResult = new SpQueryResult(); + allDataSeries = new ArrayList<>(); + spQueryResult.setHeaders(headers); + } + + public static SpQueryResultBuilder create(List<String> headers) { + return new SpQueryResultBuilder(headers); + } + + public SpQueryResult build() { + // set the header of all data series + allDataSeries + .forEach(series -> series.setHeaders(spQueryResult.getHeaders())); + spQueryResult.setAllDataSeries(allDataSeries); + spQueryResult.setTotal(allDataSeries.size()); + return spQueryResult; + } + + public SpQueryResultBuilder withDataSeries(List<DataSeries> allDataSeries) { + this.allDataSeries.addAll(allDataSeries); + return this; + } + + public SpQueryResultBuilder withDataSeries(DataSeries dataSeries) { + this.allDataSeries.add(dataSeries); + return this; + } + + public SpQueryResultBuilder withSourceIndex(int sourceIndex) { + spQueryResult.setSourceIndex(sourceIndex); + return this; + } + + public SpQueryResultBuilder withSpQueryStatus(SpQueryStatus spQueryStatus) { + spQueryResult.setSpQueryStatus(spQueryStatus); + return this; + } + + public SpQueryResultBuilder withForId(String forId) { + spQueryResult.setForId(forId); + return this; + } + +} diff --git a/streampipes-model/src/test/java/org/apache/streampipes/model/datalake/DataSeriesBuilderTest.java b/streampipes-model/src/test/java/org/apache/streampipes/model/datalake/DataSeriesBuilderTest.java new file mode 100644 index 000000000..1b1111f43 --- /dev/null +++ b/streampipes-model/src/test/java/org/apache/streampipes/model/datalake/DataSeriesBuilderTest.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.streampipes.model.datalake; + + +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class DataSeriesBuilderTest { + + private static final List<Object> rowOne = List.of(1, "v1"); + private static final List<Object> rowTwo = List.of(2, "v2"); + @Test + public void withHeadersTest() { + var headers = List.of("h1", "h2"); + var result = DataSeriesBuilder.create() + .withHeaders(headers) + .build(); + + assertEquals(2, result.getHeaders().size()); + assertEquals(headers, result.getHeaders()); + } + + + @Test + public void withRowTest() { + + var result = DataSeriesBuilder.create() + .withRow(rowOne) + .build(); + + assertEquals(1, result.getRows().size()); + assertEquals(1, result.getTotal()); + assertEquals(List.of(rowOne), result.getRows()); + } + + @Test + public void withTwoRowsTest() { + var result = DataSeriesBuilder.create() + .withRow(rowOne) + .withRow(rowTwo) + .build(); + + assertEquals(2, result.getRows().size()); + assertEquals(2, result.getTotal()); + assertEquals(List.of(rowOne, rowTwo), result.getRows()); + } + + @Test + public void withRowsTest() { + var result = DataSeriesBuilder.create() + .withRows(List.of(rowOne, rowTwo)) + .build(); + + assertEquals(2, result.getRows().size()); + assertEquals(2, result.getTotal()); + assertEquals(List.of(rowOne, rowTwo), result.getRows()); + } + + @Test + public void withTagsTest() { + Map<String, String> tags = Map.of("t1", "v1"); + + var result = DataSeriesBuilder.create() + .withTags(tags) + .build(); + + assertEquals(tags, result.getTags()); + } +} \ No newline at end of file diff --git a/streampipes-model/src/test/java/org/apache/streampipes/model/datalake/SpQueryResultBuilderTest.java b/streampipes-model/src/test/java/org/apache/streampipes/model/datalake/SpQueryResultBuilderTest.java new file mode 100644 index 000000000..a453795c5 --- /dev/null +++ b/streampipes-model/src/test/java/org/apache/streampipes/model/datalake/SpQueryResultBuilderTest.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.streampipes.model.datalake; + + +import org.junit.Test; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class SpQueryResultBuilderTest { + + List<String> headers = List.of("h1", "h2"); + @Test + public void withHeadersTest() { + var result = SpQueryResultBuilder.create(headers) + .build(); + + assertEquals(2, result.getHeaders().size()); + assertEquals(headers, result.getHeaders()); + } + + @Test + public void withSourceIndexTest() { + var sourceIndex = 1; + var result = SpQueryResultBuilder.create(headers) + .withSourceIndex(sourceIndex) + .build(); + + assertEquals(sourceIndex, result.getSourceIndex()); + } + + @Test + public void withQueryStatusDefaultTest() { + var result = SpQueryResultBuilder.create(headers) + .build(); + + assertEquals(SpQueryStatus.OK, result.getSpQueryStatus()); + } + + @Test + public void withQueryStatusTooMuchDataTest() { + var result = SpQueryResultBuilder.create(headers) + .withSpQueryStatus(SpQueryStatus.TOO_MUCH_DATA) + .build(); + + assertEquals(SpQueryStatus.TOO_MUCH_DATA, result.getSpQueryStatus()); + } + + @Test + public void withForId() { + var forId = "id"; + var result = SpQueryResultBuilder.create(headers) + .withForId(forId) + .build(); + + assertEquals(forId, result.getForId()); + } + + @Test + public void withDataSeriesTest() { + List<Object> row = List.of("v1", 1); + + var result = SpQueryResultBuilder.create(headers) + .withDataSeries( + DataSeriesBuilder.create() + .withRow(row) + .build() + ) + .build(); + + assertEquals(1, result.getAllDataSeries().size()); + assertEquals(1, result.getAllDataSeries().get(0).getRows().size()); + assertEquals(row, result.getAllDataSeries().get(0).getRows().get(0)); + } + + @Test + public void completeExampleTest() { + + List<String> headers = List.of("timestamp", "id", "value"); + List<List<Object>> rows = List.of( + List.of(1234L, "one", 1.1), + List.of(1235L, "two", 1.0) + ); + + var spQueryResult = SpQueryResultBuilder.create(headers) + .withDataSeries( + DataSeriesBuilder.create() + .withRows(rows) + .build() + ) + .build(); + + assertEquals(1, spQueryResult.getTotal()); + assertEquals(headers, spQueryResult.getAllDataSeries().get(0).getHeaders()); + assertEquals(2, spQueryResult.getAllDataSeries().get(0).getRows().size()); + assertEquals(rows, spQueryResult.getAllDataSeries().get(0).getRows()); + } +} \ No newline at end of file
