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