blob: 65e57c30ccb087670cf93e8aa5e299693661eaf9 [file] [log] [blame]
margaretha92ad2ec2023-05-15 14:10:00 +02001package de.ids_mannheim.korap.web.lite;
margaretha34954472018-10-24 20:05:17 +02002
Marc Kupietzd43a98d2023-09-22 17:11:46 +02003import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertTrue;
margaretha34954472018-10-24 20:05:17 +02005
margaretha072a6a72019-11-22 12:12:03 +01006import java.io.IOException;
7
margaretha96c309d2023-08-16 12:24:12 +02008import jakarta.ws.rs.client.Entity;
9import jakarta.ws.rs.core.HttpHeaders;
10import jakarta.ws.rs.core.MediaType;
11import jakarta.ws.rs.core.Response;
12import jakarta.ws.rs.core.Response.Status;
margaretha072a6a72019-11-22 12:12:03 +010013
Marc Kupietzd43a98d2023-09-22 17:11:46 +020014import org.junit.jupiter.api.Test;
margaretha34954472018-10-24 20:05:17 +020015import com.fasterxml.jackson.databind.JsonNode;
margaretha3d55b002019-03-19 12:00:44 +010016import de.ids_mannheim.korap.config.LiteJerseyTest;
margaretha34954472018-10-24 20:05:17 +020017import de.ids_mannheim.korap.exceptions.KustvaktException;
margarethaf3ff6272019-07-11 12:57:13 +020018import de.ids_mannheim.korap.exceptions.StatusCodes;
margaretha34954472018-10-24 20:05:17 +020019import de.ids_mannheim.korap.utils.JsonUtils;
20
Marc Kupietzd43a98d2023-09-22 17:11:46 +020021public class LiteStatisticControllerTest extends LiteJerseyTest {
margaretha34954472018-10-24 20:05:17 +020022
23 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020024 public void testStatisticsWithCq() throws KustvaktException {
25 Response response = target().path(API_VERSION).path("statistics").queryParam("cq", "textType=Abhandlung & corpusSigle=GOE").request().method("GET");
26 assertEquals(Status.OK.getStatusCode(), response.getStatus());
27 assertEquals(response.getHeaders().getFirst("X-Index-Revision"), "Wes8Bd4h1OypPqbWF5njeQ==");
abcpro173fe8f22022-11-08 19:56:52 +000028 String query = response.readEntity(String.class);
margarethaf3ff6272019-07-11 12:57:13 +020029 JsonNode node = JsonUtils.readTree(query);
30 assertEquals(2, node.at("/documents").asInt());
31 assertEquals(138180, node.at("/tokens").asInt());
32 assertEquals(5687, node.at("/sentences").asInt());
33 assertEquals(258, node.at("/paragraphs").asInt());
margarethaa6cd0d92019-07-15 18:39:04 +020034 assertTrue(node.at("/warnings").isMissingNode());
margarethaf3ff6272019-07-11 12:57:13 +020035 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020036
margarethaf3ff6272019-07-11 12:57:13 +020037 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020038 public void testStatisticsWithCqAndCorpusQuery() throws KustvaktException {
39 Response response = target().path(API_VERSION).path("statistics").queryParam("cq", "textType=Abhandlung & corpusSigle=GOE").queryParam("corpusQuery", "textType=Autobiographie & corpusSigle=GOE").request().method("GET");
40 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000041 String query = response.readEntity(String.class);
margarethaf3ff6272019-07-11 12:57:13 +020042 JsonNode node = JsonUtils.readTree(query);
43 assertEquals(2, node.at("/documents").asInt());
44 assertEquals(138180, node.at("/tokens").asInt());
45 assertEquals(5687, node.at("/sentences").asInt());
46 assertEquals(258, node.at("/paragraphs").asInt());
margarethaa6cd0d92019-07-15 18:39:04 +020047 assertTrue(node.at("/warnings").isMissingNode());
margarethaf3ff6272019-07-11 12:57:13 +020048 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020049
margarethaf3ff6272019-07-11 12:57:13 +020050 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020051 public void testStatisticsWithCorpusQuery() throws KustvaktException {
52 Response response = target().path(API_VERSION).path("statistics").queryParam("corpusQuery", "textType=Autobiographie & corpusSigle=GOE").request().method("GET");
53 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000054 String query = response.readEntity(String.class);
margaretha34954472018-10-24 20:05:17 +020055 JsonNode node = JsonUtils.readTree(query);
56 assertEquals(9, node.at("/documents").asInt());
57 assertEquals(527662, node.at("/tokens").asInt());
58 assertEquals(19387, node.at("/sentences").asInt());
59 assertEquals(514, node.at("/paragraphs").asInt());
Marc Kupietzd43a98d2023-09-22 17:11:46 +020060 assertEquals(StatusCodes.DEPRECATED, node.at("/warnings/0/0").asInt());
61 assertEquals(node.at("/warnings/0/1").asText(), "Parameter corpusQuery is deprecated in favor of cq.");
margaretha34954472018-10-24 20:05:17 +020062 }
63
64 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020065 public void testEmptyStatistics() throws KustvaktException {
66 Response response = target().path(API_VERSION).path("statistics").queryParam("corpusQuery", "").request().method("GET");
67 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000068 String query = response.readEntity(String.class);
margaretha34954472018-10-24 20:05:17 +020069 JsonNode node = JsonUtils.readTree(query);
70 assertEquals(11, node.at("/documents").asInt());
71 assertEquals(665842, node.at("/tokens").asInt());
72 assertEquals(25074, node.at("/sentences").asInt());
73 assertEquals(772, node.at("/paragraphs").asInt());
Marc Kupietzd43a98d2023-09-22 17:11:46 +020074 response = target().path(API_VERSION).path("statistics").request().method("GET");
75 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000076 query = response.readEntity(String.class);
margaretha34954472018-10-24 20:05:17 +020077 node = JsonUtils.readTree(query);
78 assertEquals(11, node.at("/documents").asInt());
79 assertEquals(665842, node.at("/tokens").asInt());
80 assertEquals(25074, node.at("/sentences").asInt());
81 assertEquals(772, node.at("/paragraphs").asInt());
82 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +020083
margaretha072a6a72019-11-22 12:12:03 +010084 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020085 public void testGetStatisticsWithKoralQuery() throws IOException, KustvaktException {
86 Response response = target().path(API_VERSION).path("statistics").request().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).post(Entity.json("{ \"collection\" : {\"@type\": " + "\"koral:doc\", \"key\": \"availability\", \"match\": " + "\"match:eq\", \"type\": \"type:regex\", \"value\": " + "\"CC-BY.*\"} }"));
87 assertEquals(Status.OK.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000088 String ent = response.readEntity(String.class);
Marc Kupietzd43a98d2023-09-22 17:11:46 +020089 assertEquals(response.getHeaders().getFirst("X-Index-Revision"), "Wes8Bd4h1OypPqbWF5njeQ==");
margaretha072a6a72019-11-22 12:12:03 +010090 JsonNode node = JsonUtils.readTree(ent);
91 assertEquals(2, node.at("/documents").asInt());
92 assertEquals(72770, node.at("/tokens").asInt());
93 assertEquals(2985, node.at("/sentences").asInt());
94 assertEquals(128, node.at("/paragraphs").asInt());
95 }
margaretha072a6a72019-11-22 12:12:03 +010096
Marc Kupietzd43a98d2023-09-22 17:11:46 +020097 @Test
98 public void testGetStatisticsWithEmptyCollection() throws IOException, KustvaktException {
99 Response response = target().path(API_VERSION).path("statistics").request().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).post(Entity.json("{}"));
100 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000101 String ent = response.readEntity(String.class);
margaretha072a6a72019-11-22 12:12:03 +0100102 JsonNode node = JsonUtils.readTree(ent);
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200103 assertEquals(node.at("/errors/0/0").asInt(), de.ids_mannheim.korap.util.StatusCodes.MISSING_COLLECTION);
104 assertEquals(node.at("/errors/0/1").asText(), "Collection is not found");
margaretha072a6a72019-11-22 12:12:03 +0100105 }
margaretha072a6a72019-11-22 12:12:03 +0100106
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200107 @Test
108 public void testGetStatisticsWithIncorrectJson() throws IOException, KustvaktException {
109 Response response = target().path(API_VERSION).path("statistics").request().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).post(Entity.json("{ \"collection\" : }"));
110 assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000111 String ent = response.readEntity(String.class);
margaretha072a6a72019-11-22 12:12:03 +0100112 JsonNode node = JsonUtils.readTree(ent);
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200113 assertEquals(StatusCodes.DESERIALIZATION_FAILED, node.at("/errors/0/0").asInt());
114 assertEquals(node.at("/errors/0/1").asText(), "Failed deserializing json object: { \"collection\" : }");
margaretha072a6a72019-11-22 12:12:03 +0100115 }
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200116
margaretha072a6a72019-11-22 12:12:03 +0100117 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200118 public void testGetStatisticsWithoutKoralQuery() throws IOException, KustvaktException {
119 Response response = target().path(API_VERSION).path("statistics").request().post(Entity.json(""));
abcpro173fe8f22022-11-08 19:56:52 +0000120 String ent = response.readEntity(String.class);
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200121 assertEquals(Status.OK.getStatusCode(), response.getStatus());
margaretha072a6a72019-11-22 12:12:03 +0100122 JsonNode node = JsonUtils.readTree(ent);
123 assertEquals(11, node.at("/documents").asInt());
124 assertEquals(665842, node.at("/tokens").asInt());
125 assertEquals(25074, node.at("/sentences").asInt());
126 assertEquals(772, node.at("/paragraphs").asInt());
127 }
margaretha34954472018-10-24 20:05:17 +0200128}