You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2020/05/14 12:48:07 UTC

[sling-org-apache-sling-graphql-core] branch master updated: Add JsonSerializerTest

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-graphql-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 716484e  Add JsonSerializerTest
716484e is described below

commit 716484e5d8334138377e0ffc4e4a2c41368baa2a
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu May 14 14:46:40 2020 +0200

    Add JsonSerializerTest
---
 .../sling/graphql/core/json/JsonSerializer.java    |  3 +-
 .../graphql/core/json/JsonSerializerTest.java      | 86 ++++++++++++++++++++++
 2 files changed, 88 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/sling/graphql/core/json/JsonSerializer.java b/src/main/java/org/apache/sling/graphql/core/json/JsonSerializer.java
index 6856372..06bdafe 100644
--- a/src/main/java/org/apache/sling/graphql/core/json/JsonSerializer.java
+++ b/src/main/java/org/apache/sling/graphql/core/json/JsonSerializer.java
@@ -23,6 +23,7 @@ package org.apache.sling.graphql.core.json;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintWriter;
+import java.io.Writer;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -45,7 +46,7 @@ public class JsonSerializer {
         }
     };
 
-    public void sendJSON(PrintWriter out, ExecutionResult result) throws IOException {
+    public void sendJSON(Writer out, ExecutionResult result) throws IOException {
         final Object data = result.toSpecification();
         if (data == null) {
             throw new IOException("No data");
diff --git a/src/test/java/org/apache/sling/graphql/core/json/JsonSerializerTest.java b/src/test/java/org/apache/sling/graphql/core/json/JsonSerializerTest.java
new file mode 100644
index 0000000..44433e1
--- /dev/null
+++ b/src/test/java/org/apache/sling/graphql/core/json/JsonSerializerTest.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.graphql.core.json;
+
+import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import graphql.ExecutionResult;
+
+public class JsonSerializerTest {
+    private final JsonSerializer serializer = new JsonSerializer();
+
+    private static final Map<String, Object> TEST_MAP = toMap("A", "bc", "farenheit", 451, "really", true, "itsNothing",
+            null, "map", toMap("one", 1, "two", toMap("three", 3)));
+
+    private static Map<String, Object> toMap(Object... keyValuePairs) {
+        final Map<String, Object> result = new HashMap<>();
+        for (int i = 0; i < keyValuePairs.length; i += 2) {
+            result.put(keyValuePairs[i].toString(), keyValuePairs[i + 1]);
+        }
+        return result;
+    }
+
+    private void assertHasTestData(String json) {
+        assertThat(json, hasJsonPath("A", equalTo("bc")));
+        assertThat(json, hasJsonPath("farenheit", equalTo(451)));
+        assertThat(json, hasJsonPath("really", equalTo(true)));
+        assertThat(json, hasJsonPath("itsNothing", equalTo(null)));
+        assertThat(json, hasJsonPath("map.one", equalTo(1)));
+        assertThat(json, hasJsonPath("map.two.three", equalTo(3)));
+    }
+
+    @Test
+    public void testSendJSON() throws IOException {
+        final ExecutionResult r = Mockito.mock(ExecutionResult.class);
+        Mockito.when(r.toSpecification()).thenReturn(TEST_MAP);
+        final StringWriter w = new StringWriter();
+        serializer.sendJSON(w, r);
+        assertHasTestData(w.toString());
+    }
+
+    @Test
+    public void testToJSON() {
+        assertHasTestData(serializer.toJSON(TEST_MAP));
+    }
+
+    @Test
+    public void testToMap() throws UnsupportedEncodingException {
+        final String json = serializer.toJSON(TEST_MAP);
+        final Map<String, Object> map = serializer.JSONtoMaps(new ByteArrayInputStream(json.getBytes("UTF-8")));
+        assertThat(map.get("A"), is("bc"));
+        assertThat(map.get("farenheit"), is(451L));
+        assertThat(map.get("really"), is(true));
+        assertThat(map.get("itsNothing"), nullValue());
+        assertThat(map.get("map").toString(), equalTo("{one=1, two={three=3}}"));
+    }
+}