blob: 0840e2045848fc1dfef3bf22bd1ac31db47a0967 [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
Michael Hanl8abaf9e2016-05-23 16:46:35 +020018 public void testsimpleAdd () {
Michael Hanlfb839b92015-09-19 21:32:34 +020019 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);
Michael Hanl8abaf9e2016-05-23 16:46:35 +020025 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
Michael Hanl8abaf9e2016-05-23 16:46:35 +020029
Michael Hanlfb839b92015-09-19 21:32:34 +020030 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020031 public void testSimpleConjunction () {
Michael Hanlfb839b92015-09-19 21:32:34 +020032 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010033 b.addQuery("corpusID=WPD & textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020034 JsonNode node = JsonUtils.readTree(b.toJSON());
35
Michael Hanldaf86602016-05-12 14:31:52 +020036 assertNotNull(node);
Michael Hanl8abaf9e2016-05-23 16:46:35 +020037 assertEquals("koral:docGroup", node.at("/collection/@type").asText());
38 assertEquals("operation:and", node.at("/collection/operation").asText());
Michael Hanldaf86602016-05-12 14:31:52 +020039
Michael Hanl8abaf9e2016-05-23 16:46:35 +020040 assertEquals("corpusID", node.at("/collection/operands/0/key").asText());
41 assertEquals("textClass", node.at("/collection/operands/1/key")
42 .asText());
Michael Hanlfb839b92015-09-19 21:32:34 +020043 }
44
Michael Hanl8abaf9e2016-05-23 16:46:35 +020045
Michael Hanlfb839b92015-09-19 21:32:34 +020046 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020047 public void testSimpleDisjunction () {
Michael Hanlfb839b92015-09-19 21:32:34 +020048 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010049 b.addQuery("corpusID=WPD | textClass=freizeit");
Michael Hanl482f30d2015-09-25 12:39:46 +020050 JsonNode node = JsonUtils.readTree(b.toJSON());
51
Michael Hanl19390652016-01-16 11:01:24 +010052 System.out.println("_____________________________________________");
53 System.out.println(node);
54
Michael Hanl482f30d2015-09-25 12:39:46 +020055 assert node != null;
56 assert node.at("/collection/operation").asText().equals("operation:or");
57 assert node.at("/collection/operands/0/key").asText()
58 .equals("corpusID");
59 assert node.at("/collection/operands/1/key").asText()
60 .equals("textClass");
Michael Hanlfb839b92015-09-19 21:32:34 +020061 }
62
Michael Hanl8abaf9e2016-05-23 16:46:35 +020063
Michael Hanlfb839b92015-09-19 21:32:34 +020064 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020065 public void testComplexSubQuery () {
Michael Hanlfb839b92015-09-19 21:32:34 +020066 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl8abaf9e2016-05-23 16:46:35 +020067 b.addQuery("(corpusID=WPD) | (textClass=freizeit & corpusID=WPD)");
Michael Hanl482f30d2015-09-25 12:39:46 +020068 JsonNode node = JsonUtils.readTree(b.toJSON());
Michael Hanlfb839b92015-09-19 21:32:34 +020069
Michael Hanl8abaf9e2016-05-23 16:46:35 +020070 System.out
71 .println("_____________________________________________ COMPLEX");
Michael Hanl19390652016-01-16 11:01:24 +010072 System.out.println(node);
Michael Hanl482f30d2015-09-25 12:39:46 +020073 assert node != null;
74 assert node.at("/collection/operation").asText().equals("operation:or");
75 assert node.at("/collection/operands/0/key").asText()
76 .equals("corpusID");
77 assert node.at("/collection/operands/1/@type").asText()
78 .equals("koral:docGroup");
Michael Hanlfb839b92015-09-19 21:32:34 +020079
Michael Hanlfb839b92015-09-19 21:32:34 +020080 }
81
Michael Hanl8abaf9e2016-05-23 16:46:35 +020082
Michael Hanlfb839b92015-09-19 21:32:34 +020083 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020084 public void testAddResourceQueryAfter () {
Michael Hanlfb839b92015-09-19 21:32:34 +020085 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +010086 b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)");
Michael Hanlfb839b92015-09-19 21:32:34 +020087
88 CollectionQueryBuilder3 c = new CollectionQueryBuilder3();
89 c.setBaseQuery(b.toJSON());
Michael Hanl19390652016-01-16 11:01:24 +010090 c.addQuery("textClass=wissenschaft");
Michael Hanlfb839b92015-09-19 21:32:34 +020091
92 JsonNode node = JsonUtils.readTree(c.toJSON());
93
94 assert node != null;
95 assert node.at("/collection/operands/2/@type").asText()
96 .equals("koral:doc");
97 assert node.at("/collection/operands/2/value").asText()
98 .equals("wissenschaft");
99 }
100
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200101
Michael Hanl482f30d2015-09-25 12:39:46 +0200102 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200103 public void testAddComplexResourceQueryAfter () {
Michael Hanl482f30d2015-09-25 12:39:46 +0200104 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
Michael Hanl19390652016-01-16 11:01:24 +0100105 b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200106
107 CollectionQueryBuilder3 c = new CollectionQueryBuilder3();
108 c.setBaseQuery(b.toJSON());
Michael Hanl19390652016-01-16 11:01:24 +0100109 c.addQuery("(textClass=politik & corpusID=AZPS)");
Michael Hanl482f30d2015-09-25 12:39:46 +0200110
111 JsonNode node = JsonUtils.readTree(c.toJSON());
112
113 assert node != null;
114 assert node.at("/collection/operands/2/@type").asText()
115 .equals("koral:docGroup");
116 assert node.at("/collection/operands/2/operands/0/value").asText()
117 .equals("politik");
118 assert node.at("/collection/operands/2/operands/1/value").asText()
119 .equals("AZPS");
120
121 }
122
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200123
Michael Hanl19390652016-01-16 11:01:24 +0100124 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200125 public void buildQuery () {
Michael Hanl19390652016-01-16 11:01:24 +0100126 String query = "[base=Haus]";
127 QuerySerializer s = new QuerySerializer();
128 s.setQuery(query, "poliqarp");
129 CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
130 b.addQuery("corpusID=WPD");
131 s.setCollection("corpusID=WPD");
132
133 System.out.println("QUERY " + s.toJSON());
134 }
135
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200136
Michael Hanl19390652016-01-16 11:01:24 +0100137 @Test
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200138 public void testBaseQueryBuild () {
Michael Hanl19390652016-01-16 11:01:24 +0100139
140 }
141
Michael Hanlfb839b92015-09-19 21:32:34 +0200142}