blob: 03cd54924ad464b17cf6a38d8a225294f4bad995 [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 Hanl942fa022014-02-24 19:48:57 +00004import de.ids_mannheim.korap.query.serialize.CollectionQuery;
Michael Hanl4fe41cc2013-12-10 17:59:51 +00005import de.ids_mannheim.korap.query.serialize.QuerySerializer;
Michael Hanl698da8c2013-12-08 21:12:36 +00006import de.ids_mannheim.korap.query.serialize.MetaQuery;
Joachim Bingel16da4e12013-12-17 09:48:12 +00007import de.ids_mannheim.korap.util.QueryException;
8
Michael Hanl296a0312013-12-05 17:27:51 +00009import org.joda.time.DateTime;
Michael Hanlb64fdce2013-12-08 23:17:19 +000010import org.junit.Assert;
Michael Hanl55191d92013-12-05 11:53:10 +000011import org.junit.Test;
12import org.junit.runner.RunWith;
13import org.junit.runners.JUnit4;
14
15import java.io.IOException;
Michael Hanl55191d92013-12-05 11:53:10 +000016
17/**
18 * @author hanl
19 * @date 04/12/2013
20 */
21
22@RunWith(JUnit4.class)
23public class MetaQuerySerializationTest {
24
Michael Hanl55191d92013-12-05 11:53:10 +000025
26 @Test
27 public void test() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000028 StringBuffer b = new StringBuffer();
29 b.append("author:Goethe");
30 b.append(" AND ");
31 b.append("pubPlace:Erfurt");
32 b.append(" AND ");
33 b.append("textClass:wissenschaft");
Michael Hanl942fa022014-02-24 19:48:57 +000034 CollectionQuery qu = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanlb64fdce2013-12-08 23:17:19 +000035// System.out.println("value reference " + qu.stringify());
36// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000037 }
38
39 @Test
40 public void testSingle() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +000041 CollectionQuery query = new CollectionQuery().addMetaFilter("textClass", "wissenschaft");
Michael Hanlb64fdce2013-12-08 23:17:19 +000042// System.out.println("------ TEXT SINGLE " + query.stringify());
Michael Hanl560b4cd2013-12-06 14:26:44 +000043 System.out.println();
Michael Hanl296a0312013-12-05 17:27:51 +000044 }
Michael Hanl55191d92013-12-05 11:53:10 +000045
Michael Hanl296a0312013-12-05 17:27:51 +000046 @Test
47 public void testDates() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000048 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000049 b.append("pubDate:>2013-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000050 b.append(" AND ");
Michael Hanl4fe13462013-12-11 18:03:42 +000051 b.append("pubDate:<2012-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000052 b.append(" AND ");
53 b.append("author:Goethe");
Michael Hanl942fa022014-02-24 19:48:57 +000054 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanl4fe13462013-12-11 18:03:42 +000055 System.out.println("value until/since : " + query.stringify());
Michael Hanl942fa022014-02-24 19:48:57 +000056 System.out.println("meta value until/since " + query.toCollections());
Michael Hanl4fe13462013-12-11 18:03:42 +000057 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 +000058// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000059 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000060
61 @Test
62 public void testUntil() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000063 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000064 b.append("pubDate:>2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000065 b.append(" AND ");
66 b.append("author:Hesse");
Michael Hanl942fa022014-02-24 19:48:57 +000067 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanlb64fdce2013-12-08 23:17:19 +000068 System.out.println("Running date check (until) with additional attribute author");
Joachim Bingel94a1ccd2013-12-10 10:37:29 +000069 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 +000070// System.out.println("value until : " + query.stringify());
71// System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000072 }
73
74 @Test
75 public void testSince() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000076 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000077 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000078 b.append(" AND ");
79 b.append("author:Kafka");
Michael Hanl942fa022014-02-24 19:48:57 +000080 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanl4fe13462013-12-11 18:03:42 +000081 System.out.println("value since : " + query.stringify());
Michael Hanl942fa022014-02-24 19:48:57 +000082 System.out.println("meta value since " + query.toCollections());
Michael Hanlb64fdce2013-12-08 23:17:19 +000083// System.out.println();
84 System.out.println("Running date check (since) with additional attribute author");
Joachim Bingel94a1ccd2013-12-10 10:37:29 +000085 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 +000086 }
87
Michael Hanl53b0fd02013-12-06 21:07:52 +000088 @Test
Joachim Bingel16da4e12013-12-17 09:48:12 +000089 public void testGenerator() throws QueryException {
Michael Hanlababa392013-12-05 21:57:37 +000090 /*
91 * just for testing...
92 */
Michael Hanl4fe41cc2013-12-10 17:59:51 +000093 QuerySerializer jg = new QuerySerializer();
Michael Hanlababa392013-12-05 21:57:37 +000094 int i = 0;
95 String[] queries;
96 queries = new String[]{
97 "shrink({[base=foo]})",
98 "shrink({[base=foo]}[orth=bar])",
99 "shrink(1:[base=Der]{1:[base=Mann]})",
100 };
101
102 for (String q : queries) {
103 i++;
104 try {
105 System.out.println(q);
106 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
107 System.out.println();
108 } catch (NullPointerException npe) {
109 npe.printStackTrace();
110 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +0000111 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000112 } catch (JsonGenerationException e) {
113 e.printStackTrace();
114 } catch (JsonMappingException e) {
115 e.printStackTrace();
116 } catch (IOException e) {
117 e.printStackTrace();
118 }
119 }
120 }
121
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000122 @Test
123 public void testLists() {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000124 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000125 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000126 b.append(" AND ");
127 b.append("author:Kafka");
Michael Hanl942fa022014-02-24 19:48:57 +0000128 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanl698da8c2013-12-08 21:12:36 +0000129 query.addMetaExtend("author", "Hesse");
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000130
Michael Hanlb64fdce2013-12-08 23:17:19 +0000131// System.out.println("--- ALL " + query.stringify());
132// System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000133
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000134 }
135
136 @Test
137 public void testJSONArray() throws JsonProcessingException {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000138 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000139 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000140 b.append(" AND ");
141 b.append("author:Kafka");
Michael Hanl942fa022014-02-24 19:48:57 +0000142 CollectionQuery q = new CollectionQuery().addMetaExtend(b.toString());
Michael Hanlb64fdce2013-12-08 23:17:19 +0000143// System.out.println("array repres " + q.toMeta());
144// System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000145 }
146
147 @Test
148 public void testCollections() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +0000149 CollectionQuery q = new CollectionQuery().addMetaFilter("corpusID", "A00");
Michael Hanl698da8c2013-12-08 21:12:36 +0000150 q.addMetaExtend("corpusID", "A01");
151
Michael Hanlb64fdce2013-12-08 23:17:19 +0000152// System.out.println("results stringified " + q.stringify());
153// System.out.println("results to meta" + q.toMeta());
154// System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000155 }
156
Michael Hanl698da8c2013-12-08 21:12:36 +0000157 /**
158 * asserts equality. input should be equal to output,
159 * since there is no other metadata added to the meta query
160 *
161 * @throws IOException
162 */
Michael Hanl53b0fd02013-12-06 21:07:52 +0000163 @Test
164 public void testResources() throws IOException {
165 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl942fa022014-02-24 19:48:57 +0000166 CollectionQuery q = new CollectionQuery().addResource(meta);
Michael Hanl698da8c2013-12-08 21:12:36 +0000167 org.junit.Assert.assertEquals("String should be empty", "", q.stringify());
Michael Hanlb64fdce2013-12-08 23:17:19 +0000168// System.out.println("meta string " + q.toMeta());
169 System.out.println("Testing Resource Meta data");
Michael Hanl942fa022014-02-24 19:48:57 +0000170 org.junit.Assert.assertEquals("{\"meta\":" + meta + "}", q.toCollections());
Michael Hanl53b0fd02013-12-06 21:07:52 +0000171 }
172
173 @Test
174 public void testA00() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +0000175 CollectionQuery q = new CollectionQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000176// System.out.println("A meta: " + q.stringify());
177// System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000178 }
179
Michael Hanl53b0fd02013-12-06 21:07:52 +0000180 @Test
Michael Hanlb64fdce2013-12-08 23:17:19 +0000181 public void testResources2() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000182 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl942fa022014-02-24 19:48:57 +0000183 CollectionQuery q = new CollectionQuery().addResource(meta);
Michael Hanl698da8c2013-12-08 21:12:36 +0000184 q.addMetaFilter("textClass", "wissenschaft");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000185// System.out.println("stringified meta " + q.stringify());
186// System.out.println("meta string " + q.toMeta());
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000187 }
188
Michael Hanl55191d92013-12-05 11:53:10 +0000189}