| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | |
| 5 | import org.apache.http.entity.ContentType; |
| 6 | import org.junit.Test; |
| 7 | |
| 8 | import com.fasterxml.jackson.databind.JsonNode; |
| 9 | import com.google.common.net.HttpHeaders; |
| 10 | import com.sun.jersey.api.client.ClientHandlerException; |
| 11 | import com.sun.jersey.api.client.ClientResponse; |
| 12 | import com.sun.jersey.api.client.ClientResponse.Status; |
| 13 | import com.sun.jersey.api.client.UniformInterfaceException; |
| 14 | |
| 15 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 16 | import de.ids_mannheim.korap.config.Attributes; |
| 17 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 18 | import de.ids_mannheim.korap.constant.ResourceType; |
| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 19 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | 89bd8f5 | 2021-02-26 17:08:01 +0100 | [diff] [blame] | 20 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| margaretha | 479a447 | 2021-02-18 12:00:55 +0100 | [diff] [blame] | 21 | import de.ids_mannheim.korap.user.User.CorpusAccess; |
| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 22 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 23 | |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 24 | public class QueryReferenceControllerTest extends SpringJerseyTest { |
| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 25 | |
| 26 | private String testUser = "qRefControllerTest"; |
| margaretha | 89bd8f5 | 2021-02-26 17:08:01 +0100 | [diff] [blame] | 27 | private String adminUser = "admin"; |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 28 | private String system = "system"; |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 29 | |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 30 | 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 | |
| margaretha | 3b6972a | 2021-08-12 11:22:07 +0200 | [diff] [blame] | 45 | assertEquals(qName, node.at("/name").asText()); |
| 46 | assertEquals(resourceType.displayName(), node.at("/type").asText()); |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 47 | assertEquals(queryCreator, node.at("/createdBy").asText()); |
| margaretha | 3b6972a | 2021-08-12 11:22:07 +0200 | [diff] [blame] | 48 | assertEquals(query, node.at("/query").asText()); |
| 49 | assertEquals("poliqarp", node.at("/queryLanguage").asText()); |
| 50 | assertEquals(access.name(), node.at("/requiredAccess").asText()); |
| margaretha | 3b6972a | 2021-08-12 11:22:07 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 53 | 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 | |
| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 72 | @Test |
| 73 | public void testCreatePrivateQuery () throws KustvaktException { |
| margaretha | 89bd8f5 | 2021-02-26 17:08:01 +0100 | [diff] [blame] | 74 | String json = "{\"type\": \"PRIVATE\"" |
| 75 | + ",\"queryType\": \"QUERY\"" |
| 76 | + ",\"queryLanguage\": \"poliqarp\"" |
| 77 | + ",\"query\": \"der\"}"; |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 78 | |
| 79 | String qName = "new_query"; |
| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 80 | ClientResponse response = resource().path(API_VERSION).path("query") |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 81 | .path("~" + testUser).path(qName) |
| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 82 | .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler |
| 83 | .createBasicAuthorizationHeaderValue(testUser, "pass")) |
| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 84 | .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON) |
| 85 | .put(ClientResponse.class, json); |
| 86 | |
| 87 | assertEquals(Status.CREATED.getStatusCode(), response.getStatus()); |
| 88 | |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 89 | testRetrieveQueryByName(qName, "der", testUser, testUser, ResourceType.PRIVATE, |
| margaretha | 3b6972a | 2021-08-12 11:22:07 +0200 | [diff] [blame] | 90 | CorpusAccess.PUB); |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 91 | |
| 92 | testUpdateQuery(qName, testUser, testUser,ResourceType.PRIVATE); |
| 93 | testDeleteQueryByName(qName, testUser, testUser); |
| margaretha | 89bd8f5 | 2021-02-26 17:08:01 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Test |
| margaretha | 1703c16 | 2021-08-13 17:13:42 +0200 | [diff] [blame] | 97 | 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 | |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 113 | testRetrieveQueryByName(qName, "Regen", testUser, testUser, ResourceType.PUBLISHED, |
| margaretha | 1703c16 | 2021-08-13 17:13:42 +0200 | [diff] [blame] | 114 | CorpusAccess.PUB); |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 115 | testDeleteQueryByName(qName, testUser, testUser); |
| margaretha | 1703c16 | 2021-08-13 17:13:42 +0200 | [diff] [blame] | 116 | // check if hidden group has been created |
| 117 | |
| 118 | } |
| 119 | |
| 120 | @Test |
| margaretha | 89bd8f5 | 2021-02-26 17:08:01 +0100 | [diff] [blame] | 121 | 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()); |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 136 | |
| 137 | testRetrieveQueryByName(qName, "Sommer", "marlin", adminUser, ResourceType.PRIVATE, CorpusAccess.PUB); |
| 138 | |
| 139 | testUpdateQuery(qName, "marlin", adminUser, ResourceType.PRIVATE); |
| 140 | testDeleteQueryByName(qName, "marlin", adminUser); |
| margaretha | 89bd8f5 | 2021-02-26 17:08:01 +0100 | [diff] [blame] | 141 | } |
| 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()); |
| margaretha | 1703c16 | 2021-08-13 17:13:42 +0200 | [diff] [blame] | 159 | |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 160 | testRetrieveQueryByName(qName, "Sommer", system, adminUser, ResourceType.SYSTEM, CorpusAccess.PUB); |
| 161 | testUpdateQuery(qName, system, adminUser, ResourceType.SYSTEM); |
| 162 | testDeleteSystemQueryUnauthorized(qName); |
| 163 | testDeleteQueryByName(qName, system, adminUser); |
| margaretha | 89bd8f5 | 2021-02-26 17:08:01 +0100 | [diff] [blame] | 164 | } |
| 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 |
| margaretha | 3b6972a | 2021-08-12 11:22:07 +0200 | [diff] [blame] | 191 | 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 | |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 206 | testRetrieveQueryByName(qName, "Sohn", testUser, testUser, ResourceType.PRIVATE, |
| margaretha | 3b6972a | 2021-08-12 11:22:07 +0200 | [diff] [blame] | 207 | CorpusAccess.PUB); |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 208 | testDeleteQueryByName(qName, testUser, testUser); |
| margaretha | 3b6972a | 2021-08-12 11:22:07 +0200 | [diff] [blame] | 209 | } |
| 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 | |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 279 | 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 | |
| margaretha | 3b6972a | 2021-08-12 11:22:07 +0200 | [diff] [blame] | 290 | @Test |
| margaretha | 89bd8f5 | 2021-02-26 17:08:01 +0100 | [diff] [blame] | 291 | 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 | |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 308 | 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 | |
| margaretha | 89bd8f5 | 2021-02-26 17:08:01 +0100 | [diff] [blame] | 325 | @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()); |
| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 344 | } |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 345 | |
| 346 | @Test |
| margaretha | b3ecbe3 | 2021-08-16 12:55:54 +0200 | [diff] [blame] | 347 | public void testListAvailableQueryForDory () throws UniformInterfaceException, |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 348 | ClientHandlerException, KustvaktException { |
| margaretha | 90adf31 | 2021-02-17 10:12:41 +0100 | [diff] [blame] | 349 | 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()); |
| margaretha | 326520b | 2021-12-08 17:58:09 +0100 | [diff] [blame^] | 365 | 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()); |
| margaretha | 90adf31 | 2021-02-17 10:12:41 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | private JsonNode testListAvailableQuery (String username) |
| 371 | throws UniformInterfaceException, ClientHandlerException, |
| 372 | KustvaktException { |
| 373 | |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 374 | 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); |
| margaretha | 90adf31 | 2021-02-17 10:12:41 +0100 | [diff] [blame] | 383 | // System.out.println(entity); |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 384 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 90adf31 | 2021-02-17 10:12:41 +0100 | [diff] [blame] | 385 | return node; |
| margaretha | 652c4dc | 2021-02-12 17:07:44 +0100 | [diff] [blame] | 386 | } |
| margaretha | 90adf31 | 2021-02-17 10:12:41 +0100 | [diff] [blame] | 387 | |
| Akron | da08015 | 2020-12-03 13:53:29 +0100 | [diff] [blame] | 388 | } |