| Akron | 8154dbe | 2020-11-13 17:52:53 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.plkexport; |
| 2 | |
| 3 | import java.io.IOException; |
| Akron | 14c2346 | 2020-11-14 13:51:40 +0100 | [diff] [blame^] | 4 | import java.io.File; |
| 5 | import java.io.BufferedReader; |
| 6 | import java.io.FileReader; |
| Akron | 8154dbe | 2020-11-13 17:52:53 +0100 | [diff] [blame] | 7 | |
| 8 | import static org.junit.Assert.assertEquals; |
| 9 | import static org.junit.Assert.assertNull; |
| 10 | import static org.junit.Assert.assertTrue; |
| 11 | import static org.junit.Assert.assertFalse; |
| 12 | import static org.junit.Assert.fail; |
| 13 | import org.junit.Test; |
| 14 | |
| 15 | import javax.ws.rs.core.Response; |
| 16 | |
| 17 | import de.ids_mannheim.korap.plkexport.JsonExporter; |
| 18 | |
| 19 | public class JsonExportTest { |
| 20 | |
| 21 | @Test |
| 22 | public void testInit () throws IOException { |
| 23 | JsonExporter json = new JsonExporter(); |
| 24 | json.init("{\"query\":\"cool\"}"); |
| 25 | |
| 26 | Response resp = json.serve().build(); |
| 27 | String x = (String) resp.getEntity(); |
| 28 | resp.close(); |
| 29 | assertEquals(x,"{\"query\":\"cool\",\"matches\":[]}"); |
| 30 | }; |
| 31 | |
| 32 | @Test |
| 33 | public void testInitFull () throws IOException { |
| 34 | JsonExporter json = new JsonExporter(); |
| 35 | json.init("{\"meta\":\"ja\",\"collection\":\"hm\",\"query\":\"cool\",\"matches\":[\"first\",\"second\"]}"); |
| 36 | |
| 37 | Response resp = json.serve().build(); |
| 38 | String x = (String) resp.getEntity(); |
| 39 | resp.close(); |
| 40 | assertEquals(x,"{\"query\":\"cool\",\"meta\":\"ja\",\"collection\":\"hm\",\"matches\":[\"first\",\"second\"]}"); |
| 41 | }; |
| Akron | 14c2346 | 2020-11-14 13:51:40 +0100 | [diff] [blame^] | 42 | |
| 43 | @Test |
| 44 | public void testPaging () throws IOException { |
| 45 | JsonExporter json = new JsonExporter(); |
| 46 | json.init("{\"meta\":\"ja\",\"collection\":\"hm\",\"query\":\"cool\",\"matches\":[\"first\",\"second\"]}"); |
| 47 | json.appendMatches("{\"meta\":\"ja2\",\"collection\":\"hm2\",\"query\":\"cool2\",\"matches\":[\"third\",\"fourth\"]}"); |
| 48 | |
| 49 | Response resp = json.serve().build(); |
| 50 | File x = (File) resp.getEntity(); |
| 51 | resp.close(); |
| 52 | assertEquals(slurp(x),"{\"query\":\"cool\",\"meta\":\"ja\",\"collection\":\"hm\",\"matches\":[\"first\",\"second\",\"third\",\"fourth\"]}"); |
| 53 | }; |
| 54 | |
| 55 | public static String slurp (File file) throws IOException { |
| 56 | BufferedReader br = new BufferedReader(new FileReader(file)); |
| 57 | String string; |
| 58 | |
| 59 | StringBuilder contentBuilder = new StringBuilder(); |
| 60 | |
| 61 | while ((string = br.readLine()) != null) |
| 62 | contentBuilder.append(string); |
| 63 | |
| 64 | return contentBuilder.toString(); |
| 65 | }; |
| 66 | |
| Akron | 8154dbe | 2020-11-13 17:52:53 +0100 | [diff] [blame] | 67 | }; |