View Javadoc
1   package de.dlr.shepard.data.timeseries.daos;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   
6   import de.dlr.shepard.auth.security.AuthenticationContext;
7   import de.dlr.shepard.auth.security.JwtPrincipal;
8   import de.dlr.shepard.auth.users.entities.User;
9   import de.dlr.shepard.auth.users.services.UserService;
10  import de.dlr.shepard.context.semantic.SemanticRepositoryType;
11  import de.dlr.shepard.context.semantic.entities.SemanticRepository;
12  import de.dlr.shepard.context.semantic.io.SemanticAnnotationIO;
13  import de.dlr.shepard.context.semantic.io.SemanticRepositoryIO;
14  import de.dlr.shepard.context.semantic.services.SemanticAnnotationService;
15  import de.dlr.shepard.context.semantic.services.SemanticRepositoryService;
16  import de.dlr.shepard.data.timeseries.io.TimeseriesContainerIO;
17  import de.dlr.shepard.data.timeseries.io.TimeseriesIO;
18  import de.dlr.shepard.data.timeseries.model.Timeseries;
19  import de.dlr.shepard.data.timeseries.model.TimeseriesContainer;
20  import de.dlr.shepard.data.timeseries.model.TimeseriesDataPoint;
21  import de.dlr.shepard.data.timeseries.model.TimeseriesTuple;
22  import de.dlr.shepard.data.timeseries.model.enums.DataPointValueType;
23  import de.dlr.shepard.data.timeseries.services.TimeseriesContainerService;
24  import de.dlr.shepard.data.timeseries.services.TimeseriesService;
25  import de.dlr.shepard.integrationtests.WireMockResource;
26  import io.quarkus.test.common.WithTestResource;
27  import io.quarkus.test.junit.QuarkusTest;
28  import jakarta.inject.Inject;
29  import jakarta.transaction.Transactional;
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.stream.Stream;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.MethodOrderer;
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.api.TestMethodOrder;
38  
39  @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
40  @QuarkusTest
41  @WithTestResource(WireMockResource.class)
42  public class TimeseriesDAOQuarkusTest {
43  
44    @Inject
45    TimeseriesService timeseriesService;
46  
47    @Inject
48    TimeseriesDAO timeseriesDao;
49  
50    @Inject
51    UserService userService;
52  
53    @Inject
54    SemanticRepositoryService semanticRepositoryService;
55  
56    @Inject
57    SemanticAnnotationService semanticAnnotationService;
58  
59    @Inject
60    TimeseriesContainerService timeseriesContainerService;
61  
62    @Inject
63    AuthenticationContext authenticationContext;
64  
65    private static TimeseriesContainer tsc1;
66    private static TimeseriesContainer tsc2;
67    private static TimeseriesIO timeseriesIO1;
68    private static TimeseriesIO timeseriesIO2;
69    private static TimeseriesIO timeseriesIOWithoutAnnotation;
70    private static String tsc1Name;
71    private static String tsc2Name;
72    private static User user1;
73    private static User user2;
74    private static String username1;
75    private static String username2;
76    private static Timeseries timeseries1;
77    private static Timeseries timeseriesWithoutAnnotation;
78    private static Timeseries timeseries2;
79    private static SemanticRepository repository1;
80    private static SemanticRepository repository2;
81    private static SemanticAnnotationIO annotation1;
82    private static SemanticAnnotationIO annotation1a;
83    private static SemanticAnnotationIO annotation2;
84  
85    @BeforeEach
86    public void setUp() {
87      if (user1 == null) {
88        // create users
89        username1 = "user1_" + System.currentTimeMillis();
90        username2 = "user2_" + System.currentTimeMillis();
91        user1 = new User(username1);
92        user2 = new User(username2);
93        userService.createOrUpdateUser(user1);
94        userService.createOrUpdateUser(user2);
95        // create timeseriescontainers
96        tsc1Name = "tsc1_" + System.currentTimeMillis();
97        tsc2Name = "tsc2_" + System.currentTimeMillis();
98        TimeseriesContainerIO tsc1IO = new TimeseriesContainerIO();
99        TimeseriesContainerIO tsc2IO = new TimeseriesContainerIO();
100       tsc1IO.setName(tsc1Name);
101       tsc2IO.setName(tsc2Name);
102       authenticationContext.setPrincipal(new JwtPrincipal(username1, "key1"));
103       tsc1 = timeseriesContainerService.createContainer(tsc1IO);
104       authenticationContext.setPrincipal(new JwtPrincipal(username2, "key2"));
105       tsc2 = timeseriesContainerService.createContainer(tsc2IO);
106       // create timeseries
107       TimeseriesDataPoint dataPoint = new TimeseriesDataPoint(4l, true);
108       List<TimeseriesDataPoint> points = new ArrayList<TimeseriesDataPoint>();
109       points.add(dataPoint);
110       TimeseriesTuple ts1 = new TimeseriesTuple("m1", "d1", "l1", "s1", "f1");
111       TimeseriesTuple tsWA = new TimeseriesTuple("m1", "d1", "l1", "WA", "f1");
112       TimeseriesTuple ts2 = new TimeseriesTuple("m2", "d2", "l2", "s2", "f2");
113       authenticationContext.setPrincipal(new JwtPrincipal(username1, "key1"));
114       timeseries1 = timeseriesService.saveDataPoints(tsc1.getId(), ts1, points);
115       timeseriesWithoutAnnotation = timeseriesService.saveDataPoints(tsc1.getId(), tsWA, points);
116       authenticationContext.setPrincipal(new JwtPrincipal(username2, "key2"));
117       timeseries2 = timeseriesService.saveDataPoints(tsc2.getId(), ts2, points);
118       // create repositories
119       SemanticRepositoryIO repToCreate1 = new SemanticRepositoryIO();
120       repToCreate1.setName("SemanticRepository1");
121       repToCreate1.setType(SemanticRepositoryType.SPARQL);
122       repToCreate1.setEndpoint(WireMockResource.getWireMockServerURlWithPath("/sparql"));
123       repository1 = semanticRepositoryService.createRepository(repToCreate1);
124       SemanticRepositoryIO repToCreate2 = new SemanticRepositoryIO();
125       repToCreate2.setName("SemanticRepository2");
126       repToCreate2.setType(SemanticRepositoryType.SPARQL);
127       repToCreate2.setEndpoint(WireMockResource.getWireMockServerURlWithPath("/sparql"));
128       repository2 = semanticRepositoryService.createRepository(repToCreate2);
129       // create annotations
130       annotation1 = new SemanticAnnotationIO();
131       annotation1.setPropertyIRI("http://dbpedia.org/ontology/ingredient");
132       annotation1.setPropertyRepositoryId(repository1.getId());
133       annotation1.setValueIRI("http://dbpedia.org/resource/Almond_milk");
134       annotation1.setValueRepositoryId(repository1.getId());
135       annotation1a = new SemanticAnnotationIO();
136       annotation1a.setPropertyIRI("http://dbpedia.org/ontology/ingredient");
137       annotation1a.setPropertyRepositoryId(repository1.getId());
138       annotation1a.setValueIRI("http://dbpedia.org/resource/milk");
139       annotation1a.setValueRepositoryId(repository1.getId());
140       annotation2 = new SemanticAnnotationIO();
141       annotation2.setPropertyIRI("http://dbpedia.org/resource/Almond_milk");
142       annotation2.setPropertyRepositoryId(repository2.getId());
143       annotation2.setValueIRI("http://dbpedia.org/ontology/ingredient");
144       annotation2.setValueRepositoryId(repository2.getId());
145       // attach annotations to timeseries
146       annotation1 = new SemanticAnnotationIO(
147         semanticAnnotationService.createAnnotationByNeo4jId(timeseries1.getId(), annotation1)
148       );
149       annotation1a = new SemanticAnnotationIO(
150         semanticAnnotationService.createAnnotationByNeo4jId(timeseries1.getId(), annotation1a)
151       );
152       annotation2 = new SemanticAnnotationIO(
153         semanticAnnotationService.createAnnotationByNeo4jId(timeseries2.getId(), annotation2)
154       );
155       // update timeseries
156       timeseries1 = timeseriesDao.findByTimeseriesId(timeseries1.getTimeseriesId()).orElseThrow();
157       timeseries2 = timeseriesDao.findByTimeseriesId(timeseries2.getTimeseriesId()).orElseThrow();
158       timeseriesWithoutAnnotation = timeseriesDao
159         .findByTimeseriesId(timeseriesWithoutAnnotation.getTimeseriesId())
160         .orElseThrow();
161       timeseriesIO1 = new TimeseriesIO(timeseries1);
162       timeseriesIO2 = new TimeseriesIO(timeseries2);
163       timeseriesIOWithoutAnnotation = new TimeseriesIO(timeseriesWithoutAnnotation);
164     }
165   }
166 
167   @Test
168   public void checkTimeseries() {
169     assertEquals("m1", timeseriesIO1.getMeasurement());
170     assertEquals("f1", timeseriesIO1.getField());
171     assertEquals("d1", timeseriesIO1.getDevice());
172     assertEquals("l1", timeseriesIO1.getLocation());
173     assertEquals("s1", timeseriesIO1.getSymbolicName());
174     assertEquals(tsc1.getId(), timeseriesIO1.getContainerId());
175     assertEquals(2, timeseriesIO1.getSemanticAnnotationIds().length);
176     assertTrue(
177       timeseriesIO1.getSemanticAnnotationIds()[0] == annotation1.getId() ||
178       timeseriesIO1.getSemanticAnnotationIds()[1] == annotation1.getId()
179     );
180     assertTrue(
181       timeseriesIO1.getSemanticAnnotationIds()[0] == annotation1a.getId() ||
182       timeseriesIO1.getSemanticAnnotationIds()[1] == annotation1a.getId()
183     );
184     assertEquals(DataPointValueType.Boolean, timeseriesIO1.getValueType());
185 
186     assertEquals("m1", timeseriesIOWithoutAnnotation.getMeasurement());
187     assertEquals("f1", timeseriesIOWithoutAnnotation.getField());
188     assertEquals("d1", timeseriesIOWithoutAnnotation.getDevice());
189     assertEquals("l1", timeseriesIOWithoutAnnotation.getLocation());
190     assertEquals("WA", timeseriesIOWithoutAnnotation.getSymbolicName());
191     assertEquals(tsc1.getId(), timeseriesIOWithoutAnnotation.getContainerId());
192     assertEquals(0, timeseriesIOWithoutAnnotation.getSemanticAnnotationIds().length);
193     assertEquals(DataPointValueType.Boolean, timeseriesIO1.getValueType());
194 
195     assertEquals("m2", timeseriesIO2.getMeasurement());
196     assertEquals("f2", timeseriesIO2.getField());
197     assertEquals("d2", timeseriesIO2.getDevice());
198     assertEquals("l2", timeseriesIO2.getLocation());
199     assertEquals("s2", timeseriesIO2.getSymbolicName());
200     assertEquals(tsc2.getId(), timeseriesIO2.getContainerId());
201     assertEquals(1, timeseriesIO2.getSemanticAnnotationIds().length);
202     assertTrue(timeseriesIO2.getSemanticAnnotationIds()[0] == annotation2.getId());
203     assertEquals(DataPointValueType.Boolean, timeseriesIO2.getValueType());
204   }
205 
206   @Test
207   @Transactional
208   public void getByQuintupleInContainer() {
209     Stream<Timeseries> timeseriesFound = timeseriesDao.findByQuintupleInContainer(
210       timeseriesIO1.getContainerId(),
211       timeseriesIO1.getMeasurement(),
212       timeseriesIO1.getDevice(),
213       timeseriesIO1.getLocation(),
214       timeseriesIO1.getSymbolicName(),
215       timeseriesIO1.getField()
216     );
217     Iterator<Timeseries> iterator = timeseriesFound.iterator();
218     Timeseries firstTimeseriesFound = iterator.next();
219     assertEquals(timeseriesIO1, new TimeseriesIO(firstTimeseriesFound));
220     assertEquals(false, iterator.hasNext());
221   }
222 
223   @Test
224   @Transactional
225   public void getByQuintupleInWrongContainer() {
226     Stream<Timeseries> timeseriesFound = timeseriesDao.findByQuintupleInContainer(
227       timeseriesIO2.getContainerId(),
228       timeseriesIO1.getMeasurement(),
229       timeseriesIO1.getDevice(),
230       timeseriesIO1.getLocation(),
231       timeseriesIO1.getSymbolicName(),
232       timeseriesIO1.getField()
233     );
234     Iterator<Timeseries> iterator = timeseriesFound.iterator();
235     assertEquals(false, iterator.hasNext());
236   }
237 
238   @Test
239   @Transactional
240   public void getByQuintupleInContainerWA() {
241     Stream<Timeseries> timeseriesFound = timeseriesDao.findByQuintupleInContainer(
242       timeseriesIOWithoutAnnotation.getContainerId(),
243       timeseriesIOWithoutAnnotation.getMeasurement(),
244       timeseriesIOWithoutAnnotation.getDevice(),
245       timeseriesIOWithoutAnnotation.getLocation(),
246       timeseriesIOWithoutAnnotation.getSymbolicName(),
247       timeseriesIOWithoutAnnotation.getField()
248     );
249     Iterator<Timeseries> iterator = timeseriesFound.iterator();
250     Timeseries firstTimeseriesFound = iterator.next();
251     assertEquals(timeseriesIOWithoutAnnotation, new TimeseriesIO(firstTimeseriesFound));
252     assertEquals(false, iterator.hasNext());
253   }
254 
255   @Test
256   @Transactional
257   public void getTwoByIncompleteQuintupleInContainer() {
258     Stream<Timeseries> timeseriesFound = timeseriesDao.findByQuintupleInContainer(
259       timeseriesIOWithoutAnnotation.getContainerId(),
260       timeseriesIOWithoutAnnotation.getMeasurement(),
261       timeseriesIOWithoutAnnotation.getDevice(),
262       timeseriesIOWithoutAnnotation.getLocation(),
263       null,
264       timeseriesIOWithoutAnnotation.getField()
265     );
266     ArrayList<TimeseriesIO> timeseriesIOFoundList = new ArrayList<TimeseriesIO>();
267     timeseriesFound.forEach(timeseries -> timeseriesIOFoundList.add(new TimeseriesIO(timeseries)));
268     assertEquals(2, timeseriesIOFoundList.size());
269     // assertEquals(expectedSet, foundSet);
270     assertEquals(
271       true,
272       timeseriesIOFoundList.get(0).equals(timeseriesIO1) || timeseriesIOFoundList.get(1).equals(timeseriesIO1)
273     );
274     assertEquals(
275       true,
276       timeseriesIOFoundList.get(0).equals(timeseriesIOWithoutAnnotation) ||
277       timeseriesIOFoundList.get(1).equals(timeseriesIOWithoutAnnotation)
278     );
279   }
280 }