JAVA-627: Moved 2 articles from libraries-2

This commit is contained in:
sampadawagde 2020-04-18 11:43:52 +05:30
parent ca443a08a3
commit 1ec2d9a4ed
4 changed files with 1 additions and 151 deletions

View File

@ -14,11 +14,9 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m
- [Guide to Java Parallel Collectors Library](https://www.baeldung.com/java-parallel-collectors)
- [Templating with Handlebars](https://www.baeldung.com/handlebars)
- [A Guide to Crawler4j](https://www.baeldung.com/crawler4j)
- [Decode an OkHttp JSON Response](https://www.baeldung.com/okhttp-json-response)
- [Key Value Store with Chronicle Map](https://www.baeldung.com/java-chronicle-map)
- [Guide to MapDB](https://www.baeldung.com/mapdb)
- [A Guide to Apache Mesos](https://www.baeldung.com/apache-mesos)
- [JasperReports with Spring](https://www.baeldung.com/spring-jasper)
- [Jetty ReactiveStreams HTTP Client](https://www.baeldung.com/jetty-reactivestreams-http-client)
- More articles [[<-- prev]](/libraries)

View File

@ -75,28 +75,7 @@
</exclusion>
</exclusions>
</dependency>
<!-- Dependencies for response decoder with okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${mockwebserver.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>edu.uci.ics</groupId>
<artifactId>crawler4j</artifactId>
@ -151,9 +130,6 @@
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
<mesos.library.version>0.28.3</mesos.library.version>
<parallel-collectors.version>1.1.0</parallel-collectors.version>
<okhttp.version>3.14.2</okhttp.version>
<gson.version>2.8.5</gson.version>
<mockwebserver.version>3.14.2</mockwebserver.version>
<handlebars.version>4.1.2</handlebars.version>
<jasperreports.version>6.6.0</jasperreports.version>
<spring.version>5.1.9.RELEASE</spring.version>

View File

@ -1,102 +0,0 @@
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

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