blob: ab9706aee989d9173c5881c0b587699344873d33 [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;
Michael Hanldaf86602016-05-12 14:31:52 +02005import org.junit.Assert;
Michael Hanlfb839b92015-09-19 21:32:34 +02006import org.junit.Test;
7
Michael Hanldaf86602016-05-12 14:31:52 +02008import static org.junit.Assert.assertEquals;
9import static org.junit.Assert.assertNotNull;
10
Michael Hanlfb839b92015-09-19 21:32:34 +020011/**
12 * @author hanl
13 * @date 12/08/2015
14 */
15public class CollectionQueryBuilderTest {
16
17 @Test
18 public void testsimpleAdd() {
19 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010020 b.addQuery("corpusID=WPD");
Michael Hanl482f30d2015-09-25 12:39:46 +020021
22 JsonNode node = JsonUtils.readTree(b.toJSON());
23
Michael Hanldaf86602016-05-12 14:31:52 +020024 assertNotNull(node);
25 assertEquals("koral:doc",node.at("/collection/@type").asText());
26 assertEquals("corpusID",node.at("/collection/key").asText());
Michael Hanlfb839b92015-09-19 21:32:34 +020027 }
28
29 @Test
Michael Hanl482f30d2015-09-25 12:39:46 +020030 public void testSimpleConjunction() {
Michael Hanlfb839b92015-09-19 21:32:34 +020031 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010032 b.addQuery("corpusID=WPD & textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020033 JsonNode node = JsonUtils.readTree(b.toJSON());
34
Michael Hanldaf86602016-05-12 14:31:52 +020035 assertNotNull(node);
36 assertEquals("koral:docGroup",node.at("/collection/@type").asText());
37 assertEquals("operation:and",node.at("/collection/operation").asText());
38
39 assertEquals("corpusID",node.at("/collection/operands/0/key").asText());
40 assertEquals("textClass",node.at("/collection/operands/1/key").asText());
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 Hanl19390652016-01-16 11:01:24 +010046 b.addQuery("corpusID=WPD | textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020047 JsonNode node = JsonUtils.readTree(b.toJSON());
48
Michael Hanl19390652016-01-16 11:01:24 +010049 System.out.println("_____________________________________________");
50 System.out.println(node);
51
Michael Hanl482f30d2015-09-25 12:39:46 +020052 assert node != null;
53 assert node.at("/collection/operation").asText().equals("operation:or");
54 assert node.at("/collection/operands/0/key").asText()
55 .equals("corpusID");
56 assert node.at("/collection/operands/1/key").asText()
57 .equals("textClass");
Michael Hanlfb839b92015-09-19 21:32:34 +020058 }
59
60 @Test
61 public void testComplexSubQuery() {
Michael Hanlfb839b92015-09-19 21:32:34 +020062 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010063 b.addQuery(
64 "(corpusID=WPD) | (textClass=freizeit & corpusID=WPD)");
Michael Hanl482f30d2015-09-25 12:39:46 +020065 JsonNode node = JsonUtils.readTree(b.toJSON());
Michael Hanlfb839b92015-09-19 21:32:34 +020066
Michael Hanl19390652016-01-16 11:01:24 +010067 System.out.println(
68 "_____________________________________________ COMPLEX");
69 System.out.println(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020070 assert node != null;
71 assert node.at("/collection/operation").asText().equals("operation:or");
72 assert node.at("/collection/operands/0/key").asText()
73 .equals("corpusID");
74 assert node.at("/collection/operands/1/@type").asText()
75 .equals("koral:docGroup");
Michael Hanlfb839b92015-09-19 21:32:34 +020076
Michael Hanlfb839b92015-09-19 21:32:34 +020077 }
78
79 @Test
Michael Hanl482f30d2015-09-25 12:39:46 +020080 public void testAddResourceQueryAfter() {
Michael Hanlfb839b92015-09-19 21:32:34 +020081 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010082 b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)");
Michael Hanlfb839b92015-09-19 21:32:34 +020083
84 CollectionQueryBuilder3 c = new CollectionQueryBuilder3();
85 c.setBaseQuery(b.toJSON());
Michael Hanl19390652016-01-16 11:01:24 +010086 c.addQuery("textClass=wissenschaft");
Michael Hanlfb839b92015-09-19 21:32:34 +020087
88 JsonNode node = JsonUtils.readTree(c.toJSON());
89
90 assert node != null;
91 assert node.at("/collection/operands/2/@type").asText()
92 .equals("koral:doc");
93 assert node.at("/collection/operands/2/value").asText()
94 .equals("wissenschaft");
95 }
96
Michael Hanl482f30d2015-09-25 12:39:46 +020097 @Test
98 public void testAddComplexResourceQueryAfter() {
99 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +0100100 b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200101
102 CollectionQueryBuilder3 c = new CollectionQueryBuilder3();
103 c.setBaseQuery(b.toJSON());
Michael Hanl19390652016-01-16 11:01:24 +0100104 c.addQuery("(textClass=politik & corpusID=AZPS)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200105
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 Hanl19390652016-01-16 11:01:24 +0100118 @Test
119 public void buildQuery() {
120 String query = "[base=Haus]";
121 QuerySerializer s = new QuerySerializer();
122 s.setQuery(query, "poliqarp");
123 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
124 b.addQuery("corpusID=WPD");
125 s.setCollection("corpusID=WPD");
126
127 System.out.println("QUERY " + s.toJSON());
128 }
129
130 @Test
131 public void testBaseQueryBuild() {
132
133 }
134
Michael Hanlfb839b92015-09-19 21:32:34 +0200135}