| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.service.full; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 5 | import org.junit.Test; |
| 6 | import static org.junit.Assert.assertEquals; |
| 7 | |
| 8 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 9 | import com.fasterxml.jackson.databind.JsonNode; |
| 10 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | import com.sun.jersey.api.client.ClientResponse; |
| 12 | |
| 13 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 14 | import de.ids_mannheim.korap.web.service.FastJerseyTest; |
| 15 | |
| 16 | /** |
| 17 | * @author margaretha |
| margaretha | 5413490 | 2017-09-27 18:43:11 +0200 | [diff] [blame] | 18 | * @date 27/09/2017 |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | public class StatisticsServiceTest extends FastJerseyTest { |
| 22 | |
| 23 | private ObjectMapper mapper = new ObjectMapper(); |
| 24 | |
| 25 | |
| 26 | @Override |
| 27 | public void initMethod () throws KustvaktException { |
| 28 | |
| 29 | } |
| 30 | |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 31 | @Test |
| 32 | public void testGetStatisticsNoResource () |
| 33 | throws JsonProcessingException, IOException { |
| 34 | String collectionQuery = "corpusSigle=WPD15"; |
| margaretha | 6c2a20f | 2017-07-24 15:33:01 +0200 | [diff] [blame] | 35 | ClientResponse response = resource() |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 36 | .path("statistics") |
| 37 | .queryParam("collectionQuery", collectionQuery) |
| 38 | .get(ClientResponse.class); |
| 39 | |
| 40 | assert ClientResponse.Status.OK.getStatusCode() == response.getStatus(); |
| 41 | |
| 42 | String ent = response.getEntity(String.class); |
| 43 | JsonNode node = mapper.readTree(ent); |
| 44 | assertEquals(node.get("documents").asInt(),0); |
| 45 | assertEquals(node.get("tokens").asInt(),0); |
| 46 | } |
| 47 | |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 48 | |
| 49 | @Test |
| 50 | public void testGetStatisticsWithCollectionQuery1 () |
| 51 | throws JsonProcessingException, IOException { |
| 52 | String collectionQuery = "corpusSigle=GOE"; |
| margaretha | 6c2a20f | 2017-07-24 15:33:01 +0200 | [diff] [blame] | 53 | ClientResponse response = resource() |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 54 | .path("statistics") |
| 55 | .queryParam("collectionQuery", collectionQuery) |
| 56 | .get(ClientResponse.class); |
| 57 | |
| 58 | assert ClientResponse.Status.OK.getStatusCode() == response.getStatus(); |
| 59 | |
| 60 | String ent = response.getEntity(String.class); |
| 61 | JsonNode node = mapper.readTree(ent); |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 62 | assertEquals(node.get("documents").asInt(),11); |
| 63 | assertEquals(node.get("tokens").asInt(),665842); |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
| 67 | @Test |
| 68 | public void testGetStatisticsWithCollectionQuery2 () |
| 69 | throws JsonProcessingException, IOException { |
| margaretha | 6c2a20f | 2017-07-24 15:33:01 +0200 | [diff] [blame] | 70 | ClientResponse response = resource() |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 71 | .path("statistics") |
| 72 | .queryParam("collectionQuery", "creationDate since 1810") |
| 73 | .get(ClientResponse.class); |
| 74 | String ent = response.getEntity(String.class); |
| 75 | JsonNode node = mapper.readTree(ent); |
| 76 | assert ClientResponse.Status.OK.getStatusCode() == response.getStatus(); |
| margaretha | 586d794 | 2017-07-04 16:22:32 +0200 | [diff] [blame] | 77 | assertEquals(node.get("documents").asInt(),7); |
| 78 | assertEquals(node.get("tokens").asInt(),279402); |
| Akron | a3afa7d | 2017-07-04 16:13:22 +0200 | [diff] [blame] | 79 | assertEquals(node.get("sentences").asInt(), 11047); |
| 80 | assertEquals(node.get("paragraphs").asInt(), 489); |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
| 84 | @Test |
| 85 | public void testGetStatisticsWithWrongCollectionQuery () |
| 86 | throws JsonProcessingException, IOException { |
| margaretha | 6c2a20f | 2017-07-24 15:33:01 +0200 | [diff] [blame] | 87 | ClientResponse response = resource() |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 88 | .path("statistics") |
| 89 | .queryParam("collectionQuery", "creationDate geq 1810") |
| 90 | .get(ClientResponse.class); |
| 91 | |
| 92 | assert ClientResponse.Status.BAD_REQUEST.getStatusCode() == response |
| 93 | .getStatus(); |
| 94 | String ent = response.getEntity(String.class); |
| 95 | JsonNode node = mapper.readTree(ent); |
| 96 | assertEquals(node.at("/errors/0/0").asInt(), 302); |
| 97 | assertEquals(node.at("/errors/0/1").asText(), |
| 98 | "Could not parse query >>> (creationDate geq 1810) <<<."); |
| 99 | assertEquals(node.at("/errors/0/2").asText(), |
| 100 | "(creationDate geq 1810)"); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | @Test |
| 105 | public void testGetStatisticsWithWrongCollectionQuery2 () |
| 106 | throws JsonProcessingException, IOException { |
| margaretha | 6c2a20f | 2017-07-24 15:33:01 +0200 | [diff] [blame] | 107 | ClientResponse response = resource() |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 108 | .path("statistics") |
| 109 | .queryParam("collectionQuery", "creationDate >= 1810") |
| 110 | .get(ClientResponse.class); |
| 111 | |
| 112 | assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(), |
| 113 | response.getStatus()); |
| 114 | String ent = response.getEntity(String.class); |
| 115 | JsonNode node = mapper.readTree(ent); |
| 116 | assertEquals(node.at("/errors/0/0").asInt(), 305); |
| 117 | assertEquals(node.at("/errors/0/1").asText(), |
| 118 | "Operator >= is not acceptable."); |
| 119 | assertEquals(node.at("/errors/0/2").asText(), ">="); |
| 120 | } |
| 121 | |
| margaretha | 5413490 | 2017-09-27 18:43:11 +0200 | [diff] [blame] | 122 | |
| 123 | @Test |
| 124 | public void testGetStatisticsWithoutCollectionQuery () |
| 125 | throws JsonProcessingException, IOException { |
| 126 | ClientResponse response = resource() |
| 127 | .path("statistics") |
| 128 | .get(ClientResponse.class); |
| 129 | |
| 130 | assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(), |
| 131 | response.getStatus()); |
| 132 | String ent = response.getEntity(String.class); |
| margaretha | f6d5a82 | 2017-10-19 19:51:20 +0200 | [diff] [blame] | 133 | System.out.println(ent); |
| margaretha | 5413490 | 2017-09-27 18:43:11 +0200 | [diff] [blame] | 134 | JsonNode node = mapper.readTree(ent); |
| 135 | assertEquals(node.at("/errors/0/0").asInt(), 105); |
| 136 | assertEquals(node.at("/errors/0/1").asText(), |
| 137 | "Parameter collectionQuery is missing."); |
| 138 | assertEquals(node.at("/errors/0/2").asText(), "collectionQuery"); |
| 139 | } |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 140 | } |