blob: 8acd4fb27da5850f9ba36346aa6fb1d2bfd10fbd [file] [log] [blame]
Michael Hanlfb839b92015-09-19 21:32:34 +02001import com.fasterxml.jackson.databind.JsonNode;
Michael Hanl19390652016-01-16 11:01:24 +01002import de.ids_mannheim.korap.query.serialize.QuerySerializer;
Michael Hanlfb839b92015-09-19 21:32:34 +02003import de.ids_mannheim.korap.utils.CollectionQueryBuilder3;
4import de.ids_mannheim.korap.utils.JsonUtils;
5import org.junit.Test;
6
7/**
8 * @author hanl
9 * @date 12/08/2015
10 */
11public class CollectionQueryBuilderTest {
12
13 @Test
14 public void testsimpleAdd() {
15 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010016 b.addQuery("corpusID=WPD");
Michael Hanl482f30d2015-09-25 12:39:46 +020017
18 JsonNode node = JsonUtils.readTree(b.toJSON());
19
20 assert node != null;
21 assert node.at("/collection/@type").asText().equals("koral:doc");
22 assert node.at("/collection/key").asText().equals("corpusID");
23
Michael Hanlfb839b92015-09-19 21:32:34 +020024 }
25
26 @Test
Michael Hanl482f30d2015-09-25 12:39:46 +020027 public void testSimpleConjunction() {
Michael Hanlfb839b92015-09-19 21:32:34 +020028 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010029 b.addQuery("corpusID=WPD & textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020030 JsonNode node = JsonUtils.readTree(b.toJSON());
31
32 assert node != null;
33 assert node.at("/collection/@type").asText().equals("koral:docGroup");
34 assert node.at("/collection/operation").asText()
35 .equals("operation:and");
36 assert node.at("/collection/operands/0/key").asText()
37 .equals("corpusID");
38 assert node.at("/collection/operands/1/key").asText()
39 .equals("textClass");
Michael Hanlfb839b92015-09-19 21:32:34 +020040 }
41
42 @Test
Michael Hanl482f30d2015-09-25 12:39:46 +020043 public void testSimpleDisjunction() {
Michael Hanlfb839b92015-09-19 21:32:34 +020044 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010045 b.addQuery("corpusID=WPD | textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020046 JsonNode node = JsonUtils.readTree(b.toJSON());
47
Michael Hanl19390652016-01-16 11:01:24 +010048 System.out.println("_____________________________________________");
49 System.out.println(node);
50
Michael Hanl482f30d2015-09-25 12:39:46 +020051 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 Hanl19390652016-01-16 11:01:24 +010062 b.addQuery(
63 "(corpusID=WPD) | (textClass=freizeit & corpusID=WPD)");
Michael Hanl482f30d2015-09-25 12:39:46 +020064 JsonNode node = JsonUtils.readTree(b.toJSON());
Michael Hanlfb839b92015-09-19 21:32:34 +020065
Michael Hanl19390652016-01-16 11:01:24 +010066 System.out.println(
67 "_____________________________________________ COMPLEX");
68 System.out.println(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020069 assert node != null;
70 assert node.at("/collection/operation").asText().equals("operation:or");
71 assert node.at("/collection/operands/0/key").asText()
72 .equals("corpusID");
73 assert node.at("/collection/operands/1/@type").asText()
74 .equals("koral:docGroup");
Michael Hanlfb839b92015-09-19 21:32:34 +020075
Michael Hanlfb839b92015-09-19 21:32:34 +020076 }
77
78 @Test
Michael Hanl482f30d2015-09-25 12:39:46 +020079 public void testAddResourceQueryAfter() {
Michael Hanlfb839b92015-09-19 21:32:34 +020080 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010081 b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)");
Michael Hanlfb839b92015-09-19 21:32:34 +020082
83 CollectionQueryBuilder3 c = new CollectionQueryBuilder3();
84 c.setBaseQuery(b.toJSON());
Michael Hanl19390652016-01-16 11:01:24 +010085 c.addQuery("textClass=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();
Michael Hanl19390652016-01-16 11:01:24 +010099 b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200100
101 CollectionQueryBuilder3 c = new CollectionQueryBuilder3();
102 c.setBaseQuery(b.toJSON());
Michael Hanl19390652016-01-16 11:01:24 +0100103 c.addQuery("(textClass=politik & corpusID=AZPS)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200104
105 JsonNode node = JsonUtils.readTree(c.toJSON());
106
107 assert node != null;
108 assert node.at("/collection/operands/2/@type").asText()
109 .equals("koral:docGroup");
110 assert node.at("/collection/operands/2/operands/0/value").asText()
111 .equals("politik");
112 assert node.at("/collection/operands/2/operands/1/value").asText()
113 .equals("AZPS");
114
115 }
116
Michael Hanl19390652016-01-16 11:01:24 +0100117 @Test
118 public void buildQuery() {
119 String query = "[base=Haus]";
120 QuerySerializer s = new QuerySerializer();
121 s.setQuery(query, "poliqarp");
122 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
123 b.addQuery("corpusID=WPD");
124 s.setCollection("corpusID=WPD");
125
126 System.out.println("QUERY " + s.toJSON());
127 }
128
129 @Test
130 public void testBaseQueryBuild() {
131
132 }
133
Michael Hanlfb839b92015-09-19 21:32:34 +0200134}