blob: aeb753f767fbd7d686758e4966a19469c9c4e7cd [file] [log] [blame]
Michael Hanlfb839b92015-09-19 21:32:34 +02001import com.fasterxml.jackson.databind.JsonNode;
2import de.ids_mannheim.korap.utils.CollectionQueryBuilder3;
3import de.ids_mannheim.korap.utils.JsonUtils;
4import org.junit.Test;
5
6/**
7 * @author hanl
8 * @date 12/08/2015
9 */
10public class CollectionQueryBuilderTest {
11
12 @Test
13 public void testsimpleAdd() {
14 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
15 b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "WPD");
Michael Hanl482f30d2015-09-25 12:39:46 +020016
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 Hanlfb839b92015-09-19 21:32:34 +020023 }
24
25 @Test
Michael Hanl482f30d2015-09-25 12:39:46 +020026 public void testSimpleConjunction() {
Michael Hanlfb839b92015-09-19 21:32:34 +020027 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
28 b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "WPD").and()
29 .addSegment("textClass", CollectionQueryBuilder3.EQ.EQUAL,
30 "freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020031 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 Hanlfb839b92015-09-19 21:32:34 +020041 }
42
43 @Test
Michael Hanl482f30d2015-09-25 12:39:46 +020044 public void testSimpleDisjunction() {
Michael Hanlfb839b92015-09-19 21:32:34 +020045 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl482f30d2015-09-25 12:39:46 +020046 b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "WPD").or()
Michael Hanlfb839b92015-09-19 21:32:34 +020047 .addSegment("textClass", CollectionQueryBuilder3.EQ.EQUAL,
48 "freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020049 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 Hanlfb839b92015-09-19 21:32:34 +020057 }
58
59 @Test
60 public void testComplexSubQuery() {
Michael Hanlfb839b92015-09-19 21:32:34 +020061 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl482f30d2015-09-25 12:39:46 +020062 b.addSegment("corpusID", CollectionQueryBuilder3.EQ.EQUAL, "ADF").or()
Michael Hanlfb839b92015-09-19 21:32:34 +020063 .addSub("textClass=freizeit & corpusID=WPD");
Michael Hanlfb839b92015-09-19 21:32:34 +020064
Michael Hanl482f30d2015-09-25 12:39:46 +020065 JsonNode node = JsonUtils.readTree(b.toJSON());
Michael Hanlfb839b92015-09-19 21:32:34 +020066
Michael Hanl482f30d2015-09-25 12:39:46 +020067 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 Hanlfb839b92015-09-19 21:32:34 +020073
Michael Hanlfb839b92015-09-19 21:32:34 +020074 }
75
76 @Test
Michael Hanl482f30d2015-09-25 12:39:46 +020077 public void testAddResourceQueryAfter() {
Michael Hanlfb839b92015-09-19 21:32:34 +020078 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 Hanl482f30d2015-09-25 12:39:46 +020084 c.addSegment("textClass", CollectionQueryBuilder3.EQ.EQUAL,
85 "wissenschaft");
Michael Hanlfb839b92015-09-19 21:32:34 +020086
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 Hanl482f30d2015-09-25 12:39:46 +020096 @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 Hanlfb839b92015-09-19 21:32:34 +0200118}