Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 1 | import com.fasterxml.jackson.databind.JsonNode; |
| 2 | import de.ids_mannheim.korap.utils.CollectionQueryBuilder3; |
| 3 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 4 | import org.junit.Test; |
| 5 | |
| 6 | /** |
| 7 | * @author hanl |
| 8 | * @date 12/08/2015 |
| 9 | */ |
| 10 | public class CollectionQueryBuilderTest { |
| 11 | |
| 12 | @Test |
| 13 | public void testsimpleAdd() { |
| 14 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
| 15 | b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "WPD"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 16 | |
| 17 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 18 | |
| 19 | assert node != null; |
| 20 | assert node.at("/collection/@type").asText().equals("koral:doc"); |
| 21 | assert node.at("/collection/key").asText().equals("corpusID"); |
| 22 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | @Test |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 26 | public void testSimpleConjunction() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 27 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
| 28 | b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "WPD").and() |
| 29 | .addSegment("textClass", CollectionQueryBuilder3.EQ.EQUAL, |
| 30 | "freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 31 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 32 | |
| 33 | assert node != null; |
| 34 | assert node.at("/collection/@type").asText().equals("koral:docGroup"); |
| 35 | assert node.at("/collection/operation").asText() |
| 36 | .equals("operation:and"); |
| 37 | assert node.at("/collection/operands/0/key").asText() |
| 38 | .equals("corpusID"); |
| 39 | assert node.at("/collection/operands/1/key").asText() |
| 40 | .equals("textClass"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 44 | public void testSimpleDisjunction() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 45 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 46 | b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "WPD").or() |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 47 | .addSegment("textClass", CollectionQueryBuilder3.EQ.EQUAL, |
| 48 | "freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 49 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 50 | |
| 51 | assert node != null; |
| 52 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 53 | assert node.at("/collection/operands/0/key").asText() |
| 54 | .equals("corpusID"); |
| 55 | assert node.at("/collection/operands/1/key").asText() |
| 56 | .equals("textClass"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testComplexSubQuery() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 61 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 62 | b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "ADF").or() |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 63 | .addSub("textClass=freizeit & corpusID=WPD"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 64 | |
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 | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 67 | assert node != null; |
| 68 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 69 | assert node.at("/collection/operands/0/key").asText() |
| 70 | .equals("corpusID"); |
| 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 | |
| 76 | @Test |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 77 | public void testAddResourceQueryAfter() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 78 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
| 79 | b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "ADF").or() |
| 80 | .addSub("textClass=freizeit & corpusID=WPD"); |
| 81 | |
| 82 | CollectionQueryBuilder3 c = new CollectionQueryBuilder3(); |
| 83 | c.setBaseQuery(b.toJSON()); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 84 | c.addSegment("textClass", CollectionQueryBuilder3.EQ.EQUAL, |
| 85 | "wissenschaft"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 86 | |
| 87 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
| 88 | |
| 89 | assert node != null; |
| 90 | assert node.at("/collection/operands/2/@type").asText() |
| 91 | .equals("koral:doc"); |
| 92 | assert node.at("/collection/operands/2/value").asText() |
| 93 | .equals("wissenschaft"); |
| 94 | } |
| 95 | |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame^] | 96 | @Test |
| 97 | public void testAddComplexResourceQueryAfter() { |
| 98 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
| 99 | b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "ADF").or() |
| 100 | .addSub("textClass=freizeit & corpusID=WPD"); |
| 101 | |
| 102 | CollectionQueryBuilder3 c = new CollectionQueryBuilder3(); |
| 103 | c.setBaseQuery(b.toJSON()); |
| 104 | c.addSub("(textClass=politik & corpusID=AZPS)"); |
| 105 | |
| 106 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
| 107 | |
| 108 | assert node != null; |
| 109 | assert node.at("/collection/operands/2/@type").asText() |
| 110 | .equals("koral:docGroup"); |
| 111 | assert node.at("/collection/operands/2/operands/0/value").asText() |
| 112 | .equals("politik"); |
| 113 | assert node.at("/collection/operands/2/operands/1/value").asText() |
| 114 | .equals("AZPS"); |
| 115 | |
| 116 | } |
| 117 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 118 | } |