blob: 7e430faf7ce9bfe076abc7acf4d2191f92314545 [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;
7
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());
33 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanl2a30f422014-04-01 16:41:44 +000034 System.out.println(query.buildString());
Michael Hanl1ee40e62014-03-26 17:22:26 +000035 System.out.println(query.toCollections());
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 Hanl2a30f422014-04-01 16:41:44 +000044 System.out.println(query.buildString());
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 Hanl942fa022014-02-24 19:48:57 +000055 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanl2a30f422014-04-01 16:41:44 +000056 System.out.println("value until/since : " + query.buildString());
Michael Hanl942fa022014-02-24 19:48:57 +000057 System.out.println("meta value until/since " + query.toCollections());
Michael Hanl2a30f422014-04-01 16:41:44 +000058 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());
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 Hanl942fa022014-02-24 19:48:57 +000068 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanlb64fdce2013-12-08 23:17:19 +000069 System.out.println("Running date check (until) with additional attribute author");
Michael Hanl2a30f422014-04-01 16:41:44 +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());
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 Hanl942fa022014-02-24 19:48:57 +000081 CollectionQuery query = new CollectionQuery().addMetaFilter(b.toString());
Michael Hanl2a30f422014-04-01 16:41:44 +000082 System.out.println("value since : " + query.buildString());
Michael Hanl942fa022014-02-24 19:48:57 +000083 System.out.println("meta value since " + query.toCollections());
Michael Hanlb64fdce2013-12-08 23:17:19 +000084// System.out.println();
85 System.out.println("Running date check (since) with additional attribute author");
Michael Hanl2a30f422014-04-01 16:41:44 +000086 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());
Michael Hanl08b93ed2013-12-05 18:04:45 +000087 }
88
Michael Hanl53b0fd02013-12-06 21:07:52 +000089 @Test
Joachim Bingel16da4e12013-12-17 09:48:12 +000090 public void testGenerator() throws QueryException {
Michael Hanlababa392013-12-05 21:57:37 +000091 /*
92 * just for testing...
93 */
Michael Hanl4fe41cc2013-12-10 17:59:51 +000094 QuerySerializer jg = new QuerySerializer();
Michael Hanlababa392013-12-05 21:57:37 +000095 int i = 0;
96 String[] queries;
97 queries = new String[]{
98 "shrink({[base=foo]})",
99 "shrink({[base=foo]}[orth=bar])",
100 "shrink(1:[base=Der]{1:[base=Mann]})",
101 };
102
103 for (String q : queries) {
104 i++;
105 try {
106 System.out.println(q);
107 jg.run(q, "poliqarp", System.getProperty("user.home") + "/bsp" + i + ".json");
108 System.out.println();
109 } catch (NullPointerException npe) {
110 npe.printStackTrace();
111 System.out.println("null\n");
Michael Hanl560b4cd2013-12-06 14:26:44 +0000112 System.out.println();
Michael Hanlababa392013-12-05 21:57:37 +0000113 } catch (JsonGenerationException e) {
114 e.printStackTrace();
115 } catch (JsonMappingException e) {
116 e.printStackTrace();
117 } catch (IOException e) {
118 e.printStackTrace();
119 }
120 }
121 }
122
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000123 @Test
124 public void testLists() {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000125 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000126 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000127 b.append(" AND ");
128 b.append("author:Kafka");
Michael Hanl1ee40e62014-03-26 17:22:26 +0000129 CollectionQuery q = new CollectionQuery().addMetaFilter(b.toString());
130 q.addMetaExtend("author", "Hesse");
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000131
Michael Hanl2a30f422014-04-01 16:41:44 +0000132 System.out.println("--- ALL " + q.buildString());
Michael Hanl1ee40e62014-03-26 17:22:26 +0000133 System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000134
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000135 }
136
137 @Test
138 public void testJSONArray() throws JsonProcessingException {
Michael Hanlb64fdce2013-12-08 23:17:19 +0000139 StringBuffer b = new StringBuffer();
Michael Hanl4fe13462013-12-11 18:03:42 +0000140 b.append("pubDate:<2013-12-10");
Michael Hanlb64fdce2013-12-08 23:17:19 +0000141 b.append(" AND ");
142 b.append("author:Kafka");
Michael Hanl942fa022014-02-24 19:48:57 +0000143 CollectionQuery q = new CollectionQuery().addMetaExtend(b.toString());
Michael Hanl2a30f422014-04-01 16:41:44 +0000144 System.out.println("array repres " + q.buildString());
Michael Hanl1ee40e62014-03-26 17:22:26 +0000145 System.out.println();
Michael Hanl560b4cd2013-12-06 14:26:44 +0000146 }
147
148 @Test
149 public void testCollections() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +0000150 CollectionQuery q = new CollectionQuery().addMetaFilter("corpusID", "A00");
Michael Hanl698da8c2013-12-08 21:12:36 +0000151 q.addMetaExtend("corpusID", "A01");
152
Michael Hanl2a30f422014-04-01 16:41:44 +0000153 System.out.println("results stringified " + q.buildString());
Michael Hanl1ee40e62014-03-26 17:22:26 +0000154 System.out.println("results to meta" + q.toCollections());
155 System.out.println();
Michael Hanl53b0fd02013-12-06 21:07:52 +0000156 }
157
Michael Hanl698da8c2013-12-08 21:12:36 +0000158 /**
159 * asserts equality. input should be equal to output,
160 * since there is no other metadata added to the meta query
161 *
162 * @throws IOException
163 */
Michael Hanl53b0fd02013-12-06 21:07:52 +0000164 @Test
165 public void testResources() throws IOException {
166 String meta = "[{\"@type\":\"korap:meta-filter\",\"@value\":{\"@type\":\"korap:term\",\"@field\":\"korap:field#corpusID\",\"@value\":\"WPD\"}}]";
Michael Hanl942fa022014-02-24 19:48:57 +0000167 CollectionQuery q = new CollectionQuery().addResource(meta);
Michael Hanlb64fdce2013-12-08 23:17:19 +0000168 System.out.println("Testing Resource Meta data");
Michael Hanl1ee40e62014-03-26 17:22:26 +0000169 org.junit.Assert.assertEquals("{\"collections\":" + meta + "}", q.toCollections());
Michael Hanl53b0fd02013-12-06 21:07:52 +0000170 }
171
172 @Test
173 public void testA00() throws IOException {
Michael Hanl942fa022014-02-24 19:48:57 +0000174 CollectionQuery q = new CollectionQuery().addMetaExtend("corpusID", "A00").addMetaExtend("corpusID", "A01");
Michael Hanl2a30f422014-04-01 16:41:44 +0000175 System.out.println("A meta: " + q.buildString());
Michael Hanl1ee40e62014-03-26 17:22:26 +0000176 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\"}}]";
Michael Hanl942fa022014-02-24 19:48:57 +0000182 CollectionQuery q = new CollectionQuery().addResource(meta);
Michael Hanl698da8c2013-12-08 21:12:36 +0000183 q.addMetaFilter("textClass", "wissenschaft");
Michael Hanl2a30f422014-04-01 16:41:44 +0000184 System.out.println("stringified meta " + q.buildString());
Michael Hanl1ee40e62014-03-26 17:22:26 +0000185 System.out.println("meta string " + q.toCollections());
Michael Hanl9ca5edd2013-12-06 05:13:24 +0000186 }
187
Michael Hanl55191d92013-12-05 11:53:10 +0000188}