blob: 4bea7db0c22ae4625d4d2aeef849faa7afcf1708 [file] [log] [blame]
Akron8154dbe2020-11-13 17:52:53 +01001package de.ids_mannheim.korap.plkexport;
2
3import java.io.IOException;
Akron14c23462020-11-14 13:51:40 +01004import java.io.File;
5import java.io.BufferedReader;
6import java.io.FileReader;
Akron8154dbe2020-11-13 17:52:53 +01007
8import static org.junit.Assert.assertEquals;
9import static org.junit.Assert.assertNull;
10import static org.junit.Assert.assertTrue;
11import static org.junit.Assert.assertFalse;
12import static org.junit.Assert.fail;
13import org.junit.Test;
14
15import javax.ws.rs.core.Response;
16
Akrona84260b2020-11-20 14:29:38 +010017public class JsonExporterTest {
Akron8154dbe2020-11-13 17:52:53 +010018
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 };
Akron14c23462020-11-14 13:51:40 +010040
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 };
Akron876017d2020-11-17 09:19:24 +010052
53 @Test
54 public void testAttributes () throws IOException {
55 JsonExporter json = new JsonExporter();
Akron74122712020-11-17 09:41:21 +010056 json.setFileName("Beispiel");
57 assertEquals(json.getFileName(),"Beispiel");
Akron876017d2020-11-17 09:19:24 +010058 assertEquals(json.getMimeType(),"application/json");
59 assertEquals(json.getSuffix(),"json");
60 };
Akron14c23462020-11-14 13:51:40 +010061
Akron876017d2020-11-17 09:19:24 +010062
Akron14c23462020-11-14 13:51:40 +010063 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
Akron8154dbe2020-11-13 17:52:53 +010075};