Updated exception tests.
Change-Id: Ib01f7c2fed7ad99ba28bd1b5e46fb2587ec44a31
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CqlQueryProcessor.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CqlQueryProcessor.java
index e2070bf..2dd8985 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/CqlQueryProcessor.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CqlQueryProcessor.java
@@ -12,7 +12,7 @@
/**
* @author margaretha
- * @date 09.05.14
+ * @date 30.05.16
*/
public class CqlQueryProcessor extends AbstractQueryProcessor {
@@ -26,12 +26,10 @@
private static final String OPERATION_OR = "operation:or";
private static final String OPERATION_SEQUENCE = "operation:sequence";
private static final String OPERATION_POSITION = "operation:position";
- private static final String KORAP_CONTEXT = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld";
private String version;
private boolean isCaseSensitive; // default true
-
public CqlQueryProcessor (String query) {
this(query, VERSION_1_2, true);
}
@@ -41,24 +39,23 @@
}
public CqlQueryProcessor (String query, String version,
- boolean isCaseSensitive) {
+ boolean isCaseSensitive) {
+ super();
this.version = version;
this.isCaseSensitive = isCaseSensitive;
- this.requestMap = new LinkedHashMap<>();
- requestMap.put("@context", KORAP_CONTEXT);
process(query);
}
@Override
- public Map<String, Object> getRequestMap () {
+ public Map<String, Object> getRequestMap() {
return this.requestMap;
}
@Override
- public void process (String query) {
+ public void process(String query) {
if ((query == null) || query.isEmpty())
- addError(StatusCodes.MALFORMED_QUERY,
- "SRU diagnostic 27: An empty query is unsupported.");
+ addError(StatusCodes.NO_QUERY,
+ "SRU diagnostic 27: Empty query is unsupported.");
CQLNode cqlNode = parseQuerytoCQLNode(query);
Map<String, Object> queryMap = parseCQLNode(cqlNode);
@@ -66,8 +63,7 @@
// requestMap.put("query", sentenceWrapper(queryMap));
}
-
- private Map<String, Object> sentenceWrapper (Map<String, Object> m) {
+ private Map<String, Object> sentenceWrapper(Map<String, Object> m) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("@type", "koral:group");
map.put("operation", OPERATION_POSITION);
@@ -85,8 +81,7 @@
return map;
}
-
- private CQLNode parseQuerytoCQLNode (String query) {
+ private CQLNode parseQuerytoCQLNode(String query) {
try {
int compat = -1;
switch (version) {
@@ -105,8 +100,7 @@
}
}
-
- private Map<String, Object> parseCQLNode (CQLNode node) {
+ private Map<String, Object> parseCQLNode(CQLNode node) {
if (node instanceof CQLTermNode) {
return parseTermNode((CQLTermNode) node);
@@ -126,8 +120,7 @@
}
}
-
- private Map<String, Object> parseTermNode (CQLTermNode node) {
+ private Map<String, Object> parseTermNode(CQLTermNode node) {
checkTermNode(node);
final String term = node.getTerm();
if ((term == null) || term.isEmpty()) {
@@ -143,8 +136,7 @@
}
}
-
- private Map<String, Object> parseAndNode (CQLAndNode node) {
+ private Map<String, Object> parseAndNode(CQLAndNode node) {
checkBooleanModifier(node);
Map<String, Object> map = new LinkedHashMap<String, Object>();
@@ -169,8 +161,7 @@
return map;
}
-
- private Map<String, Object> parseOrNode (CQLOrNode node) {
+ private Map<String, Object> parseOrNode(CQLOrNode node) {
checkBooleanModifier(node);
Map<String, Object> map = new LinkedHashMap<String, Object>();
@@ -185,8 +176,7 @@
return map;
}
-
- private Map<String, Object> writeSequence (String str) {
+ private Map<String, Object> writeSequence(String str) {
Map<String, Object> sequenceMap = new LinkedHashMap<String, Object>();
sequenceMap.put("@type", "koral:group");
sequenceMap.put("operation", OPERATION_SEQUENCE);
@@ -201,8 +191,7 @@
return sequenceMap;
}
-
- private Map<String, Object> writeTerm (String term) {
+ private Map<String, Object> writeTerm(String term) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
map.put("@type", "koral:term");
if (!isCaseSensitive) {
@@ -218,8 +207,7 @@
return tokenMap;
}
-
- private void checkBooleanModifier (CQLBooleanNode node) {
+ private void checkBooleanModifier(CQLBooleanNode node) {
List<Modifier> modifiers = node.getModifiers();
if ((modifiers != null) && !modifiers.isEmpty()) {
Modifier modifier = modifiers.get(0);
@@ -229,8 +217,7 @@
}
}
-
- private void checkTermNode (CQLTermNode node) {
+ private void checkTermNode(CQLTermNode node) {
// only allow "cql.serverChoice" and "words" index
if (!(INDEX_CQL_SERVERCHOICE.equals(node.getIndex()) || INDEX_WORDS
.equals(node.getIndex()))) {
diff --git a/src/test/java/de/ids_mannheim/korap/query/serialize/CqlQueryProcessorTest.java b/src/test/java/de/ids_mannheim/korap/query/serialize/CqlQueryProcessorTest.java
index 6f2bdd9..5af4b60 100644
--- a/src/test/java/de/ids_mannheim/korap/query/serialize/CqlQueryProcessorTest.java
+++ b/src/test/java/de/ids_mannheim/korap/query/serialize/CqlQueryProcessorTest.java
@@ -3,81 +3,58 @@
import static org.junit.Assert.assertEquals;
import java.io.IOException;
+import java.util.List;
import org.junit.Test;
import org.z3950.zing.cql.CQLParseException;
-import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
-import de.ids_mannheim.korap.query.serialize.CqlQueryProcessor;
-import de.ids_mannheim.korap.query.serialize.Cosmas2QueryProcessor;
-
-
public class CqlQueryProcessorTest {
String query;
- String version = "1.2";
+ String VERSION = "1.2";
ObjectMapper mapper = new ObjectMapper();
-
- @Test
- public void testExceptions () throws CQLParseException, IOException {
- query = "(Kuh) prox (Germ) ";
- try {
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
- }
- catch (Exception e) {
- int errorCode = Integer.parseInt(e.getMessage().split(":")[0]
- .replace("SRU diagnostic ", ""));
- assertEquals(48, errorCode);
- }
-
- query = "(Kuh) or/rel.combine=sum (Germ) ";
- try {
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
- }
- catch (Exception e) {
- int errorCode = Integer.parseInt(e.getMessage().split(":")[0]
- .replace("SRU diagnostic ", ""));
- assertEquals(20, errorCode);
- }
-
- query = "dc.title any Germ ";
- try {
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
- }
- catch (Exception e) {
- int errorCode = Integer.parseInt(e.getMessage().split(":")[0]
- .replace("SRU diagnostic ", ""));
- assertEquals(16, errorCode);
- }
-
- query = "cql.serverChoice any Germ ";
- try {
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
- }
- catch (Exception e) {
- int errorCode = Integer.parseInt(e.getMessage().split(":")[0]
- .replace("SRU diagnostic ", ""));
- assertEquals(19, errorCode);
- }
-
- query = "";
- try {
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
- }
- catch (Exception e) {
- int errorCode = Integer.parseInt(e.getMessage().split(":")[0]
- .replace("SRU diagnostic ", ""));
- assertEquals(27, errorCode);
- }
+ private List<Object> getError(CqlQueryProcessor processor) {
+ List<Object> errors = (List<Object>) processor.requestMap.get("errors");
+ return (List<Object>) errors.get(0);
}
+ @Test
+ public void testExceptions() throws CQLParseException, IOException {
+ query = "(Kuh) prox (Germ) ";
+ CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, VERSION);
+ List<Object> error = getError(cqlTree);
+ assertEquals(
+ "SRU diagnostic 48: Only basic search including term-only "
+ + "and boolean (AND,OR) operator queries are currently supported.",
+ error.get(1));
+
+ query = "(Kuh) or/rel.combine=sum (Germ) ";
+ error = getError(new CqlQueryProcessor(query, VERSION));
+ assertEquals(
+ "SRU diagnostic 20: Relation modifier rel.combine = sum is not supported.",
+ error.get(1));
+
+ query = "dc.title any Germ ";
+ error = getError(new CqlQueryProcessor(query, VERSION));
+ assertEquals("SRU diagnostic 16: Index dc.title is not supported.",
+ error.get(1));
+
+ query = "cql.serverChoice any Germ ";
+ error = getError(new CqlQueryProcessor(query, VERSION));
+ assertEquals("SRU diagnostic 19: Relation any is not supported.",
+ error.get(1));
+
+ query = "";
+ error = getError(new CqlQueryProcessor(query, VERSION));
+ assertEquals("SRU diagnostic 27: Empty query is unsupported.",
+ error.get(1));
+ }
@Test
- public void testAndQuery () throws CQLParseException, IOException,
- Exception {
+ public void testAndQuery() throws CQLParseException, IOException, Exception {
query = "(Sonne) and (scheint)";
String jsonLd = "{@type : koral:group, operation : operation:sequence, inOrder : false,"
+ "distances:[ "
@@ -87,19 +64,20 @@
+ "{@type : koral:token,wrap : {@type : koral:term,key : scheint,layer : orth,match : match:eq}"
+ "}]}";
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
+ CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, VERSION);
String serializedQuery = mapper.writeValueAsString(cqlTree
.getRequestMap().get("query"));
assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
- // /System.out.println(serializedQuery);
- //CosmasTree ct = new CosmasTree("Sonne und scheint");
- //serializedQuery = mapper.writeValueAsString(ct.getRequestMap().get("query"));
- //assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
+ // /System.out.println(serializedQuery);
+ // CosmasTree ct = new CosmasTree("Sonne und scheint");
+ // serializedQuery =
+ // mapper.writeValueAsString(ct.getRequestMap().get("query"));
+ // assertEquals(jsonLd.replace(" ", ""),
+ // serializedQuery.replace("\"", ""));
}
-
@Test
- public void testBooleanQuery () throws CQLParseException, IOException,
+ public void testBooleanQuery() throws CQLParseException, IOException,
Exception {
query = "((Sonne) or (Mond)) and (scheint)";
String jsonLd = "{@type:koral:group, operation:operation:sequence, inOrder : false, distances:["
@@ -111,12 +89,11 @@
+ "]},"
+ "{@type:koral:token, wrap:{@type:koral:term, key:scheint, layer:orth, match:match:eq}}"
+ "]}";
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
+ CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, VERSION);
String serializedQuery = mapper.writeValueAsString(cqlTree
.getRequestMap().get("query"));
assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
-
query = "(scheint) and ((Sonne) or (Mond))";
jsonLd = "{@type:koral:group, operation:operation:sequence, inOrder : false, distances:["
+ "{@type:koral:distance, key:s, min:0, max:0}"
@@ -126,23 +103,22 @@
+ "{@type:koral:token, wrap:{@type:koral:term, key:Sonne, layer:orth, match:match:eq}},"
+ "{@type:koral:token, wrap:{@type:koral:term, key:Mond, layer:orth, match:match:eq}}"
+ "]}" + "]}";
- cqlTree = new CqlQueryProcessor(query, version);
+ cqlTree = new CqlQueryProcessor(query, VERSION);
serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap()
.get("query"));
assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
}
-
@Test
- public void testOrQuery () throws CQLParseException, IOException, Exception {
+ public void testOrQuery() throws CQLParseException, IOException, Exception {
query = "(Sonne) or (Mond)";
String jsonLd = "{@type:koral:group, operation:operation:or, operands:["
+ "{@type:koral:token, wrap:{@type:koral:term, key:Sonne, layer:orth, match:match:eq}},"
+ "{@type:koral:token, wrap:{@type:koral:term, key:Mond, layer:orth, match:match:eq}}"
+ "]}";
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
+ CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, VERSION);
String serializedQuery = mapper.writeValueAsString(cqlTree
.getRequestMap().get("query"));
assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
@@ -156,7 +132,7 @@
+ "{@type:koral:token, wrap:{@type:koral:term, key:Mond, layer:orth, match:match:eq}}"
+ "]}";
- cqlTree = new CqlQueryProcessor(query, version);
+ cqlTree = new CqlQueryProcessor(query, VERSION);
serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap()
.get("query"));
assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
@@ -171,27 +147,25 @@
+ "{@type:koral:token, wrap:{@type:koral:term, key:Mond, layer:orth, match:match:eq}},"
+ "{@type:koral:token, wrap:{@type:koral:term, key:scheint, layer:orth, match:match:eq}}"
+ "]}" + "]}";
- cqlTree = new CqlQueryProcessor(query, version);
+ cqlTree = new CqlQueryProcessor(query, VERSION);
serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap()
.get("query"));
assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
}
-
@Test
- public void testTermQuery () throws CQLParseException, IOException,
+ public void testTermQuery() throws CQLParseException, IOException,
Exception {
query = "Sonne";
String jsonLd = "{@type:koral:token, wrap:{@type:koral:term, key:Sonne, layer:orth, match:match:eq}}";
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
+ CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, VERSION);
String serializedQuery = mapper.writeValueAsString(cqlTree
.getRequestMap().get("query"));
assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
}
-
@Test
- public void testPhraseQuery () throws CQLParseException, IOException,
+ public void testPhraseQuery() throws CQLParseException, IOException,
Exception {
query = "\"der Mann\"";
String jsonLd = "{@type:koral:group, operation:operation:sequence, operands:["
@@ -199,12 +173,11 @@
+ "{@type:koral:token, wrap:{@type:koral:term, key:Mann, layer:orth, match:match:eq}}"
+ "]}";
- CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, version);
+ CqlQueryProcessor cqlTree = new CqlQueryProcessor(query, VERSION);
String serializedQuery = mapper.writeValueAsString(cqlTree
.getRequestMap().get("query"));
assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
-
query = "der Mann schläft";
jsonLd = "{@type:koral:group, operation:operation:sequence, operands:["
+ "{@type:koral:token, wrap:{@type:koral:term, key:der, layer:orth, match:match:eq}},"
@@ -212,7 +185,7 @@
+ "{@type:koral:token, wrap:{@type:koral:term, key:schläft, layer:orth, match:match:eq}}"
+ "]}";
- cqlTree = new CqlQueryProcessor(query, version);
+ cqlTree = new CqlQueryProcessor(query, VERSION);
serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap()
.get("query"));
assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));