blob: b32b8f6f3d9940da92bb850cad855e6d1f44b97d [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;
Michael Hanl53b0fd02013-12-06 21:07:52 +00004import de.ids_mannheim.korap.query.serialize.*;
Michael Hanl296a0312013-12-05 17:27:51 +00005import org.joda.time.DateTime;
Michael Hanl55191d92013-12-05 11:53:10 +00006import org.junit.Test;
7import org.junit.runner.RunWith;
8import org.junit.runners.JUnit4;
9
10import java.io.IOException;
Michael Hanl560b4cd2013-12-06 14:26:44 +000011import java.util.*;
Michael Hanl55191d92013-12-05 11:53:10 +000012
13/**
14 * @author hanl
15 * @date 04/12/2013
16 */
17
18@RunWith(JUnit4.class)
19public class MetaQuerySerializationTest {
20
Michael Hanl296a0312013-12-05 17:27:51 +000021 private MetaQuerySerializer querySerializer;
22 private MetaCollectionSerializer collSerializer;
Michael Hanl9ca5edd2013-12-06 05:13:24 +000023 private Serializer ser;
Michael Hanl55191d92013-12-05 11:53:10 +000024
25 public MetaQuerySerializationTest() {
Michael Hanl296a0312013-12-05 17:27:51 +000026 querySerializer = new MetaQuerySerializer();
27 collSerializer = new MetaCollectionSerializer();
Michael Hanl9ca5edd2013-12-06 05:13:24 +000028 ser = new Serializer();
Michael Hanl55191d92013-12-05 11:53:10 +000029 }
30
31 @Test
32 public void test() throws IOException {
33 Map<String, String> j = new HashMap();
34 j.put("author", "Goethe");
Michael Hanl296a0312013-12-05 17:27:51 +000035 j.put("pubPlace", "Erfurt");
Michael Hanl55191d92013-12-05 11:53:10 +000036 j.put("textClass", "wissenschaft");
Michael Hanl296a0312013-12-05 17:27:51 +000037 String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER);
Michael Hanl55191d92013-12-05 11:53:10 +000038// System.out.println("value reference " + s);
39 }
40
41 @Test
42 public void testSingle() throws IOException {
43 Map<String, String> j = new HashMap();
44 j.put("textClass", "wissenschaft");
Michael Hanl296a0312013-12-05 17:27:51 +000045 String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER);
Michael Hanlababa392013-12-05 21:57:37 +000046 System.out.println("------ TEXT SINGLE " + s);
Michael Hanl560b4cd2013-12-06 14:26:44 +000047 System.out.println();
Michael Hanl296a0312013-12-05 17:27:51 +000048 }
Michael Hanl55191d92013-12-05 11:53:10 +000049
Michael Hanl296a0312013-12-05 17:27:51 +000050 @Test
51 public void testDates() throws IOException {
52 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000053 queries.put("pubDate", String.valueOf(new DateTime().getMillis()) + "~"
54 + String.valueOf(new DateTime().getMillis() + 2));
Michael Hanl296a0312013-12-05 17:27:51 +000055 queries.put("author", "Goethe");
56 String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
Michael Hanlababa392013-12-05 21:57:37 +000057 System.out.println("value until/since : " + f);
Michael Hanl560b4cd2013-12-06 14:26:44 +000058 System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000059 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000060
61 @Test
62 public void testUntil() 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()));
Michael Hanl08b93ed2013-12-05 18:04:45 +000065 queries.put("author", "Hesse");
66 String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
67 System.out.println("value until : " + f);
Michael Hanl560b4cd2013-12-06 14:26:44 +000068 System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000069 }
70
71 @Test
72 public void testSince() 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", "Kafka");
76 String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
77 System.out.println("value since : " + f);
Michael Hanl560b4cd2013-12-06 14:26:44 +000078 System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000079 }
80
Michael Hanl53b0fd02013-12-06 21:07:52 +000081 @Test
Michael Hanlababa392013-12-05 21:57:37 +000082 public void testGenerator() {
83 /*
84 * just for testing...
85 */
86 JsonGenerator jg = new JsonGenerator();
87 int i = 0;
88 String[] queries;
89 queries = new String[]{
90 "shrink({[base=foo]})",
91 "shrink({[base=foo]}[orth=bar])",
92 "shrink(1:[base=Der]{1:[base=Mann]})",
93 };
94
95 for (String q : queries) {
96 i++;
97 try {
98 System.out.println(q);
99 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
100 System.out.println();
101 } catch (NullPointerException npe) {
102 npe.printStackTrace();
103 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +0000104 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000105 } catch (JsonGenerationException e) {
106 e.printStackTrace();
107 } catch (JsonMappingException e) {
108 e.printStackTrace();
109 } catch (IOException e) {
110 e.printStackTrace();
111 }
112 }
113 }
114
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000115 @Test
116 public void testLists() {
117 Map<String, String> queries = new LinkedHashMap<>();
118 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
119 queries.put("author", "Kafka");
120 List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER);
121
122 queries.clear();
123 queries.put("author", "Hesse");
124
125 List f = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.EXTEND);
126 s.addAll(f);
127 System.out.println("--- ALL " + s);
Michael Hanl560b4cd2013-12-06 14:26:44 +0000128 System.out.println();
129
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000130 }
131
132 @Test
133 public void testJSONArray() throws JsonProcessingException {
134 Map<String, String> queries = new LinkedHashMap<>();
135 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
136 queries.put("author", "Kafka");
137 List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER);
Michael Hanl560b4cd2013-12-06 14:26:44 +0000138 System.out.println("array repres " + ser.serializeToMeta(s));
139 System.out.println();
140 }
141
142 @Test
143 public void testCollections() throws IOException {
144 Map<String, String> query = new LinkedHashMap<>();
145 Serializer ser = new Serializer();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000146 query.put("corpusID", "A00");
147 List<Map> l = ser.serializeQueries(query, MetaQuerySerializer.TYPE.FILTER);
148 query.clear();
149 query.put("corpusID", "A01");
150 List<Map> u = ser.serializeQueries(query, MetaQuerySerializer.TYPE.EXTEND);
151 l.addAll(u);
152 String val = ser.stringify(l);
153 System.out.println("results " + val);
Michael Hanl560b4cd2013-12-06 14:26:44 +0000154 System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000155
Michael Hanl53b0fd02013-12-06 21:07:52 +0000156
157 String meta = ser.queryToMeta(val);
158
159 System.out.println("meta query " + meta);
160// List<String> list = new ArrayList<>();
161// list.add(val);
162// List s = collSerializer.serializeResource(list);
163 System.out.println("printable list ");
164 System.out.println();
165
166 }
167
168 @Test
169 public void testResources() throws IOException {
170 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
171 List s = new ArrayList();
172 s.add(meta);
173 List fin = ser.serializeResources(s);
174
175 String string = ser.serializeToMeta(fin);
176 System.out.println("meta string " + string);
177
178 }
179
180 @Test
181 public void testA00() throws IOException {
182 Map<String, String> query = new LinkedHashMap<>();
183 Serializer ser = new Serializer();
184 query.put("corpusID", "A00");
185 List<Map> l = ser.serializeQueries(query, MetaQuerySerializer.TYPE.FILTER);
186 query.clear();
187 query.put("corpusID", "A01");
188 List<Map> u = ser.serializeQueries(query, MetaQuerySerializer.TYPE.EXTEND);
189 l.addAll(u);
190 String val = ser.stringify(l);
191 List s = new ArrayList();
192 s.add(val);
193
194 List fin = ser.serializeResources(s);
195 System.out.println("A00 meta: " + fin);
196
197 }
198
199
200 @Test
201 public void testnewMetaQuery() throws IOException {
202 Map<String, String> qu = new LinkedHashMap<>();
203 Serializer ser = new Serializer();
204 qu.put("corpusID", "A00");
205 List<Map> l = ser.serializeQueries(qu, MetaQuerySerializer.TYPE.FILTER);
206 qu.clear();
207 qu.put("corpusID", "A01");
208 List<Map> u = ser.serializeQueries(qu, MetaQuerySerializer.TYPE.EXTEND);
209 l.addAll(u);
210 String val = ser.stringify(l);
211 List s = new ArrayList();
212 s.add(val);
213
214 List fin = ser.serializeResources(s);
215
216
217 System.out.println("======= METAQUERY TESTING BEGIN =======");
218
219 MetaQuery query = new MetaQuery();
220 query.addMetaFilter("textClass", "wissenschaft");
221 query.addResources(s);
222 System.out.println("query "+ query.toMeta());
223 System.out.println("string "+ query.stringify());
224 System.out.println("======= METAQUERY TESTING END =======");
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000225 }
226
Michael Hanl55191d92013-12-05 11:53:10 +0000227}