blob: 73bd736d5489fce54e0514fc88c19743c4f02ea7 [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
17import de.ids_mannheim.korap.plkexport.JsonExporter;
18
19public 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 };
Akron14c23462020-11-14 13:51:40 +010042
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 };
Akron876017d2020-11-17 09:19:24 +010054
55 @Test
56 public void testAttributes () throws IOException {
57 JsonExporter json = new JsonExporter();
Akron74122712020-11-17 09:41:21 +010058 json.setFileName("Beispiel");
59 assertEquals(json.getFileName(),"Beispiel");
Akron876017d2020-11-17 09:19:24 +010060 assertEquals(json.getMimeType(),"application/json");
61 assertEquals(json.getSuffix(),"json");
62 };
Akron14c23462020-11-14 13:51:40 +010063
Akron876017d2020-11-17 09:19:24 +010064
Akron14c23462020-11-14 13:51:40 +010065 public static String slurp (File file) throws IOException {
66 BufferedReader br = new BufferedReader(new FileReader(file));
67 String string;
68
69 StringBuilder contentBuilder = new StringBuilder();
70
71 while ((string = br.readLine()) != null)
72 contentBuilder.append(string);
73
74 return contentBuilder.toString();
75 };
76
Akron8154dbe2020-11-13 17:52:53 +010077};