blob: 4c3b3555173000ac08c8e220fd37980515184677 [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 Hanl698da8c2013-12-08 21:12:36 +00004import de.ids_mannheim.korap.query.serialize.JsonGenerator;
5import de.ids_mannheim.korap.query.serialize.MetaQuery;
Michael Hanl296a0312013-12-05 17:27:51 +00006import org.joda.time.DateTime;
Michael Hanl55191d92013-12-05 11:53:10 +00007import org.junit.Test;
8import org.junit.runner.RunWith;
9import org.junit.runners.JUnit4;
10
11import java.io.IOException;
Michael Hanl698da8c2013-12-08 21:12:36 +000012import java.util.HashMap;
13import java.util.LinkedHashMap;
14import java.util.Map;
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 Hanl55191d92013-12-05 11:53:10 +000024
25 @Test
26 public void test() throws IOException {
27 Map<String, String> j = new HashMap();
28 j.put("author", "Goethe");
Michael Hanl296a0312013-12-05 17:27:51 +000029 j.put("pubPlace", "Erfurt");
Michael Hanl55191d92013-12-05 11:53:10 +000030 j.put("textClass", "wissenschaft");
Michael Hanl698da8c2013-12-08 21:12:36 +000031 MetaQuery qu = new MetaQuery().addMetaFilter(j);
32 System.out.println("value reference " + qu.stringify());
33 System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000034 }
35
36 @Test
37 public void testSingle() throws IOException {
38 Map<String, String> j = new HashMap();
39 j.put("textClass", "wissenschaft");
Michael Hanl698da8c2013-12-08 21:12:36 +000040 MetaQuery query = new MetaQuery().addMetaFilter("textClass", "wissenschaft");
41 System.out.println("------ TEXT SINGLE " + query.stringify());
Michael Hanl560b4cd2013-12-06 14:26:44 +000042 System.out.println();
Michael Hanl296a0312013-12-05 17:27:51 +000043 }
Michael Hanl55191d92013-12-05 11:53:10 +000044
Michael Hanl296a0312013-12-05 17:27:51 +000045 @Test
46 public void testDates() throws IOException {
47 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000048 queries.put("pubDate", String.valueOf(new DateTime().getMillis()) + "~"
49 + String.valueOf(new DateTime().getMillis() + 2));
Michael Hanl296a0312013-12-05 17:27:51 +000050 queries.put("author", "Goethe");
Michael Hanl698da8c2013-12-08 21:12:36 +000051 MetaQuery query = new MetaQuery().addMetaFilter(queries);
52 System.out.println("value until/since : " + query.stringify());
Michael Hanl560b4cd2013-12-06 14:26:44 +000053 System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000054 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000055
56 @Test
57 public void testUntil() throws IOException {
58 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000059 queries.put("pubDate", ">" + String.valueOf(new DateTime().getMillis()));
Michael Hanl08b93ed2013-12-05 18:04:45 +000060 queries.put("author", "Hesse");
Michael Hanl698da8c2013-12-08 21:12:36 +000061 MetaQuery query = new MetaQuery().addMetaFilter(queries);
62 System.out.println("value until : " + query.stringify());
Michael Hanl560b4cd2013-12-06 14:26:44 +000063 System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000064 }
65
66 @Test
67 public void testSince() throws IOException {
68 Map<String, String> queries = new LinkedHashMap<>();
Michael Hanlababa392013-12-05 21:57:37 +000069 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
Michael Hanl08b93ed2013-12-05 18:04:45 +000070 queries.put("author", "Kafka");
Michael Hanl698da8c2013-12-08 21:12:36 +000071 MetaQuery query = new MetaQuery().addMetaFilter(queries);
72 System.out.println("value since : " + query.stringify());
Michael Hanl560b4cd2013-12-06 14:26:44 +000073 System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000074 }
75
Michael Hanl53b0fd02013-12-06 21:07:52 +000076 @Test
Michael Hanlababa392013-12-05 21:57:37 +000077 public void testGenerator() {
78 /*
79 * just for testing...
80 */
81 JsonGenerator jg = new JsonGenerator();
82 int i = 0;
83 String[] queries;
84 queries = new String[]{
85 "shrink({[base=foo]})",
86 "shrink({[base=foo]}[orth=bar])",
87 "shrink(1:[base=Der]{1:[base=Mann]})",
88 };
89
90 for (String q : queries) {
91 i++;
92 try {
93 System.out.println(q);
94 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
95 System.out.println();
96 } catch (NullPointerException npe) {
97 npe.printStackTrace();
98 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +000099 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000100 } catch (JsonGenerationException e) {
101 e.printStackTrace();
102 } catch (JsonMappingException e) {
103 e.printStackTrace();
104 } catch (IOException e) {
105 e.printStackTrace();
106 }
107 }
108 }
109
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000110 @Test
111 public void testLists() {
112 Map<String, String> queries = new LinkedHashMap<>();
113 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
114 queries.put("author", "Kafka");
Michael Hanl698da8c2013-12-08 21:12:36 +0000115 MetaQuery query = new MetaQuery().addMetaFilter(queries);
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000116
Michael Hanl698da8c2013-12-08 21:12:36 +0000117 query.addMetaExtend("author", "Hesse");
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000118
Michael Hanl698da8c2013-12-08 21:12:36 +0000119 System.out.println("--- ALL " + query.stringify());
Michael Hanl560b4cd2013-12-06 14:26:44 +0000120 System.out.println();
121
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000122 }
123
124 @Test
125 public void testJSONArray() throws JsonProcessingException {
126 Map<String, String> queries = new LinkedHashMap<>();
127 queries.put("pubDate", "<" + String.valueOf(new DateTime().getMillis()));
128 queries.put("author", "Kafka");
Michael Hanl698da8c2013-12-08 21:12:36 +0000129 MetaQuery q = new MetaQuery().addMetaExtend(queries);
130 System.out.println("array repres " + q.toMeta());
Michael Hanl560b4cd2013-12-06 14:26:44 +0000131 System.out.println();
132 }
133
134 @Test
135 public void testCollections() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000136 MetaQuery q = new MetaQuery().addMetaFilter("corpusID", "A00");
137 q.addMetaExtend("corpusID", "A01");
138
139 System.out.println("results stringified " + q.stringify());
140 System.out.println("results to meta" + q.toMeta());
Michael Hanl560b4cd2013-12-06 14:26:44 +0000141 System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000142 }
143
Michael Hanl698da8c2013-12-08 21:12:36 +0000144 /**
145 * asserts equality. input should be equal to output,
146 * since there is no other metadata added to the meta query
147 *
148 * @throws IOException
149 */
Michael Hanl53b0fd02013-12-06 21:07:52 +0000150 @Test
151 public void testResources() throws IOException {
152 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl698da8c2013-12-08 21:12:36 +0000153 MetaQuery q = new MetaQuery().addResource(meta);
154 org.junit.Assert.assertEquals("String should be empty", "", q.stringify());
155 System.out.println("meta string " + q.toMeta());
156 org.junit.Assert.assertEquals(meta, q.toMeta());
Michael Hanl53b0fd02013-12-06 21:07:52 +0000157 }
158
159 @Test
160 public void testA00() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000161 MetaQuery q = new MetaQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01");
162 System.out.println("A meta: " + q.stringify());
163 System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000164 }
165
Michael Hanl53b0fd02013-12-06 21:07:52 +0000166 @Test
167 public void testnewMetaQuery() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000168 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
169 MetaQuery q = new MetaQuery().addResource(meta);
170 q.addMetaFilter("textClass", "wissenschaft");
171 System.out.println("meta string " + q.toMeta());
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000172 }
173
Michael Hanl55191d92013-12-05 11:53:10 +0000174}