blob: 38a5ac47f4b3c42a44cd28125b01f1be19269e93 [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;
5import org.junit.Test;
6
Michael Hanldaf86602016-05-12 14:31:52 +02007import static org.junit.Assert.assertEquals;
Michael Hanl00b64e02016-05-24 20:24:27 +02008import static org.junit.Assert.assertNotEquals;
Michael Hanldaf86602016-05-12 14:31:52 +02009import static org.junit.Assert.assertNotNull;
10
Michael Hanlfb839b92015-09-19 21:32:34 +020011/**
12 * @author hanl
13 * @date 12/08/2015
14 */
15public class CollectionQueryBuilderTest {
16
17 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020018 public void testsimpleAdd () {
Michael Hanl00b64e02016-05-24 20:24:27 +020019 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
20 b.with("corpusSigle=WPD");
Michael Hanl482f30d2015-09-25 12:39:46 +020021
22 JsonNode node = JsonUtils.readTree(b.toJSON());
23
Michael Hanldaf86602016-05-12 14:31:52 +020024 assertNotNull(node);
Michael Hanl8abaf9e2016-05-23 16:46:35 +020025 assertEquals("koral:doc", node.at("/collection/@type").asText());
Michael Hanl00b64e02016-05-24 20:24:27 +020026 assertEquals("corpusSigle", node.at("/collection/key").asText());
Michael Hanlfb839b92015-09-19 21:32:34 +020027 }
28
Michael Hanl8abaf9e2016-05-23 16:46:35 +020029
Michael Hanlfb839b92015-09-19 21:32:34 +020030 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020031 public void testSimpleConjunction () {
Michael Hanl00b64e02016-05-24 20:24:27 +020032 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
33 b.with("corpusSigle=WPD & textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020034 JsonNode node = JsonUtils.readTree(b.toJSON());
35
Michael Hanldaf86602016-05-12 14:31:52 +020036 assertNotNull(node);
Michael Hanl8abaf9e2016-05-23 16:46:35 +020037 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
38 assertEquals("operation:and", node.at("/collection/operation").asText());
Michael Hanldaf86602016-05-12 14:31:52 +020039
Michael Hanl00b64e02016-05-24 20:24:27 +020040 assertEquals("corpusSigle", node.at("/collection/operands/0/key").asText());
Michael Hanl8abaf9e2016-05-23 16:46:35 +020041 assertEquals("textClass", node.at("/collection/operands/1/key")
42 .asText());
Michael Hanlfb839b92015-09-19 21:32:34 +020043 }
44
Michael Hanl8abaf9e2016-05-23 16:46:35 +020045
Michael Hanlfb839b92015-09-19 21:32:34 +020046 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020047 public void testSimpleDisjunction () {
Michael Hanl00b64e02016-05-24 20:24:27 +020048 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
49 b.with("corpusSigle=WPD | textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020050 JsonNode node = JsonUtils.readTree(b.toJSON());
51
Michael Hanl00b64e02016-05-24 20:24:27 +020052 assertNotNull(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020053 assert node.at("/collection/operation").asText().equals("operation:or");
54 assert node.at("/collection/operands/0/key").asText()
Michael Hanl00b64e02016-05-24 20:24:27 +020055 .equals("corpusSigle");
Michael Hanl482f30d2015-09-25 12:39:46 +020056 assert node.at("/collection/operands/1/key").asText()
57 .equals("textClass");
Michael Hanlfb839b92015-09-19 21:32:34 +020058 }
59
Michael Hanl8abaf9e2016-05-23 16:46:35 +020060
Michael Hanlfb839b92015-09-19 21:32:34 +020061 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020062 public void testComplexSubQuery () {
Michael Hanl00b64e02016-05-24 20:24:27 +020063 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
64 b.with("(corpusSigle=WPD) | (textClass=freizeit & corpusSigle=BRZ13)");
Michael Hanl482f30d2015-09-25 12:39:46 +020065 JsonNode node = JsonUtils.readTree(b.toJSON());
Michael Hanlfb839b92015-09-19 21:32:34 +020066
Michael Hanl00b64e02016-05-24 20:24:27 +020067 assertNotNull(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020068 assert node.at("/collection/operation").asText().equals("operation:or");
69 assert node.at("/collection/operands/0/key").asText()
Michael Hanl00b64e02016-05-24 20:24:27 +020070 .equals("corpusSigle");
Michael Hanl482f30d2015-09-25 12:39:46 +020071 assert node.at("/collection/operands/1/@type").asText()
72 .equals("koral:docGroup");
Michael Hanlfb839b92015-09-19 21:32:34 +020073
Michael Hanlfb839b92015-09-19 21:32:34 +020074 }
75
Michael Hanl8abaf9e2016-05-23 16:46:35 +020076
Michael Hanlfb839b92015-09-19 21:32:34 +020077 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020078 public void testAddResourceQueryAfter () {
Michael Hanl00b64e02016-05-24 20:24:27 +020079 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
80 b.with("(textClass=politik & title=\"random title\") | textClass=wissenschaft");
Michael Hanlfb839b92015-09-19 21:32:34 +020081
Michael Hanl00b64e02016-05-24 20:24:27 +020082 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
Michael Hanlfb839b92015-09-19 21:32:34 +020083 c.setBaseQuery(b.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +020084 c.with("corpusSigle=WPD");
Michael Hanlfb839b92015-09-19 21:32:34 +020085
86 JsonNode node = JsonUtils.readTree(c.toJSON());
87
Michael Hanl00b64e02016-05-24 20:24:27 +020088 assertNotNull(node);
89 assertEquals("koral:doc", node.at("/collection/operands/0/@type")
90 .asText());
91 assertEquals("WPD", node.at("/collection/operands/0/value").asText());
92 assertEquals(2, node.at("/collection/operands").size());
Michael Hanlfb839b92015-09-19 21:32:34 +020093 }
94
Michael Hanl8abaf9e2016-05-23 16:46:35 +020095
Michael Hanl482f30d2015-09-25 12:39:46 +020096 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020097 public void testAddComplexResourceQueryAfter () {
Michael Hanl00b64e02016-05-24 20:24:27 +020098 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
99 b.with("(title=\"random title\") | (textClass=wissenschaft)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200100
Michael Hanl00b64e02016-05-24 20:24:27 +0200101 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
Michael Hanl482f30d2015-09-25 12:39:46 +0200102 c.setBaseQuery(b.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +0200103 c.with("(corpusSigle=BRZ13 | corpusSigle=AZPS)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200104
105 JsonNode node = JsonUtils.readTree(c.toJSON());
Michael Hanl00b64e02016-05-24 20:24:27 +0200106 assertNotNull(node);
107 assertEquals("koral:docGroup", node.at("/collection/operands/0/@type")
108 .asText());
109 assertEquals("koral:docGroup", node.at("/collection/operands/1/@type")
110 .asText());
111 assertEquals("BRZ13", node
112 .at("/collection/operands/0/operands/0/value").asText());
113 assertEquals("AZPS", node.at("/collection/operands/0/operands/1/value")
114 .asText());
115 assertEquals("random title",
116 node.at("/collection/operands/1/operands/0/value").asText());
117 assertEquals("wissenschaft",
118 node.at("/collection/operands/1/operands/1/value").asText());
Michael Hanl482f30d2015-09-25 12:39:46 +0200119 }
120
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200121
Michael Hanl19390652016-01-16 11:01:24 +0100122 @Test
Michael Hanl00b64e02016-05-24 20:24:27 +0200123 public void testBuildQuery () {
124 String coll = "corpusSigle=WPD";
Michael Hanl19390652016-01-16 11:01:24 +0100125 String query = "[base=Haus]";
Michael Hanl00b64e02016-05-24 20:24:27 +0200126 QuerySerializer check = new QuerySerializer();
127 check.setQuery(query, "poliqarp");
128 check.setCollection(coll);
Michael Hanl19390652016-01-16 11:01:24 +0100129
Michael Hanl00b64e02016-05-24 20:24:27 +0200130 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
131 b.setBaseQuery(check.toJSON());
132 b.with("textClass=freizeit");
133
134 JsonNode res = (JsonNode) b.rebaseCollection();
135 assertNotNull(res);
136 assertEquals("koral:docGroup", res.at("/collection/@type").asText());
137 assertEquals("operation:and", res.at("/collection/operation").asText());
138 assertEquals("koral:doc", res.at("/collection/operands/0/@type")
139 .asText());
140 assertEquals("freizeit", res.at("/collection/operands/0/value")
141 .asText());
142 assertEquals("textClass", res.at("/collection/operands/0/key").asText());
143
144 assertEquals("koral:doc", res.at("/collection/operands/1/@type")
145 .asText());
146 assertEquals("WPD", res.at("/collection/operands/1/value").asText());
147 assertEquals("corpusSigle", res.at("/collection/operands/1/key").asText());
148
149 // check also that query is still there
150 assertEquals("koral:token", res.at("/query/@type").asText());
151 assertEquals("koral:term", res.at("/query/wrap/@type").asText());
152 assertEquals("Haus", res.at("/query/wrap/key").asText());
153 assertEquals("lemma", res.at("/query/wrap/layer").asText());
Michael Hanl19390652016-01-16 11:01:24 +0100154 }
155
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200156
Michael Hanl19390652016-01-16 11:01:24 +0100157 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200158 public void testBaseQueryBuild () {
Michael Hanl00b64e02016-05-24 20:24:27 +0200159 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
160 b.with("(corpusSigle=ADF) | (textClass=freizeit & corpusSigle=WPD)");
161
162 KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder();
163 c.setBaseQuery(b.toJSON());
164
165 c.with("corpusSigle=BRZ13");
166 JsonNode base = (JsonNode) c.rebaseCollection();
167 assertNotNull(base);
168 assertEquals(base.at("/collection/@type").asText(), "koral:docGroup");
169 assertEquals(base.at("/collection/operands/0/@type").asText(),
170 "koral:doc");
171 assertEquals(base.at("/collection/operands/0/value").asText(), "BRZ13");
172 assertEquals(base.at("/collection/operands/1/@type").asText(),
173 "koral:docGroup");
174 assertEquals(base.at("/collection/operands/1/operands").size(), 2);
175 }
176
177 @Test
178 public void testNodeMergeWithBase() {
179 String coll = "corpusSigle=WPD";
180 String query = "[base=Haus]";
181 QuerySerializer check = new QuerySerializer();
182 check.setQuery(query, "poliqarp");
183 check.setCollection(coll);
184
185 KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder();
186 b.setBaseQuery(check.toJSON());
187
188 KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder();
189 test.with("textClass=wissenschaft | textClass=politik");
190 JsonNode node = (JsonNode) test.rebaseCollection();
191 node = b.mergeWith(node);
192 assertNotNull(node);
193 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
194 assertEquals("operation:and", node.at("/collection/operation").asText());
195 assertEquals(2, node.at("/collection/operands").size());
196 }
197
198
199 @Test
200 public void testStoredCollectionBaseQueryBuild () {
Michael Hanl19390652016-01-16 11:01:24 +0100201
202 }
203
Michael Hanlfb839b92015-09-19 21:32:34 +0200204}