From d824a489eb3938b0088b9d7c28ab1d2376ab180e Mon Sep 17 00:00:00 2001 From: zdh431079799 <57171915+zdh431079799@users.noreply.github.com> Date: Sun, 31 May 2020 19:03:07 +0800 Subject: [PATCH 1/3] Interface for issue 534 --- .../java/spark/ResponseTransformerExtend.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/spark/ResponseTransformerExtend.java diff --git a/src/main/java/spark/ResponseTransformerExtend.java b/src/main/java/spark/ResponseTransformerExtend.java new file mode 100644 index 0000000000..4887f65380 --- /dev/null +++ b/src/main/java/spark/ResponseTransformerExtend.java @@ -0,0 +1,18 @@ +package spark; + + +@FunctionalInterface +public interface ResponseTransformerExtend { + + /** + * Method called for rendering the output. + * + * @param model object used to render output. + * @param req request object to render output. + * @param res response object to render output. + * @return message that it is sent to client. + * @throws java.lang.Exception when render fails + */ + String render(Object model, Request req, Response res) throws Exception; + +} From 13f208c43a225a6fe92152d76f73be960523d920 Mon Sep 17 00:00:00 2001 From: zdh431079799 <57171915+zdh431079799@users.noreply.github.com> Date: Sun, 31 May 2020 19:06:05 +0800 Subject: [PATCH 2/3] example for issue 534 --- .../transformer/TransformerExtend.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/test/java/spark/examples/transformer/TransformerExtend.java diff --git a/src/test/java/spark/examples/transformer/TransformerExtend.java b/src/test/java/spark/examples/transformer/TransformerExtend.java new file mode 100644 index 0000000000..74ab4f29c9 --- /dev/null +++ b/src/test/java/spark/examples/transformer/TransformerExtend.java @@ -0,0 +1,18 @@ +package spark; + +import com.google.gson.Gson; + +//CS304 Issue link: https://github.com/perwendel/spark/issues/534 +public class TransformerExtend implements ResponseTransformerExtend { + + @Override + public String render(Object model, Request req, Response res) throws Exception { + if(req.contentType().equals("application/json")) { + Gson gson = new Gson(); + res.type("application/json"); + System.out.println(res.type()); + return gson.toJson(model); + } + return "Not Json type message. Other types are not implemented."; + } +} From dc50b3e2efb95c25ba0b3f6fdcc221a57341400a Mon Sep 17 00:00:00 2001 From: zdh431079799 <57171915+zdh431079799@users.noreply.github.com> Date: Sun, 31 May 2020 19:07:30 +0800 Subject: [PATCH 3/3] test for issue 534 --- .../java/spark/TransformerExtendTest.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/java/spark/TransformerExtendTest.java diff --git a/src/test/java/spark/TransformerExtendTest.java b/src/test/java/spark/TransformerExtendTest.java new file mode 100644 index 0000000000..a4507c163e --- /dev/null +++ b/src/test/java/spark/TransformerExtendTest.java @@ -0,0 +1,56 @@ +package spark; + +import org.junit.Before; +import org.junit.Test; +import spark.examples.transformer.MyMessage; +import spark.routematch.RouteMatch; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +//CS304 Issue link: https://github.com/perwendel/spark/issues/534 +public class TransformerExtendTest { + + HttpServletRequest servletRequest; + HttpServletResponse servletResponse; + Request request; + Response response; + RouteMatch match = new RouteMatch(null, "/hi", "/hi", "text/html"); + TransformerExtend transformer; + + @Before + public void setup() { + servletRequest = mock(HttpServletRequest.class); + request = new Request(match, servletRequest); + + + servletResponse = mock(HttpServletResponse.class); + response = new Response(servletResponse); + + transformer = new TransformerExtend(); + } + + @Test + public void testJsonRequest() throws Exception { + final String contentType = "application/json"; + + when(servletRequest.getContentType()).thenReturn(contentType); + + assertEquals("{\"message\":\"Hello World\"}", transformer.render(new MyMessage("Hello World"), request, response)); + + } + + @Test + public void testNotJsonRequest() throws Exception { + final String contentType = "text/xml"; + + when(servletRequest.getContentType()).thenReturn(contentType); + + assertEquals("Not Json type message. Other types are not implemented.", transformer.render(new MyMessage("Hello World"), request, response)); + + } +}