| margaretha | 541b8cc | 2018-01-10 13:02:46 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 2 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 4 | import static org.junit.Assert.assertTrue; |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 5 | |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 6 | import java.io.IOException; |
| 7 | |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 8 | import javax.ws.rs.core.HttpHeaders; |
| 9 | import javax.ws.rs.core.MediaType; |
| 10 | |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 11 | import org.junit.Test; |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 12 | |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 13 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 14 | import com.sun.jersey.api.client.ClientResponse; |
| 15 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 16 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 17 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 18 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 19 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 20 | |
| 21 | /** |
| Akron | 6091cf3 | 2018-07-03 20:45:49 +0200 | [diff] [blame] | 22 | * @author margaretha, diewald |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 23 | * |
| 24 | */ |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 25 | public class StatisticsControllerTest extends SpringJerseyTest { |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 26 | |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 27 | @Test |
| 28 | public void testGetStatisticsNoResource () |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 29 | throws IOException, KustvaktException { |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 30 | String corpusQuery = "corpusSigle=WPD15"; |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 31 | ClientResponse response = resource().path(API_VERSION) |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 32 | .path("statistics") |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 33 | .queryParam("corpusQuery", corpusQuery) |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 34 | .get(ClientResponse.class); |
| 35 | |
| 36 | assert ClientResponse.Status.OK.getStatusCode() == response.getStatus(); |
| 37 | |
| 38 | String ent = response.getEntity(String.class); |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 39 | JsonNode node = JsonUtils.readTree(ent); |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 40 | assertEquals(node.get("documents").asInt(),0); |
| 41 | assertEquals(node.get("tokens").asInt(),0); |
| 42 | } |
| 43 | |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 44 | @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()); |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 58 | |
| 59 | assertTrue(node.at("/warnings").isMissingNode()); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 60 | } |
| 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()); |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 77 | |
| 78 | assertTrue(node.at("/warnings").isMissingNode()); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 79 | } |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 80 | |
| 81 | @Test |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 82 | public void testGetStatisticsWithcorpusQuery1 () |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 83 | throws IOException, KustvaktException { |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 84 | String corpusQuery = "corpusSigle=GOE"; |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 85 | ClientResponse response = resource().path(API_VERSION) |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 86 | .path("statistics") |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 87 | .queryParam("corpusQuery", corpusQuery) |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 88 | .get(ClientResponse.class); |
| 89 | |
| 90 | assert ClientResponse.Status.OK.getStatusCode() == response.getStatus(); |
| 91 | |
| 92 | String ent = response.getEntity(String.class); |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 93 | JsonNode node = JsonUtils.readTree(ent); |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 94 | assertEquals(node.get("documents").asInt(),11); |
| 95 | assertEquals(node.get("tokens").asInt(),665842); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 96 | |
| 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()); |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | |
| 104 | @Test |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 105 | public void testGetStatisticsWithcorpusQuery2 () |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 106 | throws IOException, KustvaktException { |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 107 | ClientResponse response = resource().path(API_VERSION) |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 108 | .path("statistics") |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 109 | .queryParam("corpusQuery", "creationDate since 1810") |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 110 | .get(ClientResponse.class); |
| 111 | String ent = response.getEntity(String.class); |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 112 | JsonNode node = JsonUtils.readTree(ent); |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 113 | assert ClientResponse.Status.OK.getStatusCode() == response.getStatus(); |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 114 | assertEquals(node.get("documents").asInt(),7); |
| 115 | assertEquals(node.get("tokens").asInt(),279402); |
| Akron | a3afa7d | 2017-07-04 16:13:22 +0200 | [diff] [blame] | 116 | assertEquals(node.get("sentences").asInt(), 11047); |
| 117 | assertEquals(node.get("paragraphs").asInt(), 489); |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | @Test |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 122 | public void testGetStatisticsWithWrongcorpusQuery () |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 123 | throws IOException, KustvaktException { |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 124 | ClientResponse response = resource().path(API_VERSION) |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 125 | .path("statistics") |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 126 | .queryParam("corpusQuery", "creationDate geq 1810") |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 127 | .get(ClientResponse.class); |
| 128 | |
| 129 | assert ClientResponse.Status.BAD_REQUEST.getStatusCode() == response |
| 130 | .getStatus(); |
| 131 | String ent = response.getEntity(String.class); |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 132 | JsonNode node = JsonUtils.readTree(ent); |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 133 | 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 |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 142 | public void testGetStatisticsWithWrongcorpusQuery2 () |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 143 | throws IOException, KustvaktException { |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 144 | ClientResponse response = resource().path(API_VERSION) |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 145 | .path("statistics") |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 146 | .queryParam("corpusQuery", "creationDate >= 1810") |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 147 | .get(ClientResponse.class); |
| 148 | |
| margaretha | 2558a7c | 2019-02-18 16:48:54 +0100 | [diff] [blame] | 149 | String ent = response.getEntity(String.class); |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 150 | assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(), |
| 151 | response.getStatus()); |
| margaretha | 2558a7c | 2019-02-18 16:48:54 +0100 | [diff] [blame] | 152 | |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 153 | JsonNode node = JsonUtils.readTree(ent); |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 154 | 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 | |
| margaretha | 5413490 | 2017-09-27 18:43:11 +0200 | [diff] [blame] | 160 | |
| 161 | @Test |
| margaretha | e8ab51d | 2018-01-16 19:27:40 +0100 | [diff] [blame] | 162 | public void testGetStatisticsWithoutcorpusQuery () |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 163 | throws IOException, KustvaktException { |
| margaretha | ee0cbfe | 2018-08-28 17:47:14 +0200 | [diff] [blame] | 164 | ClientResponse response = resource().path(API_VERSION) |
| margaretha | 5413490 | 2017-09-27 18:43:11 +0200 | [diff] [blame] | 165 | .path("statistics") |
| 166 | .get(ClientResponse.class); |
| 167 | |
| Akron | 6091cf3 | 2018-07-03 20:45:49 +0200 | [diff] [blame] | 168 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 169 | response.getStatus()); |
| margaretha | 5413490 | 2017-09-27 18:43:11 +0200 | [diff] [blame] | 170 | String ent = response.getEntity(String.class); |
| Akron | 6091cf3 | 2018-07-03 20:45:49 +0200 | [diff] [blame] | 171 | |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 172 | JsonNode node = JsonUtils.readTree(ent); |
| Akron | 6091cf3 | 2018-07-03 20:45:49 +0200 | [diff] [blame] | 173 | 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()); |
| margaretha | 5413490 | 2017-09-27 18:43:11 +0200 | [diff] [blame] | 177 | } |
| margaretha | c7196d2 | 2018-08-27 14:20:03 +0200 | [diff] [blame] | 178 | |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 179 | @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); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame^] | 242 | |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 243 | String ent = response.getEntity(String.class); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame^] | 244 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 245 | response.getStatus()); |
| 246 | |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 247 | JsonNode node = JsonUtils.readTree(ent); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame^] | 248 | 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()); |
| margaretha | 802dabc | 2019-09-23 12:47:59 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 254 | } |