blob: 31ca11409282fdb8628430fec81f3e5446dec26b [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 Hanlcb2d3f92016-06-02 17:34:06 +02003import de.ids_mannheim.korap.resources.KustvaktResource;
4import de.ids_mannheim.korap.resources.VirtualCollection;
Michael Hanl00b64e02016-05-24 20:24:27 +02005import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder;
Michael Hanlfb839b92015-09-19 21:32:34 +02006import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanlcedf7212016-05-28 10:43:09 +02007import org.junit.Ignore;
Michael Hanlfb839b92015-09-19 21:32:34 +02008import org.junit.Test;
9
Michael Hanldaf86602016-05-12 14:31:52 +020010import static org.junit.Assert.assertEquals;
Michael Hanl00b64e02016-05-24 20:24:27 +020011import static org.junit.Assert.assertNotEquals;
Michael Hanldaf86602016-05-12 14:31:52 +020012import static org.junit.Assert.assertNotNull;
13
Michael Hanlfb839b92015-09-19 21:32:34 +020014/**
15 * @author hanl
16 * @date 12/08/2015
17 */
18public class CollectionQueryBuilderTest {
19
20 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020021 public void testsimpleAdd () {
Michael Hanl00b64e02016-05-24 20:24:27 +020022 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
23 b.with("corpusSigle=WPD");
Michael Hanl482f30d2015-09-25 12:39:46 +020024
25 JsonNode node = JsonUtils.readTree(b.toJSON());
26
Michael Hanldaf86602016-05-12 14:31:52 +020027 assertNotNull(node);
Michael Hanl8abaf9e2016-05-23 16:46:35 +020028 assertEquals("koral:doc", node.at("/collection/@type").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +020029 assertEquals("corpusSigle", node.at("/collection/key").asText());
Michael Hanlfb839b92015-09-19 21:32:34 +020030 }
31
Michael Hanl8abaf9e2016-05-23 16:46:35 +020032
Michael Hanlfb839b92015-09-19 21:32:34 +020033 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020034 public void testSimpleConjunction () {
Michael Hanl00b64e02016-05-24 20:24:27 +020035 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
36 b.with("corpusSigle=WPD & textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020037 JsonNode node = JsonUtils.readTree(b.toJSON());
38
Michael Hanldaf86602016-05-12 14:31:52 +020039 assertNotNull(node);
Michael Hanl8abaf9e2016-05-23 16:46:35 +020040 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
41 assertEquals("operation:and", node.at("/collection/operation").asText());
Michael Hanldaf86602016-05-12 14:31:52 +020042
Michael Hanl33829ec2016-05-28 17:03:38 +020043 assertEquals("corpusSigle", node.at("/collection/operands/0/key")
44 .asText());
Michael Hanl8abaf9e2016-05-23 16:46:35 +020045 assertEquals("textClass", node.at("/collection/operands/1/key")
46 .asText());
Michael Hanlfb839b92015-09-19 21:32:34 +020047 }
48
Michael Hanl8abaf9e2016-05-23 16:46:35 +020049
Michael Hanlfb839b92015-09-19 21:32:34 +020050 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020051 public void testSimpleDisjunction () {
Michael Hanl00b64e02016-05-24 20:24:27 +020052 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
53 b.with("corpusSigle=WPD | textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020054 JsonNode node = JsonUtils.readTree(b.toJSON());
55
Michael Hanl00b64e02016-05-24 20:24:27 +020056 assertNotNull(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020057 assert node.at("/collection/operation").asText().equals("operation:or");
58 assert node.at("/collection/operands/0/key").asText()
Michael Hanl00b64e02016-05-24 20:24:27 +020059 .equals("corpusSigle");
Michael Hanl482f30d2015-09-25 12:39:46 +020060 assert node.at("/collection/operands/1/key").asText()
61 .equals("textClass");
Michael Hanlfb839b92015-09-19 21:32:34 +020062 }
63
Michael Hanl8abaf9e2016-05-23 16:46:35 +020064
Michael Hanlfb839b92015-09-19 21:32:34 +020065 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020066 public void testComplexSubQuery () {
Michael Hanl00b64e02016-05-24 20:24:27 +020067 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
68 b.with("(corpusSigle=WPD) | (textClass=freizeit & corpusSigle=BRZ13)");
Michael Hanl482f30d2015-09-25 12:39:46 +020069 JsonNode node = JsonUtils.readTree(b.toJSON());
Michael Hanlfb839b92015-09-19 21:32:34 +020070
Michael Hanl00b64e02016-05-24 20:24:27 +020071 assertNotNull(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020072 assert node.at("/collection/operation").asText().equals("operation:or");
73 assert node.at("/collection/operands/0/key").asText()
Michael Hanl00b64e02016-05-24 20:24:27 +020074 .equals("corpusSigle");
Michael Hanl482f30d2015-09-25 12:39:46 +020075 assert node.at("/collection/operands/1/@type").asText()
76 .equals("koral:docGroup");
Michael Hanlfb839b92015-09-19 21:32:34 +020077
Michael Hanlfb839b92015-09-19 21:32:34 +020078 }
79
Michael Hanl8abaf9e2016-05-23 16:46:35 +020080
Michael Hanlfb839b92015-09-19 21:32:34 +020081 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020082 public void testAddResourceQueryAfter () {
Michael Hanl00b64e02016-05-24 20:24:27 +020083 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
84 b.with("(textClass=politik & title=\"random title\") | textClass=wissenschaft");
Michael Hanlfb839b92015-09-19 21:32:34 +020085
Michael Hanl00b64e02016-05-24 20:24:27 +020086 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
Michael Hanlfb839b92015-09-19 21:32:34 +020087 c.setBaseQuery(b.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +020088 c.with("corpusSigle=WPD");
Michael Hanlfb839b92015-09-19 21:32:34 +020089
90 JsonNode node = JsonUtils.readTree(c.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +020091 assertNotNull(node);
Michael Hanlcedf7212016-05-28 10:43:09 +020092 assertEquals("koral:doc", node.at("/collection/operands/1/@type")
Michael Hanl00b64e02016-05-24 20:24:27 +020093 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +020094 assertEquals("koral:docGroup", node.at("/collection/operands/0/@type")
95 .asText());
Michael Hanl00b64e02016-05-24 20:24:27 +020096 assertEquals(2, node.at("/collection/operands").size());
Michael Hanlcedf7212016-05-28 10:43:09 +020097 assertEquals(2, node.at("/collection/operands/0/operands").size());
Michael Hanl33829ec2016-05-28 17:03:38 +020098 assertEquals(2, node.at("/collection/operands/0/operands/0/operands")
99 .size());
Michael Hanlcedf7212016-05-28 10:43:09 +0200100
101 assertEquals("operation:and", node.at("/collection/operation").asText());
Michael Hanl33829ec2016-05-28 17:03:38 +0200102 assertEquals("operation:or", node
103 .at("/collection/operands/0/operation").asText());
104 assertEquals("operation:and",
105 node.at("/collection/operands/0/operands/0/operation").asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200106 assertEquals("WPD", node.at("/collection/operands/1/value").asText());
Michael Hanlfb839b92015-09-19 21:32:34 +0200107 }
108
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200109
Michael Hanl482f30d2015-09-25 12:39:46 +0200110 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200111 public void testAddComplexResourceQueryAfter () {
Michael Hanl00b64e02016-05-24 20:24:27 +0200112 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
113 b.with("(title=\"random title\") | (textClass=wissenschaft)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200114
Michael Hanl00b64e02016-05-24 20:24:27 +0200115 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
Michael Hanl482f30d2015-09-25 12:39:46 +0200116 c.setBaseQuery(b.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +0200117 c.with("(corpusSigle=BRZ13 | corpusSigle=AZPS)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200118
119 JsonNode node = JsonUtils.readTree(c.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +0200120 assertNotNull(node);
121 assertEquals("koral:docGroup", node.at("/collection/operands/0/@type")
122 .asText());
123 assertEquals("koral:docGroup", node.at("/collection/operands/1/@type")
124 .asText());
125 assertEquals("BRZ13", node
Michael Hanlcedf7212016-05-28 10:43:09 +0200126 .at("/collection/operands/1/operands/0/value").asText());
127 assertEquals("AZPS", node.at("/collection/operands/1/operands/1/value")
Michael Hanl00b64e02016-05-24 20:24:27 +0200128 .asText());
129 assertEquals("random title",
Michael Hanlcedf7212016-05-28 10:43:09 +0200130 node.at("/collection/operands/0/operands/0/value").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +0200131 assertEquals("wissenschaft",
Michael Hanlcedf7212016-05-28 10:43:09 +0200132 node.at("/collection/operands/0/operands/1/value").asText());
Michael Hanl482f30d2015-09-25 12:39:46 +0200133 }
134
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200135
Michael Hanl19390652016-01-16 11:01:24 +0100136 @Test
Michael Hanl00b64e02016-05-24 20:24:27 +0200137 public void testBuildQuery () {
138 String coll = "corpusSigle=WPD";
Michael Hanl19390652016-01-16 11:01:24 +0100139 String query = "[base=Haus]";
Michael Hanl00b64e02016-05-24 20:24:27 +0200140 QuerySerializer check = new QuerySerializer();
141 check.setQuery(query, "poliqarp");
142 check.setCollection(coll);
Michael Hanl19390652016-01-16 11:01:24 +0100143
Michael Hanl00b64e02016-05-24 20:24:27 +0200144 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
145 b.setBaseQuery(check.toJSON());
146 b.with("textClass=freizeit");
147
Michael Hanl9a242162016-06-03 11:18:06 +0200148 JsonNode res = (JsonNode) b.rebaseCollection();
Michael Hanl00b64e02016-05-24 20:24:27 +0200149 assertNotNull(res);
150 assertEquals("koral:docGroup", res.at("/collection/@type").asText());
151 assertEquals("operation:and", res.at("/collection/operation").asText());
152 assertEquals("koral:doc", res.at("/collection/operands/0/@type")
153 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200154 assertEquals("freizeit", res.at("/collection/operands/1/value")
Michael Hanl00b64e02016-05-24 20:24:27 +0200155 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200156 assertEquals("textClass", res.at("/collection/operands/1/key").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +0200157
158 assertEquals("koral:doc", res.at("/collection/operands/1/@type")
159 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200160 assertEquals("WPD", res.at("/collection/operands/0/value").asText());
Michael Hanl33829ec2016-05-28 17:03:38 +0200161 assertEquals("corpusSigle", res.at("/collection/operands/0/key")
162 .asText());
Michael Hanl00b64e02016-05-24 20:24:27 +0200163
164 // check also that query is still there
165 assertEquals("koral:token", res.at("/query/@type").asText());
166 assertEquals("koral:term", res.at("/query/wrap/@type").asText());
167 assertEquals("Haus", res.at("/query/wrap/key").asText());
168 assertEquals("lemma", res.at("/query/wrap/layer").asText());
Michael Hanl19390652016-01-16 11:01:24 +0100169 }
170
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200171
Michael Hanl19390652016-01-16 11:01:24 +0100172 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200173 public void testBaseQueryBuild () {
Michael Hanl00b64e02016-05-24 20:24:27 +0200174 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
175 b.with("(corpusSigle=ADF) | (textClass=freizeit & corpusSigle=WPD)");
176
177 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
178 c.setBaseQuery(b.toJSON());
179
180 c.with("corpusSigle=BRZ13");
Michael Hanl9a242162016-06-03 11:18:06 +0200181 JsonNode base = (JsonNode) c.rebaseCollection();
Michael Hanl00b64e02016-05-24 20:24:27 +0200182 assertNotNull(base);
183 assertEquals(base.at("/collection/@type").asText(), "koral:docGroup");
Michael Hanl00b64e02016-05-24 20:24:27 +0200184 assertEquals(base.at("/collection/operands/1/@type").asText(),
Michael Hanlcedf7212016-05-28 10:43:09 +0200185 "koral:doc");
186 assertEquals(base.at("/collection/operands/1/value").asText(), "BRZ13");
187 assertEquals(base.at("/collection/operands/0/@type").asText(),
Michael Hanl00b64e02016-05-24 20:24:27 +0200188 "koral:docGroup");
Michael Hanlcedf7212016-05-28 10:43:09 +0200189 assertEquals(base.at("/collection/operands/0/operands").size(), 2);
Michael Hanl00b64e02016-05-24 20:24:27 +0200190 }
191
Michael Hanl33829ec2016-05-28 17:03:38 +0200192
Michael Hanl00b64e02016-05-24 20:24:27 +0200193 @Test
Michael Hanl33829ec2016-05-28 17:03:38 +0200194 public void testNodeMergeWithBase () {
Michael Hanl00b64e02016-05-24 20:24:27 +0200195 String coll = "corpusSigle=WPD";
196 String query = "[base=Haus]";
197 QuerySerializer check = new QuerySerializer();
198 check.setQuery(query, "poliqarp");
199 check.setCollection(coll);
200
201 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
202 b.setBaseQuery(check.toJSON());
203
204 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
205 test.with("textClass=wissenschaft | textClass=politik");
Michael Hanl9a242162016-06-03 11:18:06 +0200206 JsonNode node = (JsonNode) test.rebaseCollection();
Michael Hanl00b64e02016-05-24 20:24:27 +0200207 node = b.mergeWith(node);
208 assertNotNull(node);
209 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
210 assertEquals("operation:and", node.at("/collection/operation").asText());
211 assertEquals(2, node.at("/collection/operands").size());
212 }
213
214
215 @Test
Michael Hanl9a242162016-06-03 11:18:06 +0200216 public void testNodeMergeWithoutBase () {
217 String query = "[base=Haus]";
218 QuerySerializer check = new QuerySerializer();
219 check.setQuery(query, "poliqarp");
220
221 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
222 b.setBaseQuery(check.toJSON());
223
224 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
225 test.with("corpusSigle=WPD");
226 String json = test.toJSON();
227 System.out.println(json);
228 //JsonNode node = (JsonNode) test.rebaseCollection(null);
229 //node = b.mergeWith(node);
230 //assertNotNull(node);
231 //assertEquals("koral:doc", node.at("/collection/@type").asText());
232 //assertEquals("corpusSigle", node.at("/collection/key").asText());
233 }
234
235
236 @Test
237 public void testNodeMergeWithoutBaseWrongOperator () {
238 String query = "[base=Haus]";
239 QuerySerializer check = new QuerySerializer();
240 check.setQuery(query, "poliqarp");
241
242 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
243 b.setBaseQuery(check.toJSON());
244
245 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
246 // operator is not supposed to be here!
247 test.and().with("corpusSigle=WPD");
248 String json = test.toJSON();
249 System.out.println(json);
250 //JsonNode node = (JsonNode) test.rebaseCollection(null);
251 //node = b.mergeWith(node);
252 //assertNotNull(node);
253 //assertEquals("koral:doc", node.at("/collection/@type").asText());
254 //assertEquals("corpusSigle", node.at("/collection/key").asText());
255 }
256
257
258 @Test
Michael Hanl00b64e02016-05-24 20:24:27 +0200259 public void testStoredCollectionBaseQueryBuild () {
Michael Hanl19390652016-01-16 11:01:24 +0100260
261 }
262
Michael Hanl33829ec2016-05-28 17:03:38 +0200263
Michael Hanlcedf7212016-05-28 10:43:09 +0200264 @Test
Michael Hanl33829ec2016-05-28 17:03:38 +0200265 public void testAddOROperator () {
Michael Hanlcedf7212016-05-28 10:43:09 +0200266 String coll = "corpusSigle=WPD";
267 String query = "[base=Haus]";
268 QuerySerializer check = new QuerySerializer();
269 check.setQuery(query, "poliqarp");
270 check.setCollection(coll);
271
272 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
273 test.setBaseQuery(check.toJSON());
274 test.or().with("textClass=wissenschaft | textClass=politik");
Michael Hanl9a242162016-06-03 11:18:06 +0200275 JsonNode node = (JsonNode) test.rebaseCollection();
Michael Hanlcedf7212016-05-28 10:43:09 +0200276 assertNotNull(node);
277 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
278 assertEquals("operation:or", node.at("/collection/operation").asText());
279 assertEquals(2, node.at("/collection/operands/1/operands").size());
280 }
281
Michael Hanl33829ec2016-05-28 17:03:38 +0200282
Michael Hanlcedf7212016-05-28 10:43:09 +0200283 @Test
Michael Hanl33829ec2016-05-28 17:03:38 +0200284 public void testAddANDOperator () {
Michael Hanlcedf7212016-05-28 10:43:09 +0200285 String coll = "corpusSigle=WPD";
286 String query = "[base=Haus]";
287 QuerySerializer check = new QuerySerializer();
288 check.setQuery(query, "poliqarp");
289 check.setCollection(coll);
290
291 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
292 test.setBaseQuery(check.toJSON());
293 test.and().with("textClass=wissenschaft | textClass=politik");
Michael Hanl9a242162016-06-03 11:18:06 +0200294 JsonNode node = (JsonNode) test.rebaseCollection();
Michael Hanlcedf7212016-05-28 10:43:09 +0200295 assertNotNull(node);
296 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
297 assertEquals("operation:and", node.at("/collection/operation").asText());
298 assertEquals(2, node.at("/collection/operands/1/operands").size());
299 }
300
Michael Hanl33829ec2016-05-28 17:03:38 +0200301
Michael Hanlcedf7212016-05-28 10:43:09 +0200302 @Test
Michael Hanl33829ec2016-05-28 17:03:38 +0200303 public void testAddDefaultOperator () {
Michael Hanlcedf7212016-05-28 10:43:09 +0200304 String coll = "corpusSigle=WPD";
305 String query = "[base=Haus]";
306 QuerySerializer check = new QuerySerializer();
307 check.setQuery(query, "poliqarp");
308 check.setCollection(coll);
309
310 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
311 test.setBaseQuery(check.toJSON());
312 test.with("textClass=wissenschaft | textClass=politik");
Michael Hanl9a242162016-06-03 11:18:06 +0200313 JsonNode node = (JsonNode) test.rebaseCollection();
Michael Hanlcedf7212016-05-28 10:43:09 +0200314 assertNotNull(node);
315 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
316 assertEquals("operation:and", node.at("/collection/operation").asText());
317 assertEquals(2, node.at("/collection/operands/1/operands").size());
318 }
319
Michael Hanl33829ec2016-05-28 17:03:38 +0200320
Michael Hanlcedf7212016-05-28 10:43:09 +0200321 @Test
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200322 public void testCollectionMergeWithFromResource () {
323 KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
324 builder.with("textClass=politik & corpusSigle=WPD");
325 KustvaktResource resource = new VirtualCollection();
326 resource.setName("collection_1");
327 String json = builder.toJSON();
328 resource.setFields(json);
Michael Hanlcedf7212016-05-28 10:43:09 +0200329
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200330 assertEquals(json, resource.getStringData());
331 builder = new KoralCollectionQueryBuilder();
332 builder.setBaseQuery(resource.getData());
333 builder.or().with("pubPlace=Mannheim");
Michael Hanl9a242162016-06-03 11:18:06 +0200334
335 System.out.println("query " + builder.toJSON());
336 // todo: assertions
337 }
338
339
340 @Test
341 public void testCollectionMergeWithFromResourceNoCollection () {
342 KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
343 builder.with("textClass=politik & corpusSigle=WPD");
344 KustvaktResource resource = new VirtualCollection();
345 resource.setName("collection_1");
346 String json = builder.toJSON();
347 resource.setFields(json);
348
349 assertEquals(json, resource.getStringData());
350 builder = new KoralCollectionQueryBuilder();
351 builder.setBaseQuery(resource.getData());
352 builder.or().with("pubPlace=Mannheim");
353
354 System.out.println("query " + builder.toJSON());
355 // todo: assertions
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200356 }
Michael Hanlcedf7212016-05-28 10:43:09 +0200357
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200358
359 @Test
360 public void testCollectionMergeFromQuerySerializer () {
361 QuerySerializer s = new QuerySerializer();
362 s.setQuery("[base=Haus]", "poliqarp");
363 KoralCollectionQueryBuilder total = new KoralCollectionQueryBuilder();
364 total.setBaseQuery(s.toJSON());
365
366
367 KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
368 builder.with("textClass=politik & corpusSigle=WPD");
369 KustvaktResource resource = new VirtualCollection();
370 resource.setName("collection_1");
371 String json = builder.toJSON();
372 resource.setFields(json);
373 // operator is irrelevant here
374 JsonNode node = total.or().mergeWith(resource.getData());
Michael Hanlcedf7212016-05-28 10:43:09 +0200375 assertNotNull(node);
376 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
377 assertEquals("operation:and", node.at("/collection/operation").asText());
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200378 assertEquals("textClass", node.at("/collection/operands/0/key")
379 .asText());
380 assertEquals("corpusSigle", node.at("/collection/operands/1/key")
381 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200382 }
383
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200384
385 @Test
386 public void testBaseCollectionNull () {
387 // base is missing collection segment
388 QuerySerializer s = new QuerySerializer();
389 s.setQuery("[base=Haus]", "poliqarp");
390
391 KoralCollectionQueryBuilder total = new KoralCollectionQueryBuilder();
392 total.setBaseQuery(s.toJSON());
393
394 KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
395 builder.with("textClass=politik & corpusSigle=WPD");
396 JsonNode node = total.and().mergeWith(
Michael Hanl9a242162016-06-03 11:18:06 +0200397 (JsonNode) builder.rebaseCollection());
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200398
399 assertNotNull(node);
400 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
401 assertEquals("operation:and", node.at("/collection/operation").asText());
402 assertEquals("koral:doc", node.at("/collection/operands/0/@type")
403 .asText());
404 assertEquals("koral:doc", node.at("/collection/operands/1/@type")
405 .asText());
406 assertEquals("textClass", node.at("/collection/operands/0/key")
407 .asText());
408 assertEquals("corpusSigle", node.at("/collection/operands/1/key")
409 .asText());
410 }
411
412
413 @Test
414 public void testMergeCollectionNull () {
415 // merge json is missing collection segment
416 QuerySerializer s = new QuerySerializer();
417 s.setQuery("[base=Haus]", "poliqarp");
418 s.setCollection("textClass=wissenschaft");
419
420 KoralCollectionQueryBuilder total = new KoralCollectionQueryBuilder();
421 total.setBaseQuery(s.toJSON());
422
423 KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder();
424 JsonNode node = total.and().mergeWith(
Michael Hanl9a242162016-06-03 11:18:06 +0200425 (JsonNode) builder.rebaseCollection());
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200426 assertNotNull(node);
427 assertEquals("koral:doc", node.at("/collection/@type").asText());
428 assertEquals("textClass", node.at("/collection/key").asText());
429 }
430
431
Michael Hanlfb839b92015-09-19 21:32:34 +0200432}