| 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 | |
| Akron | a84260b | 2020-11-20 14:29:38 +0100 | [diff] [blame^] | 17 | public class JsonExporterTest { |
| Akron | 8154dbe | 2020-11-13 17:52:53 +0100 | [diff] [blame] | 18 | |
| 19 | @Test |
| 20 | public void testInit () throws IOException { |
| 21 | JsonExporter json = new JsonExporter(); |
| 22 | json.init("{\"query\":\"cool\"}"); |
| 23 | |
| 24 | Response resp = json.serve().build(); |
| 25 | String x = (String) resp.getEntity(); |
| 26 | resp.close(); |
| 27 | assertEquals(x,"{\"query\":\"cool\",\"matches\":[]}"); |
| 28 | }; |
| 29 | |
| 30 | @Test |
| 31 | public void testInitFull () throws IOException { |
| 32 | JsonExporter json = new JsonExporter(); |
| 33 | json.init("{\"meta\":\"ja\",\"collection\":\"hm\",\"query\":\"cool\",\"matches\":[\"first\",\"second\"]}"); |
| 34 | |
| 35 | Response resp = json.serve().build(); |
| 36 | String x = (String) resp.getEntity(); |
| 37 | resp.close(); |
| 38 | assertEquals(x,"{\"query\":\"cool\",\"meta\":\"ja\",\"collection\":\"hm\",\"matches\":[\"first\",\"second\"]}"); |
| 39 | }; |
| Akron | 14c2346 | 2020-11-14 13:51:40 +0100 | [diff] [blame] | 40 | |
| 41 | @Test |
| 42 | public void testPaging () throws IOException { |
| 43 | JsonExporter json = new JsonExporter(); |
| 44 | json.init("{\"meta\":\"ja\",\"collection\":\"hm\",\"query\":\"cool\",\"matches\":[\"first\",\"second\"]}"); |
| 45 | json.appendMatches("{\"meta\":\"ja2\",\"collection\":\"hm2\",\"query\":\"cool2\",\"matches\":[\"third\",\"fourth\"]}"); |
| 46 | |
| 47 | Response resp = json.serve().build(); |
| 48 | File x = (File) resp.getEntity(); |
| 49 | resp.close(); |
| 50 | assertEquals(slurp(x),"{\"query\":\"cool\",\"meta\":\"ja\",\"collection\":\"hm\",\"matches\":[\"first\",\"second\",\"third\",\"fourth\"]}"); |
| 51 | }; |
| Akron | 876017d | 2020-11-17 09:19:24 +0100 | [diff] [blame] | 52 | |
| 53 | @Test |
| 54 | public void testAttributes () throws IOException { |
| 55 | JsonExporter json = new JsonExporter(); |
| Akron | 7412271 | 2020-11-17 09:41:21 +0100 | [diff] [blame] | 56 | json.setFileName("Beispiel"); |
| 57 | assertEquals(json.getFileName(),"Beispiel"); |
| Akron | 876017d | 2020-11-17 09:19:24 +0100 | [diff] [blame] | 58 | assertEquals(json.getMimeType(),"application/json"); |
| 59 | assertEquals(json.getSuffix(),"json"); |
| 60 | }; |
| Akron | 14c2346 | 2020-11-14 13:51:40 +0100 | [diff] [blame] | 61 | |
| Akron | 876017d | 2020-11-17 09:19:24 +0100 | [diff] [blame] | 62 | |
| Akron | 14c2346 | 2020-11-14 13:51:40 +0100 | [diff] [blame] | 63 | public static String slurp (File file) throws IOException { |
| 64 | BufferedReader br = new BufferedReader(new FileReader(file)); |
| 65 | String string; |
| 66 | |
| 67 | StringBuilder contentBuilder = new StringBuilder(); |
| 68 | |
| 69 | while ((string = br.readLine()) != null) |
| 70 | contentBuilder.append(string); |
| 71 | |
| 72 | return contentBuilder.toString(); |
| 73 | }; |
| 74 | |
| Akron | 8154dbe | 2020-11-13 17:52:53 +0100 | [diff] [blame] | 75 | }; |