blob: dc5d3c7ba79956bdb22993c85a78d123af9725da [file] [log] [blame]
margaretha541b8cc2018-01-10 13:02:46 +01001package de.ids_mannheim.korap.web.controller;
margarethab7864112017-06-29 16:36:13 +02002
margaretha45667922018-01-25 21:23:03 +01003import static org.junit.Assert.assertEquals;
margarethaa6cd0d92019-07-15 18:39:04 +02004import static org.junit.Assert.assertTrue;
margaretha45667922018-01-25 21:23:03 +01005
margarethab7864112017-06-29 16:36:13 +02006import java.io.IOException;
7
margaretha802dabc2019-09-23 12:47:59 +02008import javax.ws.rs.core.HttpHeaders;
9import javax.ws.rs.core.MediaType;
10
margarethab7864112017-06-29 16:36:13 +020011import org.junit.Test;
margarethab7864112017-06-29 16:36:13 +020012
margarethab7864112017-06-29 16:36:13 +020013import com.fasterxml.jackson.databind.JsonNode;
margarethab7864112017-06-29 16:36:13 +020014import com.sun.jersey.api.client.ClientResponse;
15
margaretha45667922018-01-25 21:23:03 +010016import de.ids_mannheim.korap.config.SpringJerseyTest;
margarethaf3ff6272019-07-11 12:57:13 +020017import de.ids_mannheim.korap.exceptions.KustvaktException;
18import de.ids_mannheim.korap.exceptions.StatusCodes;
19import de.ids_mannheim.korap.utils.JsonUtils;
margarethab7864112017-06-29 16:36:13 +020020
21/**
Akron6091cf32018-07-03 20:45:49 +020022 * @author margaretha, diewald
margarethab7864112017-06-29 16:36:13 +020023 *
24 */
margaretha45667922018-01-25 21:23:03 +010025public class StatisticsControllerTest extends SpringJerseyTest {
margarethab7864112017-06-29 16:36:13 +020026
margaretha586d7942017-07-04 16:22:32 +020027 @Test
28 public void testGetStatisticsNoResource ()
margaretha802dabc2019-09-23 12:47:59 +020029 throws IOException, KustvaktException {
margarethae8ab51d2018-01-16 19:27:40 +010030 String corpusQuery = "corpusSigle=WPD15";
margarethaee0cbfe2018-08-28 17:47:14 +020031 ClientResponse response = resource().path(API_VERSION)
margaretha586d7942017-07-04 16:22:32 +020032 .path("statistics")
margarethae8ab51d2018-01-16 19:27:40 +010033 .queryParam("corpusQuery", corpusQuery)
margaretha586d7942017-07-04 16:22:32 +020034 .get(ClientResponse.class);
35
36 assert ClientResponse.Status.OK.getStatusCode() == response.getStatus();
37
38 String ent = response.getEntity(String.class);
margaretha802dabc2019-09-23 12:47:59 +020039 JsonNode node = JsonUtils.readTree(ent);
margaretha586d7942017-07-04 16:22:32 +020040 assertEquals(node.get("documents").asInt(),0);
41 assertEquals(node.get("tokens").asInt(),0);
42 }
43
margarethaf3ff6272019-07-11 12:57:13 +020044 @Test
45 public void testStatisticsWithCq () throws KustvaktException{
46 ClientResponse response = resource().path(API_VERSION)
47 .path("statistics")
48 .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE")
49 .method("GET", ClientResponse.class);
50 assertEquals(ClientResponse.Status.OK.getStatusCode(),
51 response.getStatus());
52 String query = response.getEntity(String.class);
53 JsonNode node = JsonUtils.readTree(query);
54 assertEquals(2, node.at("/documents").asInt());
55 assertEquals(138180, node.at("/tokens").asInt());
56 assertEquals(5687, node.at("/sentences").asInt());
57 assertEquals(258, node.at("/paragraphs").asInt());
margarethaa6cd0d92019-07-15 18:39:04 +020058
59 assertTrue(node.at("/warnings").isMissingNode());
margarethaf3ff6272019-07-11 12:57:13 +020060 }
61
62 @Test
63 public void testStatisticsWithCqAndCorpusQuery () throws KustvaktException{
64 ClientResponse response = resource().path(API_VERSION)
65 .path("statistics")
66 .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE")
67 .queryParam("corpusQuery", "textType=Autobiographie & corpusSigle=GOE")
68 .method("GET", ClientResponse.class);
69 assertEquals(ClientResponse.Status.OK.getStatusCode(),
70 response.getStatus());
71 String query = response.getEntity(String.class);
72 JsonNode node = JsonUtils.readTree(query);
73 assertEquals(2, node.at("/documents").asInt());
74 assertEquals(138180, node.at("/tokens").asInt());
75 assertEquals(5687, node.at("/sentences").asInt());
76 assertEquals(258, node.at("/paragraphs").asInt());
margarethaa6cd0d92019-07-15 18:39:04 +020077
78 assertTrue(node.at("/warnings").isMissingNode());
margarethaf3ff6272019-07-11 12:57:13 +020079 }
margarethab7864112017-06-29 16:36:13 +020080
81 @Test
margarethae8ab51d2018-01-16 19:27:40 +010082 public void testGetStatisticsWithcorpusQuery1 ()
margaretha802dabc2019-09-23 12:47:59 +020083 throws IOException, KustvaktException {
margarethae8ab51d2018-01-16 19:27:40 +010084 String corpusQuery = "corpusSigle=GOE";
margarethaee0cbfe2018-08-28 17:47:14 +020085 ClientResponse response = resource().path(API_VERSION)
margarethab7864112017-06-29 16:36:13 +020086 .path("statistics")
margarethae8ab51d2018-01-16 19:27:40 +010087 .queryParam("corpusQuery", corpusQuery)
margarethab7864112017-06-29 16:36:13 +020088 .get(ClientResponse.class);
89
90 assert ClientResponse.Status.OK.getStatusCode() == response.getStatus();
91
92 String ent = response.getEntity(String.class);
margaretha802dabc2019-09-23 12:47:59 +020093 JsonNode node = JsonUtils.readTree(ent);
margaretha586d7942017-07-04 16:22:32 +020094 assertEquals(node.get("documents").asInt(),11);
95 assertEquals(node.get("tokens").asInt(),665842);
margarethaf3ff6272019-07-11 12:57:13 +020096
97 assertEquals(StatusCodes.DEPRECATED_PARAMETER,
98 node.at("/warnings/0/0").asInt());
99 assertEquals("Parameter corpusQuery is deprecated in favor of cq.",
100 node.at("/warnings/0/1").asText());
margarethab7864112017-06-29 16:36:13 +0200101 }
102
103
104 @Test
margarethae8ab51d2018-01-16 19:27:40 +0100105 public void testGetStatisticsWithcorpusQuery2 ()
margaretha802dabc2019-09-23 12:47:59 +0200106 throws IOException, KustvaktException {
margarethaee0cbfe2018-08-28 17:47:14 +0200107 ClientResponse response = resource().path(API_VERSION)
margarethab7864112017-06-29 16:36:13 +0200108 .path("statistics")
margarethae8ab51d2018-01-16 19:27:40 +0100109 .queryParam("corpusQuery", "creationDate since 1810")
margarethab7864112017-06-29 16:36:13 +0200110 .get(ClientResponse.class);
111 String ent = response.getEntity(String.class);
margaretha802dabc2019-09-23 12:47:59 +0200112 JsonNode node = JsonUtils.readTree(ent);
margarethab7864112017-06-29 16:36:13 +0200113 assert ClientResponse.Status.OK.getStatusCode() == response.getStatus();
margaretha586d7942017-07-04 16:22:32 +0200114 assertEquals(node.get("documents").asInt(),7);
115 assertEquals(node.get("tokens").asInt(),279402);
Akrona3afa7d2017-07-04 16:13:22 +0200116 assertEquals(node.get("sentences").asInt(), 11047);
117 assertEquals(node.get("paragraphs").asInt(), 489);
margarethab7864112017-06-29 16:36:13 +0200118 }
119
120
121 @Test
margarethae8ab51d2018-01-16 19:27:40 +0100122 public void testGetStatisticsWithWrongcorpusQuery ()
margaretha802dabc2019-09-23 12:47:59 +0200123 throws IOException, KustvaktException {
margarethaee0cbfe2018-08-28 17:47:14 +0200124 ClientResponse response = resource().path(API_VERSION)
margarethab7864112017-06-29 16:36:13 +0200125 .path("statistics")
margarethae8ab51d2018-01-16 19:27:40 +0100126 .queryParam("corpusQuery", "creationDate geq 1810")
margarethab7864112017-06-29 16:36:13 +0200127 .get(ClientResponse.class);
128
129 assert ClientResponse.Status.BAD_REQUEST.getStatusCode() == response
130 .getStatus();
131 String ent = response.getEntity(String.class);
margaretha802dabc2019-09-23 12:47:59 +0200132 JsonNode node = JsonUtils.readTree(ent);
margarethab7864112017-06-29 16:36:13 +0200133 assertEquals(node.at("/errors/0/0").asInt(), 302);
134 assertEquals(node.at("/errors/0/1").asText(),
135 "Could not parse query >>> (creationDate geq 1810) <<<.");
136 assertEquals(node.at("/errors/0/2").asText(),
137 "(creationDate geq 1810)");
138 }
139
140
141 @Test
margarethae8ab51d2018-01-16 19:27:40 +0100142 public void testGetStatisticsWithWrongcorpusQuery2 ()
margaretha802dabc2019-09-23 12:47:59 +0200143 throws IOException, KustvaktException {
margarethaee0cbfe2018-08-28 17:47:14 +0200144 ClientResponse response = resource().path(API_VERSION)
margarethab7864112017-06-29 16:36:13 +0200145 .path("statistics")
margarethae8ab51d2018-01-16 19:27:40 +0100146 .queryParam("corpusQuery", "creationDate >= 1810")
margarethab7864112017-06-29 16:36:13 +0200147 .get(ClientResponse.class);
148
margaretha2558a7c2019-02-18 16:48:54 +0100149 String ent = response.getEntity(String.class);
margarethab7864112017-06-29 16:36:13 +0200150 assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
151 response.getStatus());
margaretha2558a7c2019-02-18 16:48:54 +0100152
margaretha802dabc2019-09-23 12:47:59 +0200153 JsonNode node = JsonUtils.readTree(ent);
margarethab7864112017-06-29 16:36:13 +0200154 assertEquals(node.at("/errors/0/0").asInt(), 305);
155 assertEquals(node.at("/errors/0/1").asText(),
156 "Operator >= is not acceptable.");
157 assertEquals(node.at("/errors/0/2").asText(), ">=");
158 }
159
margaretha54134902017-09-27 18:43:11 +0200160
161 @Test
margarethae8ab51d2018-01-16 19:27:40 +0100162 public void testGetStatisticsWithoutcorpusQuery ()
margaretha802dabc2019-09-23 12:47:59 +0200163 throws IOException, KustvaktException {
margarethaee0cbfe2018-08-28 17:47:14 +0200164 ClientResponse response = resource().path(API_VERSION)
margaretha54134902017-09-27 18:43:11 +0200165 .path("statistics")
166 .get(ClientResponse.class);
167
Akron6091cf32018-07-03 20:45:49 +0200168 assertEquals(ClientResponse.Status.OK.getStatusCode(),
169 response.getStatus());
margaretha54134902017-09-27 18:43:11 +0200170 String ent = response.getEntity(String.class);
Akron6091cf32018-07-03 20:45:49 +0200171
margaretha802dabc2019-09-23 12:47:59 +0200172 JsonNode node = JsonUtils.readTree(ent);
Akron6091cf32018-07-03 20:45:49 +0200173 assertEquals(11, node.at("/documents").asInt());
174 assertEquals(665842, node.at("/tokens").asInt());
175 assertEquals(25074, node.at("/sentences").asInt());
176 assertEquals(772, node.at("/paragraphs").asInt());
margaretha54134902017-09-27 18:43:11 +0200177 }
margarethac7196d22018-08-27 14:20:03 +0200178
margaretha802dabc2019-09-23 12:47:59 +0200179 @Test
180 public void testGetStatisticsWithKoralQuery ()
181 throws IOException, KustvaktException {
182 ClientResponse response = resource().path(API_VERSION)
183 .path("statistics")
184 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
185 .post(ClientResponse.class,"{ \"collection\" : {\"@type\": "
186 + "\"koral:doc\", \"key\": \"availability\", \"match\": "
187 + "\"match:eq\", \"type\": \"type:regex\", \"value\": "
188 + "\"CC-BY.*\"} }");
189
190 assertEquals(ClientResponse.Status.OK.getStatusCode(),
191 response.getStatus());
192 String ent = response.getEntity(String.class);
193
194 JsonNode node = JsonUtils.readTree(ent);
195 assertEquals(2, node.at("/documents").asInt());
196 assertEquals(72770, node.at("/tokens").asInt());
197 assertEquals(2985, node.at("/sentences").asInt());
198 assertEquals(128, node.at("/paragraphs").asInt());
199 }
200
201 @Test
202 public void testGetStatisticsWithEmptyCollection ()
203 throws IOException, KustvaktException {
204 ClientResponse response = resource().path(API_VERSION)
205 .path("statistics")
206 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
207 .post(ClientResponse.class,"{}");
208
209 assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
210 response.getStatus());
211 String ent = response.getEntity(String.class);
212 JsonNode node = JsonUtils.readTree(ent);
213 assertEquals(node.at("/errors/0/0").asInt(),
214 de.ids_mannheim.korap.util.StatusCodes.MISSING_COLLECTION);
215 assertEquals(node.at("/errors/0/1").asText(),
216 "Collection is not found");
217 }
218
219 @Test
220 public void testGetStatisticsWithIncorrectJson ()
221 throws IOException, KustvaktException {
222 ClientResponse response = resource().path(API_VERSION)
223 .path("statistics")
224 .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
225 .post(ClientResponse.class,"{ \"collection\" : }");
226
227 assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
228 response.getStatus());
229 String ent = response.getEntity(String.class);
230 JsonNode node = JsonUtils.readTree(ent);
231 assertEquals(node.at("/errors/0/0").asInt(),
232 de.ids_mannheim.korap.util.StatusCodes.UNABLE_TO_PARSE_JSON);
233 assertEquals(node.at("/errors/0/1").asText(),
234 "Unable to parse JSON");
235 }
236
237 @Test
238 public void testGetStatisticsWithoutKoralQuery ()
239 throws IOException, KustvaktException {
240 ClientResponse response = resource().path(API_VERSION)
241 .path("statistics").post(ClientResponse.class);
margaretha072a6a72019-11-22 12:12:03 +0100242
margaretha802dabc2019-09-23 12:47:59 +0200243 String ent = response.getEntity(String.class);
margaretha072a6a72019-11-22 12:12:03 +0100244 assertEquals(ClientResponse.Status.OK.getStatusCode(),
245 response.getStatus());
246
margaretha802dabc2019-09-23 12:47:59 +0200247 JsonNode node = JsonUtils.readTree(ent);
margaretha072a6a72019-11-22 12:12:03 +0100248 assertEquals(11, node.at("/documents").asInt());
249 assertEquals(665842, node.at("/tokens").asInt());
250 assertEquals(25074, node.at("/sentences").asInt());
251 assertEquals(772, node.at("/paragraphs").asInt());
margaretha802dabc2019-09-23 12:47:59 +0200252 }
253
margarethab7864112017-06-29 16:36:13 +0200254}