blob: da68e6adce2e17021c1cd0668abe3ae722b65f5b [file] [log] [blame]
margaretha541b8cc2018-01-10 13:02:46 +01001package de.ids_mannheim.korap.web.controller;
margaretha0c47c652017-04-19 18:44:40 +02002
Marc Kupietzd43a98d2023-09-22 17:11:46 +02003import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertFalse;
5import static org.junit.jupiter.api.Assertions.assertNotEquals;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7import static org.junit.jupiter.api.Assertions.assertTrue;
margaretha0c47c652017-04-19 18:44:40 +02008
Marc Kupietzd43a98d2023-09-22 17:11:46 +02009import org.junit.jupiter.api.Disabled;
10import org.junit.jupiter.api.Test;
margaretha279ad6e2025-08-14 11:25:48 +020011import org.springframework.beans.factory.annotation.Autowired;
12
margaretha0c47c652017-04-19 18:44:40 +020013import com.fasterxml.jackson.databind.JsonNode;
margaretha58e18632018-02-15 13:04:42 +010014import com.google.common.net.HttpHeaders;
margaretha0c47c652017-04-19 18:44:40 +020015
margaretha56e8e552017-12-05 16:31:21 +010016import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
margaretha0c47c652017-04-19 18:44:40 +020017import de.ids_mannheim.korap.config.Attributes;
margaretha82c75c22024-12-11 09:36:50 +010018import de.ids_mannheim.korap.config.FullConfiguration;
margaretha7a25f012018-03-22 19:49:01 +010019import de.ids_mannheim.korap.config.SpringJerseyTest;
margaretha0c47c652017-04-19 18:44:40 +020020import de.ids_mannheim.korap.exceptions.KustvaktException;
margarethac1db9132019-08-28 11:32:04 +020021import de.ids_mannheim.korap.exceptions.StatusCodes;
margaretha0c47c652017-04-19 18:44:40 +020022import de.ids_mannheim.korap.query.serialize.QuerySerializer;
margaretha0c47c652017-04-19 18:44:40 +020023import de.ids_mannheim.korap.utils.JsonUtils;
margaretha279ad6e2025-08-14 11:25:48 +020024import jakarta.ws.rs.client.Entity;
25import jakarta.ws.rs.core.MediaType;
26import jakarta.ws.rs.core.Response;
27import jakarta.ws.rs.core.Response.Status;
margaretha0c47c652017-04-19 18:44:40 +020028
29/**
margaretha852a0f62019-02-19 12:14:30 +010030 * @author hanl, margaretha
margaretha3d55b002019-03-19 12:00:44 +010031 * @lastUpdate 18/03/2019
margaretha0c47c652017-04-19 18:44:40 +020032 */
margaretha7a25f012018-03-22 19:49:01 +010033public class SearchControllerTest extends SpringJerseyTest {
margaretha0c47c652017-04-19 18:44:40 +020034
Marc Kupietz805afde2020-03-24 09:08:50 +010035 @Autowired
margaretha82c75c22024-12-11 09:36:50 +010036 private FullConfiguration config;
margaretha279ad6e2025-08-14 11:25:48 +020037
38 private double apiVersion = Double.parseDouble(API_VERSION.substring(1));
Marc Kupietz805afde2020-03-24 09:08:50 +010039
margaretha35e1ca22023-11-16 22:00:01 +010040 private JsonNode requestSearchWithFields (String fields)
41 throws KustvaktException {
42 Response response = target().path(API_VERSION).path("search")
43 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
44 .queryParam("fields", fields).queryParam("context", "sentence")
45 .queryParam("count", "13").request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +020046 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000047 String query = response.readEntity(String.class);
margaretha852a0f62019-02-19 12:14:30 +010048 JsonNode node = JsonUtils.readTree(query);
49 return node;
50 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020051
margaretha35e1ca22023-11-16 22:00:01 +010052 private String createJsonQuery () {
margaretha279ad6e2025-08-14 11:25:48 +020053 QuerySerializer s = new QuerySerializer(apiVersion);
margaretha2544cdf2019-07-08 11:39:43 +020054 s.setQuery("[orth=der]", "poliqarp");
55 s.setCollection("corpusSigle=GOE");
56 s.setQuery("Wasser", "poliqarp");
57 return s.toJSON();
58 }
Marc Kupietz805afde2020-03-24 09:08:50 +010059
60 @Test
margaretha35e1ca22023-11-16 22:00:01 +010061 public void testApiWelcomeMessage () {
Marc Kupietzd43a98d2023-09-22 17:11:46 +020062 Response response = target().path(API_VERSION).path("").request().get();
63 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margaretha35e1ca22023-11-16 22:00:01 +010064 assertEquals(response.getHeaders().getFirst("X-Index-Revision"),
65 "Wes8Bd4h1OypPqbWF5njeQ==");
abcpro173fe8f22022-11-08 19:56:52 +000066 String message = response.readEntity(String.class);
Marc Kupietz805afde2020-03-24 09:08:50 +010067 assertEquals(message, config.getApiWelcomeMessage());
68 }
69
margaretha852a0f62019-02-19 12:14:30 +010070 @Test
margaretha35e1ca22023-11-16 22:00:01 +010071 public void testSearchShowTokens () throws KustvaktException {
72 Response response = target().path(API_VERSION).path("search")
73 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
74 .queryParam("show-tokens", true).request().get();
margarethaac85ae12023-05-08 11:09:13 +020075 assertEquals(Status.OK.getStatusCode(), response.getStatus());
76 String entity = response.readEntity(String.class);
77 JsonNode node = JsonUtils.readTree(entity);
78 assertEquals(3, node.at("/matches/0/tokens").size());
79 assertFalse(node.at("/matches/0/snippet").isMissingNode());
80 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020081
margarethaac85ae12023-05-08 11:09:13 +020082 @Test
margaretha35e1ca22023-11-16 22:00:01 +010083 public void testSearchDisableSnippet () throws KustvaktException {
84 Response response = target().path(API_VERSION).path("search")
85 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
86 .queryParam("show-snippet", false)
87 .queryParam("show-tokens", true).request().get();
margarethaac85ae12023-05-08 11:09:13 +020088 assertEquals(Status.OK.getStatusCode(), response.getStatus());
89 String entity = response.readEntity(String.class);
90 JsonNode node = JsonUtils.readTree(entity);
91 assertTrue(node.at("/matches/0/snippet").isMissingNode());
92 assertEquals(3, node.at("/matches/0/tokens").size());
93 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020094
margarethaac85ae12023-05-08 11:09:13 +020095 @Test
margaretha35e1ca22023-11-16 22:00:01 +010096 public void testSearchWithField () throws KustvaktException {
margaretha852a0f62019-02-19 12:14:30 +010097 JsonNode node = requestSearchWithFields("author");
98 assertNotEquals(0, node.at("/matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +010099 assertEquals("[\"author\"]", node.at("/meta/fields").toString());
margarethaac85ae12023-05-08 11:09:13 +0200100 assertTrue(node.at("/matches/0/tokens").isMissingNode());
margaretha852a0f62019-02-19 12:14:30 +0100101 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200102
margaretha852a0f62019-02-19 12:14:30 +0100103 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100104 public void testSearchWithMultipleFields () throws KustvaktException {
margaretha852a0f62019-02-19 12:14:30 +0100105 JsonNode node = requestSearchWithFields("author, title");
106 assertNotEquals(0, node.at("/matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100107 assertEquals("[\"author\",\"title\"]",
108 node.at("/meta/fields").toString());
margaretha852a0f62019-02-19 12:14:30 +0100109 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200110
margaretha0c47c652017-04-19 18:44:40 +0200111 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100112 public void testSearchQueryPublicCorpora () throws KustvaktException {
113 Response response = target().path(API_VERSION).path("search")
114 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
115 .request().accept(MediaType.APPLICATION_JSON).get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200116 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000117 String ent = response.readEntity(String.class);
margaretha0c47c652017-04-19 18:44:40 +0200118 JsonNode node = JsonUtils.readTree(ent);
119 assertNotNull(node);
margaretha1c0a41d2024-11-13 15:44:25 +0100120 assertEquals("koral:doc", node.at("/collection/@type").asText());
121 assertEquals("availability", node.at("/collection/key").asText());
122 assertEquals("CC.*", node.at("/collection/value").asText());
margaretha8489f862025-02-05 11:32:16 +0100123 assertEquals(freeCorpusAccess,
124 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100125 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100126 node.at("/collection/rewrites/0/operation").asText());
margaretha0c47c652017-04-19 18:44:40 +0200127 }
128
Akrond5058162018-07-05 11:17:15 +0200129 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100130 public void testSearchQueryFailure () throws KustvaktException {
131 Response response = target().path(API_VERSION).path("search")
132 .queryParam("q", "[orth=der").queryParam("ql", "poliqarp")
133 .queryParam("cq", "corpusSigle=WPD | corpusSigle=GOE")
134 .queryParam("count", "13").request()
135 .accept(MediaType.APPLICATION_JSON).get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200136 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000137 String ent = response.readEntity(String.class);
margaretha064eb6f2018-07-10 18:33:01 +0200138 JsonNode node = JsonUtils.readTree(ent);
Akrond5058162018-07-05 11:17:15 +0200139 assertNotNull(node);
140 assertEquals(302, node.at("/errors/0/0").asInt());
141 assertEquals(302, node.at("/errors/1/0").asInt());
margaretha064eb6f2018-07-10 18:33:01 +0200142 assertTrue(node.at("/errors/2").isMissingNode());
margaretha26f3fd52025-08-19 10:51:50 +0200143 assertFalse(node.at("/"+COLLECTION_NODE_NAME).isMissingNode());
Akrond5058162018-07-05 11:17:15 +0200144 assertEquals(13, node.at("/meta/count").asInt());
145 }
146
margaretha3fc0fd42017-05-02 18:08:31 +0200147 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100148 public void testSearchQueryWithMeta () throws KustvaktException {
149 Response response = target().path(API_VERSION).path("search")
150 .queryParam("q", "[orth=Bachelor]").queryParam("ql", "poliqarp")
151 .queryParam("cutoff", "true").queryParam("count", "5")
152 .queryParam("page", "1").queryParam("context", "40-t,30-t")
153 .request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200154 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000155 String ent = response.readEntity(String.class);
margaretha3fc0fd42017-05-02 18:08:31 +0200156 JsonNode node = JsonUtils.readTree(ent);
157 assertNotNull(node);
158 assertTrue(node.at("/meta/cutOff").asBoolean());
159 assertEquals(5, node.at("/meta/count").asInt());
160 assertEquals(0, node.at("/meta/startIndex").asInt());
margaretha1c0a41d2024-11-13 15:44:25 +0100161 assertEquals("token", node.at("/meta/context/left/0").asText());
margaretha3fc0fd42017-05-02 18:08:31 +0200162 assertEquals(40, node.at("/meta/context/left/1").asInt());
163 assertEquals(30, node.at("/meta/context/right/1").asInt());
margarethaa89c3f92017-05-30 19:02:08 +0200164 assertEquals(-1, node.at("/meta/totalResults").asInt());
margaretha35e1ca22023-11-16 22:00:01 +0100165 for (String path : new String[] { "/meta/count", "/meta/startIndex",
166 "/meta/context/left/1", "/meta/context/right/1",
167 "/meta/totalResults", "/meta/itemsPerPage" }) {
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200168 assertTrue(node.at(path).isNumber(), path + " should be a number");
Marc Kupietz824f2042023-09-18 14:50:21 +0200169 }
margaretha3fc0fd42017-05-02 18:08:31 +0200170 }
171
margarethaebe869a2017-06-01 19:07:41 +0200172 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100173 public void testSearchQueryFreeExtern () throws KustvaktException {
174 Response response = target().path(API_VERSION).path("search")
175 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
176 .request().header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
177 .get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200178 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000179 String entity = response.readEntity(String.class);
margarethaebe869a2017-06-01 19:07:41 +0200180 JsonNode node = JsonUtils.readTree(entity);
181 assertNotNull(node);
182 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100183 assertEquals("koral:doc", node.at("/collection/@type").asText());
184 assertEquals("availability", node.at("/collection/key").asText());
185 assertEquals("CC.*", node.at("/collection/value").asText());
margaretha8489f862025-02-05 11:32:16 +0100186 assertEquals(freeCorpusAccess,
187 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100188 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100189 node.at("/collection/rewrites/0/operation").asText());
margarethaebe869a2017-06-01 19:07:41 +0200190 }
margaretha064eb6f2018-07-10 18:33:01 +0200191
margarethaebe869a2017-06-01 19:07:41 +0200192 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100193 public void testSearchQueryFreeIntern () throws KustvaktException {
194 Response response = target().path(API_VERSION).path("search")
195 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
196 .request().header(HttpHeaders.X_FORWARDED_FOR, "172.27.0.32")
197 .get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200198 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000199 String entity = response.readEntity(String.class);
margarethaebe869a2017-06-01 19:07:41 +0200200 JsonNode node = JsonUtils.readTree(entity);
201 assertNotNull(node);
202 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100203 assertEquals("koral:doc", node.at("/collection/@type").asText());
204 assertEquals("availability", node.at("/collection/key").asText());
205 assertEquals("CC.*", node.at("/collection/value").asText());
margaretha8489f862025-02-05 11:32:16 +0100206 assertEquals(freeCorpusAccess,
207 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100208 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100209 node.at("/collection/rewrites/0/operation").asText());
margarethaebe869a2017-06-01 19:07:41 +0200210 }
margaretha064eb6f2018-07-10 18:33:01 +0200211
margaretha0c47c652017-04-19 18:44:40 +0200212 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100213 public void testSearchQueryExternAuthorized () throws KustvaktException {
214 Response response = target().path(API_VERSION).path("search")
215 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
216 .request()
217 .header(Attributes.AUTHORIZATION,
218 HttpAuthorizationHandler
219 .createBasicAuthorizationHeaderValue("kustvakt",
220 "kustvakt2015"))
221 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200222 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000223 String entity = response.readEntity(String.class);
margaretha61471cc2017-04-20 18:42:23 +0200224 JsonNode node = JsonUtils.readTree(entity);
margaretha064eb6f2018-07-10 18:33:01 +0200225 // System.out.println(entity);
margaretha0c47c652017-04-19 18:44:40 +0200226 assertNotNull(node);
margaretha65b67142017-05-29 16:23:16 +0200227 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100228 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
229 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
230 assertEquals("ACA.*",
231 node.at("/collection/operands/1/operands/0/value").asText());
232 assertEquals("QAO-NC",
233 node.at("/collection/operands/1/operands/1/value").asText());
234 assertEquals("operation:or", node.at("/collection/operation").asText());
margaretha8489f862025-02-05 11:32:16 +0100235 assertEquals(publicCorpusAccess,
236 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100237 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100238 node.at("/collection/rewrites/0/operation").asText());
margaretha0c47c652017-04-19 18:44:40 +0200239 }
240
margaretha65b67142017-05-29 16:23:16 +0200241 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100242 public void testSearchQueryInternAuthorized () throws KustvaktException {
243 Response response = target().path(API_VERSION).path("search")
244 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
245 .request()
246 .header(Attributes.AUTHORIZATION,
247 HttpAuthorizationHandler
248 .createBasicAuthorizationHeaderValue("kustvakt",
249 "kustvakt2015"))
250 .header(HttpHeaders.X_FORWARDED_FOR, "172.27.0.32").get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200251 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000252 String entity = response.readEntity(String.class);
margaretha65b67142017-05-29 16:23:16 +0200253 JsonNode node = JsonUtils.readTree(entity);
254 assertNotNull(node);
255 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100256 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
margaretha82c75c22024-12-11 09:36:50 +0100257 assertEquals(config.getFreeOnlyRegex(),
258 node.at("/collection/operands/0/value").asText());
margaretha1c0a41d2024-11-13 15:44:25 +0100259 assertEquals("ACA.*",
260 node.at("/collection/operands/1/operands/0/value").asText());
261 assertEquals("QAO-NC",
margaretha35e1ca22023-11-16 22:00:01 +0100262 node.at("/collection/operands/1/operands/1/operands/0/value")
margaretha1c0a41d2024-11-13 15:44:25 +0100263 .asText());
margaretha82c75c22024-12-11 09:36:50 +0100264 assertEquals(config.getAllOnlyRegex(),
margaretha35e1ca22023-11-16 22:00:01 +0100265 node.at("/collection/operands/1/operands/1/operands/1/value")
margaretha1c0a41d2024-11-13 15:44:25 +0100266 .asText());
267 assertEquals("operation:or", node.at("/collection/operation").asText());
margaretha8489f862025-02-05 11:32:16 +0100268 assertEquals(allCorpusAccess,
269 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100270 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100271 node.at("/collection/rewrites/0/operation").asText());
margaretha65b67142017-05-29 16:23:16 +0200272 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200273
margarethaac85ae12023-05-08 11:09:13 +0200274 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100275 public void testSearchWithCorpusQuery () throws KustvaktException {
276 Response response = target().path(API_VERSION).path("search")
277 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
278 .queryParam("cq", "corpusTitle=gingko").request()
279 .accept(MediaType.APPLICATION_JSON).get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200280 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethaac85ae12023-05-08 11:09:13 +0200281 String ent = response.readEntity(String.class);
282 JsonNode node = JsonUtils.readTree(ent);
margaretha1c0a41d2024-11-13 15:44:25 +0100283 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
284 assertEquals("operation:and",
285 node.at("/collection/operation").asText());
margarethaac85ae12023-05-08 11:09:13 +0200286 assertEquals(2, node.at("/collection/operands").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100287 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
288 assertEquals("gingko",
289 node.at("/collection/operands/1/value").asText());
290 assertEquals("match:eq",
291 node.at("/collection/operands/1/match").asText());
margarethaac85ae12023-05-08 11:09:13 +0200292 assertTrue(node.at("/collection/operands/1/type").isMissingNode());
293 }
margaretha0c47c652017-04-19 18:44:40 +0200294
295 @Test
margaretha1c0a41d2024-11-13 15:44:25 +0100296 public void testSearchWithCorpusQueryNoQuery () throws KustvaktException {
297 Response response = target().path(API_VERSION).path("search")
298 .queryParam("q", "NOQUERY").queryParam("ql", "poliqarp")
299 .queryParam("count", 0).queryParam("cutoff", "true")
300 .queryParam("cq", "corpusSigle=WPD17").request()
301 .accept(MediaType.APPLICATION_JSON).get();
302
303 assertEquals(Status.OK.getStatusCode(), response.getStatus());
304
305 String ent = response.readEntity(String.class);
306 JsonNode node = JsonUtils.readTree(ent);
307
308 assertEquals(0, node.at("/meta/count").asInt());
309 assertEquals(-1, node.at("/meta/totalResults").asInt());
310 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
311 assertEquals("corpusSigle",
312 node.at("/collection/operands/1/key").asText());
313 assertEquals("WPD17", node.at("/collection/operands/1/value").asText());
314 assertEquals("match:eq",
315 node.at("/collection/operands/1/match").asText());
316 }
317
318 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100319 public void testSearchQueryWithCollectionQueryAuthorizedWithoutIP ()
320 throws KustvaktException {
321 Response response = target().path(API_VERSION).path("search")
322 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
323 .queryParam("cq", "textClass=politik & corpusSigle=BRZ10")
324 .request()
325 .header(Attributes.AUTHORIZATION,
326 HttpAuthorizationHandler
327 .createBasicAuthorizationHeaderValue("kustvakt",
328 "kustvakt2015"))
329 .get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200330 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000331 JsonNode node = JsonUtils.readTree(response.readEntity(String.class));
margaretha0c47c652017-04-19 18:44:40 +0200332 assertNotNull(node);
margarethad9e43ec2024-12-20 12:11:43 +0100333 assertEquals("operation:override",
margaretha1c0a41d2024-11-13 15:44:25 +0100334 node.at("/collection/rewrites/0/operation").asText());
margarethad9e43ec2024-12-20 12:11:43 +0100335// assertEquals("availability(FREE)",
336// node.at("/collection/rewrites/0/scope").asText());
margarethaa89c3f92017-05-30 19:02:08 +0200337 // EM: double AND operations
margaretha1c0a41d2024-11-13 15:44:25 +0100338 assertEquals("availability",
339 node.at("/collection/operands/0/key").asText());
340 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
341 assertEquals("textClass",
342 node.at("/collection/operands/1/operands/0/key").asText());
343 assertEquals("corpusSigle",
344 node.at("/collection/operands/1/operands/1/key").asText());
margaretha0c47c652017-04-19 18:44:40 +0200345 }
margaretha064eb6f2018-07-10 18:33:01 +0200346
margarethaa89c3f92017-05-30 19:02:08 +0200347 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200348 @Disabled
margaretha35e1ca22023-11-16 22:00:01 +0100349 public void testSearchQueryAuthorizedWithoutIP () throws KustvaktException {
350 Response response = target().path(API_VERSION).path("search")
351 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
352 .request()
353 .header(Attributes.AUTHORIZATION,
354 HttpAuthorizationHandler
355 .createBasicAuthorizationHeaderValue("kustvakt",
356 "kustvakt2015"))
357 .get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200358 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000359 String entity = response.readEntity(String.class);
margarethaebe869a2017-06-01 19:07:41 +0200360 JsonNode node = JsonUtils.readTree(entity);
margarethaa89c3f92017-05-30 19:02:08 +0200361 assertNotNull(node);
margarethaebe869a2017-06-01 19:07:41 +0200362 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100363 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
364 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
365 assertEquals("ACA.*", node.at("/collection/operands/1/value").asText());
366 assertEquals("operation:or", node.at("/collection/operation").asText());
margaretha8489f862025-02-05 11:32:16 +0100367 assertEquals(publicCorpusAccess,
margaretha1c0a41d2024-11-13 15:44:25 +0100368 node.at("/collection/rewrites/0/scope").asText());
margarethaa89c3f92017-05-30 19:02:08 +0200369 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200370
margarethac1db9132019-08-28 11:32:04 +0200371 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100372 public void testSearchWithInvalidPage () throws KustvaktException {
373 Response response = target().path(API_VERSION).path("search")
374 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
375 .queryParam("page", "0").request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200376 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000377 String entity = response.readEntity(String.class);
margarethac1db9132019-08-28 11:32:04 +0200378 JsonNode node = JsonUtils.readTree(entity);
margaretha35e1ca22023-11-16 22:00:01 +0100379 assertEquals(StatusCodes.INVALID_ARGUMENT,
380 node.at("/errors/0/0").asInt());
margaretha1c0a41d2024-11-13 15:44:25 +0100381 assertEquals("page must start from 1", node.at("/errors/0/1").asText());
margarethac1db9132019-08-28 11:32:04 +0200382 }
margaretha064eb6f2018-07-10 18:33:01 +0200383
margaretha0c47c652017-04-19 18:44:40 +0200384 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100385 public void testSearchSentenceMeta () throws KustvaktException {
386 Response response = target().path(API_VERSION).path("search")
387 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
388 .queryParam("context", "sentence").request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200389 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000390 String ent = response.readEntity(String.class);
margaretha0c47c652017-04-19 18:44:40 +0200391 JsonNode node = JsonUtils.readTree(ent);
392 assertNotNull(node);
margaretha1c0a41d2024-11-13 15:44:25 +0100393 assertEquals("base/s:s", node.at("/meta/context").asText());
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200394 assertNotEquals("/meta/version", "${project.version}");
margaretha0c47c652017-04-19 18:44:40 +0200395 }
396
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200397 // EM: The API is disabled
398 @Disabled
margaretha0c47c652017-04-19 18:44:40 +0200399 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100400 public void testSearchSimpleCQL () throws KustvaktException {
margaretha279ad6e2025-08-14 11:25:48 +0200401 QuerySerializer s = new QuerySerializer(apiVersion);
margaretha0c47c652017-04-19 18:44:40 +0200402 s.setQuery("(der) or (das)", "CQL");
margaretha35e1ca22023-11-16 22:00:01 +0100403 Response response = target().path(API_VERSION).path("search").request()
404 .post(Entity.json(s.toJSON()));
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200405 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000406 String ent = response.readEntity(String.class);
margaretha0c47c652017-04-19 18:44:40 +0200407 JsonNode node = JsonUtils.readTree(ent);
408 assertNotNull(node);
409 assertNotEquals(0, node.path("matches").size());
margaretha064eb6f2018-07-10 18:33:01 +0200410 // assertEquals(17027, node.at("/meta/totalResults").asInt());
margaretha0c47c652017-04-19 18:44:40 +0200411 }
412
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200413 // EM: The API is disabled
margaretha0c47c652017-04-19 18:44:40 +0200414 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200415 @Disabled
margaretha35e1ca22023-11-16 22:00:01 +0100416 public void testSearchRawQuery () throws KustvaktException {
417 Response response = target().path(API_VERSION).path("search").request()
418 .post(Entity.json(createJsonQuery()));
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200419 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000420 String ent = response.readEntity(String.class);
margaretha0c47c652017-04-19 18:44:40 +0200421 JsonNode node = JsonUtils.readTree(ent);
422 assertNotNull(node);
423 assertNotEquals(0, node.path("matches").size());
margaretha8489f862025-02-05 11:32:16 +0100424 assertEquals(freeCorpusAccess,
margaretha1c0a41d2024-11-13 15:44:25 +0100425 node.at("/collection/rewrites/0/scope").asText());
margaretha2544cdf2019-07-08 11:39:43 +0200426 }
margaretha2544cdf2019-07-08 11:39:43 +0200427
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200428 // EM: The API is disabled
429 @Test
430 @Disabled
margaretha35e1ca22023-11-16 22:00:01 +0100431 public void testSearchPostAll () throws KustvaktException {
432 Response response = target().path(API_VERSION).path("search").request()
433 .header(HttpHeaders.X_FORWARDED_FOR, "10.27.0.32")
434 .header(Attributes.AUTHORIZATION,
435 HttpAuthorizationHandler
436 .createBasicAuthorizationHeaderValue("kustvakt",
437 "kustvakt2015"))
438 .post(Entity.json(createJsonQuery()));
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200439 assertEquals(Status.OK.getStatusCode(), response.getStatus());
440 String ent = response.readEntity(String.class);
margaretha2544cdf2019-07-08 11:39:43 +0200441 JsonNode node = JsonUtils.readTree(ent);
442 assertNotNull(node);
443 assertNotEquals(0, node.path("matches").size());
margaretha8489f862025-02-05 11:32:16 +0100444 assertEquals(allCorpusAccess,
margaretha1c0a41d2024-11-13 15:44:25 +0100445 node.at("/collection/rewrites/0/scope").asText());
margaretha2544cdf2019-07-08 11:39:43 +0200446 }
margaretha2544cdf2019-07-08 11:39:43 +0200447
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200448 // EM: The API is disabled
449 @Test
450 @Disabled
margaretha35e1ca22023-11-16 22:00:01 +0100451 public void testSearchPostPublic () throws KustvaktException {
452 Response response = target().path(API_VERSION).path("search").request()
453 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
454 .header(Attributes.AUTHORIZATION,
455 HttpAuthorizationHandler
456 .createBasicAuthorizationHeaderValue("kustvakt",
457 "kustvakt2015"))
458 .post(Entity.json(createJsonQuery()));
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200459 assertEquals(Status.OK.getStatusCode(), response.getStatus());
460 String ent = response.readEntity(String.class);
margaretha2544cdf2019-07-08 11:39:43 +0200461 JsonNode node = JsonUtils.readTree(ent);
462 assertNotNull(node);
463 assertNotEquals(0, node.path("matches").size());
margaretha8489f862025-02-05 11:32:16 +0100464 assertEquals(publicCorpusAccess,
margaretha1c0a41d2024-11-13 15:44:25 +0100465 node.at("/collection/rewrites/0/scope").asText());
margaretha0c47c652017-04-19 18:44:40 +0200466 }
margaretha0c47c652017-04-19 18:44:40 +0200467}