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