Fixed SpanWithinQuery deserialization, issue #21.

Change-Id: Ibf64e09b3a7073355f86f5cbb1511243c9d65ce1
diff --git a/src/main/java/de/ids_mannheim/korap/KrillQuery.java b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
index 5fb8c0e..4f5901f 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
@@ -32,6 +32,7 @@
 import de.ids_mannheim.korap.query.wrap.SpanWithinQueryWrapper;
 import de.ids_mannheim.korap.response.Notifications;
 import de.ids_mannheim.korap.util.QueryException;
+import de.ids_mannheim.korap.util.StatusCodes;
 
 /**
  * <p>
@@ -570,15 +571,17 @@
             direction = "<:";
         }
 
-        if (!relation.has("@type"))
+        if (!relation.has("@type")){
             throw new QueryException(701,
                     "JSON-LD group has no @type attribute");
-
+        }
+        
+        SpanRelationWrapper spanRelationWrapper;
         if (relation.get("@type").asText().equals("koral:relation")) {
             if (!relation.has("wrap")) {
                 throw new QueryException(718, "Missing relation term");
             }
-            // fix me: termgroup relation
+            
             SpanQueryWrapper relationWrapper =
                     _termFromJson(relation.get("wrap"), direction);
             return new SpanRelationWrapper(relationWrapper, operand1, operand2);
@@ -586,6 +589,13 @@
         else {
             throw new QueryException(713, "Query type is not supported");
         }
+        
+//        if (relation.has("boundary")){
+//            _operationRepetitionFromJson(relation, operands);
+//        }
+//        else{
+//            
+//        }
     }
 
 
@@ -634,6 +644,11 @@
         byte flag = WITHIN;
         switch (frame) {
             case "isAround":
+                JsonNode operand = operands.get(0);
+                if (operand.get("@type").asText().equals("koral:token")){
+                    throw new QueryException(StatusCodes.INVALID_QUERY, 
+                            "Token cannot contain another token or element.");
+                }
                 break;
             case "strictlyContains":
                 flag = REAL_WITHIN;
@@ -676,7 +691,7 @@
                 throw new QueryException(706, "Frame type is unknown");
         };
 
-        // The exclusion operator is no longer relevant
+        
         // <legacyCode>
         Boolean exclude;
         if (json.has("exclude") && json.get("exclude").asBoolean()) {
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanWithinQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanWithinQueryJSON.java
new file mode 100644
index 0000000..3b135be
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanWithinQueryJSON.java
@@ -0,0 +1,30 @@
+package de.ids_mannheim.korap.query;
+
+import static de.ids_mannheim.korap.TestSimple.getJSONQuery;
+import static org.junit.Assert.assertEquals;
+
+import org.apache.lucene.search.spans.SpanQuery;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
+import de.ids_mannheim.korap.util.QueryException;
+
+public class TestSpanWithinQueryJSON {
+    
+    @Rule
+    public ExpectedException exception = ExpectedException.none();
+    
+    @Test
+    public void testTokenContainSentence () throws QueryException {
+        exception.expectMessage("Token cannot contain another token or element.");
+        
+        String filepath = getClass()
+                .getResource(
+                        "/queries/position/token-contain-sentence.json")
+                .getFile();
+        SpanQueryWrapper sqwi = getJSONQuery(filepath);
+        SpanQuery sq = sqwi.toQuery();
+    }
+}
diff --git a/src/test/resources/queries/position/token-contain-sentence.json b/src/test/resources/queries/position/token-contain-sentence.json
new file mode 100644
index 0000000..02ce5f0
--- /dev/null
+++ b/src/test/resources/queries/position/token-contain-sentence.json
@@ -0,0 +1,29 @@
+{
+    "query": {
+        "operands": [
+            {
+                "@type": "koral:token",
+                "wrap": {
+                    "foundry": "sgbr",
+                    "@type": "koral:term",
+                    "match": "match:eq",
+                    "key": "ADJA",
+                    "layer": "p"
+                }
+            },
+            {
+                "@type": "koral:span",
+                "wrap": {
+                    "foundry": "base",
+                    "@type": "koral:term",
+                    "layer": "s",
+                    "key": "s"
+                }
+            }
+        ],
+        "frames": ["frames:isAround"],
+        "@type": "koral:group",
+        "operation": "operation:position"
+    },
+    "@context": "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld"
+}