blob: 8ab7acb7a02d7ce8c1e7a1a45da30709bf7a91d6 [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 };
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
Akron8154dbe2020-11-13 17:52:53 +010067};