blob: 335f564925b54d41ff3c2c66549facd94be02a80 [file] [log] [blame]
margarethab7864112017-06-29 16:36:13 +02001package de.ids_mannheim.korap.web.service.full;
2
3import java.io.IOException;
4
5import org.junit.BeforeClass;
6import org.junit.Test;
7import static org.junit.Assert.assertEquals;
8
9import com.fasterxml.jackson.core.JsonProcessingException;
10import com.fasterxml.jackson.databind.JsonNode;
11import com.fasterxml.jackson.databind.ObjectMapper;
12import com.sun.jersey.api.client.ClientResponse;
13
14import de.ids_mannheim.korap.exceptions.KustvaktException;
15import de.ids_mannheim.korap.web.service.FastJerseyTest;
16
17/**
18 * @author margaretha
19 * @date 29/06/2017
20 *
21 */
22public 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
margaretha586d7942017-07-04 16:22:32 +020039 @Test
40 public void testGetStatisticsNoResource ()
41 throws JsonProcessingException, IOException {
42 String collectionQuery = "corpusSigle=WPD15";
margaretha6c2a20f2017-07-24 15:33:01 +020043 ClientResponse response = resource()
margaretha586d7942017-07-04 16:22:32 +020044 .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
margarethab7864112017-06-29 16:36:13 +020056
57 @Test
58 public void testGetStatisticsWithCollectionQuery1 ()
59 throws JsonProcessingException, IOException {
60 String collectionQuery = "corpusSigle=GOE";
margaretha6c2a20f2017-07-24 15:33:01 +020061 ClientResponse response = resource()
margarethab7864112017-06-29 16:36:13 +020062 .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);
margaretha586d7942017-07-04 16:22:32 +020070 assertEquals(node.get("documents").asInt(),11);
71 assertEquals(node.get("tokens").asInt(),665842);
margarethab7864112017-06-29 16:36:13 +020072 }
73
74
75 @Test
76 public void testGetStatisticsWithCollectionQuery2 ()
77 throws JsonProcessingException, IOException {
margaretha6c2a20f2017-07-24 15:33:01 +020078 ClientResponse response = resource()
margarethab7864112017-06-29 16:36:13 +020079 .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();
margaretha586d7942017-07-04 16:22:32 +020085 assertEquals(node.get("documents").asInt(),7);
86 assertEquals(node.get("tokens").asInt(),279402);
margarethab7864112017-06-29 16:36:13 +020087 // EM: why zero?
Akrona3afa7d2017-07-04 16:13:22 +020088 assertEquals(node.get("sentences").asInt(), 11047);
89 assertEquals(node.get("paragraphs").asInt(), 489);
margarethab7864112017-06-29 16:36:13 +020090 }
91
92
93 @Test
94 public void testGetStatisticsWithWrongCollectionQuery ()
95 throws JsonProcessingException, IOException {
margaretha6c2a20f2017-07-24 15:33:01 +020096 ClientResponse response = resource()
margarethab7864112017-06-29 16:36:13 +020097 .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 {
margaretha6c2a20f2017-07-24 15:33:01 +0200116 ClientResponse response = resource()
margarethab7864112017-06-29 16:36:13 +0200117 .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}