blob: f9a13cfc78e44a80920a6d37f0cc17d2cc0086c1 [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 Hanl4fe41cc2013-12-10 17:59:51 +00004import de.ids_mannheim.korap.query.serialize.QuerySerializer;
Michael Hanl698da8c2013-12-08 21:12:36 +00005import de.ids_mannheim.korap.query.serialize.MetaQuery;
Michael Hanl296a0312013-12-05 17:27:51 +00006import org.joda.time.DateTime;
Michael Hanlb64fdce2013-12-08 23:17:19 +00007import org.junit.Assert;
Michael Hanl55191d92013-12-05 11:53:10 +00008import org.junit.Test;
9import org.junit.runner.RunWith;
10import org.junit.runners.JUnit4;
11
12import java.io.IOException;
Michael Hanl55191d92013-12-05 11:53:10 +000013
14/**
15 * @author hanl
16 * @date 04/12/2013
17 */
18
19@RunWith(JUnit4.class)
20public class MetaQuerySerializationTest {
21
Michael Hanl55191d92013-12-05 11:53:10 +000022
23 @Test
24 public void test() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000025 StringBuffer b = new StringBuffer();
26 b.append("author:Goethe");
27 b.append(" AND ");
28 b.append("pubPlace:Erfurt");
29 b.append(" AND ");
30 b.append("textClass:wissenschaft");
31 MetaQuery qu = new MetaQuery().addMetaFilter(b.toString());
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 {
Michael Hanl698da8c2013-12-08 21:12:36 +000038 MetaQuery query = new MetaQuery().addMetaFilter("textClass", "wissenschaft");
Michael Hanlb64fdce2013-12-08 23:17:19 +000039// System.out.println("------ TEXT SINGLE " + query.stringify());
Michael Hanl560b4cd2013-12-06 14:26:44 +000040 System.out.println();
Michael Hanl296a0312013-12-05 17:27:51 +000041 }
Michael Hanl55191d92013-12-05 11:53:10 +000042
Michael Hanl296a0312013-12-05 17:27:51 +000043 @Test
44 public void testDates() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000045 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000046 b.append("pubDate:>2013-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000047 b.append(" AND ");
Michael Hanl4fe13462013-12-11 18:03:42 +000048 b.append("pubDate:<2012-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000049 b.append(" AND ");
50 b.append("author:Goethe");
51 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
Michael Hanl4fe13462013-12-11 18:03:42 +000052 System.out.println("value until/since : " + query.stringify());
53 System.out.println("meta value until/since " + query.toMeta());
54 Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Goethe\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"between\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2012-04-01\"},{\"@type\":\"korap:date\",\"@value\":\"2013-04-01\"}]}]}}]", query.stringify());
Michael Hanlb64fdce2013-12-08 23:17:19 +000055// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000056 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000057
58 @Test
59 public void testUntil() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000060 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000061 b.append("pubDate:>2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000062 b.append(" AND ");
63 b.append("author:Hesse");
64 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
65 System.out.println("Running date check (until) with additional attribute author");
Joachim Bingel94a1ccd2013-12-10 10:37:29 +000066 Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Hesse\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"until\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2013-12-10\"}]}]}}]", query.stringify());
Michael Hanlb64fdce2013-12-08 23:17:19 +000067// System.out.println("value until : " + query.stringify());
68// System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000069 }
70
71 @Test
72 public void testSince() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000073 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000074 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000075 b.append(" AND ");
76 b.append("author:Kafka");
77 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
Michael Hanl4fe13462013-12-11 18:03:42 +000078 System.out.println("value since : " + query.stringify());
79 System.out.println("meta value since " + query.toMeta());
Michael Hanlb64fdce2013-12-08 23:17:19 +000080// System.out.println();
81 System.out.println("Running date check (since) with additional attribute author");
Joachim Bingel94a1ccd2013-12-10 10:37:29 +000082 Assert.assertEquals("[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:group\",\"relation\":\"and\",\"operands\":[{\"@type\":\"korap:term\",\"@field\":\"korap:field#author\",\"@value\":\"Kafka\"},{\"@type\":\"korap:group\",\"@field\":\"korap:field#pubDate\",\"relation\":\"since\",\"operands\":[{\"@type\":\"korap:date\",\"@value\":\"2013-12-10\"}]}]}}]", query.stringify());
Michael Hanl08b93ed2013-12-05 18:04:45 +000083 }
84
Michael Hanl53b0fd02013-12-06 21:07:52 +000085 @Test
Michael Hanlababa392013-12-05 21:57:37 +000086 public void testGenerator() {
87 /*
88 * just for testing...
89 */
Michael Hanl4fe41cc2013-12-10 17:59:51 +000090 QuerySerializer jg = new QuerySerializer();
Michael Hanlababa392013-12-05 21:57:37 +000091 int i = 0;
92 String[] queries;
93 queries = new String[]{
94 "shrink({[base=foo]})",
95 "shrink({[base=foo]}[orth=bar])",
96 "shrink(1:[base=Der]{1:[base=Mann]})",
97 };
98
99 for (String q : queries) {
100 i++;
101 try {
102 System.out.println(q);
103 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
104 System.out.println();
105 } catch (NullPointerException npe) {
106 npe.printStackTrace();
107 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +0000108 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000109 } catch (JsonGenerationException e) {
110 e.printStackTrace();
111 } catch (JsonMappingException e) {
112 e.printStackTrace();
113 } catch (IOException e) {
114 e.printStackTrace();
115 }
116 }
117 }
118
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000119 @Test
120 public void testLists() {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000121 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000122 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000123 b.append(" AND ");
124 b.append("author:Kafka");
125 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
Michael Hanl698da8c2013-12-08 21:12:36 +0000126 query.addMetaExtend("author", "Hesse");
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000127
Michael Hanlb64fdce2013-12-08 23:17:19 +0000128// System.out.println("--- ALL " + query.stringify());
129// System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000130
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000131 }
132
133 @Test
134 public void testJSONArray() throws JsonProcessingException {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000135 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000136 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000137 b.append(" AND ");
138 b.append("author:Kafka");
139 MetaQuery q = new MetaQuery().addMetaExtend(b.toString());
140// System.out.println("array repres " + q.toMeta());
141// System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000142 }
143
144 @Test
145 public void testCollections() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000146 MetaQuery q = new MetaQuery().addMetaFilter("corpusID", "A00");
147 q.addMetaExtend("corpusID", "A01");
148
Michael Hanlb64fdce2013-12-08 23:17:19 +0000149// System.out.println("results stringified " + q.stringify());
150// System.out.println("results to meta" + q.toMeta());
151// System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000152 }
153
Michael Hanl698da8c2013-12-08 21:12:36 +0000154 /**
155 * asserts equality. input should be equal to output,
156 * since there is no other metadata added to the meta query
157 *
158 * @throws IOException
159 */
Michael Hanl53b0fd02013-12-06 21:07:52 +0000160 @Test
161 public void testResources() throws IOException {
162 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl698da8c2013-12-08 21:12:36 +0000163 MetaQuery q = new MetaQuery().addResource(meta);
164 org.junit.Assert.assertEquals("String should be empty", "", q.stringify());
Michael Hanlb64fdce2013-12-08 23:17:19 +0000165// System.out.println("meta string " + q.toMeta());
166 System.out.println("Testing Resource Meta data");
167 org.junit.Assert.assertEquals("{\"meta\":" + meta + "}", q.toMeta());
Michael Hanl53b0fd02013-12-06 21:07:52 +0000168 }
169
170 @Test
171 public void testA00() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000172 MetaQuery q = new MetaQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000173// System.out.println("A meta: " + q.stringify());
174// System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000175 }
176
Michael Hanl53b0fd02013-12-06 21:07:52 +0000177 @Test
Michael Hanlb64fdce2013-12-08 23:17:19 +0000178 public void testResources2() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000179 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
180 MetaQuery q = new MetaQuery().addResource(meta);
181 q.addMetaFilter("textClass", "wissenschaft");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000182// System.out.println("stringified meta " + q.stringify());
183// System.out.println("meta string " + q.toMeta());
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000184 }
185
Michael Hanl55191d92013-12-05 11:53:10 +0000186}