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; |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 13 | import de.ids_mannheim.korap.util.QueryException; |
| 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) { |
| 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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 60 | @Test |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 61 | 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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 82 | public void testBooleanQuery() throws CQLParseException, IOException, QueryException{ |
| 83 | query="((Sonne) or (Mond)) and (scheint)"; |
| 84 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 85 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 91 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 92 | "{@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] | 93 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 94 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 95 | String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 96 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 97 | |
| 98 | |
| 99 | query="(scheint) and ((Sonne) or (Mond))"; |
| 100 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 101 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 108 | "]}" + |
| 109 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 110 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 111 | serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 112 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 113 | |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testOrQuery() throws CQLParseException, IOException, QueryException{ |
| 118 | query = "(Sonne) or (Mond)"; |
| 119 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 120 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 123 | "]}"; |
| 124 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 125 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 126 | String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 127 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 128 | |
| 129 | query="(\"Sonne scheint\") or (Mond)"; |
| 130 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 131 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 135 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 136 | "{@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] | 137 | "]}"; |
| 138 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 139 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 140 | serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 141 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 142 | |
| 143 | query="(\"Sonne scheint\") or (\"Mond scheint\")"; |
| 144 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 145 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 149 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 150 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 153 | "]}" + |
| 154 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 155 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 156 | serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 157 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void testTermQuery() throws CQLParseException, IOException, QueryException{ |
| 162 | query = "Sonne"; |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 163 | 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] | 164 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 165 | String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 166 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void testPhraseQuery() throws CQLParseException, IOException, QueryException{ |
| 171 | query="\"der Mann\""; |
| 172 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 173 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 177 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 178 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 179 | String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 180 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | query="der Mann schläft"; |
| 184 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 185 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 190 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 191 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 6051482 | 2014-04-23 11:32:43 +0000 | [diff] [blame] | 192 | serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query")); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 193 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 194 | } |
| 195 | } |