View Javadoc
1   package de.dlr.shepard.data.timeseries.utilities;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   import de.dlr.shepard.data.timeseries.TimeseriesTestDataGenerator;
6   import de.dlr.shepard.data.timeseries.io.TimeseriesWithDataPoints;
7   import de.dlr.shepard.data.timeseries.model.TimeseriesDataPoint;
8   import java.io.IOException;
9   import java.nio.charset.StandardCharsets;
10  import java.util.Collections;
11  import java.util.List;
12  import org.apache.commons.io.IOUtils;
13  import org.junit.jupiter.api.Test;
14  
15  class CsvColumnLineProviderTest {
16  
17    @Test
18    void testMultipleTimeseries() throws IOException {
19      var timeseries1 = TimeseriesTestDataGenerator.generateTimeseriesWithDataPoints(
20        "temperature",
21        List.of(
22          new TimeseriesDataPoint(88551122, 22.1),
23          new TimeseriesDataPoint(88551123, 4),
24          new TimeseriesDataPoint(88551124, 22.2)
25        )
26      );
27      var timeseries2 = TimeseriesTestDataGenerator.generateTimeseriesWithDataPoints(
28        "pressure",
29        List.of(
30          new TimeseriesDataPoint(88551125, 21.2),
31          new TimeseriesDataPoint(88551122, 23),
32          new TimeseriesDataPoint(88551123, 51)
33        )
34      );
35  
36      String expected =
37        """
38        timestamp,temperature-device-location-symbolicName-field,pressure-device-location-symbolicName-field
39        88551122,22.1,23
40        88551123,4,51
41        88551124,22.2,
42        88551125,,21.2
43        """;
44  
45      List<TimeseriesWithDataPoints> timeseriesWithDataPoints = List.of(timeseries1, timeseries2);
46  
47      CsvColumnLineProvider provider = new CsvColumnLineProvider(timeseriesWithDataPoints);
48  
49      String actual = IOUtils.toString(new CsvInputStream(provider), StandardCharsets.UTF_8);
50  
51      assertEquals(expected, actual);
52    }
53  
54    @Test
55    void testNoTimeseries() throws IOException {
56      List<TimeseriesWithDataPoints> timeseriesWithDataPoints = Collections.emptyList();
57  
58      CsvColumnLineProvider provider = new CsvColumnLineProvider(timeseriesWithDataPoints);
59  
60      String actual = IOUtils.toString(new CsvInputStream(provider), StandardCharsets.UTF_8);
61  
62      // We always expect a header line, without any timeseries this will only contain
63      //  timestamp and no further rows of data
64      assertEquals("timestamp\n", actual);
65    }
66  
67    @Test
68    void testEmptyTimeseries() throws IOException {
69      List<TimeseriesWithDataPoints> timeseriesWithDataPoints = List.of(
70        TimeseriesTestDataGenerator.generateTimeseriesWithDataPoints("pressure", Collections.emptyList())
71      );
72  
73      CsvColumnLineProvider provider = new CsvColumnLineProvider(timeseriesWithDataPoints);
74  
75      String actual = IOUtils.toString(new CsvInputStream(provider), StandardCharsets.UTF_8);
76  
77      // Even if no data is available, the header should still be generated for all timeseries
78      String expected =
79        """
80        timestamp,pressure-device-location-symbolicName-field
81        """;
82  
83      assertEquals(expected, actual);
84    }
85  
86    @Test
87    void testPartiallyEmptyTimeseries() throws IOException {
88      List<TimeseriesWithDataPoints> timeseriesWithDataPoints = List.of(
89        TimeseriesTestDataGenerator.generateTimeseriesWithDataPoints("pressure", Collections.emptyList()),
90        TimeseriesTestDataGenerator.generateTimeseriesWithDataPoints(
91          "temperature",
92          List.of(
93            new TimeseriesDataPoint(88551122, 22.1),
94            new TimeseriesDataPoint(88551123, 4),
95            new TimeseriesDataPoint(88551124, 22.2)
96          )
97        )
98      );
99  
100     CsvColumnLineProvider provider = new CsvColumnLineProvider(timeseriesWithDataPoints);
101 
102     String actual = IOUtils.toString(new CsvInputStream(provider), StandardCharsets.UTF_8);
103 
104     // If any timeseries does not contain any data, its header should still be generated and a
105     // completely empty column should be contained in the output
106     String expected =
107       """
108       timestamp,pressure-device-location-symbolicName-field,temperature-device-location-symbolicName-field
109       88551122,,22.1
110       88551123,,4
111       88551124,,22.2
112       """;
113 
114     assertEquals(expected, actual);
115   }
116 }