View Javadoc
1   package de.dlr.shepard.context.references.timeseriesreference;
2   
3   import static org.assertj.core.api.Assertions.assertThat;
4   import static org.junit.jupiter.api.Assertions.assertEquals;
5   import static org.mockito.Mockito.when;
6   
7   import de.dlr.shepard.auth.security.AuthenticationContext;
8   import de.dlr.shepard.auth.users.entities.User;
9   import de.dlr.shepard.auth.users.services.UserService;
10  import de.dlr.shepard.context.collection.io.CollectionIO;
11  import de.dlr.shepard.context.collection.io.DataObjectIO;
12  import de.dlr.shepard.context.collection.services.CollectionService;
13  import de.dlr.shepard.context.collection.services.DataObjectService;
14  import de.dlr.shepard.context.references.timeseriesreference.io.MetricsIO;
15  import de.dlr.shepard.context.references.timeseriesreference.io.TimeseriesReferenceIO;
16  import de.dlr.shepard.context.references.timeseriesreference.services.TimeseriesReferenceMetricsService;
17  import de.dlr.shepard.context.references.timeseriesreference.services.TimeseriesReferenceService;
18  import de.dlr.shepard.data.timeseries.TimeseriesTestDataGenerator;
19  import de.dlr.shepard.data.timeseries.io.TimeseriesContainerIO;
20  import de.dlr.shepard.data.timeseries.model.Timeseries;
21  import de.dlr.shepard.data.timeseries.model.TimeseriesDataPoint;
22  import de.dlr.shepard.data.timeseries.model.enums.AggregateFunction;
23  import de.dlr.shepard.data.timeseries.services.InstantHelper;
24  import de.dlr.shepard.data.timeseries.services.TimeseriesContainerService;
25  import de.dlr.shepard.data.timeseries.services.TimeseriesService;
26  import io.quarkus.test.InjectMock;
27  import io.quarkus.test.junit.QuarkusTest;
28  import jakarta.inject.Inject;
29  import java.util.ArrayList;
30  import java.util.List;
31  import org.junit.jupiter.api.AfterEach;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.TestInstance;
35  
36  @QuarkusTest
37  @TestInstance(TestInstance.Lifecycle.PER_CLASS)
38  public class TimeseriesReferenceMetricsServiceTest {
39  
40    @Inject
41    TimeseriesReferenceMetricsService metricsService;
42  
43    @Inject
44    TimeseriesContainerService timeseriesContainerService;
45  
46    @Inject
47    TimeseriesReferenceService timeseriesReferenceService;
48  
49    @Inject
50    CollectionService collectionService;
51  
52    @Inject
53    DataObjectService dataObjectService;
54  
55    @Inject
56    TimeseriesService timeseriesService;
57  
58    @InjectMock
59    UserService userService;
60  
61    @InjectMock
62    AuthenticationContext authenticationContext;
63  
64    private long containerId, collectionId, dataobjectId, timeseriesReferenceId, stringTimeseriesReferenceId, booleanTimeseriesReferenceId;
65    private Timeseries timeseries, stringTimeseries, booleanTimeseries;
66  
67    @BeforeEach
68    public void setup() {
69      String containerName = "containerName";
70      String collectionName = "collectionName";
71      String dataObjectName = "DataObjectName";
72      String userName = "TestUser";
73      String timeseriesReferenceName = "referenceName";
74  
75      User user = new User(userName);
76  
77      TimeseriesContainerIO containerIO = new TimeseriesContainerIO();
78      CollectionIO collectionIO = new CollectionIO();
79      DataObjectIO dataObjectIO = new DataObjectIO();
80      TimeseriesReferenceIO timeseriesReferenceIO = new TimeseriesReferenceIO();
81      timeseriesReferenceIO.setName(timeseriesReferenceName);
82      timeseriesReferenceIO.setStart(0);
83      timeseriesReferenceIO.setEnd(Long.MAX_VALUE);
84      collectionIO.setName(collectionName);
85      dataObjectIO.setName(dataObjectName);
86      containerIO.setName(containerName);
87      when(userService.getCurrentUser()).thenReturn(user);
88      when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
89  
90      collectionId = collectionService.createCollection(collectionIO).getId();
91      dataobjectId = dataObjectService.createDataObject(collectionId, dataObjectIO).getId();
92  
93      var timeseriesContainer = timeseriesContainerService.createContainer(containerIO);
94      containerId = timeseriesContainer.getId();
95      timeseries = new Timeseries("measurement", "device", "location", "symbolicName", "field");
96      stringTimeseries = new Timeseries("stringMeasurement", "device", "location", "symbolicName", "field");
97      booleanTimeseries = new Timeseries("booleanMeasurement", "device", "location", "symbolicName", "field");
98  
99      InstantHelper instantHelper = InstantHelper.fromGermanDate("01.01.2024");
100     List<TimeseriesDataPoint> dataPoints = new ArrayList<>(
101       List.of(
102         TimeseriesTestDataGenerator.generateDataPointDouble(instantHelper.toNano(), 22.1),
103         TimeseriesTestDataGenerator.generateDataPointDouble(instantHelper.addSeconds(1).toNano(), 22.3),
104         TimeseriesTestDataGenerator.generateDataPointDouble(instantHelper.addSeconds(1).toNano(), 22.2)
105       )
106     );
107 
108     timeseriesReferenceIO.setTimeseriesContainerId(containerId);
109     timeseriesReferenceIO.setTimeseries(List.of(timeseries));
110     timeseriesService.saveDataPoints(containerId, timeseries, dataPoints).getId();
111     timeseriesReferenceId = timeseriesReferenceService
112       .createReference(collectionId, dataobjectId, timeseriesReferenceIO)
113       .getId();
114 
115     dataPoints = new ArrayList<>(
116       List.of(
117         TimeseriesTestDataGenerator.generateDataPointString(instantHelper.toNano(), "Point1"),
118         TimeseriesTestDataGenerator.generateDataPointString(instantHelper.addSeconds(1).toNano(), "Point2"),
119         TimeseriesTestDataGenerator.generateDataPointString(instantHelper.addSeconds(1).toNano(), "Point3")
120       )
121     );
122 
123     timeseriesReferenceIO.setTimeseries(List.of(stringTimeseries));
124     timeseriesService.saveDataPoints(containerId, stringTimeseries, dataPoints).getId();
125     stringTimeseriesReferenceId = timeseriesReferenceService
126       .createReference(collectionId, dataobjectId, timeseriesReferenceIO)
127       .getId();
128 
129     dataPoints = new ArrayList<>(
130       List.of(
131         TimeseriesTestDataGenerator.generateDataPointBoolean(instantHelper.toNano(), true),
132         TimeseriesTestDataGenerator.generateDataPointBoolean(instantHelper.addSeconds(1).toNano(), false),
133         TimeseriesTestDataGenerator.generateDataPointBoolean(instantHelper.addSeconds(1).toNano(), true)
134       )
135     );
136 
137     timeseriesReferenceIO.setTimeseries(List.of(booleanTimeseries));
138     timeseriesService.saveDataPoints(containerId, booleanTimeseries, dataPoints).getId();
139     booleanTimeseriesReferenceId = timeseriesReferenceService
140       .createReference(collectionId, dataobjectId, timeseriesReferenceIO)
141       .getId();
142   }
143 
144   @Test
145   public void getReferenceMetrics_doubles_returnsAllMetrics() {
146     List<AggregateFunction> metrics = List.of(
147       AggregateFunction.FIRST,
148       AggregateFunction.LAST,
149       AggregateFunction.COUNT,
150       AggregateFunction.MAX,
151       AggregateFunction.MIN,
152       AggregateFunction.STDDEV,
153       AggregateFunction.MEDIAN
154     );
155 
156     var resultList = metricsService.getTimeseriesReferenceMetrics(
157       collectionId,
158       dataobjectId,
159       timeseriesReferenceId,
160       null,
161       timeseries,
162       metrics
163     );
164     assertEquals(resultList.size(), metrics.size());
165     assertThat(resultList).contains(new MetricsIO(AggregateFunction.FIRST, 22.1));
166     assertThat(resultList).contains(new MetricsIO(AggregateFunction.LAST, 22.2));
167     assertThat(resultList).contains(new MetricsIO(AggregateFunction.MIN, 22.1));
168     assertThat(resultList).contains(new MetricsIO(AggregateFunction.MAX, 22.3));
169     assertThat(resultList).contains(new MetricsIO(AggregateFunction.COUNT, Long.valueOf(3)));
170     assertThat(resultList).contains(new MetricsIO(AggregateFunction.STDDEV, 0.09999999999999787));
171     assertThat(resultList).contains(new MetricsIO(AggregateFunction.MEDIAN, 22.2));
172   }
173 
174   @Test
175   public void getReferenceMetrics_strings_returnsNeededMetrics() {
176     List<AggregateFunction> metrics = List.of(
177       AggregateFunction.FIRST,
178       AggregateFunction.LAST,
179       AggregateFunction.COUNT,
180       AggregateFunction.MAX,
181       AggregateFunction.MIN,
182       AggregateFunction.STDDEV,
183       AggregateFunction.MEDIAN
184     );
185 
186     var resultList = metricsService.getTimeseriesReferenceMetrics(
187       collectionId,
188       dataobjectId,
189       stringTimeseriesReferenceId,
190       null,
191       stringTimeseries,
192       metrics
193     );
194     assertEquals(resultList.size(), metrics.size());
195     assertThat(resultList).contains(new MetricsIO(AggregateFunction.FIRST, "Point1"));
196     assertThat(resultList).contains(new MetricsIO(AggregateFunction.LAST, "Point3"));
197     assertThat(resultList).contains(new MetricsIO(AggregateFunction.MIN, "N/A"));
198     assertThat(resultList).contains(new MetricsIO(AggregateFunction.MAX, "N/A"));
199     assertThat(resultList).contains(new MetricsIO(AggregateFunction.COUNT, Long.valueOf(3)));
200     assertThat(resultList).contains(new MetricsIO(AggregateFunction.STDDEV, "N/A"));
201     assertThat(resultList).contains(new MetricsIO(AggregateFunction.MEDIAN, "N/A"));
202   }
203 
204   @Test
205   public void getReferenceMetrics_booleans_returnsNeededMetrics() {
206     List<AggregateFunction> metrics = List.of(
207       AggregateFunction.FIRST,
208       AggregateFunction.LAST,
209       AggregateFunction.COUNT,
210       AggregateFunction.MAX,
211       AggregateFunction.MIN,
212       AggregateFunction.STDDEV,
213       AggregateFunction.MEDIAN
214     );
215 
216     var resultList = metricsService.getTimeseriesReferenceMetrics(
217       collectionId,
218       dataobjectId,
219       booleanTimeseriesReferenceId,
220       null,
221       booleanTimeseries,
222       metrics
223     );
224     assertEquals(resultList.size(), metrics.size());
225     assertThat(resultList).contains(new MetricsIO(AggregateFunction.FIRST, true));
226     assertThat(resultList).contains(new MetricsIO(AggregateFunction.LAST, true));
227     assertThat(resultList).contains(new MetricsIO(AggregateFunction.MIN, "N/A"));
228     assertThat(resultList).contains(new MetricsIO(AggregateFunction.MAX, "N/A"));
229     assertThat(resultList).contains(new MetricsIO(AggregateFunction.COUNT, Long.valueOf(3)));
230     assertThat(resultList).contains(new MetricsIO(AggregateFunction.STDDEV, "N/A"));
231     assertThat(resultList).contains(new MetricsIO(AggregateFunction.MEDIAN, "N/A"));
232   }
233 
234   @AfterEach
235   public void tearDown() {
236     timeseriesService.deleteTimeseriesByContainerId(containerId);
237     timeseriesReferenceService.deleteReference(collectionId, dataobjectId, timeseriesReferenceId);
238     timeseriesReferenceService.deleteReference(collectionId, dataobjectId, stringTimeseriesReferenceId);
239     timeseriesReferenceService.deleteReference(collectionId, dataobjectId, booleanTimeseriesReferenceId);
240     dataObjectService.deleteDataObject(collectionId, dataobjectId);
241     collectionService.deleteCollection(collectionId);
242   }
243 }