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 | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.utils.KoralCollectionQueryBuilder; |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 4 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 5 | import org.junit.Test; |
| 6 | |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 7 | import static org.junit.Assert.assertEquals; |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 8 | import static org.junit.Assert.assertNotEquals; |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 9 | import static org.junit.Assert.assertNotNull; |
| 10 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 11 | /** |
| 12 | * @author hanl |
| 13 | * @date 12/08/2015 |
| 14 | */ |
| 15 | public class CollectionQueryBuilderTest { |
| 16 | |
| 17 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 18 | public void testsimpleAdd () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 19 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 20 | b.with("corpusSigle=WPD"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 21 | |
| 22 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 23 | |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 24 | assertNotNull(node); |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 25 | assertEquals("koral:doc", node.at("/collection/@type").asText()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 26 | assertEquals("corpusSigle", node.at("/collection/key").asText()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 27 | } |
| 28 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 29 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 30 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 31 | public void testSimpleConjunction () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 32 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 33 | b.with("corpusSigle=WPD & textClass=freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 34 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 35 | |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 36 | assertNotNull(node); |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 37 | assertEquals("koral:docGroup", node.at("/collection/@type").asText()); |
| 38 | assertEquals("operation:and", node.at("/collection/operation").asText()); |
Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 39 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 40 | assertEquals("corpusSigle", node.at("/collection/operands/0/key").asText()); |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 41 | assertEquals("textClass", node.at("/collection/operands/1/key") |
| 42 | .asText()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 45 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 46 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 47 | public void testSimpleDisjunction () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 48 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 49 | b.with("corpusSigle=WPD | textClass=freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 50 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 51 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 52 | assertNotNull(node); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 53 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 54 | assert node.at("/collection/operands/0/key").asText() |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 55 | .equals("corpusSigle"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 56 | assert node.at("/collection/operands/1/key").asText() |
| 57 | .equals("textClass"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 60 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 61 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 62 | public void testComplexSubQuery () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 63 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 64 | b.with("(corpusSigle=WPD) | (textClass=freizeit & corpusSigle=BRZ13)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 65 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 66 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 67 | assertNotNull(node); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 68 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 69 | assert node.at("/collection/operands/0/key").asText() |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 70 | .equals("corpusSigle"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 71 | assert node.at("/collection/operands/1/@type").asText() |
| 72 | .equals("koral:docGroup"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 73 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 76 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 77 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 78 | public void testAddResourceQueryAfter () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 79 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 80 | b.with("(textClass=politik & title=\"random title\") | textClass=wissenschaft"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 81 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 82 | KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder(); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 83 | c.setBaseQuery(b.toJSON()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 84 | c.with("corpusSigle=WPD"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 85 | |
| 86 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
| 87 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 88 | 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 Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 95 | |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 96 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 97 | public void testAddComplexResourceQueryAfter () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 98 | KoralCollectionQueryBuilder b = new KoralCollectionQueryBuilder(); |
| 99 | b.with("(title=\"random title\") | (textClass=wissenschaft)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 100 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 101 | KoralCollectionQueryBuilder c = new KoralCollectionQueryBuilder(); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 102 | c.setBaseQuery(b.toJSON()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 103 | c.with("(corpusSigle=BRZ13 | corpusSigle=AZPS)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 104 | |
| 105 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 106 | 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 Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 121 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 122 | @Test |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 123 | public void testBuildQuery () { |
| 124 | String coll = "corpusSigle=WPD"; |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 125 | String query = "[base=Haus]"; |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 126 | QuerySerializer check = new QuerySerializer(); |
| 127 | check.setQuery(query, "poliqarp"); |
| 128 | check.setCollection(coll); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 129 | |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 130 | 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 Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 156 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 157 | @Test |
Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 158 | public void testBaseQueryBuild () { |
Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 159 | 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 Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 201 | |
| 202 | } |
| 203 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 204 | } |