| 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 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | import static org.junit.jupiter.api.Assertions.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 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 14 | import org.junit.jupiter.api.Test; |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 15 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 3d55b00 | 2019-03-19 12:00:44 +0100 | [diff] [blame] | 16 | import de.ids_mannheim.korap.config.LiteJerseyTest; |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 17 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 18 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 19 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 20 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 21 | public class LiteStatisticControllerTest extends LiteJerseyTest { |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 22 | |
| 23 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 24 | 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=="); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 28 | String query = response.readEntity(String.class); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 29 | 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()); |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 34 | assertTrue(node.at("/warnings").isMissingNode()); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 35 | } |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 36 | |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 37 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 38 | 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()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 41 | String query = response.readEntity(String.class); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 42 | 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()); |
| margaretha | a6cd0d9 | 2019-07-15 18:39:04 +0200 | [diff] [blame] | 47 | assertTrue(node.at("/warnings").isMissingNode()); |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 48 | } |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 49 | |
| margaretha | f3ff627 | 2019-07-11 12:57:13 +0200 | [diff] [blame] | 50 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 51 | 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()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 54 | String query = response.readEntity(String.class); |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 55 | 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 Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 60 | 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."); |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 65 | 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()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 68 | String query = response.readEntity(String.class); |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 69 | 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 Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 74 | response = target().path(API_VERSION).path("statistics").request().method("GET"); |
| 75 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 76 | query = response.readEntity(String.class); |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 77 | 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 Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 83 | |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 84 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 85 | 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()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 88 | String ent = response.readEntity(String.class); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 89 | assertEquals(response.getHeaders().getFirst("X-Index-Revision"), "Wes8Bd4h1OypPqbWF5njeQ=="); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 90 | 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 | } |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 96 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 97 | @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()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 101 | String ent = response.readEntity(String.class); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 102 | JsonNode node = JsonUtils.readTree(ent); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 103 | 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"); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 105 | } |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 106 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 107 | @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()); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 111 | String ent = response.readEntity(String.class); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 112 | JsonNode node = JsonUtils.readTree(ent); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 113 | assertEquals(StatusCodes.DESERIALIZATION_FAILED, node.at("/errors/0/0").asInt()); |
| 114 | assertEquals(node.at("/errors/0/1").asText(), "Failed deserializing json object: { \"collection\" : }"); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 115 | } |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 116 | |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 117 | @Test |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 118 | public void testGetStatisticsWithoutKoralQuery() throws IOException, KustvaktException { |
| 119 | Response response = target().path(API_VERSION).path("statistics").request().post(Entity.json("")); |
| abcpro1 | 73fe8f2 | 2022-11-08 19:56:52 +0000 | [diff] [blame] | 120 | String ent = response.readEntity(String.class); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 121 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | 072a6a7 | 2019-11-22 12:12:03 +0100 | [diff] [blame] | 122 | 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 | } |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 128 | } |