View Javadoc
1   package de.dlr.shepard.context.references.timeseriesreference;
2   
3   import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4   import static org.junit.jupiter.api.Assertions.assertEquals;
5   import static org.junit.jupiter.api.Assertions.assertThrows;
6   import static org.mockito.Mockito.when;
7   
8   import de.dlr.shepard.auth.permission.services.PermissionsService;
9   import de.dlr.shepard.auth.security.AuthenticationContext;
10  import de.dlr.shepard.auth.users.entities.User;
11  import de.dlr.shepard.auth.users.services.UserService;
12  import de.dlr.shepard.common.exceptions.InvalidAuthException;
13  import de.dlr.shepard.common.exceptions.InvalidBodyException;
14  import de.dlr.shepard.common.exceptions.InvalidPathException;
15  import de.dlr.shepard.common.exceptions.InvalidRequestException;
16  import de.dlr.shepard.common.util.AccessType;
17  import de.dlr.shepard.common.util.DateHelper;
18  import de.dlr.shepard.context.collection.entities.DataObject;
19  import de.dlr.shepard.context.collection.services.DataObjectService;
20  import de.dlr.shepard.context.references.timeseriesreference.daos.TimeseriesReferenceDAO;
21  import de.dlr.shepard.context.references.timeseriesreference.daos.TimeseriesTupleDAO;
22  import de.dlr.shepard.context.references.timeseriesreference.io.TimeseriesReferenceIO;
23  import de.dlr.shepard.context.references.timeseriesreference.model.TimeseriesReference;
24  import de.dlr.shepard.context.references.timeseriesreference.services.TimeseriesReferenceService;
25  import de.dlr.shepard.context.version.daos.VersionDAO;
26  import de.dlr.shepard.context.version.entities.Version;
27  import de.dlr.shepard.data.timeseries.io.TimeseriesWithDataPoints;
28  import de.dlr.shepard.data.timeseries.model.TimeseriesContainer;
29  import de.dlr.shepard.data.timeseries.model.TimeseriesDataPoint;
30  import de.dlr.shepard.data.timeseries.model.TimeseriesDataPointsQueryParams;
31  import de.dlr.shepard.data.timeseries.model.TimeseriesTuple;
32  import de.dlr.shepard.data.timeseries.model.enums.AggregateFunction;
33  import de.dlr.shepard.data.timeseries.model.enums.CsvFormat;
34  import de.dlr.shepard.data.timeseries.model.enums.FillOption;
35  import de.dlr.shepard.data.timeseries.services.TimeseriesContainerService;
36  import de.dlr.shepard.data.timeseries.services.TimeseriesCsvService;
37  import de.dlr.shepard.data.timeseries.services.TimeseriesService;
38  import io.quarkus.test.InjectMock;
39  import io.quarkus.test.component.QuarkusComponentTest;
40  import jakarta.inject.Inject;
41  import jakarta.ws.rs.NotFoundException;
42  import java.io.ByteArrayInputStream;
43  import java.io.IOException;
44  import java.util.Collections;
45  import java.util.Date;
46  import java.util.List;
47  import java.util.Optional;
48  import java.util.Set;
49  import java.util.UUID;
50  import org.junit.jupiter.api.Test;
51  
52  @QuarkusComponentTest
53  public class TimeseriesReferenceServiceTest {
54  
55    @InjectMock
56    TimeseriesReferenceDAO timeseriesReferenceDAO;
57  
58    @InjectMock
59    VersionDAO versionDAO;
60  
61    @InjectMock
62    TimeseriesService timeseriesService;
63  
64    @InjectMock
65    DataObjectService dataObjectService;
66  
67    @InjectMock
68    TimeseriesTupleDAO timeseriesDAO;
69  
70    @InjectMock
71    UserService userService;
72  
73    @InjectMock
74    DateHelper dateHelper;
75  
76    @InjectMock
77    PermissionsService permissionsService;
78  
79    @InjectMock
80    AuthenticationContext authenticationContext;
81  
82    @InjectMock
83    TimeseriesCsvService timeseriesCsvService;
84  
85    @InjectMock
86    TimeseriesContainerService timeseriesContainerService;
87  
88    @Inject
89    TimeseriesReferenceService referenceService;
90  
91    private final long collectionShepardId = 12345L;
92    private final User user = new User("Testuser");
93  
94    @Test
95    public void getTimeseriesReferenceByShepardIdTest_successful() {
96      TimeseriesReference ref = new TimeseriesReference(1L);
97      ref.setShepardId(15L);
98  
99      DataObject dataObject = new DataObject(4321L);
100     dataObject.setShepardId(54321L);
101     dataObject.setReferences(List.of(ref));
102     ref.setDataObject(dataObject);
103 
104     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
105     TimeseriesReference actual = referenceService.getReference(
106       collectionShepardId,
107       dataObject.getShepardId(),
108       ref.getShepardId(),
109       null
110     );
111     assertEquals(ref, actual);
112   }
113 
114   @Test
115   public void getTimeseriesReferenceByShepardIdTest_deleted() {
116     TimeseriesReference ref = new TimeseriesReference(1L);
117     ref.setShepardId(15L);
118     ref.setDeleted(true);
119 
120     DataObject dataObject = new DataObject(4321L);
121     dataObject.setShepardId(54321L);
122     dataObject.setReferences(List.of(ref));
123     ref.setDataObject(dataObject);
124 
125     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
126 
127     assertThrows(InvalidPathException.class, () ->
128       referenceService.getReference(collectionShepardId, dataObject.getShepardId(), ref.getShepardId(), null)
129     );
130   }
131 
132   @Test
133   public void getTimeseriesReferenceByShepardIdTest_notFound() {
134     Long shepardId = 15L;
135 
136     when(timeseriesReferenceDAO.findByShepardId(shepardId, null)).thenReturn(null);
137     assertThrows(InvalidPathException.class, () ->
138       referenceService.getReference(collectionShepardId, 54321L, shepardId, null)
139     );
140   }
141 
142   @Test
143   public void getTimeseriesReferenceByShepardIdTestIsDeleted() {
144     Long shepardId = 15L;
145     TimeseriesReference ref = new TimeseriesReference(20L);
146     ref.setShepardId(shepardId);
147     ref.setDeleted(true);
148 
149     DataObject dataObject = new DataObject(4321L);
150     dataObject.setShepardId(54321L);
151     dataObject.setReferences(List.of(ref));
152     ref.setDataObject(dataObject);
153 
154     when(timeseriesReferenceDAO.findByShepardId(shepardId, null)).thenReturn(ref);
155     assertThrows(InvalidPathException.class, () ->
156       referenceService.getReference(collectionShepardId, dataObject.getShepardId(), shepardId, null)
157     );
158   }
159 
160   @Test
161   public void getAllTimeseriesReferencesTest() {
162     DataObject dataObject = new DataObject(200L);
163     dataObject.setShepardId(2005L);
164 
165     TimeseriesReference ref1 = new TimeseriesReference(1L);
166     ref1.setShepardId(15L);
167     TimeseriesReference ref2 = new TimeseriesReference(2L);
168     ref2.setShepardId(25L);
169     dataObject.setReferences(List.of(ref1, ref2));
170     when(timeseriesReferenceDAO.findByDataObjectShepardId(dataObject.getShepardId())).thenReturn(List.of(ref1, ref2));
171     List<TimeseriesReference> actual = referenceService.getAllReferencesByDataObjectId(
172       collectionShepardId,
173       dataObject.getShepardId(),
174       null
175     );
176     assertEquals(List.of(ref1, ref2), actual);
177   }
178 
179   @Test
180   public void createTimeseriesReferenceByShepardIdTest() {
181     Version version = new Version(new UUID(1L, 2L));
182     DataObject dataObject = new DataObject(200L);
183     dataObject.setShepardId(2005L);
184     TimeseriesContainer container = new TimeseriesContainer(300L);
185     Date date = new Date(30L);
186     TimeseriesTuple timeseries = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
187 
188     TimeseriesReferenceIO input = new TimeseriesReferenceIO() {
189       {
190         setName("MyName");
191         setStart(123L);
192         setEnd(321L);
193         setTimeseries(List.of(timeseries));
194         setTimeseriesContainerId(container.getId());
195       }
196     };
197     var toCreate = new TimeseriesReference() {
198       {
199         setCreatedAt(date);
200         setCreatedBy(user);
201         setDataObject(dataObject);
202         setName(input.getName());
203         setStart(input.getStart());
204         setEnd(input.getEnd());
205         setReferencedTimeseriesList(List.of(timeseries));
206         setTimeseriesContainer(container);
207       }
208     };
209     var created = new TimeseriesReference() {
210       {
211         setId(1L);
212         setCreatedAt(toCreate.getCreatedAt());
213         setCreatedBy(toCreate.getCreatedBy());
214         setDataObject(toCreate.getDataObject());
215         setName(toCreate.getName());
216         setStart(toCreate.getStart());
217         setEnd(toCreate.getEnd());
218         setReferencedTimeseriesList(toCreate.getReferencedTimeseriesList());
219         setTimeseriesContainer(toCreate.getTimeseriesContainer());
220       }
221     };
222     var createdWithShepardId = new TimeseriesReference() {
223       {
224         setId(created.getId());
225         setShepardId(created.getId());
226         setCreatedAt(created.getCreatedAt());
227         setCreatedBy(created.getCreatedBy());
228         setDataObject(created.getDataObject());
229         setName(created.getName());
230         setStart(created.getStart());
231         setEnd(created.getEnd());
232         setReferencedTimeseriesList(created.getReferencedTimeseriesList());
233         setTimeseriesContainer(created.getTimeseriesContainer());
234       }
235     };
236 
237     when(userService.getCurrentUser()).thenReturn(user);
238     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
239     when(timeseriesContainerService.getContainer(300L)).thenReturn(container);
240     when(timeseriesDAO.find(timeseries)).thenReturn(Optional.of(timeseries));
241     when(timeseriesReferenceDAO.createOrUpdate(toCreate)).thenReturn(created);
242     when(timeseriesReferenceDAO.createOrUpdate(createdWithShepardId)).thenReturn(createdWithShepardId);
243     when(dateHelper.getDate()).thenReturn(date);
244     when(versionDAO.findVersionLightByNeo4jId(dataObject.getId())).thenReturn(version);
245     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
246     when(
247       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
248     ).thenReturn(true);
249 
250     var actual = referenceService.createReference(collectionShepardId, dataObject.getShepardId(), input);
251     assertEquals(createdWithShepardId, actual);
252   }
253 
254   @Test
255   public void createTimeseriesReferenceByShepardIdTest_timeseriesNotFound() {
256     Version version = new Version(new UUID(1L, 2L));
257     DataObject dataObject = new DataObject(200L);
258     dataObject.setShepardId(2005L);
259     TimeseriesContainer container = new TimeseriesContainer(300L);
260     Date date = new Date(30L);
261     TimeseriesTuple timeseries = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
262     TimeseriesReferenceIO input = new TimeseriesReferenceIO() {
263       {
264         setName("MyName");
265         setStart(123L);
266         setEnd(321L);
267         setTimeseries(List.of(timeseries));
268         setTimeseriesContainerId(container.getId());
269       }
270     };
271     TimeseriesReference toCreate = new TimeseriesReference() {
272       {
273         setCreatedAt(date);
274         setCreatedBy(user);
275         setDataObject(dataObject);
276         setName(input.getName());
277         setStart(input.getStart());
278         setEnd(input.getEnd());
279         setReferencedTimeseriesList(List.of(timeseries));
280         setTimeseriesContainer(container);
281       }
282     };
283     TimeseriesReference created = new TimeseriesReference() {
284       {
285         setId(1L);
286         setCreatedAt(toCreate.getCreatedAt());
287         setCreatedBy(toCreate.getCreatedBy());
288         setDataObject(toCreate.getDataObject());
289         setName(toCreate.getName());
290         setStart(toCreate.getStart());
291         setEnd(toCreate.getEnd());
292         setReferencedTimeseriesList(toCreate.getReferencedTimeseriesList());
293         setTimeseriesContainer(toCreate.getTimeseriesContainer());
294       }
295     };
296     var createdWithShepardId = new TimeseriesReference() {
297       {
298         setId(created.getId());
299         setShepardId(created.getId());
300         setCreatedAt(created.getCreatedAt());
301         setCreatedBy(created.getCreatedBy());
302         setDataObject(created.getDataObject());
303         setName(created.getName());
304         setStart(created.getStart());
305         setEnd(created.getEnd());
306         setReferencedTimeseriesList(created.getReferencedTimeseriesList());
307         setTimeseriesContainer(created.getTimeseriesContainer());
308       }
309     };
310     when(userService.getCurrentUser()).thenReturn(user);
311     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
312     when(timeseriesContainerService.getContainer(container.getId())).thenReturn(container);
313     when(timeseriesDAO.find(timeseries)).thenReturn(Optional.empty());
314     when(timeseriesReferenceDAO.createOrUpdate(toCreate)).thenReturn(created);
315     when(timeseriesReferenceDAO.createOrUpdate(createdWithShepardId)).thenReturn(createdWithShepardId);
316     when(dateHelper.getDate()).thenReturn(date);
317     when(versionDAO.findVersionLightByNeo4jId(dataObject.getId())).thenReturn(version);
318     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
319     when(
320       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
321     ).thenReturn(true);
322 
323     TimeseriesReference actual = referenceService.createReference(
324       collectionShepardId,
325       dataObject.getShepardId(),
326       input
327     );
328     assertEquals(createdWithShepardId, actual);
329   }
330 
331   @Test
332   public void createTimeseriesReferenceByShepardIdTest_invalidTimeseries() {
333     DataObject dataObject = new DataObject(200L);
334     dataObject.setShepardId(2005L);
335     TimeseriesContainer container = new TimeseriesContainer(300L);
336     TimeseriesReferenceIO input = new TimeseriesReferenceIO() {
337       {
338         setName("MyName");
339         setStart(123L);
340         setEnd(321L);
341         setTimeseries(List.of(new TimeseriesTuple("me.as", "dev", "loc", "symName", "field")));
342         setTimeseriesContainerId(container.getId());
343       }
344     };
345 
346     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
347     when(timeseriesContainerService.getContainer(container.getId())).thenReturn(container);
348     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
349     when(userService.getCurrentUser()).thenReturn(user);
350     when(
351       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
352     ).thenReturn(true);
353 
354     var ex = assertThrows(InvalidBodyException.class, () ->
355       referenceService.createReference(collectionShepardId, 2005L, input)
356     );
357     assertEquals(
358       "measurement is not allowed to be empty or contain one of those characters: 'Space, Comma, Point, Slash'",
359       ex.getMessage()
360     );
361   }
362 
363   @Test
364   public void createTimeseriesReferenceByShepardIdTest_ContainerIsNull() {
365     DataObject dataObject = new DataObject(200L);
366     dataObject.setShepardId(2005L);
367     TimeseriesContainer container = new TimeseriesContainer(300L);
368     container.setDeleted(true);
369     TimeseriesReferenceIO input = new TimeseriesReferenceIO() {
370       {
371         setName("MyName");
372         setStart(123L);
373         setEnd(321L);
374         setTimeseries(List.of(new TimeseriesTuple("meas", "dev", "loc", "symName", "field")));
375         setTimeseriesContainerId(container.getId());
376       }
377     };
378 
379     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
380     when(timeseriesContainerService.getContainer(container.getId())).thenThrow(new InvalidPathException());
381     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
382     when(userService.getCurrentUser()).thenReturn(user);
383     when(
384       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
385     ).thenReturn(true);
386 
387     var ex = assertThrows(InvalidRequestException.class, () ->
388       referenceService.createReference(collectionShepardId, 2005L, input)
389     );
390   }
391 
392   @Test
393   public void createTimeseriesReferenceByShepardIdTest_ContainerIsDeleted() {
394     DataObject dataObject = new DataObject(200L);
395     dataObject.setShepardId(2005L);
396     Long containerShepardId = 12345L;
397     TimeseriesReferenceIO input = new TimeseriesReferenceIO() {
398       {
399         setName("MyName");
400         setStart(123L);
401         setEnd(321L);
402         setTimeseries(List.of(new TimeseriesTuple("meas", "dev", "loc", "symName", "field")));
403         setTimeseriesContainerId(containerShepardId);
404       }
405     };
406     when(userService.getCurrentUser()).thenReturn(user);
407     when(dataObjectService.getDataObject(dataObject.getShepardId())).thenReturn(dataObject);
408     when(timeseriesContainerService.getContainer(12345L)).thenThrow(new InvalidPathException());
409     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
410 
411     var ex = assertThrows(InvalidRequestException.class, () ->
412       referenceService.createReference(collectionShepardId, 2005L, input)
413     );
414   }
415 
416   @Test
417   public void deleteReferenceByShepardIdTest() {
418     Date date = new Date(30L);
419     TimeseriesReference ref = new TimeseriesReference(1L);
420     ref.setShepardId(15L);
421     TimeseriesReference expected = new TimeseriesReference(ref.getId());
422     expected.setShepardId(ref.getShepardId());
423     expected.setDeleted(true);
424     expected.setUpdatedAt(date);
425     expected.setUpdatedBy(user);
426 
427     DataObject dataObject = new DataObject(6789L);
428     dataObject.setShepardId(67890L);
429     dataObject.setReferences(List.of(ref));
430     ref.setDataObject(dataObject);
431 
432     when(userService.getCurrentUser()).thenReturn(user);
433     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
434     when(dateHelper.getDate()).thenReturn(date);
435     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
436 
437     assertDoesNotThrow(() ->
438       referenceService.deleteReference(collectionShepardId, dataObject.getShepardId(), ref.getShepardId())
439     );
440   }
441 
442   @Test
443   public void getPayloadByShepardIdTest() {
444     TimeseriesContainer container = new TimeseriesContainer(2L);
445     var ts = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
446     TimeseriesReference ref = new TimeseriesReference() {
447       {
448         setId(1L);
449         setShepardId(15L);
450         setEnd(321);
451         setStart(123);
452         setReferencedTimeseriesList(List.of(ts));
453         setTimeseriesContainer(container);
454       }
455     };
456 
457     DataObject dataObject = new DataObject(6789L);
458     dataObject.setShepardId(67890L);
459     dataObject.setReferences(List.of(ref));
460     ref.setDataObject(dataObject);
461 
462     TimeseriesWithDataPoints timeseriesWithDataPoints = new TimeseriesWithDataPoints(
463       ts,
464       List.of(new TimeseriesDataPoint(50L, 7))
465     );
466     TimeseriesDataPointsQueryParams queryParams = new TimeseriesDataPointsQueryParams(
467       ref.getStart(),
468       ref.getEnd(),
469       10L,
470       FillOption.LINEAR,
471       AggregateFunction.MEAN
472     );
473     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
474     when(timeseriesService.getManyTimeseriesWithDataPoints(container.getId(), List.of(ts), queryParams)).thenReturn(
475       List.of(timeseriesWithDataPoints)
476     );
477     when(
478       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
479     ).thenReturn(true);
480     when(userService.getCurrentUser()).thenReturn(user);
481     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
482     when(timeseriesContainerService.getContainer(container.getId())).thenReturn(container);
483     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
484 
485     List<TimeseriesWithDataPoints> actual = referenceService.getReferencedTimeseriesWithDataPointsList(
486       collectionShepardId,
487       dataObject.getShepardId(),
488       ref.getShepardId(),
489       AggregateFunction.MEAN,
490       10L,
491       FillOption.LINEAR,
492       Set.of("dev"),
493       Set.of("loc"),
494       Set.of("symName"),
495       Set.of("meas"),
496       Set.of("field")
497     );
498 
499     assertEquals(List.of(timeseriesWithDataPoints), actual);
500   }
501 
502   @Test
503   public void getPayloadByShepardIdTest_ContainerIsDeleted() {
504     TimeseriesContainer container = new TimeseriesContainer(2L);
505     container.setDeleted(true);
506     var ts = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
507     TimeseriesReference ref = new TimeseriesReference() {
508       {
509         setId(1L);
510         setShepardId(15L);
511         setEnd(321);
512         setStart(123);
513         setReferencedTimeseriesList(List.of(ts));
514         setTimeseriesContainer(container);
515       }
516     };
517 
518     DataObject dataObject = new DataObject(6789L);
519     dataObject.setShepardId(67890L);
520     dataObject.setReferences(List.of(ref));
521     ref.setDataObject(dataObject);
522 
523     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
524     when(
525       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
526     ).thenReturn(true);
527     when(userService.getCurrentUser()).thenReturn(user);
528     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
529 
530     var ex = assertThrows(NotFoundException.class, () ->
531       referenceService.getReferencedTimeseriesWithDataPointsList(
532         collectionShepardId,
533         dataObject.getShepardId(),
534         ref.getShepardId(),
535         AggregateFunction.MEAN,
536         10L,
537         FillOption.LINEAR,
538         Set.of("dev"),
539         Set.of("loc"),
540         Set.of("name"),
541         Set.of("measurement"),
542         Set.of("field")
543       )
544     );
545 
546     assertEquals(
547       "Referenced Timeseries Container from reference with id 15 is null or has been deleted",
548       ex.getMessage()
549     );
550   }
551 
552   @Test
553   public void getPayloadByShepardIdTest_ContainerIsNull() {
554     var ts = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
555     TimeseriesReference ref = new TimeseriesReference() {
556       {
557         setId(1L);
558         setShepardId(15L);
559         setEnd(321);
560         setStart(123);
561         setReferencedTimeseriesList(List.of(ts));
562       }
563     };
564 
565     DataObject dataObject = new DataObject(6789L);
566     dataObject.setShepardId(67890L);
567     dataObject.setReferences(List.of(ref));
568     ref.setDataObject(dataObject);
569 
570     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
571     when(userService.getCurrentUser()).thenReturn(user);
572     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
573 
574     var ex = assertThrows(NotFoundException.class, () ->
575       referenceService.getReferencedTimeseriesWithDataPointsList(
576         collectionShepardId,
577         dataObject.getShepardId(),
578         ref.getShepardId(),
579         AggregateFunction.MEAN,
580         10L,
581         FillOption.LINEAR,
582         Set.of("dev"),
583         Set.of("loc"),
584         Set.of("name"),
585         Set.of("measurement"),
586         Set.of("field")
587       )
588     );
589 
590     assertEquals(
591       "Referenced Timeseries Container from reference with id 15 is null or has been deleted",
592       ex.getMessage()
593     );
594   }
595 
596   @Test
597   public void getPayloadByShepardIdTest_notAllowed() {
598     TimeseriesContainer container = new TimeseriesContainer(2L);
599     var ts = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
600     TimeseriesReference ref = new TimeseriesReference() {
601       {
602         setId(1L);
603         setShepardId(15L);
604         setEnd(321);
605         setStart(123);
606         setReferencedTimeseriesList(List.of(ts));
607         setTimeseriesContainer(container);
608       }
609     };
610 
611     DataObject dataObject = new DataObject(6789L);
612     dataObject.setShepardId(67890L);
613     dataObject.setReferences(List.of(ref));
614     ref.setDataObject(dataObject);
615 
616     when(
617       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
618     ).thenReturn(false);
619     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
620     when(userService.getCurrentUser()).thenReturn(user);
621     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
622     when(timeseriesContainerService.getContainer(container.getId())).thenThrow(new InvalidAuthException());
623 
624     assertThrows(InvalidAuthException.class, () ->
625       referenceService.getReferencedTimeseriesWithDataPointsList(
626         collectionShepardId,
627         dataObject.getShepardId(),
628         15L,
629         AggregateFunction.MEAN,
630         10L,
631         FillOption.LINEAR,
632         Set.of("dev"),
633         Set.of("loc"),
634         Set.of("name"),
635         Set.of("measurement"),
636         Set.of("field")
637       )
638     );
639   }
640 
641   @Test
642   public void exportByShepardIdTest() throws IOException, InvalidAuthException {
643     ByteArrayInputStream exportedFileStream = new ByteArrayInputStream("Hello World".getBytes());
644     TimeseriesContainer container = new TimeseriesContainer(2L);
645     var ts = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
646     TimeseriesReference ref = new TimeseriesReference() {
647       {
648         setId(1L);
649         setShepardId(15L);
650         setEnd(321);
651         setStart(123);
652         setReferencedTimeseriesList(List.of(ts));
653         setTimeseriesContainer(container);
654       }
655     };
656 
657     DataObject dataObject = new DataObject(6789L);
658     dataObject.setShepardId(67890L);
659     dataObject.setReferences(List.of(ref));
660     ref.setDataObject(dataObject);
661 
662     TimeseriesDataPointsQueryParams queryParams = new TimeseriesDataPointsQueryParams(
663       ref.getStart(),
664       ref.getEnd(),
665       10L,
666       FillOption.LINEAR,
667       AggregateFunction.MEAN
668     );
669 
670     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
671     when(
672       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
673     ).thenReturn(true);
674     when(
675       timeseriesCsvService.exportManyTimeseriesWithDataPointsToCsv(
676         container.getId(),
677         List.of(ts),
678         queryParams,
679         CsvFormat.ROW
680       )
681     ).thenReturn(exportedFileStream);
682     when(userService.getCurrentUser()).thenReturn(user);
683     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
684     when(timeseriesContainerService.getContainer(container.getId())).thenReturn(container);
685     when(
686       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
687     ).thenReturn(true);
688     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
689 
690     var actual = referenceService.exportReferencedTimeseriesByShepardId(
691       collectionShepardId,
692       dataObject.getShepardId(),
693       ref.getShepardId(),
694       AggregateFunction.MEAN,
695       10L,
696       FillOption.LINEAR,
697       Set.of("dev"),
698       Set.of("loc"),
699       Set.of("symName"),
700       Set.of("meas"),
701       Set.of("field"),
702       CsvFormat.ROW
703     );
704     assertEquals(exportedFileStream, actual);
705   }
706 
707   @Test
708   public void exportByShepardIdTest_lessParams() throws IOException, InvalidAuthException {
709     ByteArrayInputStream is = new ByteArrayInputStream("Hello World".getBytes());
710     TimeseriesContainer container = new TimeseriesContainer(2L);
711     var ts = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
712     TimeseriesReference ref = new TimeseriesReference() {
713       {
714         setId(1L);
715         setShepardId(15L);
716         setEnd(321);
717         setStart(123);
718         setReferencedTimeseriesList(List.of(ts));
719         setTimeseriesContainer(container);
720       }
721     };
722 
723     DataObject dataObject = new DataObject(6789L);
724     dataObject.setShepardId(67890L);
725     dataObject.setReferences(List.of(ref));
726     ref.setDataObject(dataObject);
727 
728     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
729     when(userService.getCurrentUser()).thenReturn(user);
730     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
731     when(timeseriesContainerService.getContainer(container.getId())).thenReturn(container);
732     when(
733       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
734     ).thenReturn(true);
735     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
736 
737     when(
738       referenceService.exportReferencedTimeseriesByShepardId(
739         collectionShepardId,
740         dataObject.getShepardId(),
741         ref.getShepardId(),
742         null,
743         null,
744         null,
745         Collections.emptySet(),
746         Collections.emptySet(),
747         Collections.emptySet(),
748         Collections.emptySet(),
749         Collections.emptySet(),
750         CsvFormat.ROW
751       )
752     ).thenReturn(is);
753     var actual = referenceService.exportReferencedTimeseriesByShepardId(
754       collectionShepardId,
755       dataObject.getShepardId(),
756       ref.getShepardId(),
757       CsvFormat.ROW
758     );
759     assertEquals(is, actual);
760   }
761 
762   @Test
763   public void exportByShepardIdTest_notAllowed() throws IOException, InvalidAuthException {
764     TimeseriesContainer container = new TimeseriesContainer(2L);
765     TimeseriesTuple ts = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
766     TimeseriesReference ref = new TimeseriesReference() {
767       {
768         setId(1L);
769         setShepardId(15L);
770         setEnd(321);
771         setStart(123);
772         setReferencedTimeseriesList(List.of(ts));
773         setTimeseriesContainer(container);
774       }
775     };
776 
777     DataObject dataObject = new DataObject(6789L);
778     dataObject.setShepardId(67890L);
779     dataObject.setReferences(List.of(ref));
780     ref.setDataObject(dataObject);
781 
782     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
783     when(userService.getCurrentUser()).thenReturn(user);
784     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
785     when(timeseriesContainerService.getContainer(container.getId())).thenThrow(new InvalidAuthException());
786     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
787     when(
788       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
789     ).thenReturn(false);
790 
791     assertThrows(InvalidAuthException.class, () ->
792       referenceService.exportReferencedTimeseriesByShepardId(
793         collectionShepardId,
794         dataObject.getShepardId(),
795         15L,
796         AggregateFunction.MEAN,
797         10L,
798         FillOption.LINEAR,
799         Set.of("dev"),
800         Set.of("loc"),
801         Set.of("name"),
802         Set.of("measurement"),
803         Set.of("field"),
804         CsvFormat.ROW
805       )
806     );
807   }
808 
809   @Test
810   public void exportByShepardIdTest_ContainerIsDeleted() throws IOException {
811     TimeseriesContainer container = new TimeseriesContainer(2L);
812     container.setDeleted(true);
813     TimeseriesTuple ts = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
814     TimeseriesReference ref = new TimeseriesReference() {
815       {
816         setId(1L);
817         setShepardId(15L);
818         setEnd(321);
819         setStart(123);
820         setReferencedTimeseriesList(List.of(ts));
821         setTimeseriesContainer(container);
822       }
823     };
824 
825     DataObject dataObject = new DataObject(6789L);
826     dataObject.setShepardId(67890L);
827     dataObject.setReferences(List.of(ref));
828     ref.setDataObject(dataObject);
829 
830     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
831     when(userService.getCurrentUser()).thenReturn(user);
832     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
833     when(timeseriesContainerService.getContainer(container.getId())).thenReturn(container);
834     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
835     when(
836       permissionsService.isAccessTypeAllowedForUser(container.getId(), AccessType.Read, user.getUsername())
837     ).thenReturn(true);
838 
839     assertThrows(NotFoundException.class, () ->
840       referenceService.exportReferencedTimeseriesByShepardId(
841         collectionShepardId,
842         dataObject.getShepardId(),
843         ref.getShepardId(),
844         AggregateFunction.MEAN,
845         10L,
846         FillOption.LINEAR,
847         Set.of("dev"),
848         Set.of("loc"),
849         Set.of("name"),
850         Set.of("measurement"),
851         Set.of("field"),
852         CsvFormat.ROW
853       )
854     );
855   }
856 
857   @Test
858   public void exportByShepardIdTest_ContainerIsNull() throws IOException {
859     TimeseriesTuple ts = new TimeseriesTuple("meas", "dev", "loc", "symName", "field");
860     TimeseriesReference ref = new TimeseriesReference() {
861       {
862         setId(1L);
863         setShepardId(15L);
864         setEnd(321);
865         setStart(123);
866         setReferencedTimeseriesList(List.of(ts));
867       }
868     };
869 
870     DataObject dataObject = new DataObject(6789L);
871     dataObject.setShepardId(67890L);
872     dataObject.setReferences(List.of(ref));
873     ref.setDataObject(dataObject);
874 
875     when(timeseriesReferenceDAO.findByShepardId(ref.getShepardId(), null)).thenReturn(ref);
876     when(userService.getCurrentUser()).thenReturn(user);
877     when(dataObjectService.getDataObject(collectionShepardId, dataObject.getShepardId())).thenReturn(dataObject);
878     when(authenticationContext.getCurrentUserName()).thenReturn(user.getUsername());
879 
880     assertThrows(NotFoundException.class, () ->
881       referenceService.exportReferencedTimeseriesByShepardId(
882         collectionShepardId,
883         dataObject.getShepardId(),
884         ref.getShepardId(),
885         AggregateFunction.MEAN,
886         10L,
887         FillOption.LINEAR,
888         Set.of("dev"),
889         Set.of("loc"),
890         Set.of("name"),
891         Set.of("measurement"),
892         Set.of("field"),
893         CsvFormat.ROW
894       )
895     );
896   }
897 }