Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 1 | import com.fasterxml.jackson.databind.JsonNode; |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 2 | import de.ids_mannheim.korap.query.serialize.QuerySerializer; |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.resources.KustvaktResource; |
| 4 | import de.ids_mannheim.korap.resources.VirtualCollection; |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 5 | import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder; |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 6 | import de.ids_mannheim.korap.utils.JsonUtils; |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 7 | import org.junit.Ignore; |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 8 | import org.junit.Test; |
| 9 | |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 10 | import static org.junit.Assert.assertEquals; |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 11 | import static org.junit.Assert.assertNotEquals; |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 12 | import static org.junit.Assert.assertNotNull; |
| 13 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 14 | /** |
| 15 | * @author hanl |
| 16 | * @date 12/08/2015 |
| 17 | */ |
| 18 | public class CollectionQueryBuilderTest { |
| 19 | |
| 20 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 21 | public void testsimpleAdd () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 22 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 23 | b.with("corpusSigle=WPD"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 24 | |
| 25 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 26 | |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 27 | assertNotNull(node); |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 28 | assertEquals("koral:doc", node.at("/collection/@type").asText()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 29 | assertEquals("corpusSigle", node.at("/collection/key").asText()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 32 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 33 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 34 | public void testSimpleConjunction () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 35 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 36 | b.with("corpusSigle=WPD & textClass=freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 37 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 38 | |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 39 | assertNotNull(node); |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 40 | assertEquals("koral:docGroup", node.at("/collection/@type").asText()); |
| 41 | assertEquals("operation:and", node.at("/collection/operation").asText()); |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 42 | |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 43 | assertEquals("corpusSigle", node.at("/collection/operands/0/key") |
| 44 | .asText()); |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 45 | assertEquals("textClass", node.at("/collection/operands/1/key") |
| 46 | .asText()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 49 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 50 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 51 | public void testSimpleDisjunction () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 52 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 53 | b.with("corpusSigle=WPD | textClass=freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 54 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 55 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 56 | assertNotNull(node); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 57 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 58 | assert node.at("/collection/operands/0/key").asText() |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 59 | .equals("corpusSigle"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 60 | assert node.at("/collection/operands/1/key").asText() |
| 61 | .equals("textClass"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 64 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 65 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 66 | public void testComplexSubQuery () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 67 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 68 | b.with("(corpusSigle=WPD) | (textClass=freizeit & corpusSigle=BRZ13)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 69 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 70 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 71 | assertNotNull(node); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 72 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 73 | assert node.at("/collection/operands/0/key").asText() |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 74 | .equals("corpusSigle"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 75 | assert node.at("/collection/operands/1/@type").asText() |
| 76 | .equals("koral:docGroup"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 77 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 80 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 81 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 82 | public void testAddResourceQueryAfter () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 83 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 84 | b.with("(textClass=politik & title=\"random title\") | textClass=wissenschaft"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 85 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 86 | KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder(); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 87 | c.setBaseQuery(b.toJSON()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 88 | c.with("corpusSigle=WPD"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 89 | |
| 90 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 91 | assertNotNull(node); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 92 | assertEquals("koral:doc", node.at("/collection/operands/1/@type") |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 93 | .asText()); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 94 | assertEquals("koral:docGroup", node.at("/collection/operands/0/@type") |
| 95 | .asText()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 96 | assertEquals(2, node.at("/collection/operands").size()); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 97 | assertEquals(2, node.at("/collection/operands/0/operands").size()); |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 98 | assertEquals(2, node.at("/collection/operands/0/operands/0/operands") |
| 99 | .size()); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 100 | |
| 101 | assertEquals("operation:and", node.at("/collection/operation").asText()); |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 102 | 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 Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 106 | assertEquals("WPD", node.at("/collection/operands/1/value").asText()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 109 | |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 110 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 111 | public void testAddComplexResourceQueryAfter () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 112 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 113 | b.with("(title=\"random title\") | (textClass=wissenschaft)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 114 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 115 | KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder(); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 116 | c.setBaseQuery(b.toJSON()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 117 | c.with("(corpusSigle=BRZ13 | corpusSigle=AZPS)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 118 | |
| 119 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 120 | 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 Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 126 | .at("/collection/operands/1/operands/0/value").asText()); |
| 127 | assertEquals("AZPS", node.at("/collection/operands/1/operands/1/value") |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 128 | .asText()); |
| 129 | assertEquals("random title", |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 130 | node.at("/collection/operands/0/operands/0/value").asText()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 131 | assertEquals("wissenschaft", |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 132 | node.at("/collection/operands/0/operands/1/value").asText()); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 135 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 136 | @Test |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 137 | public void testBuildQuery () { |
| 138 | String coll = "corpusSigle=WPD"; |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 139 | String query = "[base=Haus]"; |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 140 | QuerySerializer check = new QuerySerializer(); |
| 141 | check.setQuery(query, "poliqarp"); |
| 142 | check.setCollection(coll); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 143 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 144 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 145 | b.setBaseQuery(check.toJSON()); |
| 146 | b.with("textClass=freizeit"); |
| 147 | |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 148 | JsonNode res = (JsonNode) b.rebaseCollection(null); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 149 | 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 Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 154 | assertEquals("freizeit", res.at("/collection/operands/1/value") |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 155 | .asText()); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 156 | assertEquals("textClass", res.at("/collection/operands/1/key").asText()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 157 | |
| 158 | assertEquals("koral:doc", res.at("/collection/operands/1/@type") |
| 159 | .asText()); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 160 | assertEquals("WPD", res.at("/collection/operands/0/value").asText()); |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 161 | assertEquals("corpusSigle", res.at("/collection/operands/0/key") |
| 162 | .asText()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 163 | |
| 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 Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 171 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 172 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 173 | public void testBaseQueryBuild () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 174 | 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 Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 181 | JsonNode base = (JsonNode) c.rebaseCollection(null); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 182 | assertNotNull(base); |
| 183 | assertEquals(base.at("/collection/@type").asText(), "koral:docGroup"); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 184 | assertEquals(base.at("/collection/operands/1/@type").asText(), |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 185 | "koral:doc"); |
| 186 | assertEquals(base.at("/collection/operands/1/value").asText(), "BRZ13"); |
| 187 | assertEquals(base.at("/collection/operands/0/@type").asText(), |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 188 | "koral:docGroup"); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 189 | assertEquals(base.at("/collection/operands/0/operands").size(), 2); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 192 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 193 | @Test |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 194 | public void testNodeMergeWithBase () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 195 | 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 Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 206 | JsonNode node = (JsonNode) test.rebaseCollection(null); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 207 | 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 |
| 216 | public void testStoredCollectionBaseQueryBuild () { |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 217 | |
| 218 | } |
| 219 | |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 220 | |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 221 | @Test |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 222 | public void testAddOROperator () { |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 223 | String coll = "corpusSigle=WPD"; |
| 224 | String query = "[base=Haus]"; |
| 225 | QuerySerializer check = new QuerySerializer(); |
| 226 | check.setQuery(query, "poliqarp"); |
| 227 | check.setCollection(coll); |
| 228 | |
| 229 | KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder(); |
| 230 | test.setBaseQuery(check.toJSON()); |
| 231 | test.or().with("textClass=wissenschaft | textClass=politik"); |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 232 | JsonNode node = (JsonNode) test.rebaseCollection(null); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 233 | assertNotNull(node); |
| 234 | assertEquals("koral:docGroup", node.at("/collection/@type").asText()); |
| 235 | assertEquals("operation:or", node.at("/collection/operation").asText()); |
| 236 | assertEquals(2, node.at("/collection/operands/1/operands").size()); |
| 237 | } |
| 238 | |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 239 | |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 240 | @Test |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 241 | public void testAddANDOperator () { |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 242 | String coll = "corpusSigle=WPD"; |
| 243 | String query = "[base=Haus]"; |
| 244 | QuerySerializer check = new QuerySerializer(); |
| 245 | check.setQuery(query, "poliqarp"); |
| 246 | check.setCollection(coll); |
| 247 | |
| 248 | KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder(); |
| 249 | test.setBaseQuery(check.toJSON()); |
| 250 | test.and().with("textClass=wissenschaft | textClass=politik"); |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 251 | JsonNode node = (JsonNode) test.rebaseCollection(null); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 252 | assertNotNull(node); |
| 253 | assertEquals("koral:docGroup", node.at("/collection/@type").asText()); |
| 254 | assertEquals("operation:and", node.at("/collection/operation").asText()); |
| 255 | assertEquals(2, node.at("/collection/operands/1/operands").size()); |
| 256 | } |
| 257 | |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 258 | |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 259 | @Test |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 260 | public void testAddDefaultOperator () { |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 261 | String coll = "corpusSigle=WPD"; |
| 262 | String query = "[base=Haus]"; |
| 263 | QuerySerializer check = new QuerySerializer(); |
| 264 | check.setQuery(query, "poliqarp"); |
| 265 | check.setCollection(coll); |
| 266 | |
| 267 | KoralCollectionQueryBuilder test = new KoralCollectionQueryBuilder(); |
| 268 | test.setBaseQuery(check.toJSON()); |
| 269 | test.with("textClass=wissenschaft | textClass=politik"); |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 270 | JsonNode node = (JsonNode) test.rebaseCollection(null); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 271 | assertNotNull(node); |
| 272 | assertEquals("koral:docGroup", node.at("/collection/@type").asText()); |
| 273 | assertEquals("operation:and", node.at("/collection/operation").asText()); |
| 274 | assertEquals(2, node.at("/collection/operands/1/operands").size()); |
| 275 | } |
| 276 | |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 277 | |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 278 | @Test |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 279 | public void testCollectionMergeWithFromResource () { |
| 280 | KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder(); |
| 281 | builder.with("textClass=politik & corpusSigle=WPD"); |
| 282 | KustvaktResource resource = new VirtualCollection(); |
| 283 | resource.setName("collection_1"); |
| 284 | String json = builder.toJSON(); |
| 285 | resource.setFields(json); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 286 | |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 287 | assertEquals(json, resource.getStringData()); |
| 288 | builder = new KoralCollectionQueryBuilder(); |
| 289 | builder.setBaseQuery(resource.getData()); |
| 290 | builder.or().with("pubPlace=Mannheim"); |
| 291 | } |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 292 | |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 293 | |
| 294 | @Test |
| 295 | public void testCollectionMergeFromQuerySerializer () { |
| 296 | QuerySerializer s = new QuerySerializer(); |
| 297 | s.setQuery("[base=Haus]", "poliqarp"); |
| 298 | KoralCollectionQueryBuilder total = new KoralCollectionQueryBuilder(); |
| 299 | total.setBaseQuery(s.toJSON()); |
| 300 | |
| 301 | |
| 302 | KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder(); |
| 303 | builder.with("textClass=politik & corpusSigle=WPD"); |
| 304 | KustvaktResource resource = new VirtualCollection(); |
| 305 | resource.setName("collection_1"); |
| 306 | String json = builder.toJSON(); |
| 307 | resource.setFields(json); |
| 308 | // operator is irrelevant here |
| 309 | JsonNode node = total.or().mergeWith(resource.getData()); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 310 | assertNotNull(node); |
| 311 | assertEquals("koral:docGroup", node.at("/collection/@type").asText()); |
| 312 | assertEquals("operation:and", node.at("/collection/operation").asText()); |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 313 | assertEquals("textClass", node.at("/collection/operands/0/key") |
| 314 | .asText()); |
| 315 | assertEquals("corpusSigle", node.at("/collection/operands/1/key") |
| 316 | .asText()); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 319 | |
| 320 | @Test |
| 321 | public void testBaseCollectionNull () { |
| 322 | // base is missing collection segment |
| 323 | QuerySerializer s = new QuerySerializer(); |
| 324 | s.setQuery("[base=Haus]", "poliqarp"); |
| 325 | |
| 326 | KoralCollectionQueryBuilder total = new KoralCollectionQueryBuilder(); |
| 327 | total.setBaseQuery(s.toJSON()); |
| 328 | |
| 329 | KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder(); |
| 330 | builder.with("textClass=politik & corpusSigle=WPD"); |
| 331 | JsonNode node = total.and().mergeWith( |
| 332 | (JsonNode) builder.rebaseCollection(null)); |
| 333 | |
| 334 | assertNotNull(node); |
| 335 | assertEquals("koral:docGroup", node.at("/collection/@type").asText()); |
| 336 | assertEquals("operation:and", node.at("/collection/operation").asText()); |
| 337 | assertEquals("koral:doc", node.at("/collection/operands/0/@type") |
| 338 | .asText()); |
| 339 | assertEquals("koral:doc", node.at("/collection/operands/1/@type") |
| 340 | .asText()); |
| 341 | assertEquals("textClass", node.at("/collection/operands/0/key") |
| 342 | .asText()); |
| 343 | assertEquals("corpusSigle", node.at("/collection/operands/1/key") |
| 344 | .asText()); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | @Test |
| 349 | public void testMergeCollectionNull () { |
| 350 | // merge json is missing collection segment |
| 351 | QuerySerializer s = new QuerySerializer(); |
| 352 | s.setQuery("[base=Haus]", "poliqarp"); |
| 353 | s.setCollection("textClass=wissenschaft"); |
| 354 | |
| 355 | KoralCollectionQueryBuilder total = new KoralCollectionQueryBuilder(); |
| 356 | total.setBaseQuery(s.toJSON()); |
| 357 | |
| 358 | KoralCollectionQueryBuilder builder = new KoralCollectionQueryBuilder(); |
| 359 | JsonNode node = total.and().mergeWith( |
| 360 | (JsonNode) builder.rebaseCollection(null)); |
| 361 | assertNotNull(node); |
| 362 | assertEquals("koral:doc", node.at("/collection/@type").asText()); |
| 363 | assertEquals("textClass", node.at("/collection/key").asText()); |
| 364 | } |
| 365 | |
| 366 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 367 | } |