blob: 64f7c3528206b35eac219d6baa3ba219463e9602 [file] [log] [blame]
margarethab7864112017-06-29 16:36:13 +02001package de.ids_mannheim.korap.web.service.full;
2
3import java.io.IOException;
4
margarethab7864112017-06-29 16:36:13 +02005import org.junit.Test;
6import static org.junit.Assert.assertEquals;
7
8import com.fasterxml.jackson.core.JsonProcessingException;
9import com.fasterxml.jackson.databind.JsonNode;
10import com.fasterxml.jackson.databind.ObjectMapper;
11import com.sun.jersey.api.client.ClientResponse;
12
13import de.ids_mannheim.korap.exceptions.KustvaktException;
14import de.ids_mannheim.korap.web.service.FastJerseyTest;
15
16/**
17 * @author margaretha
margaretha54134902017-09-27 18:43:11 +020018 * @date 27/09/2017
margarethab7864112017-06-29 16:36:13 +020019 *
20 */
21public class StatisticsServiceTest extends FastJerseyTest {
22
23 private ObjectMapper mapper = new ObjectMapper();
24
25
26 @Override
27 public void initMethod () throws KustvaktException {
28
29 }
30
margaretha586d7942017-07-04 16:22:32 +020031 @Test
32 public void testGetStatisticsNoResource ()
33 throws JsonProcessingException, IOException {
34 String collectionQuery = "corpusSigle=WPD15";
margaretha6c2a20f2017-07-24 15:33:01 +020035 ClientResponse response = resource()
margaretha586d7942017-07-04 16:22:32 +020036 .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
margarethab7864112017-06-29 16:36:13 +020048
49 @Test
50 public void testGetStatisticsWithCollectionQuery1 ()
51 throws JsonProcessingException, IOException {
52 String collectionQuery = "corpusSigle=GOE";
margaretha6c2a20f2017-07-24 15:33:01 +020053 ClientResponse response = resource()
margarethab7864112017-06-29 16:36:13 +020054 .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);
margaretha586d7942017-07-04 16:22:32 +020062 assertEquals(node.get("documents").asInt(),11);
63 assertEquals(node.get("tokens").asInt(),665842);
margarethab7864112017-06-29 16:36:13 +020064 }
65
66
67 @Test
68 public void testGetStatisticsWithCollectionQuery2 ()
69 throws JsonProcessingException, IOException {
margaretha6c2a20f2017-07-24 15:33:01 +020070 ClientResponse response = resource()
margarethab7864112017-06-29 16:36:13 +020071 .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();
margaretha586d7942017-07-04 16:22:32 +020077 assertEquals(node.get("documents").asInt(),7);
78 assertEquals(node.get("tokens").asInt(),279402);
Akrona3afa7d2017-07-04 16:13:22 +020079 assertEquals(node.get("sentences").asInt(), 11047);
80 assertEquals(node.get("paragraphs").asInt(), 489);
margarethab7864112017-06-29 16:36:13 +020081 }
82
83
84 @Test
85 public void testGetStatisticsWithWrongCollectionQuery ()
86 throws JsonProcessingException, IOException {
margaretha6c2a20f2017-07-24 15:33:01 +020087 ClientResponse response = resource()
margarethab7864112017-06-29 16:36:13 +020088 .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 {
margaretha6c2a20f2017-07-24 15:33:01 +0200107 ClientResponse response = resource()
margarethab7864112017-06-29 16:36:13 +0200108 .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
margaretha54134902017-09-27 18:43:11 +0200122
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);
margarethaf6d5a822017-10-19 19:51:20 +0200133 System.out.println(ent);
margaretha54134902017-09-27 18:43:11 +0200134 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 }
margarethab7864112017-06-29 16:36:13 +0200140}