blob: afecf1c34379ca113c867c2a4553a0fc7d5a0244 [file] [log] [blame]
Nils Diewald14b3aa42015-02-26 22:26:20 +00001package de.ids_mannheim.korap.server;
Nils Diewaldff6f7662014-09-21 15:08:52 +00002
Nils Diewaldff6f7662014-09-21 15:08:52 +00003import java.io.*;
Nils Diewaldd723d812014-09-23 18:50:52 +00004import java.util.ArrayList;
5import java.util.List;
6
Nils Diewaldff6f7662014-09-21 15:08:52 +00007import javax.ws.rs.client.Client;
8import javax.ws.rs.client.ClientBuilder;
9import javax.ws.rs.client.WebTarget;
10import javax.ws.rs.client.Entity;
11
Akron67d2ff02018-06-19 10:51:16 +020012import static de.ids_mannheim.korap.TestSimple.*;
13
Nils Diewaldff6f7662014-09-21 15:08:52 +000014import org.glassfish.grizzly.http.server.HttpServer;
15
Nils Diewaldd723d812014-09-23 18:50:52 +000016import static org.junit.Assert.*;
Nils Diewaldff6f7662014-09-21 15:08:52 +000017import org.junit.After;
18import org.junit.Before;
19import org.junit.Test;
Nils Diewaldff6f7662014-09-21 15:08:52 +000020
21import java.io.FileInputStream;
22
Nils Diewald14b3aa42015-02-26 22:26:20 +000023import de.ids_mannheim.korap.server.Node;
Nils Diewald884dbcf2015-02-27 17:02:28 +000024import de.ids_mannheim.korap.response.Result;
Nils Diewald0881e242015-02-27 17:31:01 +000025import de.ids_mannheim.korap.response.Response;
Nils Diewaldc383ed02015-02-26 21:35:22 +000026import static de.ids_mannheim.korap.util.KrillString.*;
Nils Diewaldff6f7662014-09-21 15:08:52 +000027
Marc Kupietza61d2ba2015-03-20 16:30:26 +010028import com.fasterxml.jackson.core.JsonProcessingException;
29import com.fasterxml.jackson.databind.JsonNode;
30import com.fasterxml.jackson.databind.ObjectMapper;
Nils Diewaldb9dd4132015-02-16 16:32:41 +000031
32/**
33 * @author diewald
34 */
35// http://harryjoy.com/2012/09/08/simple-rest-client-in-java/
Nils Diewaldd723d812014-09-23 18:50:52 +000036public class TestResource {
Nils Diewaldff6f7662014-09-21 15:08:52 +000037 private HttpServer server;
38 private WebTarget target;
39
Akron98b78542015-08-06 21:43:08 +020040 final ObjectMapper mapper = new ObjectMapper();
41 long t1 = 0, t2 = 0, t3 = 0, t4 = 0;
Nils Diewaldbb33da22015-03-04 16:24:25 +000042
margarethab097bac2015-04-15 11:37:02 +020043
Nils Diewaldff6f7662014-09-21 15:08:52 +000044 @Before
Nils Diewaldb9dd4132015-02-16 16:32:41 +000045 public void setUp () throws Exception {
Nils Diewaldff6f7662014-09-21 15:08:52 +000046 // start the server
Akron98b78542015-08-06 21:43:08 +020047 t1 = System.nanoTime();
Akrona2e9b872015-08-14 19:22:46 +020048 server = Node.startServer(new String[] { "--name", "milena", "--dir",
49 ":memory:", "--port", "9157" });
Nils Diewaldff6f7662014-09-21 15:08:52 +000050 // create the client
51 Client c = ClientBuilder.newClient();
52
Akron98b78542015-08-06 21:43:08 +020053 t2 = System.nanoTime();
54
Nils Diewaldff6f7662014-09-21 15:08:52 +000055 // uncomment the following line if you want to enable
56 // support for JSON in the client (you also have to uncomment
57 // dependency on jersey-media-json module in pom.xml and Main.startServer())
58 // --
Nils Diewaldff6f7662014-09-21 15:08:52 +000059
Nils Diewaldbbd39a52015-02-23 19:56:57 +000060 // c.configuration().enable(com.sun.jersey.api.json.POJOMappingFeature());
61 // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());
Marc Kupietza61d2ba2015-03-20 16:30:26 +010062 // c.register(JacksonFeatures.class);
Nils Diewaldbbd39a52015-02-23 19:56:57 +000063 // c.register(com.fasterxml.jackson.jaxrs.annotation.JacksonFeatures.class);
64
65 /*
66 ClientConfig clientConfig = new DefaultClientConfig();
67 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
68 Client c = Client.create(clientConfig);
Nils Diewaldbb33da22015-03-04 16:24:25 +000069 */
Akrona2e9b872015-08-14 19:22:46 +020070 target = c.target(Node.getBaseURI());
Nils Diewaldff6f7662014-09-21 15:08:52 +000071 };
72
Nils Diewaldbb33da22015-03-04 16:24:25 +000073
Nils Diewaldff6f7662014-09-21 15:08:52 +000074 @After
Nils Diewaldbb33da22015-03-04 16:24:25 +000075 public void tearDown () throws Exception {
Akron98b78542015-08-06 21:43:08 +020076 t3 = System.nanoTime();
Nils Diewaldff6f7662014-09-21 15:08:52 +000077 server.stop();
Marc Kupietza61d2ba2015-03-20 16:30:26 +010078 Node.closeDBPool();
Akron98b78542015-08-06 21:43:08 +020079 t4 = System.nanoTime();
80
Akron08f4ceb2016-08-03 23:53:32 +020081 double startup = (double) (t2 - t1) / 1000000000.0;
82 double action = (double) (t3 - t2) / 1000000000.0;
Akron98b78542015-08-06 21:43:08 +020083 double shutdown = (double) (t4 - t3) / 1000000000.0;
Nils Diewaldff6f7662014-09-21 15:08:52 +000084 };
85
Nils Diewaldbb33da22015-03-04 16:24:25 +000086
Nils Diewaldff6f7662014-09-21 15:08:52 +000087 /**
Nils Diewaldbb33da22015-03-04 16:24:25 +000088 * Test to see that the message "Gimme 5 minutes, please!" is sent
89 * in the response.
Nils Diewaldff6f7662014-09-21 15:08:52 +000090 */
91 @Test
Nils Diewaldb9dd4132015-02-16 16:32:41 +000092 public void testPing () {
Nils Diewaldff6f7662014-09-21 15:08:52 +000093 String responseMsg = target.path("ping").request().get(String.class);
94 assertEquals("Gimme 5 minutes, please!", responseMsg);
95 };
96
margarethab097bac2015-04-15 11:37:02 +020097
Akron74992bc2015-08-05 22:30:57 +020098 // This tests the node info
Marc Kupietza61d2ba2015-03-20 16:30:26 +010099 @Test
100 public void testInfo () throws IOException {
101 String responseMsg = target.path("/").request().get(String.class);
102 JsonNode res = mapper.readTree(responseMsg);
Akrond504f212015-06-20 00:27:54 +0200103 assertEquals("milena", res.at("/meta/node").asText());
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100104 assertEquals(680, res.at("/messages/0/0").asInt());
105 };
Nils Diewaldbb33da22015-03-04 16:24:25 +0000106
Akron09ae3732015-08-13 20:56:20 +0200107
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100108 @Test
Akron98b78542015-08-06 21:43:08 +0200109 public void testIndexing () throws IOException {
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100110 String resp;
111 JsonNode res;
Nils Diewaldc471b182014-11-19 22:51:15 +0000112
Akron09ae3732015-08-13 20:56:20 +0200113 for (String i : new String[] { "00001", "00002", "00003", "00004",
Akron91c60112015-09-24 22:05:40 +0200114 "00005", "00006" }) {
Nils Diewaldff6f7662014-09-21 15:08:52 +0000115
Akron08f4ceb2016-08-03 23:53:32 +0200116
Akron8798be82016-06-23 23:10:25 +0200117
Eliza Margaretha6f989202016-10-14 21:48:29 +0200118 String json = StringfromFile(
119 getClass().getResource("/wiki/" + i + ".json").getFile());
Nils Diewaldb9dd4132015-02-16 16:32:41 +0000120
Nils Diewaldbbd39a52015-02-23 19:56:57 +0000121 Entity jsonE = Entity.json(json);
122
Nils Diewald14b3aa42015-02-26 22:26:20 +0000123 try {
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100124 // Put new documents to the index
125 resp = target.path("/index/" + i).request("application/json")
margarethab097bac2015-04-15 11:37:02 +0200126 .put(jsonE, String.class);
Nils Diewaldff6f7662014-09-21 15:08:52 +0000127
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100128 res = mapper.readTree(resp);
Akrond504f212015-06-20 00:27:54 +0200129 assertEquals("milena", res.at("/meta/node").asText());
Akron75ee2b82016-06-20 21:20:34 +0200130 assertEquals(681, res.at("/messages/0/0").asInt());
Nils Diewald14b3aa42015-02-26 22:26:20 +0000131 }
132 catch (Exception e) {
Nils Diewaldbb33da22015-03-04 16:24:25 +0000133 fail("Server response failed " + e.getMessage()
134 + " (Known issue)");
Nils Diewald14b3aa42015-02-26 22:26:20 +0000135 }
Nils Diewald94870ae2014-12-09 14:35:29 +0000136 };
Nils Diewaldff6f7662014-09-21 15:08:52 +0000137
Eliza Margaretha6f989202016-10-14 21:48:29 +0200138 String json = StringfromFile(
139 getClass().getResource("/wiki/02439.json").getFile());
Akron91c60112015-09-24 22:05:40 +0200140 Entity jsonE = Entity.json(json);
141
142 try {
143 // Put new documents to the index
144 resp = target.path("/index/02439").request("application/json")
Akron42993552016-02-04 13:24:24 +0100145 .put(jsonE, String.class);
Akron91c60112015-09-24 22:05:40 +0200146
147 res = mapper.readTree(resp);
148
149 // Check mirroring
150 assertEquals(2439, res.at("/text/UID").asInt());
151 assertEquals("milena", res.at("/meta/node").asText());
Akron75ee2b82016-06-20 21:20:34 +0200152 assertEquals(681, res.at("/messages/0/0").asInt());
Akron91c60112015-09-24 22:05:40 +0200153 }
154 catch (Exception e) {
Akron42993552016-02-04 13:24:24 +0100155 fail("Server response failed " + e.getMessage() + " (Known issue)");
Akron91c60112015-09-24 22:05:40 +0200156 };
157
Akron98b78542015-08-06 21:43:08 +0200158 // Commit!
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100159 resp = target.path("/index").request("application/json")
160 .post(Entity.text(""), String.class);
161 res = mapper.readTree(resp);
Akrond504f212015-06-20 00:27:54 +0200162 assertEquals("milena", res.at("/meta/node").asText());
Akron75ee2b82016-06-20 21:20:34 +0200163
164 // Staged data committed
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100165 assertEquals(683, res.at("/messages/0/0").asInt());
Akron8798be82016-06-23 23:10:25 +0200166
167
168 // Get document by UID
169 resp = target.path("/index/00005").request().get(String.class);
170 res = mapper.readTree(resp);
171
Akron08f4ceb2016-08-03 23:53:32 +0200172 assertEquals("freizeit-unterhaltung reisen schrott tabellen",
173 res.at("/textClass").asText());
Akron8798be82016-06-23 23:10:25 +0200174 assertEquals("Å (Orte in Norwegen)", res.at("/title").asText());
175 assertEquals("WPD", res.at("/corpusID").asText());
176 assertEquals(5, res.at("/UID").asInt());
177 assertEquals("WPD_AAA.00005", res.at("/ID").asText());
178
179
180 // Get document by UID
181 resp = target.path("/index/5").request().get(String.class);
182 res = mapper.readTree(resp);
183
Akron08f4ceb2016-08-03 23:53:32 +0200184 assertEquals("freizeit-unterhaltung reisen schrott tabellen",
185 res.at("/textClass").asText());
Akron8798be82016-06-23 23:10:25 +0200186 assertEquals("Å (Orte in Norwegen)", res.at("/title").asText());
187 assertEquals("WPD", res.at("/corpusID").asText());
188 assertEquals(5, res.at("/UID").asInt());
189 assertEquals("WPD_AAA.00005", res.at("/ID").asText());
Akrond7d7b1f2016-06-25 00:31:16 +0200190
191 // Get document by UID
192 resp = target.path("/index/17").request().get(String.class);
193 res = mapper.readTree(resp);
194
195 assertEquals(630, res.at("/errors/0/0").asInt());
196 assertTrue(res.at("/UID").isMissingNode());
197
198 // Get corpus statistics
199 resp = target.path("/corpus").request().get(String.class);
200 res = mapper.readTree(resp);
201
202 assertEquals(281, res.at("/stats/sentences").asInt());
203 assertEquals(174, res.at("/stats/paragraphs").asInt());
204 assertEquals(2661, res.at("/stats/tokens").asInt());
205
206 assertEquals(7, res.at("/stats/base~1texts").asInt());
Nils Diewaldff6f7662014-09-21 15:08:52 +0000207 };
Nils Diewaldd723d812014-09-23 18:50:52 +0000208
Akron8798be82016-06-23 23:10:25 +0200209
Akron75ee2b82016-06-20 21:20:34 +0200210 /*
211 @Test
212 public void testRemoving () throws IOException {
Akrond7d7b1f2016-06-25 00:31:16 +0200213 String resp;
214 JsonNode res;
Eliza Margaretha6f989202016-10-14 21:48:29 +0200215
Akrond7d7b1f2016-06-25 00:31:16 +0200216 String json = StringfromFile(getClass().getResource("/wiki/02439.json")
217 .getFile());
218 Entity jsonE = Entity.json(json);
Eliza Margaretha6f989202016-10-14 21:48:29 +0200219
Akrond7d7b1f2016-06-25 00:31:16 +0200220 try {
221 // Put new documents to the index
222 resp = target.path("/index/02439").request("application/json")
223 .put(jsonE, String.class);
Eliza Margaretha6f989202016-10-14 21:48:29 +0200224
Akrond7d7b1f2016-06-25 00:31:16 +0200225 res = mapper.readTree(resp);
Eliza Margaretha6f989202016-10-14 21:48:29 +0200226
Akrond7d7b1f2016-06-25 00:31:16 +0200227 // Check mirroring
228 assertEquals(2439, res.at("/text/UID").asInt());
229 assertEquals("milena", res.at("/meta/node").asText());
230 assertEquals(681, res.at("/messages/0/0").asInt());
231 }
232 catch (Exception e) {
233 fail("Server response failed " + e.getMessage() + " (Known issue)");
234 };
Eliza Margaretha6f989202016-10-14 21:48:29 +0200235
Akrond7d7b1f2016-06-25 00:31:16 +0200236 // Commit!
237 resp = target.path("/index").request("application/json")
238 .post(Entity.text(""), String.class);
239 res = mapper.readTree(resp);
240 assertEquals("milena", res.at("/meta/node").asText());
Eliza Margaretha6f989202016-10-14 21:48:29 +0200241
Akrond7d7b1f2016-06-25 00:31:16 +0200242 // Staged data committed
243 assertEquals(683, res.at("/messages/0/0").asInt());
Akron75ee2b82016-06-20 21:20:34 +0200244 };
245 */
Akron42993552016-02-04 13:24:24 +0100246
Akrond7d7b1f2016-06-25 00:31:16 +0200247
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100248 @Test
Nils Diewaldbb33da22015-03-04 16:24:25 +0000249 public void testCollection () throws IOException {
Nils Diewaldd723d812014-09-23 18:50:52 +0000250
Akron98b78542015-08-06 21:43:08 +0200251 // mate/l:sein
Akron67d2ff02018-06-19 10:51:16 +0200252 String json = getJsonString(getClass()
Eliza Margaretha6f989202016-10-14 21:48:29 +0200253 .getResource("/queries/bsp-uid-example.jsonld").getFile());
Nils Diewaldd723d812014-09-23 18:50:52 +0000254
Nils Diewald14b3aa42015-02-26 22:26:20 +0000255 try {
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100256 String resp = target.path("/").queryParam("uid", "1")
margarethab097bac2015-04-15 11:37:02 +0200257 .queryParam("uid", "4").request("application/json")
258 .post(Entity.json(json), String.class);
Marc Kupietza61d2ba2015-03-20 16:30:26 +0100259 JsonNode res = mapper.readTree(resp);
Akrond504f212015-06-20 00:27:54 +0200260 assertEquals(2, res.at("/meta/totalResults").asInt());
Nils Diewald14b3aa42015-02-26 22:26:20 +0000261 }
262 catch (Exception e) {
Eliza Margaretha6f989202016-10-14 21:48:29 +0200263 fail("Server response failed: " + e.getMessage()
264 + " (Known issue)");
Nils Diewald14b3aa42015-02-26 22:26:20 +0000265 };
266
Nils Diewaldd723d812014-09-23 18:50:52 +0000267 };
Nils Diewaldff6f7662014-09-21 15:08:52 +0000268};