blob: 18dd10dc9aedf855ebf8f7a372d31dd9a3e5ba59 [file] [log] [blame]
margaretha541b8cc2018-01-10 13:02:46 +01001package de.ids_mannheim.korap.web.controller;
margarethaebe869a2017-06-01 19:07:41 +02002
Marc Kupietzd43a98d2023-09-22 17:11:46 +02003import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertNotNull;
5import static org.junit.jupiter.api.Assertions.assertTrue;
margarethaebe869a2017-06-01 19:07:41 +02006
Marc Kupietzd43a98d2023-09-22 17:11:46 +02007import org.junit.jupiter.api.Test;
margaretha82c75c22024-12-11 09:36:50 +01008import org.springframework.beans.factory.annotation.Autowired;
9
margarethaebe869a2017-06-01 19:07:41 +020010import com.fasterxml.jackson.databind.JsonNode;
margaretha58e18632018-02-15 13:04:42 +010011import com.google.common.net.HttpHeaders;
margaretha96c309d2023-08-16 12:24:12 +020012import jakarta.ws.rs.ProcessingException;
13import jakarta.ws.rs.core.Response;
14import jakarta.ws.rs.core.Response.Status;
margarethaebe869a2017-06-01 19:07:41 +020015
margaretha56e8e552017-12-05 16:31:21 +010016import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
margarethaebe869a2017-06-01 19:07:41 +020017import de.ids_mannheim.korap.config.Attributes;
margaretha82c75c22024-12-11 09:36:50 +010018import de.ids_mannheim.korap.config.FullConfiguration;
margaretha45667922018-01-25 21:23:03 +010019import de.ids_mannheim.korap.config.SpringJerseyTest;
margarethaebe869a2017-06-01 19:07:41 +020020import de.ids_mannheim.korap.exceptions.KustvaktException;
margarethaebe869a2017-06-01 19:07:41 +020021import de.ids_mannheim.korap.utils.JsonUtils;
margarethaebe869a2017-06-01 19:07:41 +020022
margarethade92c202018-02-20 14:38:40 +010023public class AvailabilityTest extends SpringJerseyTest {
margaretha82c75c22024-12-11 09:36:50 +010024
25 @Autowired
26 public FullConfiguration config;
margarethade92c202018-02-20 14:38:40 +010027
margarethad9e43ec2024-12-20 12:11:43 +010028 private void checkAndFree (String json, JsonNode source)
29 throws KustvaktException {
30 JsonNode node = JsonUtils.readTree(json).at("/collection");
31 assertEquals("availability", node.at("/operands/0/key").asText());
32 assertEquals("CC.*", node.at("/operands/0/value").asText());
33 assertEquals("operation:override",
34 node.at("/rewrites/0/operation").asText());
35 assertEquals(source, node.at("/rewrites/0/source"));
36 // assertEquals(node.at("/collection/rewrites/0/scope").asText(),
37 // "availability(FREE)");
38 }
margarethaebe869a2017-06-01 19:07:41 +020039
margarethad9e43ec2024-12-20 12:11:43 +010040 private void checkAndPublic (String json, JsonNode source)
41 throws KustvaktException {
42 JsonNode node = JsonUtils.readTree(json).at("/collection");
43 assertNotNull(node);
44 System.out.println(node.at("/rewrites/0").toPrettyString());
45 assertEquals("operation:and", node.at("/operation").asText());
46 assertEquals("koral:rewrite", node.at("/rewrites/0/@type").asText());
47 assertEquals("Kustvakt", node.at("/rewrites/0/origin").asText());
48 assertEquals("operation:override", node.at("/rewrites/0/operation").asText());
49 assertEquals(source, node.at("/rewrites/0/source"));
50
51 node = node.at("/operands/0");
52 assertEquals("match:eq", node.at("/operands/0/match").asText());
53 assertEquals("type:regex", node.at("/operands/0/type").asText());
54 assertEquals("availability", node.at("/operands/0/key").asText());
55 assertEquals("CC.*", node.at("/operands/0/value").asText());
56 assertEquals("match:eq",
57 node.at("/operands/1/operands/0/match").asText());
58 assertEquals("ACA.*", node.at("/operands/1/operands/0/value").asText());
59 assertEquals("match:eq",
60 node.at("/operands/1/operands/1/match").asText());
61 assertEquals("QAO-NC",
62 node.at("/operands/1/operands/1/value").asText());
63
64 }
margarethaed7bc7a2017-11-12 21:39:41 +010065
margarethad9e43ec2024-12-20 12:11:43 +010066 private void checkAndAllWithACA (String json, JsonNode source)
67 throws KustvaktException {
68 JsonNode node = JsonUtils.readTree(json).at("/collection");
69 assertEquals("operation:and", node.at("/operation").asText());
70 assertEquals("operation:and", node.at("/operation").asText());
71 assertEquals("koral:rewrite", node.at("/rewrites/0/@type").asText());
72 assertEquals("Kustvakt", node.at("/rewrites/0/origin").asText());
73 assertEquals("operation:override", node.at("/rewrites/0/operation").asText());
74 assertEquals(source, node.at("/rewrites/0/source"));
margarethaed7bc7a2017-11-12 21:39:41 +010075
margarethad9e43ec2024-12-20 12:11:43 +010076 assertEquals("match:eq", node.at("/operands/1/match").asText());
77 assertEquals("type:regex", node.at("/operands/1/type").asText());
78 assertEquals("availability", node.at("/operands/1/key").asText());
79 assertEquals("ACA.*", node.at("/operands/1/value").asText());
80 node = node.at("/operands/0");
margaretha82c75c22024-12-11 09:36:50 +010081 assertEquals("match:eq", node.at("/operands/0/match").asText());
82 assertEquals("type:regex", node.at("/operands/0/type").asText());
83 assertEquals("availability", node.at("/operands/0/key").asText());
84 assertEquals(config.getFreeOnlyRegex(),
85 node.at("/operands/0/value").asText());
86 assertEquals("match:eq",
87 node.at("/operands/1/operands/1/operands/0/match").asText());
88 assertEquals("QAO-NC",
89 node.at("/operands/1/operands/1/operands/0/value").asText());
90 assertEquals("match:eq",
91 node.at("/operands/1/operands/1/operands/1/match").asText());
92 assertEquals(config.getAllOnlyRegex(),
93 node.at("/operands/1/operands/1/operands/1/value").asText());
margaretha416e7872017-06-20 15:05:17 +020094 }
margarethaed7bc7a2017-11-12 21:39:41 +010095
margaretha35e1ca22023-11-16 22:00:01 +010096 private Response searchQuery (String collectionQuery) {
97 return target().path(API_VERSION).path("search")
98 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
99 .queryParam("cq", collectionQuery).request().get();
margarethaebe869a2017-06-01 19:07:41 +0200100 }
101
margaretha35e1ca22023-11-16 22:00:01 +0100102 private Response searchQueryWithIP (String collectionQuery, String ip)
103 throws ProcessingException, KustvaktException {
104 return target().path(API_VERSION).path("search")
105 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
106 .queryParam("cq", collectionQuery).request()
margarethad9e43ec2024-12-20 12:11:43 +0100107 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
108 .createBasicAuthorizationHeaderValue("user", "pass"))
margaretha35e1ca22023-11-16 22:00:01 +0100109 .header(HttpHeaders.X_FORWARDED_FOR, ip).get();
margarethaebe869a2017-06-01 19:07:41 +0200110 }
111
margarethad9e43ec2024-12-20 12:11:43 +0100112
margarethaebe869a2017-06-01 19:07:41 +0200113 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100114 public void testFreeWithoutCorpusQuery () throws KustvaktException {
115 Response response = target().path(API_VERSION).path("search")
116 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
117 .request().get();
118 assertEquals(Status.OK.getStatusCode(), response.getStatus());
119
120 String json = response.readEntity(String.class);
121 JsonNode node = JsonUtils.readTree(json).at("/collection");
122 assertEquals("availability", node.at("/key").asText());
123 assertEquals("CC.*", node.at("/value").asText());
124 assertEquals("operation:injection",
125 node.at("/rewrites/0/operation").asText());
126 }
127
128 @Test
129 public void testPublicWithoutCorpusQuery () throws KustvaktException {
130 Response response = target().path(API_VERSION).path("search")
131 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
132 .request()
133 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
134 .createBasicAuthorizationHeaderValue("user", "pass"))
135 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").get();
136 assertEquals(Status.OK.getStatusCode(), response.getStatus());
137
138 String json = response.readEntity(String.class);
139 JsonNode node = JsonUtils.readTree(json).at("/collection");
140
141 String expected = """
142 {
143 "operands" : [ {
144 "@type" : "koral:doc",
145 "match" : "match:eq",
146 "type" : "type:regex",
147 "value" : "CC.*",
148 "key" : "availability"
149 }, {
150 "operands" : [ {
151 "@type" : "koral:doc",
152 "match" : "match:eq",
153 "type" : "type:regex",
154 "value" : "ACA.*",
155 "key" : "availability"
156 }, {
157 "@type" : "koral:doc",
158 "match" : "match:eq",
159 "type" : "type:regex",
160 "value" : "QAO-NC",
161 "key" : "availability"
162 } ],
163 "@type" : "koral:docGroup",
164 "operation" : "operation:or"
165 } ],
166 "@type" : "koral:docGroup",
167 "operation" : "operation:or",
168 "rewrites" : [ {
169 "@type" : "koral:rewrite",
170 "src" : "Kustvakt",
171 "origin" : "Kustvakt",
172 "operation" : "operation:injection",
173 "scope" : "availability(PUB)"
174 } ]
175 }
176 """;
177
178 assertEquals(JsonUtils.readTree(expected), node);
179 }
180
181
182 @Test
183 public void testAllWithoutCorpusQuery () throws KustvaktException {
184 Response response = target().path(API_VERSION).path("search")
185 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
186 .request()
187 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
188 .createBasicAuthorizationHeaderValue("user", "pass"))
189 .header(HttpHeaders.X_FORWARDED_FOR, "10.0.10.132").get();
190 assertEquals(Status.OK.getStatusCode(), response.getStatus());
191
192 String json = response.readEntity(String.class);
193 JsonNode node = JsonUtils.readTree(json).at("/collection");
194 String expected = """
195 {
196 "operands" : [ {
197 "@type" : "koral:doc",
198 "match" : "match:eq",
199 "type" : "type:regex",
200 "value" : "CC.*",
201 "key" : "availability"
202 }, {
203 "operands" : [ {
204 "@type" : "koral:doc",
205 "match" : "match:eq",
206 "type" : "type:regex",
207 "value" : "ACA.*",
208 "key" : "availability"
209 }, {
210 "operands" : [ {
211 "@type" : "koral:doc",
212 "match" : "match:eq",
213 "type" : "type:regex",
214 "value" : "QAO-NC",
215 "key" : "availability"
216 }, {
217 "@type" : "koral:doc",
218 "match" : "match:eq",
219 "type" : "type:regex",
220 "value" : "QAO-NC-LOC:ids.*",
221 "key" : "availability"
222 } ],
223 "@type" : "koral:docGroup",
224 "operation" : "operation:or"
225 } ],
226 "@type" : "koral:docGroup",
227 "operation" : "operation:or"
228 } ],
229 "@type" : "koral:docGroup",
230 "operation" : "operation:or",
231 "rewrites" : [ {
232 "@type" : "koral:rewrite",
233 "src" : "Kustvakt",
234 "origin" : "Kustvakt",
235 "operation" : "operation:injection",
236 "scope" : "availability(ALL)"
237 } ]
238 }
239 """;
240 assertEquals(JsonUtils.readTree(expected), node);
241 }
242
243 @Test
244 public void testFreeWithoutAvailabilityOr () throws KustvaktException {
245 Response response = searchQuery("corpusSigle=GOE | textClass=politik");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200246 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100247 String source = """
248 {
249 "operands" : [ {
250 "@type" : "koral:doc",
251 "match" : "match:eq",
252 "value" : "GOE",
253 "key" : "corpusSigle"
254 }, {
255 "@type" : "koral:doc",
256 "match" : "match:eq",
257 "value" : "politik",
258 "key" : "textClass"
259 } ],
260 "@type" : "koral:docGroup",
261 "operation" : "operation:or"
262 }
263 """;
264 checkAndFree(response.readEntity(String.class),
265 JsonUtils.readTree(source));
margarethaebe869a2017-06-01 19:07:41 +0200266 }
267
margarethaebe869a2017-06-01 19:07:41 +0200268 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100269 public void testFreeWithoutAvailability () throws KustvaktException {
270 Response response = searchQuery("corpusSigle=GOE");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200271 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100272 String source = """
273 {
274 "@type" : "koral:doc",
275 "match" : "match:eq",
276 "value" : "GOE",
277 "key" : "corpusSigle"
278 }
279 """;
280 checkAndFree(response.readEntity(String.class),
281 JsonUtils.readTree(source));
margarethaebe869a2017-06-01 19:07:41 +0200282 }
283
margarethad9e43ec2024-12-20 12:11:43 +0100284
margarethaebe869a2017-06-01 19:07:41 +0200285 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100286 public void testFreeAvailabilityNoRewrite () throws KustvaktException {
287 Response response = searchQuery("availability = /CC.*/");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200288 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100289 String json = response.readEntity(String.class);
290 JsonNode node = JsonUtils.readTree(json);
291 assertTrue(node.at("/collection/rewrite").isMissingNode());
margarethaebe869a2017-06-01 19:07:41 +0200292 }
margarethad9e43ec2024-12-20 12:11:43 +0100293
margarethaebe869a2017-06-01 19:07:41 +0200294 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100295 public void testFreeAvailabilityNoRewriteAnd () throws KustvaktException {
margaretha35e1ca22023-11-16 22:00:01 +0100296 Response response = searchQuery(
Marc Kupietze4528ee2024-07-04 16:28:37 +0200297 "availability = /CC.*/ & availability = /ACA.*/");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200298 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000299 String json = response.readEntity(String.class);
margaretha4e933c62017-06-27 16:28:40 +0200300 JsonNode node = JsonUtils.readTree(json);
margaretha35e1ca22023-11-16 22:00:01 +0100301 assertEquals(node.at("/collection/operation").asText(),
302 "operation:and");
303 assertEquals(node.at("/collection/operands/0/match").asText(),
304 "match:eq");
305 assertEquals(node.at("/collection/operands/0/type").asText(),
306 "type:regex");
307 assertEquals(node.at("/collection/operands/0/key").asText(),
308 "availability");
309 assertEquals(node.at("/collection/operands/0/value").asText(),
Marc Kupietze4528ee2024-07-04 16:28:37 +0200310 "CC.*");
margaretha35e1ca22023-11-16 22:00:01 +0100311 assertEquals(node.at("/collection/operands/1/match").asText(),
312 "match:eq");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200313 assertEquals(node.at("/collection/operands/1/value").asText(), "ACA.*");
margaretha4e933c62017-06-27 16:28:40 +0200314 }
315
margarethad9e43ec2024-12-20 12:11:43 +0100316
margarethaebe869a2017-06-01 19:07:41 +0200317 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100318 public void testFreeAvailabilityAuthorized () throws KustvaktException {
319 Response response = searchQuery("availability = CC-BY-SA");
320 assertEquals(Status.OK.getStatusCode(), response.getStatus());
321
322 String source = """
323 {
324 "@type" : "koral:doc",
325 "match" : "match:eq",
326 "value" : "CC-BY-SA",
327 "key" : "availability"
328 }
329 """;
330
331 checkAndFree(response.readEntity(String.class),
332 JsonUtils.readTree(source));
333 }
334
335 @Test
336 public void testFreeAvailabilityUnauthorized () throws KustvaktException {
337 Response response = searchQuery("availability = ACA-NC");
338 assertEquals(Status.OK.getStatusCode(), response.getStatus());
339 String source = """
340 {
341 "@type" : "koral:doc",
342 "match" : "match:eq",
343 "value" : "ACA-NC",
344 "key" : "availability"
345 }
346 """;
347 checkAndFree(response.readEntity(String.class),
348 JsonUtils.readTree(source));
349 }
350
351 @Test
352 public void testFreeAvailabilityRegexAuthorized ()
353 throws KustvaktException {
354 Response response = searchQuery("availability = /.*BY.*/");
355 assertEquals(Status.OK.getStatusCode(), response.getStatus());
356 String source = """
357 {
358 "@type" : "koral:doc",
359 "match" : "match:eq",
360 "type" : "type:regex",
361 "value" : ".*BY.*",
362 "key" : "availability"
363 }
364 """;
365 checkAndFree(response.readEntity(String.class),
366 JsonUtils.readTree(source));
367 }
368
369
370 @Test
371 public void testFreeAvailabilityRegexUnauthorized ()
372 throws KustvaktException {
373 Response response = searchQuery("availability = /ACA.*/");
374 assertEquals(Status.OK.getStatusCode(), response.getStatus());
375 String source = """
376 {
377 "@type" : "koral:doc",
378 "match" : "match:eq",
379 "type" : "type:regex",
380 "value" : "ACA.*",
381 "key" : "availability"
382 }
383 """;
384 checkAndFree(response.readEntity(String.class),
385 JsonUtils.readTree(source));
386 }
387
388 @Test
389 public void testFreeAvailabilityRegexUnauthorized2 ()
margaretha35e1ca22023-11-16 22:00:01 +0100390 throws KustvaktException {
abcpro173fe8f22022-11-08 19:56:52 +0000391 Response response = searchQuery("availability = /.*NC.*/");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200392 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100393 String source = """
394 {
395 "@type" : "koral:doc",
396 "match" : "match:eq",
397 "type" : "type:regex",
398 "value" : ".*NC.*",
399 "key" : "availability"
400 }
401 """;
402 checkAndFree(response.readEntity(String.class),
403 JsonUtils.readTree(source));
404 }
405
406 @Test
407 public void testFreeAvailabilityOr () throws KustvaktException {
408 Response response = searchQuery(
409 "availability=/CC.*/ | availability=/ACA.*/");
410 assertEquals(Status.OK.getStatusCode(), response.getStatus());
411 String source = """
412 {
413 "operands" : [ {
414 "@type" : "koral:doc",
415 "match" : "match:eq",
416 "type" : "type:regex",
417 "value" : "CC.*",
418 "key" : "availability"
419 }, {
420 "@type" : "koral:doc",
421 "match" : "match:eq",
422 "type" : "type:regex",
423 "value" : "ACA.*",
424 "key" : "availability"
425 } ],
426 "@type" : "koral:docGroup",
427 "operation" : "operation:or"
428 }
429 """;
430 checkAndFree(response.readEntity(String.class),
431 JsonUtils.readTree(source));
432 }
433 @Test
434 public void testFreeAvailabilityOrCorpusSigle () throws KustvaktException {
435 Response response = searchQuery(
436 "availability=/CC.*/ | corpusSigle=GOE");
437 assertEquals(Status.OK.getStatusCode(), response.getStatus());
438 String source = """
439 {
440 "operands" : [ {
441 "@type" : "koral:doc",
442 "match" : "match:eq",
443 "type" : "type:regex",
444 "value" : "CC.*",
445 "key" : "availability"
446 }, {
447 "@type" : "koral:doc",
448 "match" : "match:eq",
449 "value" : "GOE",
450 "key" : "corpusSigle"
451 } ],
452 "@type" : "koral:docGroup",
453 "operation" : "operation:or"
454 }
455 """;
456 checkAndFree(response.readEntity(String.class),
457 JsonUtils.readTree(source));
margarethaebe869a2017-06-01 19:07:41 +0200458 }
459
margarethad9e43ec2024-12-20 12:11:43 +0100460
margarethaebe869a2017-06-01 19:07:41 +0200461 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100462 public void testFreeAvailabilityNegationUnauthorized ()
margaretha35e1ca22023-11-16 22:00:01 +0100463 throws KustvaktException {
Marc Kupietze4528ee2024-07-04 16:28:37 +0200464 Response response = searchQuery("availability != /CC.*/");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200465 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100466 String source = """
467 {
468 "@type" : "koral:doc",
469 "match" : "match:ne",
470 "type" : "type:regex",
471 "value" : "CC.*",
472 "key" : "availability"
473 }
474 """;
475 checkAndFree(response.readEntity(String.class),
476 JsonUtils.readTree(source));
margarethaebe869a2017-06-01 19:07:41 +0200477 }
478
margarethaebe869a2017-06-01 19:07:41 +0200479 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100480 public void testFreeAvailabilityNegationUnauthorized2 ()
margaretha35e1ca22023-11-16 22:00:01 +0100481 throws KustvaktException {
abcpro173fe8f22022-11-08 19:56:52 +0000482 Response response = searchQuery("availability != /.*BY.*/");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200483 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100484 String source = """
485 {
486 "@type" : "koral:doc",
487 "match" : "match:ne",
488 "type" : "type:regex",
489 "value" : ".*BY.*",
490 "key" : "availability"
491 }
492 """;
493 checkAndFree(response.readEntity(String.class),
494 JsonUtils.readTree(source));
margarethaebe869a2017-06-01 19:07:41 +0200495 }
496
margarethaed7bc7a2017-11-12 21:39:41 +0100497 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100498 public void testFreeAvailabilityNegationOrUnauthorized ()
margaretha35e1ca22023-11-16 22:00:01 +0100499 throws KustvaktException {
500 Response response = searchQuery(
Marc Kupietze4528ee2024-07-04 16:28:37 +0200501 "availability = /CC.*/ | availability != /CC.*/");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200502 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100503 String source = """
504 {
505 "operands" : [ {
506 "@type" : "koral:doc",
507 "match" : "match:eq",
508 "type" : "type:regex",
509 "value" : "CC.*",
510 "key" : "availability"
511 }, {
512 "@type" : "koral:doc",
513 "match" : "match:ne",
514 "type" : "type:regex",
515 "value" : "CC.*",
516 "key" : "availability"
517 } ],
518 "@type" : "koral:docGroup",
519 "operation" : "operation:or"
520 }
521 """;
522 checkAndFree(response.readEntity(String.class),
523 JsonUtils.readTree(source));
margarethaed7bc7a2017-11-12 21:39:41 +0100524 }
margarethaebe869a2017-06-01 19:07:41 +0200525
526 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100527 public void testFreeAvailabilityNegationAndUnauthorized ()
margaretha35e1ca22023-11-16 22:00:01 +0100528 throws KustvaktException {
529 Response response = searchQuery(
Marc Kupietze4528ee2024-07-04 16:28:37 +0200530 "textClass=politik & availability != /CC.*/");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200531 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100532 String source = """
533 {
534 "operands" : [ {
535 "@type" : "koral:doc",
536 "match" : "match:eq",
537 "value" : "politik",
538 "key" : "textClass"
539 }, {
540 "@type" : "koral:doc",
541 "match" : "match:ne",
542 "type" : "type:regex",
543 "value" : "CC.*",
544 "key" : "availability"
545 } ],
546 "@type" : "koral:docGroup",
547 "operation" : "operation:and"
548 }
549 """;
550 checkAndFree(response.readEntity(String.class),
551 JsonUtils.readTree(source));
margarethaebe869a2017-06-01 19:07:41 +0200552 }
553
margarethaebe869a2017-06-01 19:07:41 +0200554 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100555 public void testFreeAvailabilityAndUnauthorized ()
margaretha35e1ca22023-11-16 22:00:01 +0100556 throws KustvaktException {
557 Response response = searchQuery(
558 "textClass=politik & availability=ACA-NC");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200559 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100560 String source = """
561 {
562 "operands" : [ {
563 "@type" : "koral:doc",
564 "match" : "match:eq",
565 "value" : "politik",
566 "key" : "textClass"
567 }, {
568 "@type" : "koral:doc",
569 "match" : "match:eq",
570 "value" : "ACA-NC",
571 "key" : "availability"
572 } ],
573 "@type" : "koral:docGroup",
574 "operation" : "operation:and"
575 }
576 """;
577 checkAndFree(response.readEntity(String.class),
578 JsonUtils.readTree(source));
margarethaebe869a2017-06-01 19:07:41 +0200579 }
580
margarethaebe869a2017-06-01 19:07:41 +0200581 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100582 public void testFreeAvailabilityAndUnauthorized2 ()
margaretha35e1ca22023-11-16 22:00:01 +0100583 throws KustvaktException {
584 Response response = searchQuery(
585 "textClass=politik & availability=/.*NC.*/");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200586 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100587 String source = """
588 {
589 "operands" : [ {
590 "@type" : "koral:doc",
591 "match" : "match:eq",
592 "value" : "politik",
593 "key" : "textClass"
594 }, {
595 "@type" : "koral:doc",
596 "match" : "match:eq",
597 "type" : "type:regex",
598 "value" : ".*NC.*",
599 "key" : "availability"
600 } ],
601 "@type" : "koral:docGroup",
602 "operation" : "operation:and"
603 }
604 """;
605 checkAndFree(response.readEntity(String.class),
606 JsonUtils.readTree(source));
margarethaebe869a2017-06-01 19:07:41 +0200607 }
margarethad9e43ec2024-12-20 12:11:43 +0100608
margarethaebe869a2017-06-01 19:07:41 +0200609 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100610 public void testPublicAvailabilityNoRewrite () throws KustvaktException {
margaretha35e1ca22023-11-16 22:00:01 +0100611 Response response = searchQueryWithIP(
Marc Kupietze4528ee2024-07-04 16:28:37 +0200612 "availability=/CC.*/ | availability=/ACA.*/ | availability=/QAO-NC/",
margaretha35e1ca22023-11-16 22:00:01 +0100613 "149.27.0.32");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200614 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000615 String json = response.readEntity(String.class);
margarethade92c202018-02-20 14:38:40 +0100616 JsonNode node = JsonUtils.readTree(json);
617 assertTrue(node.at("/collection/rewrites").isMissingNode());
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200618 assertEquals(node.at("/collection/operation").asText(), "operation:or");
margarethade92c202018-02-20 14:38:40 +0100619 }
620
621 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100622 public void testPublicAvailabilityAuthorized () throws KustvaktException {
623 Response response = searchQueryWithIP("availability=ACA-NC",
624 "149.27.0.32");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200625 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100626 String source = """
627 {
628 "@type" : "koral:doc",
629 "match" : "match:eq",
630 "value" : "ACA-NC",
631 "key" : "availability"
632 }
633 """;
634
635 checkAndPublic(response.readEntity(String.class),
636 JsonUtils.readTree(source));
margarethade92c202018-02-20 14:38:40 +0100637 }
638
639 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100640 public void testPublicAvailabilityUnauthorized () throws KustvaktException {
641 Response response = searchQueryWithIP("availability=QAO-NC-LOC:ids",
642 "149.27.0.32");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200643 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100644
645 String source = """
646 {
647 "@type" : "koral:doc",
648 "match" : "match:eq",
649 "value" : "QAO-NC-LOC:ids",
650 "key" : "availability"
651 }
652 """;
653 checkAndPublic(response.readEntity(String.class),
654 JsonUtils.readTree(source));
margarethade92c202018-02-20 14:38:40 +0100655 }
656
657 @Test
margarethad9e43ec2024-12-20 12:11:43 +0100658 public void testPublicAvailabilityRegexAuthorized ()
659 throws KustvaktException {
660 Response response = searchQueryWithIP("availability= /ACA.*/",
661 "149.27.0.32");
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200662 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethad9e43ec2024-12-20 12:11:43 +0100663 String source = """
664 {
665 "@type" : "koral:doc",
666 "match" : "match:eq",
667 "type" : "type:regex",
668 "value" : "ACA.*",
669 "key" : "availability"
670 }
671 """;
672 checkAndPublic(response.readEntity(String.class),
673 JsonUtils.readTree(source));
674 }
675
676 @Test
677 public void testPublicAvailabilityNegationUnauthorized ()
678 throws KustvaktException {
679 Response response = searchQueryWithIP("availability != ACA-NC",
680 "149.27.0.32");
681 assertEquals(Status.OK.getStatusCode(), response.getStatus());
682 String source = """
683 {
684 "@type" : "koral:doc",
685 "match" : "match:ne",
686 "value" : "ACA-NC",
687 "key" : "availability"
688 }
689 """;
690 checkAndPublic(response.readEntity(String.class),
691 JsonUtils.readTree(source));
692 }
693
694 @Test
695 public void testPublicAvailabilityNegationRegexUnauthorized ()
696 throws KustvaktException {
697 Response response = searchQueryWithIP("availability != /ACA.*/",
698 "149.27.0.32");
699 assertEquals(Status.OK.getStatusCode(), response.getStatus());
700 String source = """
701 {
702 "@type" : "koral:doc",
703 "match" : "match:ne",
704 "type" : "type:regex",
705 "value" : "ACA.*",
706 "key" : "availability"
707 }
708 """;
709 checkAndPublic(response.readEntity(String.class),
710 JsonUtils.readTree(source));
711 }
712
713 @Test
714 public void testPublicAvailabilityAndUnauthorized ()
715 throws KustvaktException {
716 Response response = searchQueryWithIP(
717 "textClass=politik & availability=QAO-NC-LOC:ids",
718 "149.27.0.32");
719 assertEquals(Status.OK.getStatusCode(), response.getStatus());
720 String source = """
721 {
722 "operands" : [ {
723 "@type" : "koral:doc",
724 "match" : "match:eq",
725 "value" : "politik",
726 "key" : "textClass"
727 }, {
728 "@type" : "koral:doc",
729 "match" : "match:eq",
730 "value" : "QAO-NC-LOC:ids",
731 "key" : "availability"
732 } ],
733 "@type" : "koral:docGroup",
734 "operation" : "operation:and"
735 }
736 """;
737 checkAndPublic(response.readEntity(String.class),
738 JsonUtils.readTree(source));
739 }
740
741 @Test
742 public void testPublicAvailabilityNegationAndUnauthorized ()
743 throws KustvaktException {
744 Response response = searchQueryWithIP(
745 "textClass=politik & availability!=QAO-NC-LOC:ids",
746 "149.27.0.32");
747 assertEquals(Status.OK.getStatusCode(), response.getStatus());
748 String source = """
749 {
750 "operands" : [ {
751 "@type" : "koral:doc",
752 "match" : "match:eq",
753 "value" : "politik",
754 "key" : "textClass"
755 }, {
756 "@type" : "koral:doc",
757 "match" : "match:ne",
758 "value" : "QAO-NC-LOC:ids",
759 "key" : "availability"
760 } ],
761 "@type" : "koral:docGroup",
762 "operation" : "operation:and"
763 }
764 """;
765 checkAndPublic(response.readEntity(String.class),
766 JsonUtils.readTree(source));
767 }
768
769 @Test
770 public void testAllAvailabilityRegexAuthorized () throws KustvaktException {
771 Response response = searchQueryWithIP("availability= /ACA.*/",
772 "10.27.0.32");
773 assertEquals(Status.OK.getStatusCode(), response.getStatus());
774 String source = """
775 {
776 "@type" : "koral:doc",
777 "match" : "match:eq",
778 "type" : "type:regex",
779 "value" : "ACA.*",
780 "key" : "availability"
781 }
782 }
783 """;
784 checkAndAllWithACA(response.readEntity(String.class),
785 JsonUtils.readTree(source));
margarethade92c202018-02-20 14:38:40 +0100786 }
margarethaebe869a2017-06-01 19:07:41 +0200787}