Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-14850

This commit is contained in:
amit2103 2019-06-08 12:04:38 +05:30
commit 556f62e0f2
3 changed files with 157 additions and 5 deletions

View File

@ -55,11 +55,39 @@
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
<version>${spring-boot-starter.version}</version> <version>${spring-boot-starter.version}</version>
</dependency> </dependency>
<dependency>
<groupId>edu.uci.ics</groupId> <!-- Dependencies for response decoder with okhttp -->
<artifactId>crawler4j</artifactId> <dependency>
<version>${crawler4j.version}</version> <groupId>com.squareup.okhttp3</groupId>
</dependency> <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>
<dependency>
<groupId>edu.uci.ics</groupId>
<artifactId>crawler4j</artifactId>
<version>${crawler4j.version}</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>

View File

@ -0,0 +1,102 @@
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_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 entity = objectMapper.readValue(responseBody.string(), SimpleEntity.class);
Assert.assertNotNull(entity);
Assert.assertEquals(sampleResponse.getName(), entity.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 entity = gson.fromJson(new InputStreamReader(responseBody.byteStream()), SimpleEntity.class);
Assert.assertNotNull(entity);
Assert.assertEquals(sampleResponse.getName(), entity.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 entity = gson.fromJson(responseBody.string(), SimpleEntity.class);
Assert.assertNotNull(entity);
Assert.assertEquals(sampleResponse.getName(), entity.getName());
}
}

View File

@ -0,0 +1,22 @@
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;
}
}