blob: f0a5392a2100142a4493db03c6dc5d5ee083f848 [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 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();
46 b.append("pubDate:<" + String.valueOf(new DateTime().getMillis()));
47 b.append(" AND ");
48 b.append("pubDate:>" + String.valueOf(new DateTime().getMillis() + 2));
49 b.append(" AND ");
50 b.append("author:Goethe");
51 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
52// System.out.println("value until/since : " + query.stringify());
53 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\":\"2013-12-09\"},{\"@type\":\"korap:date\",\"@value\":\"2013-12-09\"}]}]}}]", query.stringify());
54// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000055 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000056
57 @Test
58 public void testUntil() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000059 StringBuffer b = new StringBuffer();
60 b.append("pubDate:>" + String.valueOf(new DateTime().getMillis()));
61 b.append(" AND ");
62 b.append("author:Hesse");
63 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
64 System.out.println("Running date check (until) with additional attribute author");
65 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-09\"}]}]}}]", query.stringify());
66// System.out.println("value until : " + query.stringify());
67// System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000068 }
69
70 @Test
71 public void testSince() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000072 StringBuffer b = new StringBuffer();
73 b.append("pubDate:<" + String.valueOf(new DateTime().getMillis()));
74 b.append(" AND ");
75 b.append("author:Kafka");
76 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
77// System.out.println("value since : " + query.stringify());
78// System.out.println();
79 System.out.println("Running date check (since) with additional attribute author");
80 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-09\"}]}]}}]", query.stringify());
Michael Hanl08b93ed2013-12-05 18:04:45 +000081 }
82
Michael Hanl53b0fd02013-12-06 21:07:52 +000083 @Test
Michael Hanlababa392013-12-05 21:57:37 +000084 public void testGenerator() {
85 /*
86 * just for testing...
87 */
88 JsonGenerator jg = new JsonGenerator();
89 int i = 0;
90 String[] queries;
91 queries = new String[]{
92 "shrink({[base=foo]})",
93 "shrink({[base=foo]}[orth=bar])",
94 "shrink(1:[base=Der]{1:[base=Mann]})",
95 };
96
97 for (String q : queries) {
98 i++;
99 try {
100 System.out.println(q);
101 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
102 System.out.println();
103 } catch (NullPointerException npe) {
104 npe.printStackTrace();
105 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +0000106 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000107 } catch (JsonGenerationException e) {
108 e.printStackTrace();
109 } catch (JsonMappingException e) {
110 e.printStackTrace();
111 } catch (IOException e) {
112 e.printStackTrace();
113 }
114 }
115 }
116
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000117 @Test
118 public void testLists() {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000119 StringBuffer b = new StringBuffer();
120 b.append("pubDate:<" + String.valueOf(new DateTime().getMillis()));
121 b.append(" AND ");
122 b.append("author:Kafka");
123 MetaQuery query = new MetaQuery().addMetaFilter(b.toString());
Michael Hanl698da8c2013-12-08 21:12:36 +0000124 query.addMetaExtend("author", "Hesse");
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000125
Michael Hanlb64fdce2013-12-08 23:17:19 +0000126// System.out.println("--- ALL " + query.stringify());
127// System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000128
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000129 }
130
131 @Test
132 public void testJSONArray() throws JsonProcessingException {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000133 StringBuffer b = new StringBuffer();
134 b.append("pubDate:<" + String.valueOf(new DateTime().getMillis()));
135 b.append(" AND ");
136 b.append("author:Kafka");
137 MetaQuery q = new MetaQuery().addMetaExtend(b.toString());
138// System.out.println("array repres " + q.toMeta());
139// System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000140 }
141
142 @Test
143 public void testCollections() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000144 MetaQuery q = new MetaQuery().addMetaFilter("corpusID", "A00");
145 q.addMetaExtend("corpusID", "A01");
146
Michael Hanlb64fdce2013-12-08 23:17:19 +0000147// System.out.println("results stringified " + q.stringify());
148// System.out.println("results to meta" + q.toMeta());
149// System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000150 }
151
Michael Hanl698da8c2013-12-08 21:12:36 +0000152 /**
153 * asserts equality. input should be equal to output,
154 * since there is no other metadata added to the meta query
155 *
156 * @throws IOException
157 */
Michael Hanl53b0fd02013-12-06 21:07:52 +0000158 @Test
159 public void testResources() throws IOException {
160 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl698da8c2013-12-08 21:12:36 +0000161 MetaQuery q = new MetaQuery().addResource(meta);
162 org.junit.Assert.assertEquals("String should be empty", "", q.stringify());
Michael Hanlb64fdce2013-12-08 23:17:19 +0000163// System.out.println("meta string " + q.toMeta());
164 System.out.println("Testing Resource Meta data");
165 org.junit.Assert.assertEquals("{\"meta\":" + meta + "}", q.toMeta());
Michael Hanl53b0fd02013-12-06 21:07:52 +0000166 }
167
168 @Test
169 public void testA00() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000170 MetaQuery q = new MetaQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000171// System.out.println("A meta: " + q.stringify());
172// System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000173 }
174
Michael Hanl53b0fd02013-12-06 21:07:52 +0000175 @Test
Michael Hanlb64fdce2013-12-08 23:17:19 +0000176 public void testResources2() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000177 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
178 MetaQuery q = new MetaQuery().addResource(meta);
179 q.addMetaFilter("textClass", "wissenschaft");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000180// System.out.println("stringified meta " + q.stringify());
181// System.out.println("meta string " + q.toMeta());
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000182 }
183
Michael Hanl55191d92013-12-05 11:53:10 +0000184}