blob: 5c20c5eb9dd8a8606150dfd7289b59808863c195 [file] [log] [blame]
Akronda080152020-12-03 13:53:29 +01001package de.ids_mannheim.korap.web.controller;
2
3import static org.junit.Assert.assertEquals;
4
5import org.apache.http.entity.ContentType;
6import org.junit.Test;
7
8import com.fasterxml.jackson.databind.JsonNode;
9import com.google.common.net.HttpHeaders;
10import com.sun.jersey.api.client.ClientHandlerException;
11import com.sun.jersey.api.client.ClientResponse;
12import com.sun.jersey.api.client.ClientResponse.Status;
13import com.sun.jersey.api.client.UniformInterfaceException;
14
15import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
16import de.ids_mannheim.korap.config.Attributes;
17import de.ids_mannheim.korap.config.SpringJerseyTest;
margaretha652c4dc2021-02-12 17:07:44 +010018import de.ids_mannheim.korap.constant.ResourceType;
Akronda080152020-12-03 13:53:29 +010019import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha89bd8f52021-02-26 17:08:01 +010020import de.ids_mannheim.korap.exceptions.StatusCodes;
margaretha479a4472021-02-18 12:00:55 +010021import de.ids_mannheim.korap.user.User.CorpusAccess;
Akronda080152020-12-03 13:53:29 +010022import de.ids_mannheim.korap.utils.JsonUtils;
23
margaretha652c4dc2021-02-12 17:07:44 +010024public class QueryReferenceControllerTest extends SpringJerseyTest {
Akronda080152020-12-03 13:53:29 +010025
26 private String testUser = "qRefControllerTest";
margaretha89bd8f52021-02-26 17:08:01 +010027 private String adminUser = "admin";
margarethab3ecbe32021-08-16 12:55:54 +020028 private String system = "system";
margaretha652c4dc2021-02-12 17:07:44 +010029
margarethab3ecbe32021-08-16 12:55:54 +020030 private void testRetrieveQueryByName (String qName, String query,
31 String queryCreator, String username, ResourceType resourceType,
32 CorpusAccess access) throws KustvaktException {
33
34 ClientResponse response = resource().path(API_VERSION).path("query")
35 .path("~" + queryCreator).path(qName)
36 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
37 .createBasicAuthorizationHeaderValue(username, "pass"))
38 .get(ClientResponse.class);
39 String entity = response.getEntity(String.class);
40 // System.out.println(entity);
41 assertEquals(Status.OK.getStatusCode(), response.getStatus());
42
43 JsonNode node = JsonUtils.readTree(entity);
44
margaretha3b6972a2021-08-12 11:22:07 +020045 assertEquals(qName, node.at("/name").asText());
46 assertEquals(resourceType.displayName(), node.at("/type").asText());
margarethab3ecbe32021-08-16 12:55:54 +020047 assertEquals(queryCreator, node.at("/createdBy").asText());
margaretha3b6972a2021-08-12 11:22:07 +020048 assertEquals(query, node.at("/query").asText());
49 assertEquals("poliqarp", node.at("/queryLanguage").asText());
50 assertEquals(access.name(), node.at("/requiredAccess").asText());
margaretha3b6972a2021-08-12 11:22:07 +020051 }
52
margarethab3ecbe32021-08-16 12:55:54 +020053 private void testUpdateQuery (String qName, String qCreator,
54 String username, ResourceType type)
55 throws UniformInterfaceException, ClientHandlerException,
56 KustvaktException {
57 String json = "{\"query\": \"Sonne\""
58 + ",\"queryLanguage\": \"poliqarp\"}";
59
60 ClientResponse response = resource().path(API_VERSION).path("query")
61 .path("~"+qCreator).path(qName)
62 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
63 .createBasicAuthorizationHeaderValue(username, "pass"))
64 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
65 .entity(json).put(ClientResponse.class);
66
67 assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());
68
69 testRetrieveQueryByName(qName, "Sonne", qCreator, username, type, CorpusAccess.PUB);
70 }
71
Akronda080152020-12-03 13:53:29 +010072 @Test
73 public void testCreatePrivateQuery () throws KustvaktException {
margaretha89bd8f52021-02-26 17:08:01 +010074 String json = "{\"type\": \"PRIVATE\""
75 + ",\"queryType\": \"QUERY\""
76 + ",\"queryLanguage\": \"poliqarp\""
77 + ",\"query\": \"der\"}";
margaretha652c4dc2021-02-12 17:07:44 +010078
79 String qName = "new_query";
Akronda080152020-12-03 13:53:29 +010080 ClientResponse response = resource().path(API_VERSION).path("query")
margaretha652c4dc2021-02-12 17:07:44 +010081 .path("~" + testUser).path(qName)
Akronda080152020-12-03 13:53:29 +010082 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
83 .createBasicAuthorizationHeaderValue(testUser, "pass"))
Akronda080152020-12-03 13:53:29 +010084 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
85 .put(ClientResponse.class, json);
86
87 assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
88
margarethab3ecbe32021-08-16 12:55:54 +020089 testRetrieveQueryByName(qName, "der", testUser, testUser, ResourceType.PRIVATE,
margaretha3b6972a2021-08-12 11:22:07 +020090 CorpusAccess.PUB);
margarethab3ecbe32021-08-16 12:55:54 +020091
92 testUpdateQuery(qName, testUser, testUser,ResourceType.PRIVATE);
93 testDeleteQueryByName(qName, testUser, testUser);
margaretha89bd8f52021-02-26 17:08:01 +010094 }
95
96 @Test
margaretha1703c162021-08-13 17:13:42 +020097 public void testCreatePublishQuery () throws KustvaktException {
98 String json = "{\"type\": \"PUBLISHED\""
99 + ",\"queryType\": \"QUERY\""
100 + ",\"queryLanguage\": \"poliqarp\""
101 + ",\"query\": \"Regen\"}";
102
103 String qName = "publish_query";
104 ClientResponse response = resource().path(API_VERSION).path("query")
105 .path("~" + testUser).path(qName)
106 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
107 .createBasicAuthorizationHeaderValue(testUser, "pass"))
108 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
109 .put(ClientResponse.class, json);
110
111 assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
112
margarethab3ecbe32021-08-16 12:55:54 +0200113 testRetrieveQueryByName(qName, "Regen", testUser, testUser, ResourceType.PUBLISHED,
margaretha1703c162021-08-13 17:13:42 +0200114 CorpusAccess.PUB);
margarethab3ecbe32021-08-16 12:55:54 +0200115 testDeleteQueryByName(qName, testUser, testUser);
margaretha1703c162021-08-13 17:13:42 +0200116 // check if hidden group has been created
117
118 }
119
120 @Test
margaretha89bd8f52021-02-26 17:08:01 +0100121 public void testCreateUserQueryByAdmin () throws KustvaktException {
122 String json = "{\"type\": \"PRIVATE\""
123 + ",\"queryType\": \"QUERY\""
124 + ",\"queryLanguage\": \"poliqarp\""
125 + ",\"query\": \"Sommer\"}";
126
127 String qName = "marlin-query";
128 ClientResponse response = resource().path(API_VERSION).path("query")
129 .path("~marlin").path(qName)
130 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
131 .createBasicAuthorizationHeaderValue(adminUser, "pass"))
132 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
133 .entity(json).put(ClientResponse.class);
134
135 assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
margarethab3ecbe32021-08-16 12:55:54 +0200136
137 testRetrieveQueryByName(qName, "Sommer", "marlin", adminUser, ResourceType.PRIVATE, CorpusAccess.PUB);
138
139 testUpdateQuery(qName, "marlin", adminUser, ResourceType.PRIVATE);
140 testDeleteQueryByName(qName, "marlin", adminUser);
margaretha89bd8f52021-02-26 17:08:01 +0100141 }
142
143 @Test
144 public void testCreateSystemQuery () throws KustvaktException {
145 String json = "{\"type\": \"SYSTEM\""
146 + ",\"queryType\": \"QUERY\""
147 + ",\"queryLanguage\": \"poliqarp\""
148 + ",\"query\": \"Sommer\"}";
149
150 String qName = "system-query";
151 ClientResponse response = resource().path(API_VERSION).path("query")
152 .path("~system").path(qName)
153 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
154 .createBasicAuthorizationHeaderValue(adminUser, "pass"))
155 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
156 .entity(json).put(ClientResponse.class);
157
158 assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
margaretha1703c162021-08-13 17:13:42 +0200159
margarethab3ecbe32021-08-16 12:55:54 +0200160 testRetrieveQueryByName(qName, "Sommer", system, adminUser, ResourceType.SYSTEM, CorpusAccess.PUB);
161 testUpdateQuery(qName, system, adminUser, ResourceType.SYSTEM);
162 testDeleteSystemQueryUnauthorized(qName);
163 testDeleteQueryByName(qName, system, adminUser);
margaretha89bd8f52021-02-26 17:08:01 +0100164 }
165
166 @Test
167 public void testCreateSystemQueryUnauthorized () throws KustvaktException {
168 String json = "{\"type\": \"SYSTEM\""
169 + ",\"queryType\": \"QUERY\""
170 + ",\"queryLanguage\": \"poliqarp\""
171 + ",\"query\": \"Sommer\"}";
172
173 ClientResponse response = resource().path(API_VERSION).path("query")
174 .path("~"+testUser).path("system-query")
175 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
176 .createBasicAuthorizationHeaderValue(testUser, "pass"))
177 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
178 .entity(json).put(ClientResponse.class);
179
180 assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
181
182 String entity = response.getEntity(String.class);
183 JsonNode node = JsonUtils.readTree(entity);
184 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
185 node.at("/errors/0/0").asInt());
186 assertEquals("Unauthorized operation for user: " + testUser,
187 node.at("/errors/0/1").asText());
188 }
189
190 @Test
margaretha3b6972a2021-08-12 11:22:07 +0200191 public void testCreateQueryMissingQueryType () throws KustvaktException {
192 String json = "{\"type\": \"PRIVATE\""
193 + ",\"queryLanguage\": \"poliqarp\""
194 + ",\"query\": \"Sohn\"}";
195
196 String qName = "new_query";
197 ClientResponse response = resource().path(API_VERSION).path("query")
198 .path("~" + testUser).path(qName)
199 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
200 .createBasicAuthorizationHeaderValue(testUser, "pass"))
201 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
202 .put(ClientResponse.class, json);
203
204 assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
205
margarethab3ecbe32021-08-16 12:55:54 +0200206 testRetrieveQueryByName(qName, "Sohn", testUser, testUser, ResourceType.PRIVATE,
margaretha3b6972a2021-08-12 11:22:07 +0200207 CorpusAccess.PUB);
margarethab3ecbe32021-08-16 12:55:54 +0200208 testDeleteQueryByName(qName, testUser, testUser);
margaretha3b6972a2021-08-12 11:22:07 +0200209 }
210
211 @Test
212 public void testCreateQueryMissingQueryLanguage () throws KustvaktException {
213 String json = "{\"type\": \"PRIVATE\""
214 + ",\"queryType\": \"QUERY\""
215 + ",\"query\": \"Sohn\"}";
216
217 String qName = "new_query";
218 ClientResponse response = resource().path(API_VERSION).path("query")
219 .path("~" + testUser).path(qName)
220 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
221 .createBasicAuthorizationHeaderValue(testUser, "pass"))
222 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
223 .put(ClientResponse.class, json);
224
225 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
226
227 String entity = response.getEntity(String.class);
228 JsonNode node = JsonUtils.readTree(entity);
229 assertEquals(StatusCodes.INVALID_ARGUMENT, node.at("/errors/0/0").asInt());
230 assertEquals("queryLanguage is null", node.at("/errors/0/1").asText());
231 assertEquals("queryLanguage", node.at("/errors/0/2").asText());
232 }
233
234 @Test
235 public void testCreateQueryMissingQuery () throws KustvaktException {
236 String json = "{\"type\": \"PRIVATE\""
237 + ",\"queryType\": \"QUERY\""
238 + ",\"queryLanguage\": \"poliqarp\"}";
239
240 String qName = "new_query";
241 ClientResponse response = resource().path(API_VERSION).path("query")
242 .path("~" + testUser).path(qName)
243 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
244 .createBasicAuthorizationHeaderValue(testUser, "pass"))
245 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
246 .put(ClientResponse.class, json);
247
248 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
249
250 String entity = response.getEntity(String.class);
251 JsonNode node = JsonUtils.readTree(entity);
252 assertEquals(StatusCodes.INVALID_ARGUMENT, node.at("/errors/0/0").asInt());
253 assertEquals("query is null", node.at("/errors/0/1").asText());
254 assertEquals("query", node.at("/errors/0/2").asText());
255 }
256
257 @Test
258 public void testCreateQueryMissingResourceType () throws KustvaktException {
259 String json = "{\"query\": \"Wind\""
260 + ",\"queryLanguage\": \"poliqarp\"}";
261
262 String qName = "new_query";
263 ClientResponse response = resource().path(API_VERSION).path("query")
264 .path("~" + testUser).path(qName)
265 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
266 .createBasicAuthorizationHeaderValue(testUser, "pass"))
267 .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
268 .put(ClientResponse.class, json);
269
270 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
271
272 String entity = response.getEntity(String.class);
273 JsonNode node = JsonUtils.readTree(entity);
274 assertEquals(StatusCodes.INVALID_ARGUMENT, node.at("/errors/0/0").asInt());
275 assertEquals("type is null", node.at("/errors/0/1").asText());
276 assertEquals("type", node.at("/errors/0/2").asText());
277 }
278
margarethab3ecbe32021-08-16 12:55:54 +0200279 private void testDeleteQueryByName (String qName, String qCreator, String username)
280 throws KustvaktException {
281 ClientResponse response = resource().path(API_VERSION).path("query")
282 .path("~" + qCreator).path(qName)
283 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
284 .createBasicAuthorizationHeaderValue(username, "pass"))
285 .delete(ClientResponse.class);
286
287 assertEquals(Status.OK.getStatusCode(), response.getStatus());
288 }
289
margaretha3b6972a2021-08-12 11:22:07 +0200290 @Test
margaretha89bd8f52021-02-26 17:08:01 +0100291 public void testDeleteQueryUnauthorized () throws KustvaktException {
292 ClientResponse response = resource().path(API_VERSION).path("query")
293 .path("~dory").path("dory-q")
294 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
295 .createBasicAuthorizationHeaderValue(testUser, "pass"))
296 .delete(ClientResponse.class);
297
298 String entity = response.getEntity(String.class);
299 JsonNode node = JsonUtils.readTree(entity);
300
301 assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
302 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
303 node.at("/errors/0/0").asInt());
304 assertEquals("Unauthorized operation for user: " + testUser,
305 node.at("/errors/0/1").asText());
306 }
307
margarethab3ecbe32021-08-16 12:55:54 +0200308 private void testDeleteSystemQueryUnauthorized (String qName) throws KustvaktException {
309 ClientResponse response = resource().path(API_VERSION).path("query")
310 .path("~system").path(qName)
311 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
312 .createBasicAuthorizationHeaderValue(testUser, "pass"))
313 .delete(ClientResponse.class);
314
315 String entity = response.getEntity(String.class);
316 JsonNode node = JsonUtils.readTree(entity);
317
318 assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
319 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
320 node.at("/errors/0/0").asInt());
321 assertEquals("Unauthorized operation for user: " + testUser,
322 node.at("/errors/0/1").asText());
323 }
324
margaretha89bd8f52021-02-26 17:08:01 +0100325 @Test
326 public void testDeleteNonExistingQuery () throws KustvaktException {
327 ClientResponse response = resource().path(API_VERSION).path("query")
328 .path("~dory").path("non-existing-query")
329 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
330 .createBasicAuthorizationHeaderValue("dory", "pass"))
331 .delete(ClientResponse.class);
332
333 assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus());
334
335 String entity = response.getEntity(String.class);
336 JsonNode node = JsonUtils.readTree(entity);
337
338 assertEquals(StatusCodes.NO_RESOURCE_FOUND,
339 node.at("/errors/0/0").asInt());
340 assertEquals("Query dory/non-existing-query is not found.",
341 node.at("/errors/0/1").asText());
342 assertEquals("dory/non-existing-query",
343 node.at("/errors/0/2").asText());
Akronda080152020-12-03 13:53:29 +0100344 }
margaretha652c4dc2021-02-12 17:07:44 +0100345
346 @Test
margarethab3ecbe32021-08-16 12:55:54 +0200347 public void testListAvailableQueryForDory () throws UniformInterfaceException,
margaretha652c4dc2021-02-12 17:07:44 +0100348 ClientHandlerException, KustvaktException {
margaretha90adf312021-02-17 10:12:41 +0100349 JsonNode node = testListAvailableQuery("dory");
350 assertEquals(2, node.size());
351 }
352
353 @Test
354 public void testListAvailableQueryForPearl ()
355 throws UniformInterfaceException, ClientHandlerException,
356 KustvaktException {
357
358 JsonNode node = testListAvailableQuery("pearl");
359
360 assertEquals(1, node.size());
361 assertEquals("system-q", node.at("/0/name").asText());
362 assertEquals(ResourceType.SYSTEM.displayName(),
363 node.at("/0/type").asText());
364 assertEquals("\"system\" query", node.at("/0/description").asText());
margaretha326520b2021-12-08 17:58:09 +0100365 assertEquals("[]", node.at("/0/query").asText());
366 assertEquals(CorpusAccess.FREE.name(), node.at("/0/requiredAccess").asText());
367// assertEquals("koral:token", node.at("/0/koralQuery/@type").asText());
margaretha90adf312021-02-17 10:12:41 +0100368 }
369
370 private JsonNode testListAvailableQuery (String username)
371 throws UniformInterfaceException, ClientHandlerException,
372 KustvaktException {
373
margaretha652c4dc2021-02-12 17:07:44 +0100374 ClientResponse response = resource().path(API_VERSION).path("query")
375 .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
376 .createBasicAuthorizationHeaderValue(username, "pass"))
377 .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
378 .get(ClientResponse.class);
379
380 assertEquals(Status.OK.getStatusCode(), response.getStatus());
381
382 String entity = response.getEntity(String.class);
margaretha90adf312021-02-17 10:12:41 +0100383 // System.out.println(entity);
margaretha652c4dc2021-02-12 17:07:44 +0100384 JsonNode node = JsonUtils.readTree(entity);
margaretha90adf312021-02-17 10:12:41 +0100385 return node;
margaretha652c4dc2021-02-12 17:07:44 +0100386 }
margaretha90adf312021-02-17 10:12:41 +0100387
Akronda080152020-12-03 13:53:29 +0100388}