| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.server; |
| 2 | |
| 3 | /* |
| 4 | http://harryjoy.com/2012/09/08/simple-rest-client-in-java/ |
| 5 | */ |
| 6 | import java.io.*; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 7 | import java.util.ArrayList; |
| 8 | import java.util.List; |
| 9 | |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 10 | |
| 11 | import javax.ws.rs.client.Client; |
| 12 | import javax.ws.rs.client.ClientBuilder; |
| 13 | import javax.ws.rs.client.WebTarget; |
| 14 | import javax.ws.rs.client.Entity; |
| 15 | |
| 16 | import org.glassfish.grizzly.http.server.HttpServer; |
| 17 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 18 | import static org.junit.Assert.*; |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 22 | |
| 23 | import java.io.FileInputStream; |
| 24 | |
| 25 | import de.ids_mannheim.korap.KorapNode; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 26 | import de.ids_mannheim.korap.KorapResult; |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 27 | import de.ids_mannheim.korap.server.KorapResponse; |
| 28 | import static de.ids_mannheim.korap.util.KorapString.*; |
| 29 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 30 | public class TestResource { |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 31 | |
| 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 Diewald | 7cbcfe9 | 2014-09-22 22:01:51 +0000 | [diff] [blame] | 78 | KorapResponse kresp = target.path("/index/" + i). |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 79 | request("application/json"). |
| 80 | put(Entity.json(json), KorapResponse.class); |
| 81 | |
| 82 | assertEquals(kresp.getNode(), "milena"); |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 83 | assertEquals(kresp.getErr(), 0); |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 84 | assertEquals(kresp.getUnstaged(), unstaged++); |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| Nils Diewald | 7cbcfe9 | 2014-09-22 22:01:51 +0000 | [diff] [blame] | 87 | KorapResponse kresp = target.path("/index"). |
| Nils Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 88 | request("application/json"). |
| 89 | post(Entity.text(""), KorapResponse.class); |
| 90 | assertEquals(kresp.getNode(), "milena"); |
| 91 | assertEquals(kresp.getMsg(), "Unstaged data was committed"); |
| 92 | }; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 93 | |
| 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 Diewald | ff6f766 | 2014-09-21 15:08:52 +0000 | [diff] [blame] | 127 | }; |