blob: 407608d246ef85614ac020064fdd9ecb4e1fecdc [file] [log] [blame]
Michael Hanlfb839b92015-09-19 21:32:34 +02001import com.fasterxml.jackson.databind.JsonNode;
Michael Hanl19390652016-01-16 11:01:24 +01002import de.ids_mannheim.korap.query.serialize.QuerySerializer;
Michael Hanl00b64e02016-05-24 20:24:27 +02003import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
Michael Hanlfb839b92015-09-19 21:32:34 +02004import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanlcedf7212016-05-28 10:43:09 +02005import org.junit.Ignore;
Michael Hanlfb839b92015-09-19 21:32:34 +02006import org.junit.Test;
7
Michael Hanldaf86602016-05-12 14:31:52 +02008import static org.junit.Assert.assertEquals;
Michael Hanl00b64e02016-05-24 20:24:27 +02009import static org.junit.Assert.assertNotEquals;
Michael Hanldaf86602016-05-12 14:31:52 +020010import static org.junit.Assert.assertNotNull;
11
Michael Hanlfb839b92015-09-19 21:32:34 +020012/**
13 * @author hanl
14 * @date 12/08/2015
15 */
16public class CollectionQueryBuilderTest {
17
18 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020019 public void testsimpleAdd () {
Michael Hanl00b64e02016-05-24 20:24:27 +020020 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
21 b.with("corpusSigle=WPD");
Michael Hanl482f30d2015-09-25 12:39:46 +020022
23 JsonNode node = JsonUtils.readTree(b.toJSON());
24
Michael Hanldaf86602016-05-12 14:31:52 +020025 assertNotNull(node);
Michael Hanl8abaf9e2016-05-23 16:46:35 +020026 assertEquals("koral:doc", node.at("/collection/@type").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +020027 assertEquals("corpusSigle", node.at("/collection/key").asText());
Michael Hanlfb839b92015-09-19 21:32:34 +020028 }
29
Michael Hanl8abaf9e2016-05-23 16:46:35 +020030
Michael Hanlfb839b92015-09-19 21:32:34 +020031 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020032 public void testSimpleConjunction () {
Michael Hanl00b64e02016-05-24 20:24:27 +020033 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
34 b.with("corpusSigle=WPD & textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020035 JsonNode node = JsonUtils.readTree(b.toJSON());
36
Michael Hanldaf86602016-05-12 14:31:52 +020037 assertNotNull(node);
Michael Hanl8abaf9e2016-05-23 16:46:35 +020038 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
39 assertEquals("operation:and", node.at("/collection/operation").asText());
Michael Hanldaf86602016-05-12 14:31:52 +020040
Michael Hanl00b64e02016-05-24 20:24:27 +020041 assertEquals("corpusSigle", node.at("/collection/operands/0/key").asText());
Michael Hanl8abaf9e2016-05-23 16:46:35 +020042 assertEquals("textClass", node.at("/collection/operands/1/key")
43 .asText());
Michael Hanlfb839b92015-09-19 21:32:34 +020044 }
45
Michael Hanl8abaf9e2016-05-23 16:46:35 +020046
Michael Hanlfb839b92015-09-19 21:32:34 +020047 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020048 public void testSimpleDisjunction () {
Michael Hanl00b64e02016-05-24 20:24:27 +020049 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
50 b.with("corpusSigle=WPD | textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020051 JsonNode node = JsonUtils.readTree(b.toJSON());
52
Michael Hanl00b64e02016-05-24 20:24:27 +020053 assertNotNull(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020054 assert node.at("/collection/operation").asText().equals("operation:or");
55 assert node.at("/collection/operands/0/key").asText()
Michael Hanl00b64e02016-05-24 20:24:27 +020056 .equals("corpusSigle");
Michael Hanl482f30d2015-09-25 12:39:46 +020057 assert node.at("/collection/operands/1/key").asText()
58 .equals("textClass");
Michael Hanlfb839b92015-09-19 21:32:34 +020059 }
60
Michael Hanl8abaf9e2016-05-23 16:46:35 +020061
Michael Hanlfb839b92015-09-19 21:32:34 +020062 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020063 public void testComplexSubQuery () {
Michael Hanl00b64e02016-05-24 20:24:27 +020064 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
65 b.with("(corpusSigle=WPD) | (textClass=freizeit & corpusSigle=BRZ13)");
Michael Hanl482f30d2015-09-25 12:39:46 +020066 JsonNode node = JsonUtils.readTree(b.toJSON());
Michael Hanlfb839b92015-09-19 21:32:34 +020067
Michael Hanl00b64e02016-05-24 20:24:27 +020068 assertNotNull(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020069 assert node.at("/collection/operation").asText().equals("operation:or");
70 assert node.at("/collection/operands/0/key").asText()
Michael Hanl00b64e02016-05-24 20:24:27 +020071 .equals("corpusSigle");
Michael Hanl482f30d2015-09-25 12:39:46 +020072 assert node.at("/collection/operands/1/@type").asText()
73 .equals("koral:docGroup");
Michael Hanlfb839b92015-09-19 21:32:34 +020074
Michael Hanlfb839b92015-09-19 21:32:34 +020075 }
76
Michael Hanl8abaf9e2016-05-23 16:46:35 +020077
Michael Hanlfb839b92015-09-19 21:32:34 +020078 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020079 public void testAddResourceQueryAfter () {
Michael Hanl00b64e02016-05-24 20:24:27 +020080 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
81 b.with("(textClass=politik & title=\"random title\") | textClass=wissenschaft");
Michael Hanlfb839b92015-09-19 21:32:34 +020082
Michael Hanl00b64e02016-05-24 20:24:27 +020083 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
Michael Hanlfb839b92015-09-19 21:32:34 +020084 c.setBaseQuery(b.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +020085 c.with("corpusSigle=WPD");
Michael Hanlfb839b92015-09-19 21:32:34 +020086
87 JsonNode node = JsonUtils.readTree(c.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +020088 assertNotNull(node);
Michael Hanlcedf7212016-05-28 10:43:09 +020089 assertEquals("koral:doc", node.at("/collection/operands/1/@type")
Michael Hanl00b64e02016-05-24 20:24:27 +020090 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +020091 assertEquals("koral:docGroup", node.at("/collection/operands/0/@type")
92 .asText());
Michael Hanl00b64e02016-05-24 20:24:27 +020093 assertEquals(2, node.at("/collection/operands").size());
Michael Hanlcedf7212016-05-28 10:43:09 +020094 assertEquals(2, node.at("/collection/operands/0/operands").size());
95 assertEquals(2, node.at("/collection/operands/0/operands/0/operands").size());
96
97 assertEquals("operation:and", node.at("/collection/operation").asText());
98 assertEquals("operation:or", node.at("/collection/operands/0/operation").asText());
99 assertEquals("operation:and", node.at("/collection/operands/0/operands/0/operation").asText());
100 assertEquals("WPD", node.at("/collection/operands/1/value").asText());
Michael Hanlfb839b92015-09-19 21:32:34 +0200101 }
102
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200103
Michael Hanl482f30d2015-09-25 12:39:46 +0200104 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200105 public void testAddComplexResourceQueryAfter () {
Michael Hanl00b64e02016-05-24 20:24:27 +0200106 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
107 b.with("(title=\"random title\") | (textClass=wissenschaft)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200108
Michael Hanl00b64e02016-05-24 20:24:27 +0200109 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
Michael Hanl482f30d2015-09-25 12:39:46 +0200110 c.setBaseQuery(b.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +0200111 c.with("(corpusSigle=BRZ13 | corpusSigle=AZPS)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200112
113 JsonNode node = JsonUtils.readTree(c.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +0200114 assertNotNull(node);
115 assertEquals("koral:docGroup", node.at("/collection/operands/0/@type")
116 .asText());
117 assertEquals("koral:docGroup", node.at("/collection/operands/1/@type")
118 .asText());
119 assertEquals("BRZ13", node
Michael Hanlcedf7212016-05-28 10:43:09 +0200120 .at("/collection/operands/1/operands/0/value").asText());
121 assertEquals("AZPS", node.at("/collection/operands/1/operands/1/value")
Michael Hanl00b64e02016-05-24 20:24:27 +0200122 .asText());
123 assertEquals("random title",
Michael Hanlcedf7212016-05-28 10:43:09 +0200124 node.at("/collection/operands/0/operands/0/value").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +0200125 assertEquals("wissenschaft",
Michael Hanlcedf7212016-05-28 10:43:09 +0200126 node.at("/collection/operands/0/operands/1/value").asText());
Michael Hanl482f30d2015-09-25 12:39:46 +0200127 }
128
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200129
Michael Hanl19390652016-01-16 11:01:24 +0100130 @Test
Michael Hanl00b64e02016-05-24 20:24:27 +0200131 public void testBuildQuery () {
132 String coll = "corpusSigle=WPD";
Michael Hanl19390652016-01-16 11:01:24 +0100133 String query = "[base=Haus]";
Michael Hanl00b64e02016-05-24 20:24:27 +0200134 QuerySerializer check = new QuerySerializer();
135 check.setQuery(query, "poliqarp");
136 check.setCollection(coll);
Michael Hanl19390652016-01-16 11:01:24 +0100137
Michael Hanl00b64e02016-05-24 20:24:27 +0200138 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
139 b.setBaseQuery(check.toJSON());
140 b.with("textClass=freizeit");
141
142 JsonNode res = (JsonNode) b.rebaseCollection();
143 assertNotNull(res);
144 assertEquals("koral:docGroup", res.at("/collection/@type").asText());
145 assertEquals("operation:and", res.at("/collection/operation").asText());
146 assertEquals("koral:doc", res.at("/collection/operands/0/@type")
147 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200148 assertEquals("freizeit", res.at("/collection/operands/1/value")
Michael Hanl00b64e02016-05-24 20:24:27 +0200149 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200150 assertEquals("textClass", res.at("/collection/operands/1/key").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +0200151
152 assertEquals("koral:doc", res.at("/collection/operands/1/@type")
153 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200154 assertEquals("WPD", res.at("/collection/operands/0/value").asText());
155 assertEquals("corpusSigle", res.at("/collection/operands/0/key").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +0200156
157 // check also that query is still there
158 assertEquals("koral:token", res.at("/query/@type").asText());
159 assertEquals("koral:term", res.at("/query/wrap/@type").asText());
160 assertEquals("Haus", res.at("/query/wrap/key").asText());
161 assertEquals("lemma", res.at("/query/wrap/layer").asText());
Michael Hanl19390652016-01-16 11:01:24 +0100162 }
163
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200164
Michael Hanl19390652016-01-16 11:01:24 +0100165 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200166 public void testBaseQueryBuild () {
Michael Hanl00b64e02016-05-24 20:24:27 +0200167 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
168 b.with("(corpusSigle=ADF) | (textClass=freizeit & corpusSigle=WPD)");
169
170 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
171 c.setBaseQuery(b.toJSON());
172
173 c.with("corpusSigle=BRZ13");
174 JsonNode base = (JsonNode) c.rebaseCollection();
175 assertNotNull(base);
176 assertEquals(base.at("/collection/@type").asText(), "koral:docGroup");
Michael Hanl00b64e02016-05-24 20:24:27 +0200177 assertEquals(base.at("/collection/operands/1/@type").asText(),
Michael Hanlcedf7212016-05-28 10:43:09 +0200178 "koral:doc");
179 assertEquals(base.at("/collection/operands/1/value").asText(), "BRZ13");
180 assertEquals(base.at("/collection/operands/0/@type").asText(),
Michael Hanl00b64e02016-05-24 20:24:27 +0200181 "koral:docGroup");
Michael Hanlcedf7212016-05-28 10:43:09 +0200182 assertEquals(base.at("/collection/operands/0/operands").size(), 2);
Michael Hanl00b64e02016-05-24 20:24:27 +0200183 }
184
185 @Test
186 public void testNodeMergeWithBase() {
187 String coll = "corpusSigle=WPD";
188 String query = "[base=Haus]";
189 QuerySerializer check = new QuerySerializer();
190 check.setQuery(query, "poliqarp");
191 check.setCollection(coll);
192
193 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
194 b.setBaseQuery(check.toJSON());
195
196 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
197 test.with("textClass=wissenschaft | textClass=politik");
198 JsonNode node = (JsonNode) test.rebaseCollection();
199 node = b.mergeWith(node);
200 assertNotNull(node);
201 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
202 assertEquals("operation:and", node.at("/collection/operation").asText());
203 assertEquals(2, node.at("/collection/operands").size());
204 }
205
206
207 @Test
208 public void testStoredCollectionBaseQueryBuild () {
Michael Hanl19390652016-01-16 11:01:24 +0100209
210 }
211
Michael Hanlcedf7212016-05-28 10:43:09 +0200212 @Test
213 public void testAddOROperator() {
214 String coll = "corpusSigle=WPD";
215 String query = "[base=Haus]";
216 QuerySerializer check = new QuerySerializer();
217 check.setQuery(query, "poliqarp");
218 check.setCollection(coll);
219
220 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
221 test.setBaseQuery(check.toJSON());
222 test.or().with("textClass=wissenschaft | textClass=politik");
223 JsonNode node = (JsonNode) test.rebaseCollection();
224 assertNotNull(node);
225 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
226 assertEquals("operation:or", node.at("/collection/operation").asText());
227 assertEquals(2, node.at("/collection/operands/1/operands").size());
228 }
229
230 @Test
231 public void testAddANDOperator() {
232 String coll = "corpusSigle=WPD";
233 String query = "[base=Haus]";
234 QuerySerializer check = new QuerySerializer();
235 check.setQuery(query, "poliqarp");
236 check.setCollection(coll);
237
238 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
239 test.setBaseQuery(check.toJSON());
240 test.and().with("textClass=wissenschaft | textClass=politik");
241 JsonNode node = (JsonNode) test.rebaseCollection();
242 assertNotNull(node);
243 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
244 assertEquals("operation:and", node.at("/collection/operation").asText());
245 assertEquals(2, node.at("/collection/operands/1/operands").size());
246 }
247
248 @Test
249 public void testAddDefaultOperator() {
250 String coll = "corpusSigle=WPD";
251 String query = "[base=Haus]";
252 QuerySerializer check = new QuerySerializer();
253 check.setQuery(query, "poliqarp");
254 check.setCollection(coll);
255
256 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
257 test.setBaseQuery(check.toJSON());
258 test.with("textClass=wissenschaft | textClass=politik");
259 JsonNode node = (JsonNode) test.rebaseCollection();
260 assertNotNull(node);
261 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
262 assertEquals("operation:and", node.at("/collection/operation").asText());
263 assertEquals(2, node.at("/collection/operands/1/operands").size());
264 }
265
266 @Test
267 @Ignore
268 public void testMergeOperator() {
269 String coll = "corpusSigle=WPD";
270 String query = "[base=Haus]";
271 QuerySerializer check = new QuerySerializer();
272 check.setQuery(query, "poliqarp");
273 check.setCollection(coll);
274
275 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
276 b.setBaseQuery(check.toJSON());
277
278 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
279 test.with("textClass=wissenschaft | textClass=politik");
280 JsonNode node = (JsonNode) test.rebaseCollection();
281 node = b.mergeWith(node);
282 assertNotNull(node);
283 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
284 assertEquals("operation:and", node.at("/collection/operation").asText());
285 assertEquals(2, node.at("/collection/operands").size());
286 }
287
Michael Hanlfb839b92015-09-19 21:32:34 +0200288}