blob: f89596214b4b14aa2e640f350dcc415656a0b858 [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 Hanl33829ec2016-05-28 17:03:38 +020041 assertEquals("corpusSigle", node.at("/collection/operands/0/key")
42 .asText());
Michael Hanl8abaf9e2016-05-23 16:46:35 +020043 assertEquals("textClass", node.at("/collection/operands/1/key")
44 .asText());
Michael Hanlfb839b92015-09-19 21:32:34 +020045 }
46
Michael Hanl8abaf9e2016-05-23 16:46:35 +020047
Michael Hanlfb839b92015-09-19 21:32:34 +020048 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020049 public void testSimpleDisjunction () {
Michael Hanl00b64e02016-05-24 20:24:27 +020050 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
51 b.with("corpusSigle=WPD | textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020052 JsonNode node = JsonUtils.readTree(b.toJSON());
53
Michael Hanl00b64e02016-05-24 20:24:27 +020054 assertNotNull(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020055 assert node.at("/collection/operation").asText().equals("operation:or");
56 assert node.at("/collection/operands/0/key").asText()
Michael Hanl00b64e02016-05-24 20:24:27 +020057 .equals("corpusSigle");
Michael Hanl482f30d2015-09-25 12:39:46 +020058 assert node.at("/collection/operands/1/key").asText()
59 .equals("textClass");
Michael Hanlfb839b92015-09-19 21:32:34 +020060 }
61
Michael Hanl8abaf9e2016-05-23 16:46:35 +020062
Michael Hanlfb839b92015-09-19 21:32:34 +020063 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020064 public void testComplexSubQuery () {
Michael Hanl00b64e02016-05-24 20:24:27 +020065 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
66 b.with("(corpusSigle=WPD) | (textClass=freizeit & corpusSigle=BRZ13)");
Michael Hanl482f30d2015-09-25 12:39:46 +020067 JsonNode node = JsonUtils.readTree(b.toJSON());
Michael Hanlfb839b92015-09-19 21:32:34 +020068
Michael Hanl00b64e02016-05-24 20:24:27 +020069 assertNotNull(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020070 assert node.at("/collection/operation").asText().equals("operation:or");
71 assert node.at("/collection/operands/0/key").asText()
Michael Hanl00b64e02016-05-24 20:24:27 +020072 .equals("corpusSigle");
Michael Hanl482f30d2015-09-25 12:39:46 +020073 assert node.at("/collection/operands/1/@type").asText()
74 .equals("koral:docGroup");
Michael Hanlfb839b92015-09-19 21:32:34 +020075
Michael Hanlfb839b92015-09-19 21:32:34 +020076 }
77
Michael Hanl8abaf9e2016-05-23 16:46:35 +020078
Michael Hanlfb839b92015-09-19 21:32:34 +020079 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020080 public void testAddResourceQueryAfter () {
Michael Hanl00b64e02016-05-24 20:24:27 +020081 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
82 b.with("(textClass=politik & title=\"random title\") | textClass=wissenschaft");
Michael Hanlfb839b92015-09-19 21:32:34 +020083
Michael Hanl00b64e02016-05-24 20:24:27 +020084 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
Michael Hanlfb839b92015-09-19 21:32:34 +020085 c.setBaseQuery(b.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +020086 c.with("corpusSigle=WPD");
Michael Hanlfb839b92015-09-19 21:32:34 +020087
88 JsonNode node = JsonUtils.readTree(c.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +020089 assertNotNull(node);
Michael Hanlcedf7212016-05-28 10:43:09 +020090 assertEquals("koral:doc", node.at("/collection/operands/1/@type")
Michael Hanl00b64e02016-05-24 20:24:27 +020091 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +020092 assertEquals("koral:docGroup", node.at("/collection/operands/0/@type")
93 .asText());
Michael Hanl00b64e02016-05-24 20:24:27 +020094 assertEquals(2, node.at("/collection/operands").size());
Michael Hanlcedf7212016-05-28 10:43:09 +020095 assertEquals(2, node.at("/collection/operands/0/operands").size());
Michael Hanl33829ec2016-05-28 17:03:38 +020096 assertEquals(2, node.at("/collection/operands/0/operands/0/operands")
97 .size());
Michael Hanlcedf7212016-05-28 10:43:09 +020098
99 assertEquals("operation:and", node.at("/collection/operation").asText());
Michael Hanl33829ec2016-05-28 17:03:38 +0200100 assertEquals("operation:or", node
101 .at("/collection/operands/0/operation").asText());
102 assertEquals("operation:and",
103 node.at("/collection/operands/0/operands/0/operation").asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200104 assertEquals("WPD", node.at("/collection/operands/1/value").asText());
Michael Hanlfb839b92015-09-19 21:32:34 +0200105 }
106
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200107
Michael Hanl482f30d2015-09-25 12:39:46 +0200108 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200109 public void testAddComplexResourceQueryAfter () {
Michael Hanl00b64e02016-05-24 20:24:27 +0200110 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
111 b.with("(title=\"random title\") | (textClass=wissenschaft)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200112
Michael Hanl00b64e02016-05-24 20:24:27 +0200113 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
Michael Hanl482f30d2015-09-25 12:39:46 +0200114 c.setBaseQuery(b.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +0200115 c.with("(corpusSigle=BRZ13 | corpusSigle=AZPS)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200116
117 JsonNode node = JsonUtils.readTree(c.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +0200118 assertNotNull(node);
119 assertEquals("koral:docGroup", node.at("/collection/operands/0/@type")
120 .asText());
121 assertEquals("koral:docGroup", node.at("/collection/operands/1/@type")
122 .asText());
123 assertEquals("BRZ13", node
Michael Hanlcedf7212016-05-28 10:43:09 +0200124 .at("/collection/operands/1/operands/0/value").asText());
125 assertEquals("AZPS", node.at("/collection/operands/1/operands/1/value")
Michael Hanl00b64e02016-05-24 20:24:27 +0200126 .asText());
127 assertEquals("random title",
Michael Hanlcedf7212016-05-28 10:43:09 +0200128 node.at("/collection/operands/0/operands/0/value").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +0200129 assertEquals("wissenschaft",
Michael Hanlcedf7212016-05-28 10:43:09 +0200130 node.at("/collection/operands/0/operands/1/value").asText());
Michael Hanl482f30d2015-09-25 12:39:46 +0200131 }
132
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200133
Michael Hanl19390652016-01-16 11:01:24 +0100134 @Test
Michael Hanl00b64e02016-05-24 20:24:27 +0200135 public void testBuildQuery () {
136 String coll = "corpusSigle=WPD";
Michael Hanl19390652016-01-16 11:01:24 +0100137 String query = "[base=Haus]";
Michael Hanl00b64e02016-05-24 20:24:27 +0200138 QuerySerializer check = new QuerySerializer();
139 check.setQuery(query, "poliqarp");
140 check.setCollection(coll);
Michael Hanl19390652016-01-16 11:01:24 +0100141
Michael Hanl00b64e02016-05-24 20:24:27 +0200142 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
143 b.setBaseQuery(check.toJSON());
144 b.with("textClass=freizeit");
145
146 JsonNode res = (JsonNode) b.rebaseCollection();
147 assertNotNull(res);
148 assertEquals("koral:docGroup", res.at("/collection/@type").asText());
149 assertEquals("operation:and", res.at("/collection/operation").asText());
150 assertEquals("koral:doc", res.at("/collection/operands/0/@type")
151 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200152 assertEquals("freizeit", res.at("/collection/operands/1/value")
Michael Hanl00b64e02016-05-24 20:24:27 +0200153 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200154 assertEquals("textClass", res.at("/collection/operands/1/key").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +0200155
156 assertEquals("koral:doc", res.at("/collection/operands/1/@type")
157 .asText());
Michael Hanlcedf7212016-05-28 10:43:09 +0200158 assertEquals("WPD", res.at("/collection/operands/0/value").asText());
Michael Hanl33829ec2016-05-28 17:03:38 +0200159 assertEquals("corpusSigle", res.at("/collection/operands/0/key")
160 .asText());
Michael Hanl00b64e02016-05-24 20:24:27 +0200161
162 // check also that query is still there
163 assertEquals("koral:token", res.at("/query/@type").asText());
164 assertEquals("koral:term", res.at("/query/wrap/@type").asText());
165 assertEquals("Haus", res.at("/query/wrap/key").asText());
166 assertEquals("lemma", res.at("/query/wrap/layer").asText());
Michael Hanl19390652016-01-16 11:01:24 +0100167 }
168
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200169
Michael Hanl19390652016-01-16 11:01:24 +0100170 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200171 public void testBaseQueryBuild () {
Michael Hanl00b64e02016-05-24 20:24:27 +0200172 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
173 b.with("(corpusSigle=ADF) | (textClass=freizeit & corpusSigle=WPD)");
174
175 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
176 c.setBaseQuery(b.toJSON());
177
178 c.with("corpusSigle=BRZ13");
179 JsonNode base = (JsonNode) c.rebaseCollection();
180 assertNotNull(base);
181 assertEquals(base.at("/collection/@type").asText(), "koral:docGroup");
Michael Hanl00b64e02016-05-24 20:24:27 +0200182 assertEquals(base.at("/collection/operands/1/@type").asText(),
Michael Hanlcedf7212016-05-28 10:43:09 +0200183 "koral:doc");
184 assertEquals(base.at("/collection/operands/1/value").asText(), "BRZ13");
185 assertEquals(base.at("/collection/operands/0/@type").asText(),
Michael Hanl00b64e02016-05-24 20:24:27 +0200186 "koral:docGroup");
Michael Hanlcedf7212016-05-28 10:43:09 +0200187 assertEquals(base.at("/collection/operands/0/operands").size(), 2);
Michael Hanl00b64e02016-05-24 20:24:27 +0200188 }
189
Michael Hanl33829ec2016-05-28 17:03:38 +0200190
Michael Hanl00b64e02016-05-24 20:24:27 +0200191 @Test
Michael Hanl33829ec2016-05-28 17:03:38 +0200192 public void testNodeMergeWithBase () {
Michael Hanl00b64e02016-05-24 20:24:27 +0200193 String coll = "corpusSigle=WPD";
194 String query = "[base=Haus]";
195 QuerySerializer check = new QuerySerializer();
196 check.setQuery(query, "poliqarp");
197 check.setCollection(coll);
198
199 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
200 b.setBaseQuery(check.toJSON());
201
202 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
203 test.with("textClass=wissenschaft | textClass=politik");
204 JsonNode node = (JsonNode) test.rebaseCollection();
205 node = b.mergeWith(node);
206 assertNotNull(node);
207 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
208 assertEquals("operation:and", node.at("/collection/operation").asText());
209 assertEquals(2, node.at("/collection/operands").size());
210 }
211
212
213 @Test
214 public void testStoredCollectionBaseQueryBuild () {
Michael Hanl19390652016-01-16 11:01:24 +0100215
216 }
217
Michael Hanl33829ec2016-05-28 17:03:38 +0200218
Michael Hanlcedf7212016-05-28 10:43:09 +0200219 @Test
Michael Hanl33829ec2016-05-28 17:03:38 +0200220 public void testAddOROperator () {
Michael Hanlcedf7212016-05-28 10:43:09 +0200221 String coll = "corpusSigle=WPD";
222 String query = "[base=Haus]";
223 QuerySerializer check = new QuerySerializer();
224 check.setQuery(query, "poliqarp");
225 check.setCollection(coll);
226
227 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
228 test.setBaseQuery(check.toJSON());
229 test.or().with("textClass=wissenschaft | textClass=politik");
230 JsonNode node = (JsonNode) test.rebaseCollection();
231 assertNotNull(node);
232 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
233 assertEquals("operation:or", node.at("/collection/operation").asText());
234 assertEquals(2, node.at("/collection/operands/1/operands").size());
235 }
236
Michael Hanl33829ec2016-05-28 17:03:38 +0200237
Michael Hanlcedf7212016-05-28 10:43:09 +0200238 @Test
Michael Hanl33829ec2016-05-28 17:03:38 +0200239 public void testAddANDOperator () {
Michael Hanlcedf7212016-05-28 10:43:09 +0200240 String coll = "corpusSigle=WPD";
241 String query = "[base=Haus]";
242 QuerySerializer check = new QuerySerializer();
243 check.setQuery(query, "poliqarp");
244 check.setCollection(coll);
245
246 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
247 test.setBaseQuery(check.toJSON());
248 test.and().with("textClass=wissenschaft | textClass=politik");
249 JsonNode node = (JsonNode) test.rebaseCollection();
250 assertNotNull(node);
251 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
252 assertEquals("operation:and", node.at("/collection/operation").asText());
253 assertEquals(2, node.at("/collection/operands/1/operands").size());
254 }
255
Michael Hanl33829ec2016-05-28 17:03:38 +0200256
Michael Hanlcedf7212016-05-28 10:43:09 +0200257 @Test
Michael Hanl33829ec2016-05-28 17:03:38 +0200258 public void testAddDefaultOperator () {
Michael Hanlcedf7212016-05-28 10:43:09 +0200259 String coll = "corpusSigle=WPD";
260 String query = "[base=Haus]";
261 QuerySerializer check = new QuerySerializer();
262 check.setQuery(query, "poliqarp");
263 check.setCollection(coll);
264
265 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
266 test.setBaseQuery(check.toJSON());
267 test.with("textClass=wissenschaft | textClass=politik");
268 JsonNode node = (JsonNode) test.rebaseCollection();
269 assertNotNull(node);
270 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
271 assertEquals("operation:and", node.at("/collection/operation").asText());
272 assertEquals(2, node.at("/collection/operands/1/operands").size());
273 }
274
Michael Hanl33829ec2016-05-28 17:03:38 +0200275
Michael Hanlcedf7212016-05-28 10:43:09 +0200276 @Test
277 @Ignore
Michael Hanl33829ec2016-05-28 17:03:38 +0200278 public void testMergeOperator () {
Michael Hanlcedf7212016-05-28 10:43:09 +0200279 String coll = "corpusSigle=WPD";
280 String query = "[base=Haus]";
281 QuerySerializer check = new QuerySerializer();
282 check.setQuery(query, "poliqarp");
283 check.setCollection(coll);
284
285 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
286 b.setBaseQuery(check.toJSON());
287
288 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
289 test.with("textClass=wissenschaft | textClass=politik");
290 JsonNode node = (JsonNode) test.rebaseCollection();
291 node = b.mergeWith(node);
292 assertNotNull(node);
293 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
294 assertEquals("operation:and", node.at("/collection/operation").asText());
295 assertEquals(2, node.at("/collection/operands").size());
296 }
297
Michael Hanlfb839b92015-09-19 21:32:34 +0200298}