From 4ea0b45b8ae9f6e89334ccbd0c18c8b843a16c9b Mon Sep 17 00:00:00 2001 From: Gmugra Date: Mon, 28 Dec 2020 14:21:47 +0100 Subject: [PATCH] more unit tests for json-gson --- core/pom.xml | 3 +- .../routing/gson/SimpleGsonProducer.java | 4 ++ .../routing/gson/SimpleGsonConsumerTest.java | 21 +++++++-- .../routing/gson/SimpleGsonProducerTest.java | 46 ++++++++++++++++--- 4 files changed, 63 insertions(+), 11 deletions(-) diff --git a/core/pom.xml b/core/pom.xml index d2a72c3..ca381ca 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -36,12 +36,11 @@ - + maven-checkstyle-plugin true ${project.parent.basedir}/checkstyle_checks.xml - diff --git a/json-gson/src/main/java/net/cactusthorn/routing/gson/SimpleGsonProducer.java b/json-gson/src/main/java/net/cactusthorn/routing/gson/SimpleGsonProducer.java index 77a3aa7..d4b6d87 100644 --- a/json-gson/src/main/java/net/cactusthorn/routing/gson/SimpleGsonProducer.java +++ b/json-gson/src/main/java/net/cactusthorn/routing/gson/SimpleGsonProducer.java @@ -33,6 +33,10 @@ public void produce(Object object, String template, String mediaType, HttpServle throws IOException { if (object == null) { LOG.warn("Entry point return Object is NULL"); + if (resp.getStatus() == HttpServletResponse.SC_OK) { + resp.setStatus(HttpServletResponse.SC_NO_CONTENT); + } + return; } try (JsonWriter writer = new JsonWriter(resp.getWriter())) { gson.toJson(object, object.getClass(), writer); diff --git a/json-gson/src/test/java/net/cactusthorn/routing/gson/SimpleGsonConsumerTest.java b/json-gson/src/test/java/net/cactusthorn/routing/gson/SimpleGsonConsumerTest.java index 607abee..6ac087b 100644 --- a/json-gson/src/test/java/net/cactusthorn/routing/gson/SimpleGsonConsumerTest.java +++ b/json-gson/src/test/java/net/cactusthorn/routing/gson/SimpleGsonConsumerTest.java @@ -11,19 +11,22 @@ import javax.servlet.http.HttpServletRequest; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import com.google.gson.GsonBuilder; + import net.cactusthorn.routing.RequestData; public class SimpleGsonConsumerTest { - @Test // - public void consume() throws IOException { + private RequestData requestData; + @BeforeEach // + public void setUp() throws IOException { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); - RequestData requestData; try (InputStream is = SimpleGsonProducerTest.class.getClassLoader().getResourceAsStream("test.json"); Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8); BufferedReader buf = new BufferedReader(reader)) { @@ -31,10 +34,22 @@ public void consume() throws IOException { Mockito.when(request.getReader()).thenReturn(buf); requestData = new RequestData(request, null, 512); } + } + + @Test // + public void consume() throws IOException { SimpleGsonConsumer consumer = new SimpleGsonConsumer(); DataObject data = (DataObject) consumer.consume(DataObject.class, null, requestData); assertEquals("The Name \u00DF", data.getName()); assertEquals(123, data.getValue()); } + @Test // + public void consumeWithCustomGson() throws IOException { + SimpleGsonConsumer consumer = new SimpleGsonConsumer(new GsonBuilder().create()); + DataObject data = (DataObject) consumer.consume(DataObject.class, null, requestData); + assertEquals("The Name \u00DF", data.getName()); + assertEquals(123, data.getValue()); + } + } diff --git a/json-gson/src/test/java/net/cactusthorn/routing/gson/SimpleGsonProducerTest.java b/json-gson/src/test/java/net/cactusthorn/routing/gson/SimpleGsonProducerTest.java index d9ae4a3..9e2cd05 100644 --- a/json-gson/src/test/java/net/cactusthorn/routing/gson/SimpleGsonProducerTest.java +++ b/json-gson/src/test/java/net/cactusthorn/routing/gson/SimpleGsonProducerTest.java @@ -12,15 +12,22 @@ import javax.servlet.http.HttpServletResponse; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import com.google.gson.GsonBuilder; + public class SimpleGsonProducerTest { - @Test // - public void produce() throws IOException { + private HttpServletResponse response; + private StringWriter stringWriter; + private DataObject data; + private String json; - String json; + @BeforeEach // + public void setUp() throws IOException { try (InputStream is = SimpleGsonProducerTest.class.getClassLoader().getResourceAsStream("test.json"); Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) { @@ -33,18 +40,45 @@ public void produce() throws IOException { json = builder.toString(); } - DataObject data = new DataObject("The Name \u00DF", 123); + data = new DataObject("The Name \u00DF", 123); - StringWriter stringWriter = new StringWriter(); + stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter); - HttpServletResponse response = Mockito.mock(HttpServletResponse.class); + response = Mockito.mock(HttpServletResponse.class); Mockito.when(response.getWriter()).thenReturn(writer); + } + @Test // + public void produce() throws IOException { SimpleGsonProducer producer = new SimpleGsonProducer(); producer.produce(data, null, null, null, response); assertEquals(json, stringWriter.toString()); } + + @Test // + public void produceWithCustomGson() throws IOException { + SimpleGsonProducer producer = new SimpleGsonProducer(new GsonBuilder().create()); + + producer.produce(data, null, null, null, response); + + assertEquals(json, stringWriter.toString()); + } + + @Test // + public void produceNullData() throws IOException { + + Mockito.when(response.getStatus()).thenReturn(HttpServletResponse.SC_OK); + + SimpleGsonProducer producer = new SimpleGsonProducer(); + producer.produce(null, null, null, null, response); + + ArgumentCaptor code = ArgumentCaptor.forClass(Integer.class); + + Mockito.verify(response).setStatus(code.capture()); + + assertEquals(HttpServletResponse.SC_NO_CONTENT, code.getValue()); + } }