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 | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 148 | JsonNode res = (JsonNode) b.rebaseCollection(); |
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 | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 181 | JsonNode base = (JsonNode) c.rebaseCollection(); |
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 | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 206 | JsonNode node = (JsonNode) test.rebaseCollection(); |
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 |
Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 216 | 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 Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 259 | public void testStoredCollectionBaseQueryBuild () { |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 260 | |
| 261 | } |
| 262 | |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 263 | |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 264 | @Test |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 265 | public void testAddOROperator () { |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 266 | 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 Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 275 | JsonNode node = (JsonNode) test.rebaseCollection(); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 276 | 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 Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 282 | |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 283 | @Test |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 284 | public void testAddANDOperator () { |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 285 | 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 Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 294 | JsonNode node = (JsonNode) test.rebaseCollection(); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 295 | 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 Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 301 | |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 302 | @Test |
Michael Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 303 | public void testAddDefaultOperator () { |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 304 | 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 Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 313 | JsonNode node = (JsonNode) test.rebaseCollection(); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 314 | 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 Hanl | 33829ec | 2016-05-28 17:03:38 +0200 | [diff] [blame] | 320 | |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 321 | @Test |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 322 | 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 Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 329 | |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 330 | assertEquals(json, resource.getStringData()); |
| 331 | builder = new KoralCollectionQueryBuilder(); |
| 332 | builder.setBaseQuery(resource.getData()); |
| 333 | builder.or().with("pubPlace=Mannheim"); |
Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 334 | |
| 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 Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 356 | } |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 357 | |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 358 | |
| 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 Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 375 | assertNotNull(node); |
| 376 | assertEquals("koral:docGroup", node.at("/collection/@type").asText()); |
| 377 | assertEquals("operation:and", node.at("/collection/operation").asText()); |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 378 | assertEquals("textClass", node.at("/collection/operands/0/key") |
| 379 | .asText()); |
| 380 | assertEquals("corpusSigle", node.at("/collection/operands/1/key") |
| 381 | .asText()); |
Michael Hanl | cedf721 | 2016-05-28 10:43:09 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 384 | |
| 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 Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 397 | (JsonNode) builder.rebaseCollection()); |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 398 | |
| 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 Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 425 | (JsonNode) builder.rebaseCollection()); |
Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 426 | assertNotNull(node); |
| 427 | assertEquals("koral:doc", node.at("/collection/@type").asText()); |
| 428 | assertEquals("textClass", node.at("/collection/key").asText()); |
| 429 | } |
| 430 | |
| 431 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 432 | } |