blob: bd4020482cf71f4319e21f79b22e06c98c00e6f0 [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;
Michael Hanl560b4cd2013-12-06 14:26:44 +000014import java.util.*;
Michael Hanl55191d92013-12-05 11:53:10 +000015
16/**
17 * @author hanl
18 * @date 04/12/2013
19 */
20
21@RunWith(JUnit4.class)
22public class MetaQuerySerializationTest {
23
Michael Hanl296a0312013-12-05 17:27:51 +000024 private MetaQuerySerializer querySerializer;
25 private MetaCollectionSerializer collSerializer;
Michael Hanl9ca5edd2013-12-06 05:13:24 +000026 private Serializer ser;
Michael Hanl55191d92013-12-05 11:53:10 +000027
28 public MetaQuerySerializationTest() {
Michael Hanl296a0312013-12-05 17:27:51 +000029 querySerializer = new MetaQuerySerializer();
30 collSerializer = new MetaCollectionSerializer();
Michael Hanl9ca5edd2013-12-06 05:13:24 +000031 ser = new Serializer();
Michael Hanl55191d92013-12-05 11:53:10 +000032 }
33
34 @Test
35 public void test() throws IOException {
36 Map<String, String> j = new HashMap();
37 j.put("author", "Goethe");
Michael Hanl296a0312013-12-05 17:27:51 +000038 j.put("pubPlace", "Erfurt");
Michael Hanl55191d92013-12-05 11:53:10 +000039 j.put("textClass", "wissenschaft");
Michael Hanl296a0312013-12-05 17:27:51 +000040 String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER);
Michael Hanl55191d92013-12-05 11:53:10 +000041// System.out.println("value reference " + s);
42 }
43
44 @Test
45 public void testSingle() throws IOException {
46 Map<String, String> j = new HashMap();
47 j.put("textClass", "wissenschaft");
Michael Hanl296a0312013-12-05 17:27:51 +000048 String s = querySerializer.stringify(j, MetaQuerySerializer.TYPE.FILTER);
Michael Hanlababa392013-12-05 21:57:37 +000049 System.out.println("------ TEXT SINGLE " + s);
Michael Hanl560b4cd2013-12-06 14:26:44 +000050 System.out.println();
Michael Hanl296a0312013-12-05 17:27:51 +000051 }
Michael Hanl55191d92013-12-05 11:53:10 +000052
Michael Hanl296a0312013-12-05 17:27:51 +000053 @Test
54 public void testDates() throws IOException {
55 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000056 queries.put("pubDate", String.valueOf(new DateTime().getMillis()) + "~"
57 + String.valueOf(new DateTime().getMillis() + 2));
Michael Hanl296a0312013-12-05 17:27:51 +000058 queries.put("author", "Goethe");
59 String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
Michael Hanlababa392013-12-05 21:57:37 +000060 System.out.println("value until/since : " + f);
Michael Hanl560b4cd2013-12-06 14:26:44 +000061 System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000062 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000063
64 @Test
65 public void testUntil() throws IOException {
66 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000067 queries.put("pubDate", ">" + String.valueOf(new DateTime().getMillis()));
Michael Hanl08b93ed2013-12-05 18:04:45 +000068 queries.put("author", "Hesse");
69 String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
70 System.out.println("value until : " + f);
Michael Hanl560b4cd2013-12-06 14:26:44 +000071 System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000072 }
73
74 @Test
75 public void testSince() throws IOException {
76 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000077 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
Michael Hanl08b93ed2013-12-05 18:04:45 +000078 queries.put("author", "Kafka");
79 String f = querySerializer.stringify(queries, MetaQuerySerializer.TYPE.FILTER);
80 System.out.println("value since : " + f);
Michael Hanl560b4cd2013-12-06 14:26:44 +000081 System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000082 }
83
Michael Hanlababa392013-12-05 21:57:37 +000084 //@Test
85 public void testGenerator() {
86 /*
87 * just for testing...
88 */
89 JsonGenerator jg = new JsonGenerator();
90 int i = 0;
91 String[] queries;
92 queries = new String[]{
93 "shrink({[base=foo]})",
94 "shrink({[base=foo]}[orth=bar])",
95 "shrink(1:[base=Der]{1:[base=Mann]})",
96 };
97
98 for (String q : queries) {
99 i++;
100 try {
101 System.out.println(q);
102 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
103 System.out.println();
104 } catch (NullPointerException npe) {
105 npe.printStackTrace();
106 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +0000107 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000108 } catch (JsonGenerationException e) {
109 e.printStackTrace();
110 } catch (JsonMappingException e) {
111 e.printStackTrace();
112 } catch (IOException e) {
113 e.printStackTrace();
114 }
115 }
116 }
117
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000118 @Test
119 public void testLists() {
120 Map<String, String> queries = new LinkedHashMap<>();
121 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
122 queries.put("author", "Kafka");
123 List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER);
124
125 queries.clear();
126 queries.put("author", "Hesse");
127
128 List f = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.EXTEND);
129 s.addAll(f);
130 System.out.println("--- ALL " + s);
Michael Hanl560b4cd2013-12-06 14:26:44 +0000131 System.out.println();
132
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000133 }
134
135 @Test
136 public void testJSONArray() throws JsonProcessingException {
137 Map<String, String> queries = new LinkedHashMap<>();
138 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
139 queries.put("author", "Kafka");
140 List s = querySerializer.serializeQueries(queries, MetaQuerySerializer.TYPE.FILTER);
Michael Hanl560b4cd2013-12-06 14:26:44 +0000141 System.out.println("array repres " + ser.serializeToMeta(s));
142 System.out.println();
143 }
144
145 @Test
146 public void testCollections() throws IOException {
147 Map<String, String> query = new LinkedHashMap<>();
148 Serializer ser = new Serializer();
149 query.put("corpusID", "WPD");
150
151 List<Map> p = ser.serializeQueries(query, MetaQuerySerializer.TYPE.FILTER);
152 String s = ser.stringify(p);
153
154 System.out.println("results " + s);
155 System.out.println();
156 List<String> l = new ArrayList<>();
157 l.add(s);
158 collSerializer.serializeResource(l);
159
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000160 }
161
Michael Hanl55191d92013-12-05 11:53:10 +0000162}