blob: b1062d88d36a7f59837d909b83b6a0e08c67eaf5 [file] [log] [blame]
Michael Hanlababa392013-12-05 21:57:37 +00001import com.fasterxml.jackson.core.JsonGenerationException;
Michael Hanl9ca5edd2013-12-06 05:13:24 +00002import com.fasterxml.jackson.core.JsonProcessingException;
Michael Hanlababa392013-12-05 21:57:37 +00003import com.fasterxml.jackson.databind.JsonMappingException;
4import de.ids_mannheim.korap.query.serialize.JsonGenerator;
Michael Hanl55191d92013-12-05 11:53:10 +00005import de.ids_mannheim.korap.query.serialize.MetaCollectionSerializer;
6import de.ids_mannheim.korap.query.serialize.MetaQuerySerializer;
Michael Hanl9ca5edd2013-12-06 05:13:24 +00007import de.ids_mannheim.korap.query.serialize.Serializer;
Michael Hanl296a0312013-12-05 17:27:51 +00008import org.joda.time.DateTime;
Michael Hanl55191d92013-12-05 11:53:10 +00009import org.junit.Test;
10import org.junit.runner.RunWith;
11import org.junit.runners.JUnit4;
12
13import java.io.IOException;
14import java.util.HashMap;
Michael Hanl296a0312013-12-05 17:27:51 +000015import java.util.LinkedHashMap;
Michael Hanl9ca5edd2013-12-06 05:13:24 +000016import java.util.List;
Michael Hanl55191d92013-12-05 11:53:10 +000017import java.util.Map;
18
19/**
20 * @author hanl
21 * @date 04/12/2013
22 */
23
24@RunWith(JUnit4.class)
25public class MetaQuerySerializationTest {
26
Michael Hanl296a0312013-12-05 17:27:51 +000027 private MetaQuerySerializer querySerializer;
28 private MetaCollectionSerializer collSerializer;
Michael Hanl9ca5edd2013-12-06 05:13:24 +000029 private Serializer ser;
Michael Hanl55191d92013-12-05 11:53:10 +000030
31 public MetaQuerySerializationTest() {
Michael Hanl296a0312013-12-05 17:27:51 +000032 querySerializer = new MetaQuerySerializer();
33 collSerializer = new MetaCollectionSerializer();
Michael Hanl9ca5edd2013-12-06 05:13:24 +000034 ser = new Serializer();
Michael Hanl55191d92013-12-05 11:53:10 +000035 }
36
37 @Test
38 public void test() throws IOException {
39 Map<String, String> j = new HashMap();
40 j.put("author", "Goethe");
Michael Hanl296a0312013-12-05 17:27:51 +000041 j.put("pubPlace", "Erfurt");
Michael Hanl55191d92013-12-05 11:53:10 +000042 j.put("textClass", "wissenschaft");
Michael Hanl296a0312013-12-05 17:27:51 +000043 String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER);
Michael Hanl55191d92013-12-05 11:53:10 +000044// System.out.println("value reference " + s);
45 }
46
47 @Test
48 public void testSingle() throws IOException {
49 Map<String, String> j = new HashMap();
50 j.put("textClass", "wissenschaft");
Michael Hanl296a0312013-12-05 17:27:51 +000051 String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER);
Michael Hanlababa392013-12-05 21:57:37 +000052 System.out.println("------ TEXT SINGLE " + s);
Michael Hanl55191d92013-12-05 11:53:10 +000053 }
54
55 @Test
56 public void testResourceMeta() throws IOException {
Michael Hanl296a0312013-12-05 17:27:51 +000057 String s = collSerializer.serialize("25");
Michael Hanlababa392013-12-05 21:57:37 +000058 System.out.println(" --- RESULT JSON " + s);
Michael Hanl296a0312013-12-05 17:27:51 +000059 }
Michael Hanl55191d92013-12-05 11:53:10 +000060
Michael Hanl296a0312013-12-05 17:27:51 +000061 @Test
62 public void testDates() throws IOException {
63 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000064 queries.put("pubDate", String.valueOf(new DateTime().getMillis()) + "~"
65 + String.valueOf(new DateTime().getMillis() + 2));
Michael Hanl296a0312013-12-05 17:27:51 +000066 queries.put("author", "Goethe");
67 String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
Michael Hanlababa392013-12-05 21:57:37 +000068 System.out.println("value until/since : " + f);
Michael Hanl55191d92013-12-05 11:53:10 +000069 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000070
71 @Test
72 public void testUntil() throws IOException {
73 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000074 queries.put("pubDate", ">" + String.valueOf(new DateTime().getMillis()));
Michael Hanl08b93ed2013-12-05 18:04:45 +000075 queries.put("author", "Hesse");
76 String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
77 System.out.println("value until : " + f);
78 }
79
80 @Test
81 public void testSince() throws IOException {
82 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000083 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
Michael Hanl08b93ed2013-12-05 18:04:45 +000084 queries.put("author", "Kafka");
85 String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
86 System.out.println("value since : " + f);
87 }
88
Michael Hanlababa392013-12-05 21:57:37 +000089 //@Test
90 public void testGenerator() {
91 /*
92 * just for testing...
93 */
94 JsonGenerator jg = new JsonGenerator();
95 int i = 0;
96 String[] queries;
97 queries = new String[]{
98 "shrink({[base=foo]})",
99 "shrink({[base=foo]}[orth=bar])",
100 "shrink(1:[base=Der]{1:[base=Mann]})",
101 };
102
103 for (String q : queries) {
104 i++;
105 try {
106 System.out.println(q);
107 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
108 System.out.println();
109 } catch (NullPointerException npe) {
110 npe.printStackTrace();
111 System.out.println("null\n");
112 } catch (JsonGenerationException e) {
113 e.printStackTrace();
114 } catch (JsonMappingException e) {
115 e.printStackTrace();
116 } catch (IOException e) {
117 e.printStackTrace();
118 }
119 }
120 }
121
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000122 @Test
123 public void testLists() {
124 Map<String, String> queries = new LinkedHashMap<>();
125 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
126 queries.put("author", "Kafka");
127 List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER);
128
129 queries.clear();
130 queries.put("author", "Hesse");
131
132 List f = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.EXTEND);
133 s.addAll(f);
134 System.out.println("--- ALL " + s);
135 }
136
137 @Test
138 public void testJSONArray() throws JsonProcessingException {
139 Map<String, String> queries = new LinkedHashMap<>();
140 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
141 queries.put("author", "Kafka");
142 List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER);
143 System.out.println("array repres " + ser.serializeMeta(s));
144 }
145
Michael Hanl55191d92013-12-05 11:53:10 +0000146}