nullpointer fix for multiple toJson calls on one serializer instance

Change-Id: Ibc6153b82348fa43814a30af49a85162ac7febfa
diff --git a/pom.xml b/pom.xml
index 8f8a82d..7f7f2d4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -72,7 +72,8 @@
     <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
-      <version>0.11.8</version>
+      <version>1.16.6</version>
+        <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>log4j</groupId>
diff --git a/src/main/antlr/poliqarpplus/PoliqarpPlusLexer.g4 b/src/main/antlr/poliqarpplus/PoliqarpPlusLexer.g4
index f8b5d91..68c0bef 100644
--- a/src/main/antlr/poliqarpplus/PoliqarpPlusLexer.g4
+++ b/src/main/antlr/poliqarpplus/PoliqarpPlusLexer.g4
@@ -60,10 +60,8 @@
 
 NL                  : [\r\n] -> skip;
 
-
 WORD                : ALPHABET+;
 
-
 /* Complex queries */
 LPAREN      : '[';
 RPAREN      : ']';
@@ -101,4 +99,5 @@
 fragment RE_expr     : ('.' | RE_char | RE_alter | RE_chgroup | RE_opt | RE_quant | RE_group)+;
 fragment RE_dquote            : '"'  (RE_expr | '\'' | ':' )* '"';
 fragment RE_squote            : '\''  (RE_expr | '\"' | ':' )* '\'';
-REGEX             : ( RE_dquote | RE_squote );
\ No newline at end of file
+ 
+REGEX             : ( RE_dquote | RE_squote | );
diff --git a/src/main/antlr/poliqarpplus/PoliqarpPlusParser.g4 b/src/main/antlr/poliqarpplus/PoliqarpPlusParser.g4
index 9e9c595..c48a3f9 100644
--- a/src/main/antlr/poliqarpplus/PoliqarpPlusParser.g4
+++ b/src/main/antlr/poliqarpplus/PoliqarpPlusParser.g4
@@ -231,4 +231,4 @@
     Entry point for all requests. Linguistic query is obligatory, metadata filtering
     is optional.
 */
-request            : query within? meta? EOF;
\ No newline at end of file
+request            : query within? meta? EOF;
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryProcessor.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryProcessor.java
index e051e19..1baa50a 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryProcessor.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CollectionQueryProcessor.java
@@ -261,6 +261,7 @@
 
         TokenStream stream = parser.getTokenStream();
         String stm = stream.getText(valueNode.getChild(0).getSourceInterval());
+        // todo: is this correct?
         if (stm.startsWith("\"") && stm.endsWith("\""))
             stm = stm.replaceAll("\"", "");
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQueryBuilder.java b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQueryBuilder.java
index f743aba..3726a56 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQueryBuilder.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/MetaQueryBuilder.java
@@ -1,6 +1,9 @@
 package de.ids_mannheim.korap.query.serialize;
 
+import lombok.AccessLevel;
 import lombok.Data;
+import lombok.Getter;
+import lombok.Setter;
 
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessor.java b/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessor.java
index c3bd53f..3ff73df 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessor.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessor.java
@@ -327,7 +327,11 @@
             // no 'term' child, but direct key specification: process here
             LinkedHashMap<String, Object> term = KoralObjectGenerator
                     .makeTerm();
+
             String key = node.getChild(0).getText();
+            TokenStream stream = parser.getTokenStream();
+            String kval = stream.getText(node.getChild(0).getSourceInterval());
+
             if (getNodeCat(node.getChild(0).getChild(0)).equals("regex")) {
                 isRegex = true;
                 term.put("type", "type:regex");
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
index c8f6c56..27c16e7 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/QuerySerializer.java
@@ -172,7 +172,7 @@
 
     private Map raw() {
         if (ast != null) {
-            Map<String, Object> requestMap = ast.getRequestMap();
+            Map<String, Object> requestMap = new HashMap<>(ast.getRequestMap());
             Map meta = (Map) requestMap.get("meta");
             Map collection = (Map) requestMap.get("collection");
             List errors = (List) requestMap.get("errors");
@@ -181,8 +181,8 @@
             collection = mergeCollection(collection, this.collection);
             requestMap.put("collection", collection);
             if (this.meta != null) {
-                this.meta.putAll(meta);
-                requestMap.put("meta", this.meta);
+                meta.putAll(this.meta);
+                requestMap.put("meta", meta);
             }
             if (this.errors != null && !this.errors.isEmpty()) {
                 errors.addAll(this.errors);
@@ -255,11 +255,6 @@
         return this;
     }
 
-    public QuerySerializer setMeta(MetaQueryBuilder meta) {
-        this.meta = meta.raw();
-        return this;
-    }
-
     public QuerySerializer setCollection(String collection) {
         CollectionQueryProcessor tree = new CollectionQueryProcessor();
         tree.process(collection);
diff --git a/src/test/java/de/ids_mannheim/korap/query/serialize/CollectionQueryDuplicateTest.java b/src/test/java/de/ids_mannheim/korap/query/serialize/CollectionQueryDuplicateTest.java
index d3c1faa..ed8e40f 100644
--- a/src/test/java/de/ids_mannheim/korap/query/serialize/CollectionQueryDuplicateTest.java
+++ b/src/test/java/de/ids_mannheim/korap/query/serialize/CollectionQueryDuplicateTest.java
@@ -2,10 +2,14 @@
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Assert;
 import org.junit.Test;
 
 import java.io.IOException;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
 /**
  * @author hanl
  * @date 28/07/2015
@@ -13,20 +17,16 @@
 public class CollectionQueryDuplicateTest {
 
     @Test
-    public void testCollectionQueryDuplicateThrowsAssertionException() {
+    public void testCollectionQueryDuplicateThrowsAssertionException() throws IOException {
         QuerySerializer serializer = new QuerySerializer();
         serializer.setQuery("[base=Haus]", "poliqarp");
         serializer.setCollection("textClass=politik & corpusID=WPD");
         ObjectMapper m = new ObjectMapper();
-        try {
-            JsonNode first = m.readTree(serializer.toJSON());
-            JsonNode second = m.readTree(serializer.toJSON());
-
-            assert first.at("/collection").equals(second.at("/collection"));
-
-        }catch (IOException e) {
-            e.printStackTrace();
-        }
+        JsonNode first = m.readTree(serializer.toJSON());
+        assertNotNull(first);
+        assertEquals(first.at("/collection"), m.readTree(serializer.toJSON()).at("/collection"));
+        assertEquals(first.at("/collection"), m.readTree(serializer.toJSON()).at("/collection"));
+        assertEquals(first.at("/collection"), m.readTree(serializer.toJSON()).at("/collection"));
     }
 
 }
diff --git a/src/test/java/de/ids_mannheim/korap/query/serialize/EmptyResultsTest.java b/src/test/java/de/ids_mannheim/korap/query/serialize/EmptyResultsTest.java
index fd2e64f..58d775b 100644
--- a/src/test/java/de/ids_mannheim/korap/query/serialize/EmptyResultsTest.java
+++ b/src/test/java/de/ids_mannheim/korap/query/serialize/EmptyResultsTest.java
@@ -5,6 +5,8 @@
 import org.junit.Assert;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+
 /**
  * @author hanl
  * @date 02/07/2015
@@ -18,8 +20,8 @@
         QuerySerializer s = new QuerySerializer();
         s.setQuery("prox/unit=word/distance<=5", "cql");
         JsonNode node = mapper.valueToTree(s.build());
-        Assert.assertEquals(node.has("query"), false);
-        Assert.assertEquals(node.has("collection"), false);
+        assertEquals(node.has("query"), false);
+        assertEquals(node.has("collection"), false);
     }
 
     @Test
@@ -28,8 +30,8 @@
         s.setQuery("[base=Wort]", "poliqarp");
 
         JsonNode node = mapper.valueToTree(s.build());
-        Assert.assertEquals(node.has("query"), true);
-        Assert.assertEquals(node.has("collection"), false);
+        assertEquals(node.has("query"), true);
+        assertEquals(node.has("collection"), false);
     }
 
     @Test
@@ -38,7 +40,7 @@
         s.setQuery("[base=Wort]", "poliqarp");
 
         JsonNode node = mapper.valueToTree(s.build());
-        Assert.assertEquals(node.has("meta"), false);
+        assertEquals(node.has("meta"), false);
     }
 
 }
diff --git a/src/test/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessorTest.java b/src/test/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessorTest.java
index 171f1b3..b918649 100644
--- a/src/test/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessorTest.java
+++ b/src/test/java/de/ids_mannheim/korap/query/serialize/PoliqarpPlusQueryProcessorTest.java
@@ -148,6 +148,7 @@
         query = "z.B./x";
         qs.setQuery(query, "poliqarpplus");
         res = mapper.readTree(qs.toJSON());
+        System.out.println("QUERY IS  "+ res);
         assertEquals("koral:token", res.at("/query/@type").asText());
         assertEquals("koral:term", res.at("/query/wrap/@type").asText());
         assertEquals(".*?z\\.B\\..*?", res.at("/query/wrap/key").asText());
@@ -159,12 +160,13 @@
         query = "\"a\\.\"";
         qs.setQuery(query, "poliqarpplus");
         res = mapper.readTree(qs.toJSON());
+        System.out.println("QUERY IS  "+ res);
         assertEquals("koral:token", res.at("/query/@type").asText());
         assertEquals("koral:term", res.at("/query/wrap/@type").asText());
-        assertEquals("a\\.", res.at("/query/wrap/key").asText());
         assertEquals("type:regex", res.at("/query/wrap/type").asText());
         assertEquals("orth", res.at("/query/wrap/layer").asText());
         assertEquals("match:eq", res.at("/query/wrap/match").asText());
+        assertEquals("a\\.", res.at("/query/wrap/key").asText());
     }