blob: 454f9ae93e01a89a0a3c93cc2d0d646e1936ded3 [file] [log] [blame]
margaretha541b8cc2018-01-10 13:02:46 +01001package de.ids_mannheim.korap.web.controller;
margaretha61471cc2017-04-20 18:42:23 +02002
Marc Kupietzd43a98d2023-09-22 17:11:46 +02003import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertNotNull;
5import static org.junit.jupiter.api.Assertions.assertTrue;
margaretha61471cc2017-04-20 18:42:23 +02006
Marc Kupietzd43a98d2023-09-22 17:11:46 +02007import org.junit.jupiter.api.Test;
margaretha61471cc2017-04-20 18:42:23 +02008import com.fasterxml.jackson.databind.JsonNode;
margaretha58e18632018-02-15 13:04:42 +01009import com.google.common.net.HttpHeaders;
margaretha96c309d2023-08-16 12:24:12 +020010import jakarta.ws.rs.core.Response;
11import jakarta.ws.rs.core.Response.Status;
margaretha61471cc2017-04-20 18:42:23 +020012
margaretha56e8e552017-12-05 16:31:21 +010013import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
margaretha61471cc2017-04-20 18:42:23 +020014import de.ids_mannheim.korap.config.Attributes;
margarethaee0cbfe2018-08-28 17:47:14 +020015import de.ids_mannheim.korap.config.SpringJerseyTest;
margaretha61471cc2017-04-20 18:42:23 +020016import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha351f7692019-02-06 19:36:52 +010017import de.ids_mannheim.korap.exceptions.StatusCodes;
margaretha61471cc2017-04-20 18:42:23 +020018import de.ids_mannheim.korap.utils.JsonUtils;
margaretha61471cc2017-04-20 18:42:23 +020019
margarethaee0cbfe2018-08-28 17:47:14 +020020public class MatchInfoControllerTest extends SpringJerseyTest {
margaretha61471cc2017-04-20 18:42:23 +020021
margaretha61471cc2017-04-20 18:42:23 +020022 @Test
margaretha35e1ca22023-11-16 22:00:01 +010023 public void testGetMatchInfoPublicCorpus () throws KustvaktException {
24 Response response = target().path(API_VERSION).path("corpus")
25 .path("GOE").path("AGA").path("01784").path("p36-100")
margarethad709be52024-06-05 11:31:41 +020026 .queryParam("foundry", "*").request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +020027 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000028 String entity = response.readEntity(String.class);
margaretha65b67142017-05-29 16:23:16 +020029 JsonNode node = JsonUtils.readTree(entity);
margaretha65b67142017-05-29 16:23:16 +020030 assertNotNull(node);
Marc Kupietzd43a98d2023-09-22 17:11:46 +020031 assertEquals(node.at("/textSigle").asText(), "GOE/AGA/01784");
32 assertEquals(node.at("/title").asText(), "Belagerung von Mainz");
margaretha35e1ca22023-11-16 22:00:01 +010033 assertEquals(node.at("/author").asText(),
34 "Goethe, Johann Wolfgang von");
35 assertTrue(node.at("/snippet").asText()
36 .startsWith("<span class=\"context-left\"></span>"
37 + "<span class=\"match\">"));
margaretha65b67142017-05-29 16:23:16 +020038 }
margaretha064eb6f2018-07-10 18:33:01 +020039
margaretha65b67142017-05-29 16:23:16 +020040 @Test
margaretha35e1ca22023-11-16 22:00:01 +010041 public void testGetMatchInfoNotAllowed () throws KustvaktException {
42 Response response = target().path(API_VERSION).path("corpus")
43 .path("GOE").path("AGI").path("04846").path("p36875-36876")
margarethad709be52024-06-05 11:31:41 +020044 .queryParam("foundry", "*").request().get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +020045 assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000046 String entity = response.readEntity(String.class);
margaretha61471cc2017-04-20 18:42:23 +020047 JsonNode node = JsonUtils.readTree(entity);
margaretha35e1ca22023-11-16 22:00:01 +010048 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
49 node.at("/errors/0/0").asInt());
50 assertEquals(
51 "Retrieving resource with ID "
52 + "match-GOE/AGI/04846-p36875-36876 is not allowed.",
53 node.at("/errors/0/1").asText());
margarethaaec93f72017-05-29 16:51:41 +020054 assertTrue(node.at("/snippet").isMissingNode());
margaretha65b67142017-05-29 16:23:16 +020055 }
margarethac7f8f802024-06-05 12:52:45 +020056
57 @Test
58 public void testUsingDeprecatedMatchInfoService () throws KustvaktException {
59 Response response = target().path(API_VERSION).path("corpus")
60 .path("GOE").path("AGA").path("01784").path("p36-100")
61 .path("matchInfo")
62 .queryParam("foundry", "*").request().get();
63 assertEquals(Status.OK.getStatusCode(), response.getStatus());
64 String entity = response.readEntity(String.class);
65 JsonNode node = JsonUtils.readTree(entity);
66 assertEquals(StatusCodes.DEPRECATED,
67 node.at("/warnings/0/0").asInt());
68 assertEquals("This service is deprecated. Please use the following "
69 + "service URL instead: {version}/corpus/{corpusId}/{docId}/"
70 + "{textId}/{matchId}",
71 node.at("/warnings/0/1").asText());
72 }
margaretha65b67142017-05-29 16:23:16 +020073
74 @Test
margaretha35e1ca22023-11-16 22:00:01 +010075 public void testGetMatchInfoWithAuthentication () throws KustvaktException {
76 Response response = target().path(API_VERSION).path("corpus")
77 .path("GOE").path("AGI").path("04846").path("p36875-36876")
margarethad709be52024-06-05 11:31:41 +020078 .queryParam("foundry", "*").request()
margaretha35e1ca22023-11-16 22:00:01 +010079 .header(Attributes.AUTHORIZATION,
80 HttpAuthorizationHandler
81 .createBasicAuthorizationHeaderValue("kustvakt",
82 "kustvakt2015"))
83 .header(HttpHeaders.X_FORWARDED_FOR, "172.27.0.32").get();
abcpro173fe8f22022-11-08 19:56:52 +000084 String entity = response.readEntity(String.class);
Marc Kupietzd43a98d2023-09-22 17:11:46 +020085 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margaretha65b67142017-05-29 16:23:16 +020086 JsonNode node = JsonUtils.readTree(entity);
margaretha61471cc2017-04-20 18:42:23 +020087 assertNotNull(node);
Marc Kupietzd43a98d2023-09-22 17:11:46 +020088 assertEquals(node.at("/textSigle").asText(), "GOE/AGI/04846");
margaretha35e1ca22023-11-16 22:00:01 +010089 assertEquals(node.at("/title").asText(),
90 "Zweiter römischer Aufenthalt");
91 assertEquals(node.at("/subTitle").asText(),
92 "vom Juni 1787 bis April 1788");
93 assertEquals(node.at("/author").asText(),
94 "Goethe, Johann Wolfgang von");
95 assertTrue(node.at("/snippet").asText()
96 .startsWith("<span class=\"context-left\"></span>"
97 + "<span class=\"match\">"));
Marc Kupietzd43a98d2023-09-22 17:11:46 +020098 assertEquals(node.at("/availability").asText(), "QAO-NC-LOC:ids");
margaretha61471cc2017-04-20 18:42:23 +020099 }
margaretha351f7692019-02-06 19:36:52 +0100100
101 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100102 public void testAvailabilityAll () throws KustvaktException {
103 Response response = target().path(API_VERSION).path("corpus")
104 .path("GOE").path("AGD").path("00000").path("p75-76").request()
105 .header(Attributes.AUTHORIZATION,
106 HttpAuthorizationHandler
107 .createBasicAuthorizationHeaderValue("kustvakt",
108 "kustvakt2015"))
109 .header(HttpHeaders.X_FORWARDED_FOR, "10.27.0.32").get();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200110 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margaretha351f7692019-02-06 19:36:52 +0100111 }
112
113 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100114 public void testAvailabilityAllUnauthorized () throws KustvaktException {
115 Response response = target().path(API_VERSION).path("corpus")
116 .path("GOE").path("AGD").path("00000").path("p75-76").request()
117 .header(Attributes.AUTHORIZATION,
118 HttpAuthorizationHandler
119 .createBasicAuthorizationHeaderValue("kustvakt",
120 "kustvakt2015"))
121 .header(HttpHeaders.X_FORWARDED_FOR, "170.27.0.32").get();
abcpro173fe8f22022-11-08 19:56:52 +0000122 JsonNode node = JsonUtils.readTree(response.readEntity(String.class));
margarethac7f8f802024-06-05 12:52:45 +0200123
margaretha35e1ca22023-11-16 22:00:01 +0100124 assertEquals(StatusCodes.AUTHORIZATION_FAILED,
125 node.at("/errors/0/0").asInt());
126 assertEquals(
127 "Retrieving resource with ID "
128 + "match-GOE/AGD/00000-p75-76 is not allowed.",
129 node.at("/errors/0/1").asText());
margaretha351f7692019-02-06 19:36:52 +0100130 }
Akron15b05be2017-05-22 17:15:28 +0200131}