blob: 08ae610423b31f2854e65220b78f7863cd0d8ab4 [file] [log] [blame]
Nils Diewaldff6f7662014-09-21 15:08:52 +00001package de.ids_mannheim.korap.server;
2
3/*
4 http://harryjoy.com/2012/09/08/simple-rest-client-in-java/
5*/
6import java.io.*;
Nils Diewaldd723d812014-09-23 18:50:52 +00007import java.util.ArrayList;
8import java.util.List;
9
Nils Diewaldff6f7662014-09-21 15:08:52 +000010
11import javax.ws.rs.client.Client;
12import javax.ws.rs.client.ClientBuilder;
13import javax.ws.rs.client.WebTarget;
14import javax.ws.rs.client.Entity;
15
16import org.glassfish.grizzly.http.server.HttpServer;
17
Nils Diewaldd723d812014-09-23 18:50:52 +000018import static org.junit.Assert.*;
Nils Diewaldff6f7662014-09-21 15:08:52 +000019import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Nils Diewaldff6f7662014-09-21 15:08:52 +000022
23import java.io.FileInputStream;
24
25import de.ids_mannheim.korap.KorapNode;
Nils Diewaldd723d812014-09-23 18:50:52 +000026import de.ids_mannheim.korap.KorapResult;
Nils Diewaldff6f7662014-09-21 15:08:52 +000027import de.ids_mannheim.korap.server.KorapResponse;
28import static de.ids_mannheim.korap.util.KorapString.*;
29
Nils Diewaldd723d812014-09-23 18:50:52 +000030public class TestResource {
Nils Diewaldff6f7662014-09-21 15:08:52 +000031
32 private HttpServer server;
33 private WebTarget target;
34
35 @Before
36 public void setUp() throws Exception {
37 // start the server
38 server = KorapNode.startServer("milena", (String) null);
39 // create the client
40 Client c = ClientBuilder.newClient();
41
42 // uncomment the following line if you want to enable
43 // support for JSON in the client (you also have to uncomment
44 // dependency on jersey-media-json module in pom.xml and Main.startServer())
45 // --
46 // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());
47
48 target = c.target(KorapNode.BASE_URI);
49 };
50
51 @After
52 public void tearDown() throws Exception {
53 server.stop();
54 };
55
56 /**
57 * Test to see that the message "Gimme 5 minutes, please!" is sent in the response.
58 */
59 @Test
60 public void testPing() {
61 String responseMsg = target.path("ping").request().get(String.class);
62 assertEquals("Gimme 5 minutes, please!", responseMsg);
63 };
64
65 @Test
66 public void testResource() throws IOException {
67 int unstaged = 1;
68 for (String i : new String[] {"00001",
69 "00002",
70 "00003",
71 "00004",
72 "00005",
73 "00006",
74 "02439"
75 }) {
76
77 String json = StringfromFile(getClass().getResource("/wiki/" + i + ".json").getFile());
Nils Diewald7cbcfe92014-09-22 22:01:51 +000078 KorapResponse kresp = target.path("/index/" + i).
Nils Diewaldff6f7662014-09-21 15:08:52 +000079 request("application/json").
80 put(Entity.json(json), KorapResponse.class);
81
82 assertEquals(kresp.getNode(), "milena");
Nils Diewaldd723d812014-09-23 18:50:52 +000083 assertEquals(kresp.getErr(), 0);
Nils Diewaldff6f7662014-09-21 15:08:52 +000084 assertEquals(kresp.getUnstaged(), unstaged++);
Nils Diewaldff6f7662014-09-21 15:08:52 +000085 };
86
Nils Diewald7cbcfe92014-09-22 22:01:51 +000087 KorapResponse kresp = target.path("/index").
Nils Diewaldff6f7662014-09-21 15:08:52 +000088 request("application/json").
89 post(Entity.text(""), KorapResponse.class);
90 assertEquals(kresp.getNode(), "milena");
91 assertEquals(kresp.getMsg(), "Unstaged data was committed");
92 };
Nils Diewaldd723d812014-09-23 18:50:52 +000093
94 @Test
95 public void testCollection() throws IOException {
96
97 String json = getString(
98 getClass().getResource("/queries/bsp-uid-example.jsonld").getFile()
99 );
100
101 KorapResponse kresp
102 = target.path("/").
103 queryParam("uid", "1").
104 queryParam("uid", "4").
105 request("application/json").
106 post(Entity.json(json), KorapResponse.class);
107
108 assertEquals(2, kresp.getTotalResults());
109 assertEquals(0, kresp.getErr());
110 };
111
112 public static String getString (String path) {
113 StringBuilder contentBuilder = new StringBuilder();
114 try {
115 BufferedReader in = new BufferedReader(new FileReader(path));
116 String str;
117 while ((str = in.readLine()) != null) {
118 contentBuilder.append(str);
119 };
120 in.close();
121 } catch (IOException e) {
122 fail(e.getMessage());
123 }
124 return contentBuilder.toString();
125 };
126
Nils Diewaldff6f7662014-09-21 15:08:52 +0000127};