| margaretha | 92ad2ec | 2023-05-15 14:10:00 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.lite; |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 2 | |
| 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 | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 5 | |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 6 | import java.io.IOException; |
| 7 | |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame^] | 8 | import jakarta.ws.rs.client.Entity; |
| 9 | import jakarta.ws.rs.core.HttpHeaders; |
| 10 | import jakarta.ws.rs.core.MediaType; |
| 11 | import jakarta.ws.rs.core.Response; |
| 12 | import jakarta.ws.rs.core.Response.Status; |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 13 | |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 14 | import org.junit.Test; |
| 15 | |
| 16 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 17 | |
| margaretha | 3d55b00 | 2019-03-19 12:00:44 +0100 | [diff] [blame] | 18 | import de.ids_mannheim.korap.config.LiteJerseyTest; |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 19 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 20 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 21 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 22 | |
| 23 | public class LiteStatisticControllerTest extends LiteJerseyTest{ |
| 24 | |
| 25 | @Test |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 26 | public void testStatisticsWithCq () throws KustvaktException{ |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 27 | Response response = target().path(API_VERSION) |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 28 | .path("statistics") |
| 29 | .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 30 | .request() |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 31 | .method("GET"); |
| 32 | assertEquals(Status.OK.getStatusCode(), |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 33 | response.getStatus()); |
| Akron | 48e51b9 | 2020-06-02 14:50:27 +0200 | [diff] [blame] | 34 | |
| 35 | assertEquals( |
| 36 | "Wes8Bd4h1OypPqbWF5njeQ==", |
| margaretha | 9f2cff0 | 2022-01-03 15:42:45 +0100 | [diff] [blame] | 37 | response.getHeaders().getFirst("X-Index-Revision") |
| Akron | 48e51b9 | 2020-06-02 14:50:27 +0200 | [diff] [blame] | 38 | ); |
| 39 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 40 | String query = response.readEntity(String.class); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 41 | 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()); |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 46 | |
| 47 | assertTrue(node.at("/warnings").isMissingNode()); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void testStatisticsWithCqAndCorpusQuery () throws KustvaktException{ |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 52 | Response response = target().path(API_VERSION) |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 53 | .path("statistics") |
| 54 | .queryParam("cq", "textType=Abhandlung & corpusSigle=GOE") |
| 55 | .queryParam("corpusQuery", "textType=Autobiographie & corpusSigle=GOE") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 56 | .request() |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 57 | .method("GET"); |
| 58 | assertEquals(Status.OK.getStatusCode(), |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 59 | response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 60 | String query = response.readEntity(String.class); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 61 | 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 | |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 67 | assertTrue(node.at("/warnings").isMissingNode()); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Test |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 71 | public void testStatisticsWithCorpusQuery () throws KustvaktException{ |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 72 | Response response = target().path(API_VERSION) |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 73 | .path("statistics") |
| 74 | .queryParam("corpusQuery", "textType=Autobiographie & corpusSigle=GOE") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 75 | .request() |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 76 | .method("GET"); |
| 77 | assertEquals(Status.OK.getStatusCode(), |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 78 | response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 79 | String query = response.readEntity(String.class); |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 80 | 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()); |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 85 | |
| margaretha | bc3d3f7 | 2023-02-15 15:34:12 +0100 | [diff] [blame] | 86 | assertEquals(StatusCodes.DEPRECATED, |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 87 | node.at("/warnings/0/0").asInt()); |
| 88 | assertEquals("Parameter corpusQuery is deprecated in favor of cq.", |
| 89 | node.at("/warnings/0/1").asText()); |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testEmptyStatistics () throws KustvaktException{ |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 94 | Response response = target().path(API_VERSION) |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 95 | .path("statistics") |
| 96 | .queryParam("corpusQuery", "") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 97 | .request() |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 98 | .method("GET"); |
| 99 | assertEquals(Status.OK.getStatusCode(), |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 100 | response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 101 | String query = response.readEntity(String.class); |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 102 | 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 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 108 | response = target().path(API_VERSION) |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 109 | .path("statistics") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 110 | .request() |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 111 | .method("GET"); |
| 112 | assertEquals(Status.OK.getStatusCode(), |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 113 | response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 114 | query = response.readEntity(String.class); |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 115 | 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 | |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 122 | @Test |
| 123 | public void testGetStatisticsWithKoralQuery () |
| 124 | throws IOException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 125 | Response response = target().path(API_VERSION) |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 126 | .path("statistics") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 127 | .request() |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 128 | .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 129 | .post(Entity.json("{ \"collection\" : {\"@type\": " |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 130 | + "\"koral:doc\", \"key\": \"availability\", \"match\": " |
| 131 | + "\"match:eq\", \"type\": \"type:regex\", \"value\": " |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 132 | + "\"CC-BY.*\"} }")); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 133 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 134 | assertEquals(Status.OK.getStatusCode(), |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 135 | response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 136 | String ent = response.readEntity(String.class); |
| Akron | 48e51b9 | 2020-06-02 14:50:27 +0200 | [diff] [blame] | 137 | |
| 138 | assertEquals( |
| 139 | "Wes8Bd4h1OypPqbWF5njeQ==", |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 140 | response.getHeaders().getFirst("X-Index-Revision") |
| Akron | 48e51b9 | 2020-06-02 14:50:27 +0200 | [diff] [blame] | 141 | ); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 142 | |
| 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 { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 153 | Response response = target().path(API_VERSION) |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 154 | .path("statistics") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 155 | .request() |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 156 | .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 157 | .post(Entity.json("{}")); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 158 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 159 | assertEquals(Status.BAD_REQUEST.getStatusCode(), |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 160 | response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 161 | String ent = response.readEntity(String.class); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 162 | 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 { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 172 | Response response = target().path(API_VERSION) |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 173 | .path("statistics") |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 174 | .request() |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 175 | .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 176 | .post(Entity.json("{ \"collection\" : }")); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 177 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 178 | assertEquals(Status.BAD_REQUEST.getStatusCode(), |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 179 | response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 180 | String ent = response.readEntity(String.class); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 181 | JsonNode node = JsonUtils.readTree(ent); |
| margaretha | ed2ee24 | 2019-12-12 17:34:18 +0100 | [diff] [blame] | 182 | 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()); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void testGetStatisticsWithoutKoralQuery () |
| 190 | throws IOException, KustvaktException { |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 191 | Response response = target().path(API_VERSION) |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 192 | .path("statistics") |
| 193 | .request() |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 194 | .post(Entity.json("")); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 195 | |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 196 | String ent = response.readEntity(String.class); |
| 197 | assertEquals(Status.OK.getStatusCode(), |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 198 | 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 | } |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 206 | } |