blob: 919299e809e2c35dae165b4daec86bc4ae6bb31c [file] [log] [blame]
margaretha92ad2ec2023-05-15 14:10:00 +02001package de.ids_mannheim.korap.web.lite;
margaretha34954472018-10-24 20:05:17 +02002
3import static org.junit.Assert.assertEquals;
margarethaa6cd0d92019-07-15 18:39:04 +02004import static org.junit.Assert.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
margaretha34954472018-10-24 20:05:17 +020014import org.junit.Test;
15
16import com.fasterxml.jackson.databind.JsonNode;
margaretha34954472018-10-24 20:05:17 +020017
margaretha3d55b002019-03-19 12:00:44 +010018import de.ids_mannheim.korap.config.LiteJerseyTest;
margaretha34954472018-10-24 20:05:17 +020019import de.ids_mannheim.korap.exceptions.KustvaktException;
margarethaf3ff6272019-07-11 12:57:13 +020020import de.ids_mannheim.korap.exceptions.StatusCodes;
margaretha34954472018-10-24 20:05:17 +020021import de.ids_mannheim.korap.utils.JsonUtils;
22
23public class LiteStatisticControllerTest extends LiteJerseyTest{
24
25 @Test
margarethaf3ff6272019-07-11 12:57:13 +020026 public void testStatisticsWithCq () throws KustvaktException{
abcpro173fe8f22022-11-08 19:56:52 +000027 Response response = target().path(API_VERSION)
margarethaf3ff6272019-07-11 12:57:13 +020028 .path("statistics")
29 .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE")
abcpro1241bc4f2022-11-07 20:13:57 +000030 .request()
abcpro173fe8f22022-11-08 19:56:52 +000031 .method("GET");
32 assertEquals(Status.OK.getStatusCode(),
margarethaf3ff6272019-07-11 12:57:13 +020033 response.getStatus());
Akron48e51b92020-06-02 14:50:27 +020034
35 assertEquals(
36 "Wes8Bd4h1OypPqbWF5njeQ==",
margaretha9f2cff02022-01-03 15:42:45 +010037 response.getHeaders().getFirst("X-Index-Revision")
Akron48e51b92020-06-02 14:50:27 +020038 );
39
abcpro173fe8f22022-11-08 19:56:52 +000040 String query = response.readEntity(String.class);
margarethaf3ff6272019-07-11 12:57:13 +020041 JsonNode node = JsonUtils.readTree(query);
42 assertEquals(2, node.at("/documents").asInt());
43 assertEquals(138180, node.at("/tokens").asInt());
44 assertEquals(5687, node.at("/sentences").asInt());
45 assertEquals(258, node.at("/paragraphs").asInt());
margarethaa6cd0d92019-07-15 18:39:04 +020046
47 assertTrue(node.at("/warnings").isMissingNode());
margarethaf3ff6272019-07-11 12:57:13 +020048 }
49
50 @Test
51 public void testStatisticsWithCqAndCorpusQuery () throws KustvaktException{
abcpro173fe8f22022-11-08 19:56:52 +000052 Response response = target().path(API_VERSION)
margarethaf3ff6272019-07-11 12:57:13 +020053 .path("statistics")
54 .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE")
55 .queryParam("corpusQuery", "textType=Autobiographie & corpusSigle=GOE")
abcpro1241bc4f2022-11-07 20:13:57 +000056 .request()
abcpro173fe8f22022-11-08 19:56:52 +000057 .method("GET");
58 assertEquals(Status.OK.getStatusCode(),
margarethaf3ff6272019-07-11 12:57:13 +020059 response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000060 String query = response.readEntity(String.class);
margarethaf3ff6272019-07-11 12:57:13 +020061 JsonNode node = JsonUtils.readTree(query);
62 assertEquals(2, node.at("/documents").asInt());
63 assertEquals(138180, node.at("/tokens").asInt());
64 assertEquals(5687, node.at("/sentences").asInt());
65 assertEquals(258, node.at("/paragraphs").asInt());
66
margarethaa6cd0d92019-07-15 18:39:04 +020067 assertTrue(node.at("/warnings").isMissingNode());
margarethaf3ff6272019-07-11 12:57:13 +020068 }
69
70 @Test
margarethaa6cd0d92019-07-15 18:39:04 +020071 public void testStatisticsWithCorpusQuery () throws KustvaktException{
abcpro173fe8f22022-11-08 19:56:52 +000072 Response response = target().path(API_VERSION)
margaretha34954472018-10-24 20:05:17 +020073 .path("statistics")
74 .queryParam("corpusQuery", "textType=Autobiographie & corpusSigle=GOE")
abcpro1241bc4f2022-11-07 20:13:57 +000075 .request()
abcpro173fe8f22022-11-08 19:56:52 +000076 .method("GET");
77 assertEquals(Status.OK.getStatusCode(),
margaretha34954472018-10-24 20:05:17 +020078 response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +000079 String query = response.readEntity(String.class);
margaretha34954472018-10-24 20:05:17 +020080 JsonNode node = JsonUtils.readTree(query);
81 assertEquals(9, node.at("/documents").asInt());
82 assertEquals(527662, node.at("/tokens").asInt());
83 assertEquals(19387, node.at("/sentences").asInt());
84 assertEquals(514, node.at("/paragraphs").asInt());
margarethaa6cd0d92019-07-15 18:39:04 +020085
margarethabc3d3f72023-02-15 15:34:12 +010086 assertEquals(StatusCodes.DEPRECATED,
margarethaa6cd0d92019-07-15 18:39:04 +020087 node.at("/warnings/0/0").asInt());
88 assertEquals("Parameter corpusQuery is deprecated in favor of cq.",
89 node.at("/warnings/0/1").asText());
margaretha34954472018-10-24 20:05:17 +020090 }
91
92 @Test
93 public void testEmptyStatistics () throws KustvaktException{
abcpro173fe8f22022-11-08 19:56:52 +000094 Response response = target().path(API_VERSION)
margaretha34954472018-10-24 20:05:17 +020095 .path("statistics")
96 .queryParam("corpusQuery", "")
abcpro1241bc4f2022-11-07 20:13:57 +000097 .request()
abcpro173fe8f22022-11-08 19:56:52 +000098 .method("GET");
99 assertEquals(Status.OK.getStatusCode(),
margaretha34954472018-10-24 20:05:17 +0200100 response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000101 String query = response.readEntity(String.class);
margaretha34954472018-10-24 20:05:17 +0200102 JsonNode node = JsonUtils.readTree(query);
103 assertEquals(11, node.at("/documents").asInt());
104 assertEquals(665842, node.at("/tokens").asInt());
105 assertEquals(25074, node.at("/sentences").asInt());
106 assertEquals(772, node.at("/paragraphs").asInt());
107
abcpro173fe8f22022-11-08 19:56:52 +0000108 response = target().path(API_VERSION)
margaretha34954472018-10-24 20:05:17 +0200109 .path("statistics")
abcpro1241bc4f2022-11-07 20:13:57 +0000110 .request()
abcpro173fe8f22022-11-08 19:56:52 +0000111 .method("GET");
112 assertEquals(Status.OK.getStatusCode(),
margaretha34954472018-10-24 20:05:17 +0200113 response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000114 query = response.readEntity(String.class);
margaretha34954472018-10-24 20:05:17 +0200115 node = JsonUtils.readTree(query);
116 assertEquals(11, node.at("/documents").asInt());
117 assertEquals(665842, node.at("/tokens").asInt());
118 assertEquals(25074, node.at("/sentences").asInt());
119 assertEquals(772, node.at("/paragraphs").asInt());
120 }
121
margaretha072a6a72019-11-22 12:12:03 +0100122 @Test
123 public void testGetStatisticsWithKoralQuery ()
124 throws IOException, KustvaktException {
abcpro173fe8f22022-11-08 19:56:52 +0000125 Response response = target().path(API_VERSION)
margaretha072a6a72019-11-22 12:12:03 +0100126 .path("statistics")
abcpro1241bc4f2022-11-07 20:13:57 +0000127 .request()
margaretha072a6a72019-11-22 12:12:03 +0100128 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
abcpro173fe8f22022-11-08 19:56:52 +0000129 .post(Entity.json("{ \"collection\" : {\"@type\": "
margaretha072a6a72019-11-22 12:12:03 +0100130 + "\"koral:doc\", \"key\": \"availability\", \"match\": "
131 + "\"match:eq\", \"type\": \"type:regex\", \"value\": "
abcpro173fe8f22022-11-08 19:56:52 +0000132 + "\"CC-BY.*\"} }"));
margaretha072a6a72019-11-22 12:12:03 +0100133
abcpro173fe8f22022-11-08 19:56:52 +0000134 assertEquals(Status.OK.getStatusCode(),
margaretha072a6a72019-11-22 12:12:03 +0100135 response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000136 String ent = response.readEntity(String.class);
Akron48e51b92020-06-02 14:50:27 +0200137
138 assertEquals(
139 "Wes8Bd4h1OypPqbWF5njeQ==",
abcpro173fe8f22022-11-08 19:56:52 +0000140 response.getHeaders().getFirst("X-Index-Revision")
Akron48e51b92020-06-02 14:50:27 +0200141 );
margaretha072a6a72019-11-22 12:12:03 +0100142
143 JsonNode node = JsonUtils.readTree(ent);
144 assertEquals(2, node.at("/documents").asInt());
145 assertEquals(72770, node.at("/tokens").asInt());
146 assertEquals(2985, node.at("/sentences").asInt());
147 assertEquals(128, node.at("/paragraphs").asInt());
148 }
149
150 @Test
151 public void testGetStatisticsWithEmptyCollection ()
152 throws IOException, KustvaktException {
abcpro173fe8f22022-11-08 19:56:52 +0000153 Response response = target().path(API_VERSION)
margaretha072a6a72019-11-22 12:12:03 +0100154 .path("statistics")
abcpro1241bc4f2022-11-07 20:13:57 +0000155 .request()
margaretha072a6a72019-11-22 12:12:03 +0100156 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
abcpro173fe8f22022-11-08 19:56:52 +0000157 .post(Entity.json("{}"));
margaretha072a6a72019-11-22 12:12:03 +0100158
abcpro173fe8f22022-11-08 19:56:52 +0000159 assertEquals(Status.BAD_REQUEST.getStatusCode(),
margaretha072a6a72019-11-22 12:12:03 +0100160 response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000161 String ent = response.readEntity(String.class);
margaretha072a6a72019-11-22 12:12:03 +0100162 JsonNode node = JsonUtils.readTree(ent);
163 assertEquals(node.at("/errors/0/0").asInt(),
164 de.ids_mannheim.korap.util.StatusCodes.MISSING_COLLECTION);
165 assertEquals(node.at("/errors/0/1").asText(),
166 "Collection is not found");
167 }
168
169 @Test
170 public void testGetStatisticsWithIncorrectJson ()
171 throws IOException, KustvaktException {
abcpro173fe8f22022-11-08 19:56:52 +0000172 Response response = target().path(API_VERSION)
margaretha072a6a72019-11-22 12:12:03 +0100173 .path("statistics")
abcpro1241bc4f2022-11-07 20:13:57 +0000174 .request()
margaretha072a6a72019-11-22 12:12:03 +0100175 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
abcpro173fe8f22022-11-08 19:56:52 +0000176 .post(Entity.json("{ \"collection\" : }"));
margaretha072a6a72019-11-22 12:12:03 +0100177
abcpro173fe8f22022-11-08 19:56:52 +0000178 assertEquals(Status.BAD_REQUEST.getStatusCode(),
margaretha072a6a72019-11-22 12:12:03 +0100179 response.getStatus());
abcpro173fe8f22022-11-08 19:56:52 +0000180 String ent = response.readEntity(String.class);
margaretha072a6a72019-11-22 12:12:03 +0100181 JsonNode node = JsonUtils.readTree(ent);
margarethaed2ee242019-12-12 17:34:18 +0100182 assertEquals(StatusCodes.DESERIALIZATION_FAILED,
183 node.at("/errors/0/0").asInt());
184 assertEquals("Failed deserializing json object: { \"collection\" : }",
185 node.at("/errors/0/1").asText());
margaretha072a6a72019-11-22 12:12:03 +0100186 }
187
188 @Test
189 public void testGetStatisticsWithoutKoralQuery ()
190 throws IOException, KustvaktException {
abcpro173fe8f22022-11-08 19:56:52 +0000191 Response response = target().path(API_VERSION)
abcpro1241bc4f2022-11-07 20:13:57 +0000192 .path("statistics")
193 .request()
abcpro173fe8f22022-11-08 19:56:52 +0000194 .post(Entity.json(""));
margaretha072a6a72019-11-22 12:12:03 +0100195
abcpro173fe8f22022-11-08 19:56:52 +0000196 String ent = response.readEntity(String.class);
197 assertEquals(Status.OK.getStatusCode(),
margaretha072a6a72019-11-22 12:12:03 +0100198 response.getStatus());
199
200 JsonNode node = JsonUtils.readTree(ent);
201 assertEquals(11, node.at("/documents").asInt());
202 assertEquals(665842, node.at("/tokens").asInt());
203 assertEquals(25074, node.at("/sentences").asInt());
204 assertEquals(772, node.at("/paragraphs").asInt());
205 }
margaretha34954472018-10-24 20:05:17 +0200206}