1 package de.dlr.shepard.common.search.services;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4
5 import de.dlr.shepard.auth.security.AuthenticationContext;
6 import de.dlr.shepard.auth.security.JWTPrincipal;
7 import de.dlr.shepard.auth.users.entities.User;
8 import de.dlr.shepard.auth.users.services.UserService;
9 import de.dlr.shepard.common.search.io.ResponseBody;
10 import de.dlr.shepard.common.search.io.SearchBody;
11 import de.dlr.shepard.common.search.io.SearchParams;
12 import de.dlr.shepard.common.search.io.SearchScope;
13 import de.dlr.shepard.common.util.TraversalRules;
14 import de.dlr.shepard.context.collection.entities.Collection;
15 import de.dlr.shepard.context.collection.entities.DataObject;
16 import de.dlr.shepard.context.collection.io.CollectionIO;
17 import de.dlr.shepard.context.collection.io.DataObjectIO;
18 import de.dlr.shepard.context.collection.services.CollectionService;
19 import de.dlr.shepard.context.collection.services.DataObjectService;
20 import de.dlr.shepard.context.references.timeseriesreference.services.TimeseriesReferenceService;
21 import de.dlr.shepard.context.semantic.services.AnnotatableTimeseriesService;
22 import de.dlr.shepard.context.semantic.services.SemanticAnnotationService;
23 import de.dlr.shepard.context.semantic.services.SemanticRepositoryService;
24 import de.dlr.shepard.data.timeseries.services.TimeseriesContainerService;
25 import de.dlr.shepard.data.timeseries.services.TimeseriesService;
26 import de.dlr.shepard.integrationtests.WireMockResource;
27 import io.quarkus.test.common.WithTestResource;
28 import io.quarkus.test.junit.QuarkusTest;
29 import jakarta.inject.Inject;
30 import jakarta.transaction.Transactional;
31 import java.util.Arrays;
32 import java.util.HashSet;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35
36 @QuarkusTest
37 @WithTestResource(WireMockResource.class)
38 public class DataObjectSearchServiceQuarkusTest {
39
40 @Inject
41 CollectionService collectionService;
42
43 @Inject
44 DataObjectService dataObjectService;
45
46 @Inject
47 ReferenceSearchService referenceSearcher;
48
49 @Inject
50 DataObjectSearchService dataObjectSearcher;
51
52 @Inject
53 TimeseriesReferenceService timeseriesReferenceService;
54
55 @Inject
56 TimeseriesService timeseriesService;
57
58 @Inject
59 AnnotatableTimeseriesService annotatableTimeseriesService;
60
61 @Inject
62 SemanticRepositoryService semanticRepositoryService;
63
64 @Inject
65 TimeseriesContainerService timeseriesContainerService;
66
67 @Inject
68 AuthenticationContext authenticationContext;
69
70 @Inject
71 UserService userService;
72
73 @Inject
74 SemanticAnnotationService semanticAnnotationService;
75
76 private Collection collection1;
77 private DataObject dataObjectc1d1;
78 private DataObject dataObjectc1d1succ1;
79 private DataObject dataObjectc1d1succ2;
80
81 private User user;
82 private String username = "username";
83
84 @BeforeEach
85 public void setUp() {
86
87 if (user == null) {
88 User user = new User(username);
89 userService.createOrUpdateUser(user);
90 authenticationContext.setPrincipal(new JWTPrincipal(username, "key"));
91 }
92
93 if (collection1 == null) {
94 CollectionIO collection1IO = new CollectionIO();
95 collection1IO.setName("collection1" + System.currentTimeMillis());
96 collection1 = collectionService.createCollection(collection1IO);
97 }
98
99 if (dataObjectc1d1 == null) {
100 DataObjectIO dataObjectc1d1IO = new DataObjectIO();
101 dataObjectc1d1IO.setName("c1do1" + System.currentTimeMillis());
102 dataObjectc1d1 = dataObjectService.createDataObject(collection1.getShepardId(), dataObjectc1d1IO);
103 }
104
105 if (dataObjectc1d1succ1 == null) {
106 DataObjectIO dataObjectc1d1succ1IO = new DataObjectIO();
107 dataObjectc1d1succ1IO.setName("c1do1qsucc1" + System.currentTimeMillis());
108 long[] predecessorIds = { dataObjectc1d1.getShepardId() };
109 dataObjectc1d1succ1IO.setPredecessorIds(predecessorIds);
110 dataObjectc1d1succ1 = dataObjectService.createDataObject(collection1.getShepardId(), dataObjectc1d1succ1IO);
111 }
112
113 if (dataObjectc1d1succ2 == null) {
114 DataObjectIO dataObjectc1d1succ2IO = new DataObjectIO();
115 dataObjectc1d1succ2IO.setName("c1do1qsucc2" + System.currentTimeMillis());
116 dataObjectc1d1succ2IO.setParentId(dataObjectc1d1succ1.getId());
117 long[] predecessorIds = { dataObjectc1d1.getShepardId() };
118 dataObjectc1d1succ2IO.setPredecessorIds(predecessorIds);
119 dataObjectc1d1succ2 = dataObjectService.createDataObject(collection1.getShepardId(), dataObjectc1d1succ2IO);
120 }
121 }
122
123 @Test
124 @Transactional
125 public void DONeighborhoodSearch() {
126
127 SearchScope scope = new SearchScope();
128 scope.setCollectionId(collection1.getShepardId());
129 TraversalRules[] rules = {};
130 scope.setTraversalRules(rules);
131 SearchScope[] scopes = { scope };
132 long[] ids = { dataObjectc1d1succ1.getId(), dataObjectc1d1succ2.getId() };
133 String query =
134 "{\"property\": \"successorIds\", \"value\": " + Arrays.toString(ids) + ", \"operator\": \"contains\"}";
135 SearchBody body = new SearchBody();
136 body.setScopes(scopes);
137 SearchParams params = new SearchParams();
138 params.setQuery(query);
139 body.setSearchParams(params);
140 ResponseBody response = dataObjectSearcher.search(body);
141 assertEquals(1, response.getResults().length);
142 assertEquals(dataObjectc1d1.getId(), response.getResults()[0].getId());
143
144 long[] ids1 = { dataObjectc1d1succ1.getId(), dataObjectc1d1succ2.getId(), 0 };
145 query = "{\"property\": \"successorIds\", \"value\": " + Arrays.toString(ids1) + ", \"operator\": \"contains\"}";
146 body = new SearchBody();
147 body.setScopes(scopes);
148 params = new SearchParams();
149 params.setQuery(query);
150 body.setSearchParams(params);
151 response = dataObjectSearcher.search(body);
152 assertEquals(0, response.getResults().length);
153
154 query =
155 "{\"property\": \"successorIds\", \"value\": [" +
156 dataObjectc1d1succ1.getId() +
157 "], \"operator\": \"isContainedIn\"}";
158 body = new SearchBody();
159 body.setScopes(scopes);
160 params = new SearchParams();
161 params.setQuery(query);
162 body.setSearchParams(params);
163 response = dataObjectSearcher.search(body);
164 assertEquals(2, response.getResults().length);
165 HashSet<Long> idSetExpected = new HashSet<Long>();
166 idSetExpected.add(dataObjectc1d1succ1.getId());
167 idSetExpected.add(dataObjectc1d1succ2.getId());
168 HashSet<Long> idSetFound = new HashSet<Long>();
169 idSetFound.add(response.getResults()[0].getId());
170 idSetFound.add(response.getResults()[1].getId());
171 assertEquals(idSetExpected, idSetFound);
172
173 long[] ids2 = { dataObjectc1d1.getId() };
174 query = "{\"property\": \"predecessorIds\", \"value\": " + Arrays.toString(ids2) + ", \"operator\": \"eq\"}";
175 body = new SearchBody();
176 body.setScopes(scopes);
177 params = new SearchParams();
178 params.setQuery(query);
179 body.setSearchParams(params);
180 response = dataObjectSearcher.search(body);
181 assertEquals(2, response.getResults().length);
182 idSetExpected.clear();
183 idSetFound.clear();
184 idSetExpected.add(dataObjectc1d1succ1.getId());
185 idSetExpected.add(dataObjectc1d1succ2.getId());
186 idSetFound.add(response.getResults()[0].getId());
187 idSetFound.add(response.getResults()[1].getId());
188 assertEquals(idSetExpected, idSetFound);
189
190 long[] ids3 = { dataObjectc1d1succ1.getId() };
191 String childClause =
192 "{\"property\": \"parentIds\", \"value\": " + Arrays.toString(ids3) + ", \"operator\": \"eq\"}";
193 String idClause = "{\"property\": \"id\", \"value\": 0, \"operator\": \"ge\"}";
194 query = "{\"AND\" : [" + childClause + "," + idClause + "]}";
195 body = new SearchBody();
196 body.setScopes(scopes);
197 params = new SearchParams();
198 params.setQuery(query);
199 body.setSearchParams(params);
200 response = dataObjectSearcher.search(body);
201 assertEquals(1, response.getResults().length);
202 assertEquals(dataObjectc1d1succ2.getId(), response.getResults()[0].getId());
203
204 query = "{\"property\": \"childrenIds\", \"value\": [], \"operator\": \"eq\"}";
205 body = new SearchBody();
206 body.setScopes(scopes);
207 params = new SearchParams();
208 params.setQuery(query);
209 body.setSearchParams(params);
210 response = dataObjectSearcher.search(body);
211 assertEquals(2, response.getResults().length);
212 idSetExpected.clear();
213 idSetFound.clear();
214 idSetExpected.add(dataObjectc1d1.getId());
215 idSetExpected.add(dataObjectc1d1succ2.getId());
216 idSetFound.add(response.getResults()[0].getId());
217 idSetFound.add(response.getResults()[1].getId());
218 assertEquals(idSetExpected, idSetFound);
219 }
220 }