blob: 1995510a74d32084a1aaa75ce01f80e1befaaab7 [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 Hanl8a776982014-05-04 10:13:47 +00006import de.ids_mannheim.korap.resource.Relation;
Joachim Bingel16da4e12013-12-17 09:48:12 +00007import de.ids_mannheim.korap.util.QueryException;
Michael Hanlb64fdce2013-12-08 23:17:19 +00008import org.junit.Assert;
Michael Hanl55191d92013-12-05 11:53:10 +00009import org.junit.Test;
10import org.junit.runner.RunWith;
11import org.junit.runners.JUnit4;
12
13import java.io.IOException;
Michael Hanl55191d92013-12-05 11:53:10 +000014
15/**
16 * @author hanl
17 * @date 04/12/2013
18 */
19
20@RunWith(JUnit4.class)
21public class MetaQuerySerializationTest {
22
Michael Hanl55191d92013-12-05 11:53:10 +000023
24 @Test
25 public void test() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000026 StringBuffer b = new StringBuffer();
27 b.append("author:Goethe");
28 b.append(" AND ");
29 b.append("pubPlace:Erfurt");
30 b.append(" AND ");
31 b.append("textClass:wissenschaft");
Michael Hanl1ee40e62014-03-26 17:22:26 +000032// CollectionQuery qu = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanl58e37392014-04-15 20:12:38 +000033 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(),
Michael Hanl8a776982014-05-04 10:13:47 +000034 Relation.AND);
35 System.out.println(query.buildString(Relation.AND, Relation.AND));
36 System.out.println(query.toCollections(Relation.AND, Relation.AND));
Michael Hanlb64fdce2013-12-08 23:17:19 +000037// System.out.println("value reference " + qu.stringify());
38// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000039 }
40
41 @Test
42 public void testSingle() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +000043 CollectionQuery query = new CollectionQuery().addMetaFilter("textClass", "wissenschaft");
Michael Hanlb64fdce2013-12-08 23:17:19 +000044// System.out.println("------ TEXT SINGLE " + query.stringify());
Michael Hanl8a776982014-05-04 10:13:47 +000045 System.out.println(query.buildString(Relation.AND, Relation.AND));
Michael Hanl296a0312013-12-05 17:27:51 +000046 }
Michael Hanl55191d92013-12-05 11:53:10 +000047
Michael Hanl296a0312013-12-05 17:27:51 +000048 @Test
49 public void testDates() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000050 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000051 b.append("pubDate:>2013-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000052 b.append(" AND ");
Michael Hanl4fe13462013-12-11 18:03:42 +000053 b.append("pubDate:<2012-04-01");
Michael Hanlb64fdce2013-12-08 23:17:19 +000054 b.append(" AND ");
55 b.append("author:Goethe");
Michael Hanl8a776982014-05-04 10:13:47 +000056 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), Relation.AND);
57 System.out.println("value until/since : " + query.buildString(Relation.AND, Relation.AND));
58 System.out.println("meta value until/since " + query.toCollections(Relation.AND, Relation.AND));
59 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(Relation.AND, Relation.AND));
Michael Hanlb64fdce2013-12-08 23:17:19 +000060// System.out.println();
Michael Hanl55191d92013-12-05 11:53:10 +000061 }
Michael Hanl08b93ed2013-12-05 18:04:45 +000062
63 @Test
64 public void testUntil() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000065 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000066 b.append("pubDate:>2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000067 b.append(" AND ");
68 b.append("author:Hesse");
Michael Hanl8a776982014-05-04 10:13:47 +000069 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), Relation.AND);
Michael Hanlb64fdce2013-12-08 23:17:19 +000070 System.out.println("Running date check (until) with additional attribute author");
Michael Hanl8a776982014-05-04 10:13:47 +000071 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(Relation.AND, Relation.AND));
Michael Hanlb64fdce2013-12-08 23:17:19 +000072// System.out.println("value until : " + query.stringify());
73// System.out.println();
Michael Hanl08b93ed2013-12-05 18:04:45 +000074 }
75
76 @Test
77 public void testSince() throws IOException {
Michael Hanlb64fdce2013-12-08 23:17:19 +000078 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +000079 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +000080 b.append(" AND ");
81 b.append("author:Kafka");
Michael Hanl8a776982014-05-04 10:13:47 +000082 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString(), Relation.AND);
83 System.out.println("value since : " + query.buildString(Relation.AND,
84 Relation.AND));
85 System.out.println("meta value since " + query.toCollections(Relation.AND, Relation.AND));
Michael Hanlb64fdce2013-12-08 23:17:19 +000086// System.out.println();
87 System.out.println("Running date check (since) with additional attribute author");
Michael Hanl8a776982014-05-04 10:13:47 +000088 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(Relation.AND, Relation.AND));
Michael Hanl08b93ed2013-12-05 18:04:45 +000089 }
90
Michael Hanl53b0fd02013-12-06 21:07:52 +000091 @Test
Joachim Bingel16da4e12013-12-17 09:48:12 +000092 public void testGenerator() throws QueryException {
Michael Hanlababa392013-12-05 21:57:37 +000093 /*
94 * just for testing...
95 */
Michael Hanl4fe41cc2013-12-10 17:59:51 +000096 QuerySerializer jg = new QuerySerializer();
Michael Hanlababa392013-12-05 21:57:37 +000097 int i = 0;
98 String[] queries;
99 queries = new String[]{
100 "shrink({[base=foo]})",
101 "shrink({[base=foo]}[orth=bar])",
102 "shrink(1:[base=Der]{1:[base=Mann]})",
103 };
104
105 for (String q : queries) {
106 i++;
107 try {
108 System.out.println(q);
109 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
110 System.out.println();
111 } catch (NullPointerException npe) {
112 npe.printStackTrace();
113 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +0000114 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000115 } catch (JsonGenerationException e) {
116 e.printStackTrace();
117 } catch (JsonMappingException e) {
118 e.printStackTrace();
119 } catch (IOException e) {
120 e.printStackTrace();
121 }
122 }
123 }
124
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000125 @Test
126 public void testLists() {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000127 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000128 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000129 b.append(" AND ");
130 b.append("author:Kafka");
Michael Hanl8a776982014-05-04 10:13:47 +0000131 CollectionQuery q = new CollectionQuery().addMetaFilter(b.toString(), Relation.AND);
Michael Hanl1ee40e62014-03-26 17:22:26 +0000132 q.addMetaExtend("author", "Hesse");
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000133
Michael Hanl8a776982014-05-04 10:13:47 +0000134 System.out.println("--- ALL " + q.buildString(Relation.AND, Relation.AND));
Michael Hanl1ee40e62014-03-26 17:22:26 +0000135 System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000136
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000137 }
138
139 @Test
140 public void testJSONArray() throws JsonProcessingException {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000141 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000142 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000143 b.append(" AND ");
144 b.append("author:Kafka");
Michael Hanl8a776982014-05-04 10:13:47 +0000145 CollectionQuery q = new CollectionQuery().addMetaExtend(b.toString(), Relation.AND);
146 System.out.println("array repres " + q.buildString(Relation.AND, Relation.AND));
Michael Hanl1ee40e62014-03-26 17:22:26 +0000147 System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000148 }
149
150 @Test
151 public void testCollections() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +0000152 CollectionQuery q = new CollectionQuery().addMetaFilter("corpusID", "A00");
Michael Hanl698da8c2013-12-08 21:12:36 +0000153 q.addMetaExtend("corpusID", "A01");
154
Michael Hanl8a776982014-05-04 10:13:47 +0000155 System.out.println("results stringified " + q.buildString(Relation.AND, Relation.AND));
156 System.out.println("results to meta" + q.toCollections(Relation.AND, Relation.AND));
Michael Hanl1ee40e62014-03-26 17:22:26 +0000157 System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000158 }
159
Michael Hanl698da8c2013-12-08 21:12:36 +0000160 /**
161 * asserts equality. input should be equal to output,
162 * since there is no other metadata added to the meta query
163 *
164 * @throws IOException
165 */
Michael Hanl53b0fd02013-12-06 21:07:52 +0000166 @Test
167 public void testResources() throws IOException {
168 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl942fa022014-02-24 19:48:57 +0000169 CollectionQuery q = new CollectionQuery().addResource(meta);
Michael Hanlb64fdce2013-12-08 23:17:19 +0000170 System.out.println("Testing Resource Meta data");
Michael Hanl8a776982014-05-04 10:13:47 +0000171 org.junit.Assert.assertEquals("{\"collections\":" + meta + "}", q.toCollections(Relation.AND, Relation.AND));
Michael Hanl53b0fd02013-12-06 21:07:52 +0000172 }
173
174 @Test
175 public void testA00() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +0000176 CollectionQuery q = new CollectionQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01");
Michael Hanl8a776982014-05-04 10:13:47 +0000177 System.out.println("A meta: " + q.buildString(Relation.AND, Relation.AND));
Michael Hanl1ee40e62014-03-26 17:22:26 +0000178 System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000179 }
180
Michael Hanl53b0fd02013-12-06 21:07:52 +0000181 @Test
Michael Hanlb64fdce2013-12-08 23:17:19 +0000182 public void testResources2() throws IOException {
Michael Hanl698da8c2013-12-08 21:12:36 +0000183 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl942fa022014-02-24 19:48:57 +0000184 CollectionQuery q = new CollectionQuery().addResource(meta);
Michael Hanl698da8c2013-12-08 21:12:36 +0000185 q.addMetaFilter("textClass", "wissenschaft");
Michael Hanl8a776982014-05-04 10:13:47 +0000186 System.out.println("stringified meta " + q.buildString(Relation.AND, Relation.AND));
187 System.out.println("meta string " + q.toCollections(Relation.AND, Relation.AND));
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000188 }
189
Michael Hanl55191d92013-12-05 11:53:10 +0000190}