blob: a1c1879f4f0d1d62e341098b9e73f087c180151e [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;
Joachim Bingel16da4e12013-12-17 09:48:12 +00006import de.ids_mannheim.korap.util.QueryException;
7
Michael Hanl296a0312013-12-05 17:27:51 +00008import org.joda.time.DateTime;
Michael Hanlb64fdce2013-12-08 23:17:19 +00009import org.junit.Assert;
Michael Hanl55191d92013-12-05 11:53:10 +000010import org.junit.Test;
11import org.junit.runner.RunWith;
12import org.junit.runners.JUnit4;
13
14import java.io.IOException;
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 {
Michael Hanlb64fdce2013-12-08 23:17:19 +000027 StringBuffer b = new StringBuffer();
28 b.append("author:Goethe");
29 b.append(" AND ");
30 b.append("pubPlace:Erfurt");
31 b.append(" AND ");
32 b.append("textClass:wissenschaft");
33 MetaQuery qu = new MetaQuery().addMetaFilter(b.toString());
34// System.out.println("value reference " + qu.stringify());
35// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000036 }
37
38 @Test
39 public void testSingle() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +000040 MetaQuery query = new MetaQuery().addMetaFilter("textClass", "wissenschaft");
Michael Hanlb64fdce2013-12-08 23:17:19 +000041// 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 {
Michael Hanlb64fdce2013-12-08 23:17:19 +000047 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000048 b.append("pubDate:>2013-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000049 b.append(" AND ");
Michael Hanl4fe13462013-12-11 18:03:42 +000050 b.append("pubDate:<2012-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000051 b.append(" AND ");
52 b.append("author:Goethe");
53 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
Michael Hanl4fe13462013-12-11 18:03:42 +000054 System.out.println("value until/since : " + query.stringify());
55 System.out.println("meta value until/since " + query.toMeta());
56 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 +000057// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000058 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000059
60 @Test
61 public void testUntil() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000062 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000063 b.append("pubDate:>2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000064 b.append(" AND ");
65 b.append("author:Hesse");
66 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
67 System.out.println("Running date check (until) with additional attribute author");
Joachim Bingel94a1ccd2013-12-10 10:37:29 +000068 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 +000069// System.out.println("value until : " + query.stringify());
70// System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000071 }
72
73 @Test
74 public void testSince() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000075 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000076 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000077 b.append(" AND ");
78 b.append("author:Kafka");
79 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
Michael Hanl4fe13462013-12-11 18:03:42 +000080 System.out.println("value since : " + query.stringify());
81 System.out.println("meta value since " + query.toMeta());
Michael Hanlb64fdce2013-12-08 23:17:19 +000082// System.out.println();
83 System.out.println("Running date check (since) with additional attribute author");
Joachim Bingel94a1ccd2013-12-10 10:37:29 +000084 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 +000085 }
86
Michael Hanl53b0fd02013-12-06 21:07:52 +000087 @Test
Joachim Bingel16da4e12013-12-17 09:48:12 +000088 public void testGenerator() throws QueryException {
Michael Hanlababa392013-12-05 21:57:37 +000089 /*
90 * just for testing...
91 */
Michael Hanl4fe41cc2013-12-10 17:59:51 +000092 QuerySerializer jg = new QuerySerializer();
Michael Hanlababa392013-12-05 21:57:37 +000093 int i = 0;
94 String[] queries;
95 queries = new String[]{
96 "shrink({[base=foo]})",
97 "shrink({[base=foo]}[orth=bar])",
98 "shrink(1:[base=Der]{1:[base=Mann]})",
99 };
100
101 for (String q : queries) {
102 i++;
103 try {
104 System.out.println(q);
105 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
106 System.out.println();
107 } catch (NullPointerException npe) {
108 npe.printStackTrace();
109 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +0000110 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000111 } catch (JsonGenerationException e) {
112 e.printStackTrace();
113 } catch (JsonMappingException e) {
114 e.printStackTrace();
115 } catch (IOException e) {
116 e.printStackTrace();
117 }
118 }
119 }
120
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000121 @Test
122 public void testLists() {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000123 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000124 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000125 b.append(" AND ");
126 b.append("author:Kafka");
127 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
Michael Hanl698da8c2013-12-08 21:12:36 +0000128 query.addMetaExtend("author", "Hesse");
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000129
Michael Hanlb64fdce2013-12-08 23:17:19 +0000130// System.out.println("--- ALL " + query.stringify());
131// System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000132
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000133 }
134
135 @Test
136 public void testJSONArray() throws JsonProcessingException {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000137 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000138 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000139 b.append(" AND ");
140 b.append("author:Kafka");
141 MetaQuery q = new MetaQuery().addMetaExtend(b.toString());
142// System.out.println("array repres " + q.toMeta());
143// System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000144 }
145
146 @Test
147 public void testCollections() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000148 MetaQuery q = new MetaQuery().addMetaFilter("corpusID", "A00");
149 q.addMetaExtend("corpusID", "A01");
150
Michael Hanlb64fdce2013-12-08 23:17:19 +0000151// System.out.println("results stringified " + q.stringify());
152// System.out.println("results to meta" + q.toMeta());
153// System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000154 }
155
Michael Hanl698da8c2013-12-08 21:12:36 +0000156 /**
157 * asserts equality. input should be equal to output,
158 * since there is no other metadata added to the meta query
159 *
160 * @throws IOException
161 */
Michael Hanl53b0fd02013-12-06 21:07:52 +0000162 @Test
163 public void testResources() throws IOException {
164 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl698da8c2013-12-08 21:12:36 +0000165 MetaQuery q = new MetaQuery().addResource(meta);
166 org.junit.Assert.assertEquals("String should be empty", "", q.stringify());
Michael Hanlb64fdce2013-12-08 23:17:19 +0000167// System.out.println("meta string " + q.toMeta());
168 System.out.println("Testing Resource Meta data");
169 org.junit.Assert.assertEquals("{\"meta\":" + meta + "}", q.toMeta());
Michael Hanl53b0fd02013-12-06 21:07:52 +0000170 }
171
172 @Test
173 public void testA00() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000174 MetaQuery q = new MetaQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000175// System.out.println("A meta: " + q.stringify());
176// System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000177 }
178
Michael Hanl53b0fd02013-12-06 21:07:52 +0000179 @Test
Michael Hanlb64fdce2013-12-08 23:17:19 +0000180 public void testResources2() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000181 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
182 MetaQuery q = new MetaQuery().addResource(meta);
183 q.addMetaFilter("textClass", "wissenschaft");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000184// System.out.println("stringified meta " + q.stringify());
185// System.out.println("meta string " + q.toMeta());
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000186 }
187
Michael Hanl55191d92013-12-05 11:53:10 +0000188}