Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 1 | import static org.junit.Assert.assertEquals; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import org.junit.Test; |
| 6 | import org.z3950.zing.cql.CQLParseException; |
| 7 | |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 8 | import com.fasterxml.jackson.databind.JsonNode; |
| 9 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 11 | import de.ids_mannheim.korap.query.serialize.CQLTree; |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 12 | import de.ids_mannheim.korap.query.serialize.CosmasTree; |
Joachim Bingel | 6003b85 | 2014-12-18 14:20:55 +0000 | [diff] [blame] | 13 | import de.ids_mannheim.korap.query.serialize.util.QueryException; |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | public class CQLTest { |
| 17 | |
| 18 | String query; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 19 | String version ="1.2"; |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 20 | ObjectMapper mapper = new ObjectMapper(); |
Eliza Margaretha | 4679357 | 2014-04-02 15:11:25 +0000 | [diff] [blame] | 21 | |
| 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 Margaretha | f267d4a | 2014-05-09 11:50:23 +0000 | [diff] [blame] | 28 | int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", "")); |
| 29 | assertEquals(48,errorCode); |
Eliza Margaretha | 4679357 | 2014-04-02 15:11:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | query = "(Kuh) or/rel.combine=sum (Germ) "; |
| 33 | try { |
| 34 | CQLTree cqlTree = new CQLTree(query, version); |
| 35 | }catch (QueryException e) { |
Eliza Margaretha | f267d4a | 2014-05-09 11:50:23 +0000 | [diff] [blame] | 36 | int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", "")); |
| 37 | assertEquals(20,errorCode); |
Eliza Margaretha | 4679357 | 2014-04-02 15:11:25 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | query = "dc.title any Germ "; |
| 41 | try { |
| 42 | CQLTree cqlTree = new CQLTree(query, version); |
| 43 | } catch (QueryException e) { |
Eliza Margaretha | f267d4a | 2014-05-09 11:50:23 +0000 | [diff] [blame] | 44 | int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", "")); |
| 45 | assertEquals(16,errorCode); |
Eliza Margaretha | 4679357 | 2014-04-02 15:11:25 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | query = "cql.serverChoice any Germ "; |
| 49 | try { |
| 50 | CQLTree cqlTree = new CQLTree(query, version); |
| 51 | } catch (QueryException e) { |
Eliza Margaretha | f267d4a | 2014-05-09 11:50:23 +0000 | [diff] [blame] | 52 | int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", "")); |
| 53 | assertEquals(19,errorCode); |
Eliza Margaretha | 4679357 | 2014-04-02 15:11:25 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | query = ""; |
| 57 | try { |
| 58 | CQLTree cqlTree = new CQLTree(query, version); |
| 59 | } catch (QueryException e) { |
Eliza Margaretha | f267d4a | 2014-05-09 11:50:23 +0000 | [diff] [blame] | 60 | int errorCode = Integer.parseInt(e.getMessage().split(":")[0].replace("SRU diagnostic ", "")); |
| 61 | assertEquals(27,errorCode); |
Eliza Margaretha | 4679357 | 2014-04-02 15:11:25 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Eliza Margaretha | 95ccd41 | 2014-10-01 10:29:56 +0000 | [diff] [blame] | 65 | @Test |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 66 | public void testAndQuery() throws CQLParseException, IOException, QueryException{ |
| 67 | query="(Sonne) and (scheint)"; |
| 68 | String jsonLd = |
Eliza Margaretha | 95ccd41 | 2014-10-01 10:29:56 +0000 | [diff] [blame] | 69 | "{@type : korap:group, operation : operation:sequence, inOrder : false," + |
| 70 | "distances:[ "+ |
| 71 | "{@type : korap:distance, key : s, min : 0, max : 0 } ],"+ |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 72 | "operands : ["+ |
| 73 | "{@type : korap:token, wrap : {@type : korap:term,key : Sonne, layer : orth, match : match:eq}}," + |
| 74 | "{@type : korap:token,wrap : {@type : korap:term,key : scheint,layer : orth,match : match:eq}" + |
| 75 | "}]}"; |
| 76 | |
| 77 | CQLTree cqlTree = new CQLTree(query, version); |
| 78 | String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
| 79 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 95ccd41 | 2014-10-01 10:29:56 +0000 | [diff] [blame] | 80 | // /System.out.println(serializedQuery); |
| 81 | //CosmasTree ct = new CosmasTree("Sonne und scheint"); |
| 82 | //serializedQuery = mapper.writeValueAsString(ct.getRequestMap().get("query")); |
| 83 | //assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
| 84 | } |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 85 | |
Eliza Margaretha | 95ccd41 | 2014-10-01 10:29:56 +0000 | [diff] [blame] | 86 | @Test |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 87 | public void testBooleanQuery() throws CQLParseException, IOException, QueryException{ |
| 88 | query="((Sonne) or (Mond)) and (scheint)"; |
| 89 | String jsonLd = |
Eliza Margaretha | 95ccd41 | 2014-10-01 10:29:56 +0000 | [diff] [blame] | 90 | "{@type:korap:group, operation:operation:sequence, inOrder : false, distances:[" + |
| 91 | "{@type:korap:distance, key:s, min:0, max:0}" + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 92 | "], 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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 96 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 97 | "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 98 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 99 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 100 | String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 101 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | query="(scheint) and ((Sonne) or (Mond))"; |
| 105 | jsonLd = |
Eliza Margaretha | 95ccd41 | 2014-10-01 10:29:56 +0000 | [diff] [blame] | 106 | "{@type:korap:group, operation:operation:sequence, inOrder : false, distances:[" + |
| 107 | "{@type:korap:distance, key:s, min:0, max:0}" + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 108 | "], 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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 113 | "]}" + |
| 114 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 115 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 116 | serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 117 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 118 | |
Eliza Margaretha | 95ccd41 | 2014-10-01 10:29:56 +0000 | [diff] [blame] | 119 | } |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 120 | |
| 121 | @Test |
| 122 | public void testOrQuery() throws CQLParseException, IOException, QueryException{ |
| 123 | query = "(Sonne) or (Mond)"; |
| 124 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 125 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 128 | "]}"; |
| 129 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 130 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 131 | String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 132 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 133 | |
| 134 | query="(\"Sonne scheint\") or (Mond)"; |
| 135 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 136 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 140 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 141 | "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 142 | "]}"; |
| 143 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 144 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 145 | serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 146 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 147 | |
| 148 | query="(\"Sonne scheint\") or (\"Mond scheint\")"; |
| 149 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 150 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 154 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 155 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 158 | "]}" + |
| 159 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 160 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 161 | serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 162 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void testTermQuery() throws CQLParseException, IOException, QueryException{ |
| 167 | query = "Sonne"; |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 168 | String jsonLd = "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 169 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 170 | String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 171 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Test |
| 175 | public void testPhraseQuery() throws CQLParseException, IOException, QueryException{ |
| 176 | query="\"der Mann\""; |
| 177 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 178 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 182 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 183 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 184 | String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 185 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 186 | |
| 187 | |
| 188 | query="der Mann schläft"; |
| 189 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 190 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 195 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 196 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 197 | serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 198 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 199 | } |
| 200 | } |