blob: 2686b032629411fb7fabfccc6f38a06e7d039b9c [file] [log] [blame]
Eliza Margaretha39db1ab2014-04-01 10:39:54 +00001import static org.junit.Assert.assertEquals;
2
3import java.io.IOException;
4
5import org.junit.Test;
6import org.z3950.zing.cql.CQLParseException;
7
Eliza Margaretha60514822014-04-23 11:32:43 +00008import com.fasterxml.jackson.databind.JsonNode;
9import com.fasterxml.jackson.databind.ObjectMapper;
10
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000011import de.ids_mannheim.korap.query.serialize.CQLTree;
Eliza Margaretha60514822014-04-23 11:32:43 +000012import de.ids_mannheim.korap.query.serialize.CosmasTree;
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000013import de.ids_mannheim.korap.util.QueryException;
14
15
16public class CQLTest {
17
18 String query;
Eliza Margaretha724be112014-04-01 11:13:40 +000019 String version ="1.2";
Eliza Margaretha60514822014-04-23 11:32:43 +000020 ObjectMapper mapper = new ObjectMapper();
Eliza Margaretha46793572014-04-02 15:11:25 +000021
22 @Test
23 public void testExceptions() throws CQLParseException, IOException {
24 query = "(Kuh) prox (Germ) ";
25 try {
26 CQLTree cqlTree = new CQLTree(query, version);
27 } catch (QueryException e) {
28 assertEquals(48,e.getErrorCode());
29 }
30
31 query = "(Kuh) or/rel.combine=sum (Germ) ";
32 try {
33 CQLTree cqlTree = new CQLTree(query, version);
34 }catch (QueryException e) {
35 assertEquals(20,e.getErrorCode());
36 }
37
38 query = "dc.title any Germ ";
39 try {
40 CQLTree cqlTree = new CQLTree(query, version);
41 } catch (QueryException e) {
42 assertEquals(16,e.getErrorCode());
43 }
44
45 query = "cql.serverChoice any Germ ";
46 try {
47 CQLTree cqlTree = new CQLTree(query, version);
48 } catch (QueryException e) {
49 assertEquals(19,e.getErrorCode());
50 }
51
52 query = "";
53 try {
54 CQLTree cqlTree = new CQLTree(query, version);
55 } catch (QueryException e) {
56 assertEquals(27,e.getErrorCode());
57 }
58 }
59
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000060 @Test
Eliza Margaretha60514822014-04-23 11:32:43 +000061 public void testAndQuery() throws CQLParseException, IOException, QueryException{
62 query="(Sonne) and (scheint)";
63 String jsonLd =
64 "{@type : korap:group, operation : operation:sequence, distances:[ "+
65 "{@type : korap:distance, key : t, min : 0, max : 0 } ],"+
66 "operands : ["+
67 "{@type : korap:token, wrap : {@type : korap:term,key : Sonne, layer : orth, match : match:eq}}," +
68 "{@type : korap:token,wrap : {@type : korap:term,key : scheint,layer : orth,match : match:eq}" +
69 "}]}";
70
71 CQLTree cqlTree = new CQLTree(query, version);
72 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
73 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
74
75 CosmasTree ct = new CosmasTree("Sonne und scheint");
76 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
77
78 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
79 }
80
81 @Test
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000082 public void testBooleanQuery() throws CQLParseException, IOException, QueryException{
83 query="((Sonne) or (Mond)) and (scheint)";
84 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000085 "{@type:korap:group, operation:operation:sequence, distances:[" +
86 "{@type:korap:distance, key:t, min:0, max:0}" +
87 "], operands:[" +
88 "{@type:korap:group, operation:operation:or, operands:[" +
89 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
90 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000091 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000092 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000093 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +000094 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +000095 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000096 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000097
98
99 query="(scheint) and ((Sonne) or (Mond))";
100 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000101 "{@type:korap:group, operation:operation:sequence, distances:[" +
102 "{@type:korap:distance, key:t, min:0, max:0}" +
103 "], operands:[" +
104 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}," +
105 "{@type:korap:group, operation:operation:or, operands:[" +
106 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
107 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000108 "]}" +
109 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +0000110 cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000111 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000112 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000113
114 }
115
116 @Test
117 public void testOrQuery() throws CQLParseException, IOException, QueryException{
118 query = "(Sonne) or (Mond)";
119 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000120 "{@type:korap:group, operation:operation:or, operands:[" +
121 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
122 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000123 "]}";
124
Eliza Margaretha724be112014-04-01 11:13:40 +0000125 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000126 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000127 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000128
129 query="(\"Sonne scheint\") or (Mond)";
130 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000131 "{@type:korap:group, operation:operation:or, operands:[" +
132 "{@type:korap:group, operation:operation:sequence, operands:[" +
133 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
134 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000135 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000136 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000137 "]}";
138
Eliza Margaretha724be112014-04-01 11:13:40 +0000139 cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000140 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000141 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000142
143 query="(\"Sonne scheint\") or (\"Mond scheint\")";
144 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000145 "{@type:korap:group, operation:operation:or, operands:[" +
146 "{@type:korap:group, operation:operation:sequence, operands:[" +
147 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
148 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000149 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000150 "{@type:korap:group, operation:operation:sequence, operands:[" +
151 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}," +
152 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000153 "]}" +
154 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +0000155 cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000156 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000157 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000158 }
159
160 @Test
161 public void testTermQuery() throws CQLParseException, IOException, QueryException{
162 query = "Sonne";
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000163 String jsonLd = "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}";
Eliza Margaretha724be112014-04-01 11:13:40 +0000164 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000165 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000166 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000167 }
168
169 @Test
170 public void testPhraseQuery() throws CQLParseException, IOException, QueryException{
171 query="\"der Mann\"";
172 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000173 "{@type:korap:group, operation:operation:sequence, operands:[" +
174 "{@type:korap:token, wrap:{@type:korap:term, key:der, layer:orth, match:match:eq}}," +
175 "{@type:korap:token, wrap:{@type:korap:term, key:Mann, layer:orth, match:match:eq}}" +
176 "]}";
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000177
Eliza Margaretha724be112014-04-01 11:13:40 +0000178 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000179 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000180 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000181
182
183 query="der Mann schläft";
184 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000185 "{@type:korap:group, operation:operation:sequence, operands:[" +
186 "{@type:korap:token, wrap:{@type:korap:term, key:der, layer:orth, match:match:eq}}," +
187 "{@type:korap:token, wrap:{@type:korap:term, key:Mann, layer:orth, match:match:eq}}," +
188 "{@type:korap:token, wrap:{@type:korap:term, key:schläft, layer:orth, match:match:eq}}" +
189 "]}";
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000190
Eliza Margaretha724be112014-04-01 11:13:40 +0000191 cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000192 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000193 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000194 }
195}