diff --git a/libraries-2/pom.xml b/libraries-2/pom.xml index 6303c0cab5..d3c0f7204b 100644 --- a/libraries-2/pom.xml +++ b/libraries-2/pom.xml @@ -55,6 +55,33 @@ <artifactId>spring-boot-starter</artifactId> <version>${spring-boot-starter.version}</version> </dependency> + + <!-- Dependencies for response decoder with okhttp --> + <dependency> + <groupId>com.squareup.okhttp3</groupId> + <artifactId>okhttp</artifactId> + <version>3.14.2</version> + </dependency> + + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>2.9.9</version> + </dependency> + + <dependency> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.8.5</version> + </dependency> + + <dependency> + <groupId>com.squareup.okhttp3</groupId> + <artifactId>mockwebserver</artifactId> + <version>3.14.2</version> + <scope>test</scope> + </dependency> + </dependencies> <properties> diff --git a/libraries-2/src/test/java/com/baeldung/okhttp/ResponseDecoderUnitTest.java b/libraries-2/src/test/java/com/baeldung/okhttp/ResponseDecoderUnitTest.java new file mode 100644 index 0000000000..e9f829abe4 --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/okhttp/ResponseDecoderUnitTest.java @@ -0,0 +1,160 @@ +package com.baeldung.okhttp; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.ResponseBody; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.io.InputStreamReader; + +public class ResponseDecoderUnitTest { + + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + + @Rule + public MockWebServer server = new MockWebServer(); + + SimpleEntity sampleResponse; + + MockResponse mockResponse; + + OkHttpClient client; + + + @Before + public void setUp() { + sampleResponse = new SimpleEntity("Baeldung"); + client = new OkHttpClient.Builder() + .build(); + mockResponse = new MockResponse() + .setResponseCode(200) + .setHeader("Content-Type", "application/json") + .setBody(new Gson().toJson(sampleResponse)); + } + + @Test + public void givenJacksonDecoder_whenGetByteStreamOfResponse_thenExpectSimpleEntity() throws Exception { + + server.enqueue(mockResponse); + + Request request = new Request.Builder() + .url(server.url("")) + .build(); + ResponseBody responseBody = client.newCall(request).execute().body(); + + Assert.assertNotNull(responseBody); + Assert.assertNotEquals(0, responseBody.contentLength()); + + ObjectMapper objectMapper = new ObjectMapper(); + SimpleEntity response = objectMapper.readValue(responseBody.byteStream() + , SimpleEntity.class); + + Assert.assertEquals(sampleResponse.getName(), response.getName()); + } + + @Test + public void givenJacksonDecoder_whenGetStringOfResponse_thenExpectSimpleEntity() throws Exception { + + server.enqueue(mockResponse); + + Request request = new Request.Builder() + .url(server.url("")) + .build(); + ResponseBody responseBody = client.newCall(request).execute().body(); + + Assert.assertNotNull(responseBody); + Assert.assertNotEquals(0, responseBody.contentLength()); + + ObjectMapper objectMapper = new ObjectMapper(); + SimpleEntity response = objectMapper.readValue(responseBody.string(), SimpleEntity.class); + + Assert.assertEquals(sampleResponse.getName(), response.getName()); + } + + @Test + public void givenJacksonDecoder_whenGetCharStreamOfResponse_thenExpectSimpleEntity() throws Exception { + + server.enqueue(mockResponse); + + Request request = new Request.Builder() + .url(server.url("")) + .build(); + ResponseBody responseBody = client.newCall(request).execute().body(); + + Assert.assertNotNull(responseBody); + Assert.assertNotEquals(0, responseBody.contentLength()); + + ObjectMapper objectMapper = new ObjectMapper(); + SimpleEntity response = objectMapper.readValue(responseBody.charStream(), SimpleEntity.class); + + Assert.assertEquals(sampleResponse.getName(), response.getName()); + } + + @Test + public void givenGsonDecoder_whenGetByteStreamOfResponse_thenExpectSimpleEntity() throws Exception { + + server.enqueue(mockResponse); + + Request request = new Request.Builder() + .url(server.url("")) + .build(); + ResponseBody responseBody = client.newCall(request).execute().body(); + + Assert.assertNotNull(responseBody); + Assert.assertNotEquals(0, responseBody.contentLength()); + + Gson gson = new Gson(); + SimpleEntity response = gson.fromJson(new InputStreamReader(responseBody.byteStream()) + , SimpleEntity.class); + + Assert.assertEquals(sampleResponse.getName(), response.getName()); + } + + @Test + public void givenGsonDecoder_whenGetStringOfResponse_thenExpectSimpleEntity() throws Exception { + + server.enqueue(mockResponse); + + Request request = new Request.Builder() + .url(server.url("")) + .build(); + ResponseBody responseBody = client.newCall(request).execute().body(); + + Assert.assertNotNull(responseBody); + + Gson gson = new Gson(); + SimpleEntity response = gson.fromJson(responseBody.string(), SimpleEntity.class); + + Assert.assertEquals(sampleResponse.getName(), response.getName()); + } + + @Test + public void givenGsonDecoder_whenGetCharStreamOfResponse_thenExpectSimpleEntity() throws Exception { + + server.enqueue(mockResponse); + + Request request = new Request.Builder() + .url(server.url("")) + .build(); + + ResponseBody responseBody = client.newCall(request).execute().body(); + + Assert.assertNotNull(responseBody); + + Gson gson = new Gson(); + SimpleEntity response = gson.fromJson(responseBody.charStream(), SimpleEntity.class); + + Assert.assertEquals(sampleResponse.getName(), response.getName()); + } + + +} diff --git a/libraries-2/src/test/java/com/baeldung/okhttp/SimpleEntity.java b/libraries-2/src/test/java/com/baeldung/okhttp/SimpleEntity.java new file mode 100644 index 0000000000..4c7cae6fdc --- /dev/null +++ b/libraries-2/src/test/java/com/baeldung/okhttp/SimpleEntity.java @@ -0,0 +1,21 @@ +package com.baeldung.okhttp; + +public class SimpleEntity { + protected String name; + + public SimpleEntity(String name) { + this.name = name; + } + + //no-arg constructor, getters and setters here + public SimpleEntity() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +}