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 | |
| 8 | import de.ids_mannheim.korap.query.serialize.CQLTree; |
| 9 | import de.ids_mannheim.korap.util.QueryException; |
| 10 | |
| 11 | |
| 12 | public class CQLTest { |
| 13 | |
| 14 | String query; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame^] | 15 | String version ="1.2"; |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 16 | @Test |
| 17 | public void testBooleanQuery() throws CQLParseException, IOException, QueryException{ |
| 18 | query="((Sonne) or (Mond)) and (scheint)"; |
| 19 | String jsonLd = |
| 20 | "{@type=korap:group, operation=operation:sequence, distances=[" + |
| 21 | "{@type=korap:distance, key=t, min=0, max=0}" + |
| 22 | "], operands=[" + |
| 23 | "{@type=korap:group, operation=operation:or, operands=[" + |
| 24 | "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," + |
| 25 | "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" + |
| 26 | "]}," + |
| 27 | "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}" + |
| 28 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame^] | 29 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 30 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
| 31 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", "")); |
| 32 | |
| 33 | |
| 34 | query="(scheint) and ((Sonne) or (Mond))"; |
| 35 | jsonLd = |
| 36 | "{@type=korap:group, operation=operation:sequence, distances=[" + |
| 37 | "{@type=korap:distance, key=t, min=0, max=0}" + |
| 38 | "], operands=[" + |
| 39 | "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}," + |
| 40 | "{@type=korap:group, operation=operation:or, operands=[" + |
| 41 | "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," + |
| 42 | "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" + |
| 43 | "]}" + |
| 44 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame^] | 45 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 46 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
| 47 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", "")); |
| 48 | |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testOrQuery() throws CQLParseException, IOException, QueryException{ |
| 53 | query = "(Sonne) or (Mond)"; |
| 54 | String jsonLd = |
| 55 | "{@type=korap:group, operation=operation:or, operands=[" + |
| 56 | "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," + |
| 57 | "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" + |
| 58 | "]}"; |
| 59 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame^] | 60 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 61 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
| 62 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", "")); |
| 63 | |
| 64 | query="(\"Sonne scheint\") or (Mond)"; |
| 65 | jsonLd = |
| 66 | "{@type=korap:group, operation=operation:or, operands=[" + |
| 67 | "{@type=korap:group, operation=operation:sequence, operands=[" + |
| 68 | "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," + |
| 69 | "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}" + |
| 70 | "]}," + |
| 71 | "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" + |
| 72 | "]}"; |
| 73 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame^] | 74 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 75 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
| 76 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", "")); |
| 77 | |
| 78 | query="(\"Sonne scheint\") or (\"Mond scheint\")"; |
| 79 | jsonLd = |
| 80 | "{@type=korap:group, operation=operation:or, operands=[" + |
| 81 | "{@type=korap:group, operation=operation:sequence, operands=[" + |
| 82 | "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," + |
| 83 | "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}" + |
| 84 | "]}," + |
| 85 | "{@type=korap:group, operation=operation:sequence, operands=[" + |
| 86 | "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}," + |
| 87 | "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}" + |
| 88 | "]}" + |
| 89 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame^] | 90 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 91 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
| 92 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", "")); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testTermQuery() throws CQLParseException, IOException, QueryException{ |
| 97 | query = "Sonne"; |
| 98 | 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^] | 99 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 100 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
| 101 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", "")); |
| 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testPhraseQuery() throws CQLParseException, IOException, QueryException{ |
| 106 | query="\"der Mann\""; |
| 107 | String jsonLd = |
| 108 | "{@type=korap:group, operation=operation:sequence, operands=[" + |
| 109 | "{@type=korap:token, wrap={@type=korap:term, key=der, layer=orth, match=match:eq}}," + |
| 110 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" + |
| 111 | "]}"; |
| 112 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame^] | 113 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 114 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
| 115 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", "")); |
| 116 | |
| 117 | |
| 118 | query="der Mann schläft"; |
| 119 | jsonLd = |
| 120 | "{@type=korap:group, operation=operation:sequence, operands=[" + |
| 121 | "{@type=korap:token, wrap={@type=korap:term, key=der, layer=orth, match=match:eq}}," + |
| 122 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}," + |
| 123 | "{@type=korap:token, wrap={@type=korap:term, key=schläft, layer=orth, match=match:eq}}" + |
| 124 | "]}"; |
| 125 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame^] | 126 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 127 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
| 128 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", "")); |
| 129 | } |
| 130 | } |