blob: 2f7d8c3f4e49c2665d5b1c986dd37fa505583231 [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;
Joachim Bingel16da4e12013-12-17 09:48:12 +00006import de.ids_mannheim.korap.util.QueryException;
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");
Michael Hanl1ee40e62014-03-26 17:22:26 +000031// CollectionQuery qu = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanl58e37392014-04-15 20:12:38 +000032 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(),
33 CollectionQuery.RELATION.AND);
34 System.out.println(query.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
35 System.out.println(query.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanlb64fdce2013-12-08 23:17:19 +000036// System.out.println("value reference " + qu.stringify());
37// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000038 }
39
40 @Test
41 public void testSingle() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +000042 CollectionQuery query = new CollectionQuery().addMetaFilter("textClass", "wissenschaft");
Michael Hanlb64fdce2013-12-08 23:17:19 +000043// System.out.println("------ TEXT SINGLE " + query.stringify());
Michael Hanl58e37392014-04-15 20:12:38 +000044 System.out.println(query.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanl296a0312013-12-05 17:27:51 +000045 }
Michael Hanl55191d92013-12-05 11:53:10 +000046
Michael Hanl296a0312013-12-05 17:27:51 +000047 @Test
48 public void testDates() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000049 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000050 b.append("pubDate:>2013-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000051 b.append(" AND ");
Michael Hanl4fe13462013-12-11 18:03:42 +000052 b.append("pubDate:<2012-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000053 b.append(" AND ");
54 b.append("author:Goethe");
Michael Hanl58e37392014-04-15 20:12:38 +000055 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), CollectionQuery.RELATION.AND);
56 System.out.println("value until/since : " + query.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
57 System.out.println("meta value until/since " + query.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
58 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.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanlb64fdce2013-12-08 23:17:19 +000059// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000060 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000061
62 @Test
63 public void testUntil() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000064 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000065 b.append("pubDate:>2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000066 b.append(" AND ");
67 b.append("author:Hesse");
Michael Hanl58e37392014-04-15 20:12:38 +000068 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), CollectionQuery.RELATION.AND);
Michael Hanlb64fdce2013-12-08 23:17:19 +000069 System.out.println("Running date check (until) with additional attribute author");
Michael Hanl58e37392014-04-15 20:12:38 +000070 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.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanlb64fdce2013-12-08 23:17:19 +000071// System.out.println("value until : " + query.stringify());
72// System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000073 }
74
75 @Test
76 public void testSince() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000077 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000078 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000079 b.append(" AND ");
80 b.append("author:Kafka");
Michael Hanl58e37392014-04-15 20:12:38 +000081 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), CollectionQuery.RELATION.AND);
82 System.out.println("value since : " + query.buildString(CollectionQuery.RELATION.AND,
83 CollectionQuery.RELATION.AND));
84 System.out.println("meta value since " + query.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanlb64fdce2013-12-08 23:17:19 +000085// System.out.println();
86 System.out.println("Running date check (since) with additional attribute author");
Michael Hanl58e37392014-04-15 20:12:38 +000087 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.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanl08b93ed2013-12-05 18:04:45 +000088 }
89
Michael Hanl53b0fd02013-12-06 21:07:52 +000090 @Test
Joachim Bingel16da4e12013-12-17 09:48:12 +000091 public void testGenerator() throws QueryException {
Michael Hanlababa392013-12-05 21:57:37 +000092 /*
93 * just for testing...
94 */
Michael Hanl4fe41cc2013-12-10 17:59:51 +000095 QuerySerializer jg = new QuerySerializer();
Michael Hanlababa392013-12-05 21:57:37 +000096 int i = 0;
97 String[] queries;
98 queries = new String[]{
99 "shrink({[base=foo]})",
100 "shrink({[base=foo]}[orth=bar])",
101 "shrink(1:[base=Der]{1:[base=Mann]})",
102 };
103
104 for (String q : queries) {
105 i++;
106 try {
107 System.out.println(q);
108 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
109 System.out.println();
110 } catch (NullPointerException npe) {
111 npe.printStackTrace();
112 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +0000113 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000114 } catch (JsonGenerationException e) {
115 e.printStackTrace();
116 } catch (JsonMappingException e) {
117 e.printStackTrace();
118 } catch (IOException e) {
119 e.printStackTrace();
120 }
121 }
122 }
123
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000124 @Test
125 public void testLists() {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000126 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000127 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000128 b.append(" AND ");
129 b.append("author:Kafka");
Michael Hanl58e37392014-04-15 20:12:38 +0000130 CollectionQuery q = new CollectionQuery().addMetaFilter(b.toString(), CollectionQuery.RELATION.AND);
Michael Hanl1ee40e62014-03-26 17:22:26 +0000131 q.addMetaExtend("author", "Hesse");
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000132
Michael Hanl58e37392014-04-15 20:12:38 +0000133 System.out.println("--- ALL " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanl1ee40e62014-03-26 17:22:26 +0000134 System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000135
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000136 }
137
138 @Test
139 public void testJSONArray() throws JsonProcessingException {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000140 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000141 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000142 b.append(" AND ");
143 b.append("author:Kafka");
Michael Hanl58e37392014-04-15 20:12:38 +0000144 CollectionQuery q = new CollectionQuery().addMetaExtend(b.toString(), CollectionQuery.RELATION.AND);
145 System.out.println("array repres " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanl1ee40e62014-03-26 17:22:26 +0000146 System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000147 }
148
149 @Test
150 public void testCollections() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +0000151 CollectionQuery q = new CollectionQuery().addMetaFilter("corpusID", "A00");
Michael Hanl698da8c2013-12-08 21:12:36 +0000152 q.addMetaExtend("corpusID", "A01");
153
Michael Hanl58e37392014-04-15 20:12:38 +0000154 System.out.println("results stringified " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
155 System.out.println("results to meta" + q.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanl1ee40e62014-03-26 17:22:26 +0000156 System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000157 }
158
Michael Hanl698da8c2013-12-08 21:12:36 +0000159 /**
160 * asserts equality. input should be equal to output,
161 * since there is no other metadata added to the meta query
162 *
163 * @throws IOException
164 */
Michael Hanl53b0fd02013-12-06 21:07:52 +0000165 @Test
166 public void testResources() throws IOException {
167 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl942fa022014-02-24 19:48:57 +0000168 CollectionQuery q = new CollectionQuery().addResource(meta);
Michael Hanlb64fdce2013-12-08 23:17:19 +0000169 System.out.println("Testing Resource Meta data");
Michael Hanl58e37392014-04-15 20:12:38 +0000170 org.junit.Assert.assertEquals("{\"collections\":" + meta + "}", q.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
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 Hanl58e37392014-04-15 20:12:38 +0000176 System.out.println("A meta: " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanl1ee40e62014-03-26 17:22:26 +0000177 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 Hanl58e37392014-04-15 20:12:38 +0000185 System.out.println("stringified meta " + q.buildString(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
186 System.out.println("meta string " + q.toCollections(CollectionQuery.RELATION.AND, CollectionQuery.RELATION.AND));
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000187 }
188
Michael Hanl55191d92013-12-05 11:53:10 +0000189}