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