blob: b372ce0d11ba9f0600725b5e3e53b409211f2b59 [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) {
Eliza Margarethaf267d4a2014-05-09 11:50:23 +000028 int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", ""));
29 assertEquals(48,errorCode);
Eliza Margaretha46793572014-04-02 15:11:25 +000030 }
31
32 query = "(Kuh) or/rel.combine=sum (Germ) ";
33 try {
34 CQLTree cqlTree = new CQLTree(query, version);
35 }catch (QueryException e) {
Eliza Margarethaf267d4a2014-05-09 11:50:23 +000036 int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", ""));
37 assertEquals(20,errorCode);
Eliza Margaretha46793572014-04-02 15:11:25 +000038 }
39
40 query = "dc.title any Germ ";
41 try {
42 CQLTree cqlTree = new CQLTree(query, version);
43 } catch (QueryException e) {
Eliza Margarethaf267d4a2014-05-09 11:50:23 +000044 int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", ""));
45 assertEquals(16,errorCode);
Eliza Margaretha46793572014-04-02 15:11:25 +000046 }
47
48 query = "cql.serverChoice any Germ ";
49 try {
50 CQLTree cqlTree = new CQLTree(query, version);
51 } catch (QueryException e) {
Eliza Margarethaf267d4a2014-05-09 11:50:23 +000052 int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", ""));
53 assertEquals(19,errorCode);
Eliza Margaretha46793572014-04-02 15:11:25 +000054 }
55
56 query = "";
57 try {
58 CQLTree cqlTree = new CQLTree(query, version);
59 } catch (QueryException e) {
Eliza Margarethaf267d4a2014-05-09 11:50:23 +000060 int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", ""));
61 assertEquals(27,errorCode);
Eliza Margaretha46793572014-04-02 15:11:25 +000062 }
63 }
64
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000065 @Test
Eliza Margaretha60514822014-04-23 11:32:43 +000066 public void testAndQuery() throws CQLParseException, IOException, QueryException{
67 query="(Sonne) and (scheint)";
68 String jsonLd =
69 "{@type : korap:group, operation : operation:sequence, distances:[ "+
Eliza Margarethaf267d4a2014-05-09 11:50:23 +000070 "{@type : korap:distance, key : w, min : 0, max : 0 } ],"+
Eliza Margaretha60514822014-04-23 11:32:43 +000071 "operands : ["+
72 "{@type : korap:token, wrap : {@type : korap:term,key : Sonne, layer : orth, match : match:eq}}," +
73 "{@type : korap:token,wrap : {@type : korap:term,key : scheint,layer : orth,match : match:eq}" +
74 "}]}";
75
76 CQLTree cqlTree = new CQLTree(query, version);
77 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
78 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
79
80 CosmasTree ct = new CosmasTree("Sonne und scheint");
81 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
82
83 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
84 }
85
86 @Test
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000087 public void testBooleanQuery() throws CQLParseException, IOException, QueryException{
88 query="((Sonne) or (Mond)) and (scheint)";
89 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000090 "{@type:korap:group, operation:operation:sequence, distances:[" +
Eliza Margarethaf267d4a2014-05-09 11:50:23 +000091 "{@type:korap:distance, key:w, min:0, max:0}" +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000092 "], operands:[" +
93 "{@type:korap:group, operation:operation:or, operands:[" +
94 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
95 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000096 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000097 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000098 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +000099 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000100 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000101 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000102
103
104 query="(scheint) and ((Sonne) or (Mond))";
105 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000106 "{@type:korap:group, operation:operation:sequence, distances:[" +
Eliza Margarethaf267d4a2014-05-09 11:50:23 +0000107 "{@type:korap:distance, key:w, min:0, max:0}" +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000108 "], operands:[" +
109 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}," +
110 "{@type:korap:group, operation:operation:or, operands:[" +
111 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
112 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000113 "]}" +
114 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +0000115 cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000116 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000117 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000118
119 }
120
121 @Test
122 public void testOrQuery() throws CQLParseException, IOException, QueryException{
123 query = "(Sonne) or (Mond)";
124 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000125 "{@type:korap:group, operation:operation:or, operands:[" +
126 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
127 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000128 "]}";
129
Eliza Margaretha724be112014-04-01 11:13:40 +0000130 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000131 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000132 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000133
134 query="(\"Sonne scheint\") or (Mond)";
135 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000136 "{@type:korap:group, operation:operation:or, operands:[" +
137 "{@type:korap:group, operation:operation:sequence, operands:[" +
138 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
139 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000140 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000141 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000142 "]}";
143
Eliza Margaretha724be112014-04-01 11:13:40 +0000144 cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000145 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000146 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000147
148 query="(\"Sonne scheint\") or (\"Mond scheint\")";
149 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000150 "{@type:korap:group, operation:operation:or, operands:[" +
151 "{@type:korap:group, operation:operation:sequence, operands:[" +
152 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
153 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000154 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000155 "{@type:korap:group, operation:operation:sequence, operands:[" +
156 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}," +
157 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000158 "]}" +
159 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +0000160 cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000161 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000162 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000163 }
164
165 @Test
166 public void testTermQuery() throws CQLParseException, IOException, QueryException{
167 query = "Sonne";
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000168 String jsonLd = "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}";
Eliza Margaretha724be112014-04-01 11:13:40 +0000169 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000170 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000171 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000172 }
173
174 @Test
175 public void testPhraseQuery() throws CQLParseException, IOException, QueryException{
176 query="\"der Mann\"";
177 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000178 "{@type:korap:group, operation:operation:sequence, operands:[" +
179 "{@type:korap:token, wrap:{@type:korap:term, key:der, layer:orth, match:match:eq}}," +
180 "{@type:korap:token, wrap:{@type:korap:term, key:Mann, layer:orth, match:match:eq}}" +
181 "]}";
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000182
Eliza Margaretha724be112014-04-01 11:13:40 +0000183 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000184 String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000185 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000186
187
188 query="der Mann schläft";
189 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000190 "{@type:korap:group, operation:operation:sequence, operands:[" +
191 "{@type:korap:token, wrap:{@type:korap:term, key:der, layer:orth, match:match:eq}}," +
192 "{@type:korap:token, wrap:{@type:korap:term, key:Mann, layer:orth, match:match:eq}}," +
193 "{@type:korap:token, wrap:{@type:korap:term, key:schläft, layer:orth, match:match:eq}}" +
194 "]}";
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000195
Eliza Margaretha724be112014-04-01 11:13:40 +0000196 cqlTree = new CQLTree(query, version);
Eliza Margaretha60514822014-04-23 11:32:43 +0000197 serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000198 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000199 }
200}