Fixed KoralQuery mirroring o parsing failures

Change-Id: Ia9d4e78b6879187f5705e3eff43532a73ba10c07
diff --git a/Changes b/Changes
index 3390d95..0f0ddee 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.52 2015-06-30
+0.52 2015-07-02
         - [bugfix] Fixed payload filtering in FocusSpans (margaretha)
 	- [workaround] Reintroduced empty collection support,
 	  as Koral still creates them (diewald)
@@ -16,10 +16,13 @@
 	  (Kustvakt bug/diewald)
 	- [bugfix] Fixed treatment of several collection types (diewald)
 	- [bugfix] Remove legacy error handling in Match (diewald)
-	- [bugfix] JSON-serialization bug in match in case of error messages (diewald)
+	- [bugfix] JSON-serialization bug in match in case of
+	  error messages (diewald)
 	- [bugfix] 'fields' serialization (diewald)
 	- [bugfix] Deserialization of group:disjunction (diewald)
 	- [bugfix] Deserialization of wrapped spans (diewald)
+	- [bugfix] Fix KoralQuery mirroring in case of
+	  parsing failures (diewald)
 
 0.51 2015-03-17
         - This is a major version (prepared for the GitHub release)
diff --git a/src/main/java/de/ids_mannheim/korap/Krill.java b/src/main/java/de/ids_mannheim/korap/Krill.java
index 6dfed6b..ee6e42b 100644
--- a/src/main/java/de/ids_mannheim/korap/Krill.java
+++ b/src/main/java/de/ids_mannheim/korap/Krill.java
@@ -167,10 +167,10 @@
         if (json.has("query")) {
             try {
                 KrillQuery kq = new KrillQuery("tokens");
-                SpanQueryWrapper qw = kq.fromJson(json.get("query"));
-
                 this.setQuery(kq);
 
+                SpanQueryWrapper qw = kq.fromJson(json.get("query"));
+
                 // Throw an error, in case the query matches everywhere
                 if (qw.isEmpty())
                     this.addError(780, "This query matches everywhere");
@@ -212,20 +212,19 @@
 
                 // TODO: Temporary
                 if (collNode.fieldNames().hasNext()) {
-                    KrillCollection kc = new KrillCollection()
-                            .fromJson(collNode);
+                    KrillCollection kc = new KrillCollection();
                     this.setCollection(kc);
+                    kc.fromJson(collNode);
                 };
             }
 
             // <legacycode>
             else if (json.has("collections")) {
                 KrillCollection kc = new KrillCollection();
+                this.setCollection(kc);
                 for (JsonNode collection : json.get("collections")) {
                     kc.fromJsonLegacy(collection);
                 };
-
-                this.setCollection(kc);
             };
             // </legacycode>
         }
@@ -234,7 +233,8 @@
         };
 
         // Parse "meta" attribute
-        if (!this.hasErrors() && json.has("meta"))
+        // !this.hasErrors() && 
+        if (json.has("meta"))
             this.setMeta(new KrillMeta(json.get("meta")));
 
         return this;
diff --git a/src/main/java/de/ids_mannheim/korap/KrillIndex.java b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
index c10e9b9..ed3b06e 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillIndex.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillIndex.java
@@ -1240,7 +1240,7 @@
         // Todo: Make kr subclassing ks - so ks has a method for a new Result!
         Result kr = new Result(query.toString(), meta.getStartIndex(),
                 meta.getCount(), meta.getContext());
-
+        
         // Set version info to result
         if (this.getVersion() != null)
             kr.setVersion(this.getVersion());
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestTemporaryQueryLimitations.java b/src/test/java/de/ids_mannheim/korap/query/TestTemporaryQueryLimitations.java
index 180fecb..3d25377 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestTemporaryQueryLimitations.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestTemporaryQueryLimitations.java
@@ -22,6 +22,11 @@
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
+/**
+ * These tests are meant to fail in a predictable way - but they describe
+ * temporary behaviour and will be disabled once these features are part of Krill.
+ */
+
 @RunWith(JUnit4.class)
 public class TestTemporaryQueryLimitations {
 
@@ -52,9 +57,9 @@
         assertEquals(kr.getStartIndex(), 0);
 
         assertEquals("This is a warning coming from the serialization", kr
-                .getWarning(1).getMessage());
+                     .getWarning(0).getMessage());
         assertEquals("Class reference checks are currently not supported"
-                + " - results may not be correct", kr.getWarning(0)
-                .getMessage());
+                     + " - results may not be correct", kr.getWarning(1)
+                     .getMessage());
     };
 };
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestKrill.java b/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
index fd2e7c3..87eeea3 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
@@ -2,6 +2,7 @@
 
 import static de.ids_mannheim.korap.TestSimple.getString;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 
@@ -261,6 +262,41 @@
         assertEquals(kr.getTotalResults(), 0);
     };
 
+    /*
+     * Queries should be mirrored correctly for debugging reasons.
+     */
+    @Test
+    public void queryJSONmirrorTestBug () throws IOException {
+        // Construct index
+        KrillIndex ki = new KrillIndex();
+        String json = getString(getClass().getResource(
+            "/queries/bugs/failing_mirror.jsonld").getFile()
+        );
+        Krill ks = new Krill(json);
+        Result kr = ks.apply(ki);
+
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode res = mapper.readTree(kr.toJsonString());
+
+        assertEquals("Unable to parse JSON", res.at("/errors/0/1").asText());
+
+        json = getString(getClass().getResource(
+            "/queries/bugs/failing_mirror_2.jsonld").getFile()
+        );
+        ks = new Krill(json);
+        kr = ks.apply(ki);
+
+        res = mapper.readTree(kr.toJsonString());
+
+        assertEquals(23,res.at("/meta/count").asInt());
+        assertEquals(25,res.at("/meta/itemsPerPage").asInt());
+        assertEquals("base/s:p", res.at("/meta/context").asText());
+        assertFalse(res.at("/query").isMissingNode());
+        assertTrue(res.at("/query/@type").isMissingNode());
+        assertTrue(res.at("/collection/@type").isMissingNode());
+    };
+
+
 
     @Test
     public void searchJSONcontext () throws IOException {
diff --git a/src/test/resources/queries/bugs/failing_mirror.jsonld b/src/test/resources/queries/bugs/failing_mirror.jsonld
new file mode 100644
index 0000000..98f73d1
--- /dev/null
+++ b/src/test/resources/queries/bugs/failing_mirror.jsonld
@@ -0,0 +1,23 @@
+{
+  "@context":"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+  "messages":[
+    [303,"Deprecated 2014-07-24: 'min' and 'max' to be supported until 3 months from deprecation date."]
+  ],
+  "query":{
+    "wrap":{
+      "@type":"koral:term",
+      "key":"s"
+    }
+  },
+  "meta":{
+    "startPage":1,
+    "count":25,
+    "cutOff":true,
+    "context":"base/s:p"
+  },
+  "collection": {
+    "value": "Goethe",
+    "match": "match:eq",
+    "key": "author",
+  }
+}
diff --git a/src/test/resources/queries/bugs/failing_mirror_2.jsonld b/src/test/resources/queries/bugs/failing_mirror_2.jsonld
new file mode 100644
index 0000000..a89bc6c
--- /dev/null
+++ b/src/test/resources/queries/bugs/failing_mirror_2.jsonld
@@ -0,0 +1,22 @@
+{
+  "@context":"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+  "messages":[
+    [303,"Deprecated 2014-07-24: 'min' and 'max' to be supported until 3 months from deprecation date."]
+  ],
+  "query":{
+    "wrap":{
+      "@type":"koral:term",
+      "key":"s"
+    }
+  },
+  "meta":{
+    "startPage":2,
+    "count":23,
+    "context":"base/s:p"
+  },
+  "collection": {
+    "value": "Goethe",
+    "match": "match:eq",
+    "key": "author"
+  }
+}