blob: a8db188322c5c4429358360bf06a5faedd7f0dda [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
margaretha96c309d2023-08-16 12:24:12 +02009import jakarta.ws.rs.client.Entity;
10import jakarta.ws.rs.core.MediaType;
11import jakarta.ws.rs.core.Response.Status;
margaretha3d55b002019-03-19 12:00:44 +010012
Marc Kupietz805afde2020-03-24 09:08:50 +010013import de.ids_mannheim.korap.config.KustvaktConfiguration;
Marc Kupietzd43a98d2023-09-22 17:11:46 +020014import org.junit.jupiter.api.Disabled;
15import org.junit.jupiter.api.Test;
margaretha0c47c652017-04-19 18:44:40 +020016import com.fasterxml.jackson.databind.JsonNode;
margaretha58e18632018-02-15 13:04:42 +010017import com.google.common.net.HttpHeaders;
margaretha96c309d2023-08-16 12:24:12 +020018import jakarta.ws.rs.core.Response;
margaretha0c47c652017-04-19 18:44:40 +020019
margaretha56e8e552017-12-05 16:31:21 +010020import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
margaretha0c47c652017-04-19 18:44:40 +020021import de.ids_mannheim.korap.config.Attributes;
margaretha82c75c22024-12-11 09:36:50 +010022import de.ids_mannheim.korap.config.FullConfiguration;
margaretha7a25f012018-03-22 19:49:01 +010023import de.ids_mannheim.korap.config.SpringJerseyTest;
margaretha0c47c652017-04-19 18:44:40 +020024import de.ids_mannheim.korap.exceptions.KustvaktException;
margarethac1db9132019-08-28 11:32:04 +020025import de.ids_mannheim.korap.exceptions.StatusCodes;
margaretha0c47c652017-04-19 18:44:40 +020026import de.ids_mannheim.korap.query.serialize.QuerySerializer;
margaretha0c47c652017-04-19 18:44:40 +020027import de.ids_mannheim.korap.utils.JsonUtils;
Marc Kupietz805afde2020-03-24 09:08:50 +010028import org.springframework.beans.factory.annotation.Autowired;
margaretha0c47c652017-04-19 18:44:40 +020029
30/**
margaretha852a0f62019-02-19 12:14:30 +010031 * @author hanl, margaretha
margaretha3d55b002019-03-19 12:00:44 +010032 * @lastUpdate 18/03/2019
margaretha0c47c652017-04-19 18:44:40 +020033 */
margaretha7a25f012018-03-22 19:49:01 +010034public class SearchControllerTest extends SpringJerseyTest {
margaretha0c47c652017-04-19 18:44:40 +020035
Marc Kupietz805afde2020-03-24 09:08:50 +010036 @Autowired
margaretha82c75c22024-12-11 09:36:50 +010037 private FullConfiguration config;
Marc Kupietz805afde2020-03-24 09:08:50 +010038
margaretha35e1ca22023-11-16 22:00:01 +010039 private JsonNode requestSearchWithFields (String fields)
40 throws KustvaktException {
41 Response response = target().path(API_VERSION).path("search")
42 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
43 .queryParam("fields", fields).queryParam("context", "sentence")
44 .queryParam("count", "13").request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +020045 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000046 String query = response.readEntity(String.class);
margaretha852a0f62019-02-19 12:14:30 +010047 JsonNode node = JsonUtils.readTree(query);
48 return node;
49 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020050
margaretha35e1ca22023-11-16 22:00:01 +010051 private String createJsonQuery () {
margaretha2544cdf2019-07-08 11:39:43 +020052 QuerySerializer s = new QuerySerializer();
53 s.setQuery("[orth=der]", "poliqarp");
54 s.setCollection("corpusSigle=GOE");
55 s.setQuery("Wasser", "poliqarp");
56 return s.toJSON();
57 }
Marc Kupietz805afde2020-03-24 09:08:50 +010058
59 @Test
margaretha35e1ca22023-11-16 22:00:01 +010060 public void testApiWelcomeMessage () {
Marc Kupietzd43a98d2023-09-22 17:11:46 +020061 Response response = target().path(API_VERSION).path("").request().get();
62 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margaretha35e1ca22023-11-16 22:00:01 +010063 assertEquals(response.getHeaders().getFirst("X-Index-Revision"),
64 "Wes8Bd4h1OypPqbWF5njeQ==");
abcpro173fe8f22022-11-08 19:56:52 +000065 String message = response.readEntity(String.class);
Marc Kupietz805afde2020-03-24 09:08:50 +010066 assertEquals(message, config.getApiWelcomeMessage());
67 }
68
margaretha852a0f62019-02-19 12:14:30 +010069 @Test
margaretha35e1ca22023-11-16 22:00:01 +010070 public void testSearchShowTokens () throws KustvaktException {
71 Response response = target().path(API_VERSION).path("search")
72 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
73 .queryParam("show-tokens", true).request().get();
margarethaac85ae12023-05-08 11:09:13 +020074 assertEquals(Status.OK.getStatusCode(), response.getStatus());
75 String entity = response.readEntity(String.class);
76 JsonNode node = JsonUtils.readTree(entity);
77 assertEquals(3, node.at("/matches/0/tokens").size());
78 assertFalse(node.at("/matches/0/snippet").isMissingNode());
79 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020080
margarethaac85ae12023-05-08 11:09:13 +020081 @Test
margaretha35e1ca22023-11-16 22:00:01 +010082 public void testSearchDisableSnippet () throws KustvaktException {
83 Response response = target().path(API_VERSION).path("search")
84 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
85 .queryParam("show-snippet", false)
86 .queryParam("show-tokens", true).request().get();
margarethaac85ae12023-05-08 11:09:13 +020087 assertEquals(Status.OK.getStatusCode(), response.getStatus());
88 String entity = response.readEntity(String.class);
89 JsonNode node = JsonUtils.readTree(entity);
90 assertTrue(node.at("/matches/0/snippet").isMissingNode());
91 assertEquals(3, node.at("/matches/0/tokens").size());
92 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020093
margarethaac85ae12023-05-08 11:09:13 +020094 @Test
margaretha35e1ca22023-11-16 22:00:01 +010095 public void testSearchWithField () throws KustvaktException {
margaretha852a0f62019-02-19 12:14:30 +010096 JsonNode node = requestSearchWithFields("author");
97 assertNotEquals(0, node.at("/matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +010098 assertEquals("[\"author\"]", node.at("/meta/fields").toString());
margarethaac85ae12023-05-08 11:09:13 +020099 assertTrue(node.at("/matches/0/tokens").isMissingNode());
margaretha852a0f62019-02-19 12:14:30 +0100100 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200101
margaretha852a0f62019-02-19 12:14:30 +0100102 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100103 public void testSearchWithMultipleFields () throws KustvaktException {
margaretha852a0f62019-02-19 12:14:30 +0100104 JsonNode node = requestSearchWithFields("author, title");
105 assertNotEquals(0, node.at("/matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100106 assertEquals("[\"author\",\"title\"]",
107 node.at("/meta/fields").toString());
margaretha852a0f62019-02-19 12:14:30 +0100108 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200109
margaretha0c47c652017-04-19 18:44:40 +0200110 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100111 public void testSearchQueryPublicCorpora () throws KustvaktException {
112 Response response = target().path(API_VERSION).path("search")
113 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
114 .request().accept(MediaType.APPLICATION_JSON).get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200115 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000116 String ent = response.readEntity(String.class);
margaretha0c47c652017-04-19 18:44:40 +0200117 JsonNode node = JsonUtils.readTree(ent);
118 assertNotNull(node);
margaretha1c0a41d2024-11-13 15:44:25 +0100119 assertEquals("koral:doc", node.at("/collection/@type").asText());
120 assertEquals("availability", node.at("/collection/key").asText());
121 assertEquals("CC.*", node.at("/collection/value").asText());
margaretha8489f862025-02-05 11:32:16 +0100122 assertEquals(freeCorpusAccess,
123 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100124 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100125 node.at("/collection/rewrites/0/operation").asText());
margaretha0c47c652017-04-19 18:44:40 +0200126 }
127
Akrond5058162018-07-05 11:17:15 +0200128 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100129 public void testSearchQueryFailure () throws KustvaktException {
130 Response response = target().path(API_VERSION).path("search")
131 .queryParam("q", "[orth=der").queryParam("ql", "poliqarp")
132 .queryParam("cq", "corpusSigle=WPD | corpusSigle=GOE")
133 .queryParam("count", "13").request()
134 .accept(MediaType.APPLICATION_JSON).get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200135 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000136 String ent = response.readEntity(String.class);
margaretha064eb6f2018-07-10 18:33:01 +0200137 JsonNode node = JsonUtils.readTree(ent);
Akrond5058162018-07-05 11:17:15 +0200138 assertNotNull(node);
139 assertEquals(302, node.at("/errors/0/0").asInt());
140 assertEquals(302, node.at("/errors/1/0").asInt());
margaretha064eb6f2018-07-10 18:33:01 +0200141 assertTrue(node.at("/errors/2").isMissingNode());
142 assertFalse(node.at("/collection").isMissingNode());
Akrond5058162018-07-05 11:17:15 +0200143 assertEquals(13, node.at("/meta/count").asInt());
144 }
145
margaretha3fc0fd42017-05-02 18:08:31 +0200146 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100147 public void testSearchQueryWithMeta () throws KustvaktException {
148 Response response = target().path(API_VERSION).path("search")
149 .queryParam("q", "[orth=Bachelor]").queryParam("ql", "poliqarp")
150 .queryParam("cutoff", "true").queryParam("count", "5")
151 .queryParam("page", "1").queryParam("context", "40-t,30-t")
152 .request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200153 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000154 String ent = response.readEntity(String.class);
margaretha3fc0fd42017-05-02 18:08:31 +0200155 JsonNode node = JsonUtils.readTree(ent);
156 assertNotNull(node);
157 assertTrue(node.at("/meta/cutOff").asBoolean());
158 assertEquals(5, node.at("/meta/count").asInt());
159 assertEquals(0, node.at("/meta/startIndex").asInt());
margaretha1c0a41d2024-11-13 15:44:25 +0100160 assertEquals("token", node.at("/meta/context/left/0").asText());
margaretha3fc0fd42017-05-02 18:08:31 +0200161 assertEquals(40, node.at("/meta/context/left/1").asInt());
162 assertEquals(30, node.at("/meta/context/right/1").asInt());
margarethaa89c3f92017-05-30 19:02:08 +0200163 assertEquals(-1, node.at("/meta/totalResults").asInt());
margaretha35e1ca22023-11-16 22:00:01 +0100164 for (String path : new String[] { "/meta/count", "/meta/startIndex",
165 "/meta/context/left/1", "/meta/context/right/1",
166 "/meta/totalResults", "/meta/itemsPerPage" }) {
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200167 assertTrue(node.at(path).isNumber(), path + " should be a number");
Marc Kupietz824f2042023-09-18 14:50:21 +0200168 }
margaretha3fc0fd42017-05-02 18:08:31 +0200169 }
170
margarethaebe869a2017-06-01 19:07:41 +0200171 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100172 public void testSearchQueryFreeExtern () throws KustvaktException {
173 Response response = target().path(API_VERSION).path("search")
174 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
175 .request().header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
176 .get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200177 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000178 String entity = response.readEntity(String.class);
margarethaebe869a2017-06-01 19:07:41 +0200179 JsonNode node = JsonUtils.readTree(entity);
180 assertNotNull(node);
181 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100182 assertEquals("koral:doc", node.at("/collection/@type").asText());
183 assertEquals("availability", node.at("/collection/key").asText());
184 assertEquals("CC.*", node.at("/collection/value").asText());
margaretha8489f862025-02-05 11:32:16 +0100185 assertEquals(freeCorpusAccess,
186 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100187 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100188 node.at("/collection/rewrites/0/operation").asText());
margarethaebe869a2017-06-01 19:07:41 +0200189 }
margaretha064eb6f2018-07-10 18:33:01 +0200190
margarethaebe869a2017-06-01 19:07:41 +0200191 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100192 public void testSearchQueryFreeIntern () throws KustvaktException {
193 Response response = target().path(API_VERSION).path("search")
194 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
195 .request().header(HttpHeaders.X_FORWARDED_FOR, "172.27.0.32")
196 .get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200197 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000198 String entity = response.readEntity(String.class);
margarethaebe869a2017-06-01 19:07:41 +0200199 JsonNode node = JsonUtils.readTree(entity);
200 assertNotNull(node);
201 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100202 assertEquals("koral:doc", node.at("/collection/@type").asText());
203 assertEquals("availability", node.at("/collection/key").asText());
204 assertEquals("CC.*", node.at("/collection/value").asText());
margaretha8489f862025-02-05 11:32:16 +0100205 assertEquals(freeCorpusAccess,
206 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100207 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100208 node.at("/collection/rewrites/0/operation").asText());
margarethaebe869a2017-06-01 19:07:41 +0200209 }
margaretha064eb6f2018-07-10 18:33:01 +0200210
margaretha0c47c652017-04-19 18:44:40 +0200211 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100212 public void testSearchQueryExternAuthorized () throws KustvaktException {
213 Response response = target().path(API_VERSION).path("search")
214 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
215 .request()
216 .header(Attributes.AUTHORIZATION,
217 HttpAuthorizationHandler
218 .createBasicAuthorizationHeaderValue("kustvakt",
219 "kustvakt2015"))
220 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200221 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000222 String entity = response.readEntity(String.class);
margaretha61471cc2017-04-20 18:42:23 +0200223 JsonNode node = JsonUtils.readTree(entity);
margaretha064eb6f2018-07-10 18:33:01 +0200224 // System.out.println(entity);
margaretha0c47c652017-04-19 18:44:40 +0200225 assertNotNull(node);
margaretha65b67142017-05-29 16:23:16 +0200226 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100227 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
228 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
229 assertEquals("ACA.*",
230 node.at("/collection/operands/1/operands/0/value").asText());
231 assertEquals("QAO-NC",
232 node.at("/collection/operands/1/operands/1/value").asText());
233 assertEquals("operation:or", node.at("/collection/operation").asText());
margaretha8489f862025-02-05 11:32:16 +0100234 assertEquals(publicCorpusAccess,
235 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100236 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100237 node.at("/collection/rewrites/0/operation").asText());
margaretha0c47c652017-04-19 18:44:40 +0200238 }
239
margaretha65b67142017-05-29 16:23:16 +0200240 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100241 public void testSearchQueryInternAuthorized () throws KustvaktException {
242 Response response = target().path(API_VERSION).path("search")
243 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
244 .request()
245 .header(Attributes.AUTHORIZATION,
246 HttpAuthorizationHandler
247 .createBasicAuthorizationHeaderValue("kustvakt",
248 "kustvakt2015"))
249 .header(HttpHeaders.X_FORWARDED_FOR, "172.27.0.32").get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200250 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000251 String entity = response.readEntity(String.class);
margaretha65b67142017-05-29 16:23:16 +0200252 JsonNode node = JsonUtils.readTree(entity);
253 assertNotNull(node);
254 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100255 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
margaretha82c75c22024-12-11 09:36:50 +0100256 assertEquals(config.getFreeOnlyRegex(),
257 node.at("/collection/operands/0/value").asText());
margaretha1c0a41d2024-11-13 15:44:25 +0100258 assertEquals("ACA.*",
259 node.at("/collection/operands/1/operands/0/value").asText());
260 assertEquals("QAO-NC",
margaretha35e1ca22023-11-16 22:00:01 +0100261 node.at("/collection/operands/1/operands/1/operands/0/value")
margaretha1c0a41d2024-11-13 15:44:25 +0100262 .asText());
margaretha82c75c22024-12-11 09:36:50 +0100263 assertEquals(config.getAllOnlyRegex(),
margaretha35e1ca22023-11-16 22:00:01 +0100264 node.at("/collection/operands/1/operands/1/operands/1/value")
margaretha1c0a41d2024-11-13 15:44:25 +0100265 .asText());
266 assertEquals("operation:or", node.at("/collection/operation").asText());
margaretha8489f862025-02-05 11:32:16 +0100267 assertEquals(allCorpusAccess,
268 node.at("/collection/rewrites/0/_comment").asText());
margaretha9d820462024-11-25 16:11:57 +0100269 assertEquals("operation:injection",
margaretha1c0a41d2024-11-13 15:44:25 +0100270 node.at("/collection/rewrites/0/operation").asText());
margaretha65b67142017-05-29 16:23:16 +0200271 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200272
margarethaac85ae12023-05-08 11:09:13 +0200273 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100274 public void testSearchWithCorpusQuery () throws KustvaktException {
275 Response response = target().path(API_VERSION).path("search")
276 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
277 .queryParam("cq", "corpusTitle=gingko").request()
278 .accept(MediaType.APPLICATION_JSON).get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200279 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margarethaac85ae12023-05-08 11:09:13 +0200280 String ent = response.readEntity(String.class);
281 JsonNode node = JsonUtils.readTree(ent);
margaretha1c0a41d2024-11-13 15:44:25 +0100282 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
283 assertEquals("operation:and",
284 node.at("/collection/operation").asText());
margarethaac85ae12023-05-08 11:09:13 +0200285 assertEquals(2, node.at("/collection/operands").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100286 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
287 assertEquals("gingko",
288 node.at("/collection/operands/1/value").asText());
289 assertEquals("match:eq",
290 node.at("/collection/operands/1/match").asText());
margarethaac85ae12023-05-08 11:09:13 +0200291 assertTrue(node.at("/collection/operands/1/type").isMissingNode());
292 }
margaretha0c47c652017-04-19 18:44:40 +0200293
294 @Test
margaretha1c0a41d2024-11-13 15:44:25 +0100295 public void testSearchWithCorpusQueryNoQuery () throws KustvaktException {
296 Response response = target().path(API_VERSION).path("search")
297 .queryParam("q", "NOQUERY").queryParam("ql", "poliqarp")
298 .queryParam("count", 0).queryParam("cutoff", "true")
299 .queryParam("cq", "corpusSigle=WPD17").request()
300 .accept(MediaType.APPLICATION_JSON).get();
301
302 assertEquals(Status.OK.getStatusCode(), response.getStatus());
303
304 String ent = response.readEntity(String.class);
305 JsonNode node = JsonUtils.readTree(ent);
306
307 assertEquals(0, node.at("/meta/count").asInt());
308 assertEquals(-1, node.at("/meta/totalResults").asInt());
309 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
310 assertEquals("corpusSigle",
311 node.at("/collection/operands/1/key").asText());
312 assertEquals("WPD17", node.at("/collection/operands/1/value").asText());
313 assertEquals("match:eq",
314 node.at("/collection/operands/1/match").asText());
315 }
316
317 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100318 public void testSearchQueryWithCollectionQueryAuthorizedWithoutIP ()
319 throws KustvaktException {
320 Response response = target().path(API_VERSION).path("search")
321 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
322 .queryParam("cq", "textClass=politik & corpusSigle=BRZ10")
323 .request()
324 .header(Attributes.AUTHORIZATION,
325 HttpAuthorizationHandler
326 .createBasicAuthorizationHeaderValue("kustvakt",
327 "kustvakt2015"))
328 .get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200329 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000330 JsonNode node = JsonUtils.readTree(response.readEntity(String.class));
margaretha0c47c652017-04-19 18:44:40 +0200331 assertNotNull(node);
margarethad9e43ec2024-12-20 12:11:43 +0100332 assertEquals("operation:override",
margaretha1c0a41d2024-11-13 15:44:25 +0100333 node.at("/collection/rewrites/0/operation").asText());
margarethad9e43ec2024-12-20 12:11:43 +0100334// assertEquals("availability(FREE)",
335// node.at("/collection/rewrites/0/scope").asText());
margarethaa89c3f92017-05-30 19:02:08 +0200336 // EM: double AND operations
margaretha1c0a41d2024-11-13 15:44:25 +0100337 assertEquals("availability",
338 node.at("/collection/operands/0/key").asText());
339 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
340 assertEquals("textClass",
341 node.at("/collection/operands/1/operands/0/key").asText());
342 assertEquals("corpusSigle",
343 node.at("/collection/operands/1/operands/1/key").asText());
margaretha0c47c652017-04-19 18:44:40 +0200344 }
margaretha064eb6f2018-07-10 18:33:01 +0200345
margarethaa89c3f92017-05-30 19:02:08 +0200346 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200347 @Disabled
margaretha35e1ca22023-11-16 22:00:01 +0100348 public void testSearchQueryAuthorizedWithoutIP () throws KustvaktException {
349 Response response = target().path(API_VERSION).path("search")
350 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
351 .request()
352 .header(Attributes.AUTHORIZATION,
353 HttpAuthorizationHandler
354 .createBasicAuthorizationHeaderValue("kustvakt",
355 "kustvakt2015"))
356 .get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200357 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000358 String entity = response.readEntity(String.class);
margarethaebe869a2017-06-01 19:07:41 +0200359 JsonNode node = JsonUtils.readTree(entity);
margarethaa89c3f92017-05-30 19:02:08 +0200360 assertNotNull(node);
margarethaebe869a2017-06-01 19:07:41 +0200361 assertNotEquals(0, node.path("matches").size());
margaretha1c0a41d2024-11-13 15:44:25 +0100362 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
363 assertEquals("CC.*", node.at("/collection/operands/0/value").asText());
364 assertEquals("ACA.*", node.at("/collection/operands/1/value").asText());
365 assertEquals("operation:or", node.at("/collection/operation").asText());
margaretha8489f862025-02-05 11:32:16 +0100366 assertEquals(publicCorpusAccess,
margaretha1c0a41d2024-11-13 15:44:25 +0100367 node.at("/collection/rewrites/0/scope").asText());
margarethaa89c3f92017-05-30 19:02:08 +0200368 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200369
margarethac1db9132019-08-28 11:32:04 +0200370 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100371 public void testSearchWithInvalidPage () throws KustvaktException {
372 Response response = target().path(API_VERSION).path("search")
373 .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
374 .queryParam("page", "0").request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200375 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000376 String entity = response.readEntity(String.class);
margarethac1db9132019-08-28 11:32:04 +0200377 JsonNode node = JsonUtils.readTree(entity);
margaretha35e1ca22023-11-16 22:00:01 +0100378 assertEquals(StatusCodes.INVALID_ARGUMENT,
379 node.at("/errors/0/0").asInt());
margaretha1c0a41d2024-11-13 15:44:25 +0100380 assertEquals("page must start from 1", node.at("/errors/0/1").asText());
margarethac1db9132019-08-28 11:32:04 +0200381 }
margaretha064eb6f2018-07-10 18:33:01 +0200382
margaretha0c47c652017-04-19 18:44:40 +0200383 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100384 public void testSearchSentenceMeta () throws KustvaktException {
385 Response response = target().path(API_VERSION).path("search")
386 .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
387 .queryParam("context", "sentence").request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200388 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000389 String ent = response.readEntity(String.class);
margaretha0c47c652017-04-19 18:44:40 +0200390 JsonNode node = JsonUtils.readTree(ent);
391 assertNotNull(node);
margaretha1c0a41d2024-11-13 15:44:25 +0100392 assertEquals("base/s:s", node.at("/meta/context").asText());
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200393 assertNotEquals("/meta/version", "${project.version}");
margaretha0c47c652017-04-19 18:44:40 +0200394 }
395
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200396 // EM: The API is disabled
397 @Disabled
margaretha0c47c652017-04-19 18:44:40 +0200398 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100399 public void testSearchSimpleCQL () throws KustvaktException {
margaretha0c47c652017-04-19 18:44:40 +0200400 QuerySerializer s = new QuerySerializer();
401 s.setQuery("(der) or (das)", "CQL");
margaretha35e1ca22023-11-16 22:00:01 +0100402 Response response = target().path(API_VERSION).path("search").request()
403 .post(Entity.json(s.toJSON()));
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200404 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000405 String ent = response.readEntity(String.class);
margaretha0c47c652017-04-19 18:44:40 +0200406 JsonNode node = JsonUtils.readTree(ent);
407 assertNotNull(node);
408 assertNotEquals(0, node.path("matches").size());
margaretha064eb6f2018-07-10 18:33:01 +0200409 // assertEquals(17027, node.at("/meta/totalResults").asInt());
margaretha0c47c652017-04-19 18:44:40 +0200410 }
411
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200412 // EM: The API is disabled
margaretha0c47c652017-04-19 18:44:40 +0200413 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200414 @Disabled
margaretha35e1ca22023-11-16 22:00:01 +0100415 public void testSearchRawQuery () throws KustvaktException {
416 Response response = target().path(API_VERSION).path("search").request()
417 .post(Entity.json(createJsonQuery()));
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200418 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000419 String ent = response.readEntity(String.class);
margaretha0c47c652017-04-19 18:44:40 +0200420 JsonNode node = JsonUtils.readTree(ent);
421 assertNotNull(node);
422 assertNotEquals(0, node.path("matches").size());
margaretha8489f862025-02-05 11:32:16 +0100423 assertEquals(freeCorpusAccess,
margaretha1c0a41d2024-11-13 15:44:25 +0100424 node.at("/collection/rewrites/0/scope").asText());
margaretha2544cdf2019-07-08 11:39:43 +0200425 }
margaretha2544cdf2019-07-08 11:39:43 +0200426
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200427 // EM: The API is disabled
428 @Test
429 @Disabled
margaretha35e1ca22023-11-16 22:00:01 +0100430 public void testSearchPostAll () throws KustvaktException {
431 Response response = target().path(API_VERSION).path("search").request()
432 .header(HttpHeaders.X_FORWARDED_FOR, "10.27.0.32")
433 .header(Attributes.AUTHORIZATION,
434 HttpAuthorizationHandler
435 .createBasicAuthorizationHeaderValue("kustvakt",
436 "kustvakt2015"))
437 .post(Entity.json(createJsonQuery()));
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200438 assertEquals(Status.OK.getStatusCode(), response.getStatus());
439 String ent = response.readEntity(String.class);
margaretha2544cdf2019-07-08 11:39:43 +0200440 JsonNode node = JsonUtils.readTree(ent);
441 assertNotNull(node);
442 assertNotEquals(0, node.path("matches").size());
margaretha8489f862025-02-05 11:32:16 +0100443 assertEquals(allCorpusAccess,
margaretha1c0a41d2024-11-13 15:44:25 +0100444 node.at("/collection/rewrites/0/scope").asText());
margaretha2544cdf2019-07-08 11:39:43 +0200445 }
margaretha2544cdf2019-07-08 11:39:43 +0200446
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200447 // EM: The API is disabled
448 @Test
449 @Disabled
margaretha35e1ca22023-11-16 22:00:01 +0100450 public void testSearchPostPublic () throws KustvaktException {
451 Response response = target().path(API_VERSION).path("search").request()
452 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
453 .header(Attributes.AUTHORIZATION,
454 HttpAuthorizationHandler
455 .createBasicAuthorizationHeaderValue("kustvakt",
456 "kustvakt2015"))
457 .post(Entity.json(createJsonQuery()));
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200458 assertEquals(Status.OK.getStatusCode(), response.getStatus());
459 String ent = response.readEntity(String.class);
margaretha2544cdf2019-07-08 11:39:43 +0200460 JsonNode node = JsonUtils.readTree(ent);
461 assertNotNull(node);
462 assertNotEquals(0, node.path("matches").size());
margaretha8489f862025-02-05 11:32:16 +0100463 assertEquals(publicCorpusAccess,
margaretha1c0a41d2024-11-13 15:44:25 +0100464 node.at("/collection/rewrites/0/scope").asText());
margaretha0c47c652017-04-19 18:44:40 +0200465 }
margaretha0c47c652017-04-19 18:44:40 +0200466}