BAEL-1576 Guide to unirest (#3779)

This commit is contained in:
Dhrubajyoti Bhattacharjee 2018-03-13 02:37:03 +05:30 committed by Grzegorz Piwowarek
parent d0b42d10f2
commit ade15ea80a
4 changed files with 218 additions and 1 deletions

View File

@ -627,6 +627,11 @@
<groupId>org.milyn</groupId>
<artifactId>milyn-smooks-all</artifactId>
<version>${smooks.version}</version>
</dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>${unirest.version}</version>
</dependency>
</dependencies>
<repositories>
@ -814,6 +819,7 @@
<tomcat.version>8.5.24</tomcat.version>
<async.http.client.version>2.2.0</async.http.client.version>
<infinispan.version>9.1.5.Final</infinispan.version>
<unirest.version>1.4.9</unirest.version>
</properties>
</project>

View File

@ -0,0 +1,39 @@
package com.baeldung.unirest;
public class Article {
private String id;
private String title;
private String author;
public Article(String id, String title, String author) {
super();
this.id = id;
this.title = title;
this.author = author;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}

View File

@ -0,0 +1,172 @@
package com.baeldung.unirest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.apache.http.entity.ContentType;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.baeldung.unirest.Article;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.ObjectMapper;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.async.Callback;
import com.mashape.unirest.http.exceptions.UnirestException;
public class HttpClientTest {
@BeforeClass
public static void setup() {
// Unirest.setProxy(new HttpHost("localhost", 8080));
Unirest.setTimeouts(20000, 15000);
Unirest.setDefaultHeader("X-app-name", "baeldung-unirest");
Unirest.setDefaultHeader("X-request-id", "100004f00ab5");
Unirest.setConcurrency(20, 5);
Unirest.setObjectMapper(new ObjectMapper() {
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
public String writeValue(Object value) {
try {
return mapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public <T> T readValue(String value, Class<T> valueType) {
try {
return mapper.readValue(value, valueType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
@AfterClass
public static void tearDown() throws IOException {
Unirest.clearDefaultHeaders();
Unirest.shutdown();
}
@Test
public void shouldReturnStatusOkay() throws UnirestException {
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://www.mocky.io/v2/5a9ce37b3100004f00ab5154")
.header("accept", "application/json")
.queryString("apiKey", "123")
.asJson();
assertNotNull(jsonResponse.getBody());
assertEquals(200, jsonResponse.getStatus());
}
@Test
public void shouldReturnStatusAccepted() throws UnirestException {
Map<String, String> headers = new HashMap<String, String>();
headers.put("accept", "application/json");
headers.put("Authorization", "Bearer 5a9ce37b3100004f00ab5154");
Map<String, Object> fields = new HashMap<String, Object>();
fields.put("name", "Sam Baeldung");
fields.put("id", "PSP123");
HttpResponse<JsonNode> jsonResponse = Unirest.put("http://www.mocky.io/v2/5a9ce7853100002a00ab515e")
.headers(headers)
.fields(fields)
.asJson();
assertNotNull(jsonResponse.getBody());
assertEquals(202, jsonResponse.getStatus());
}
@Test
public void givenRequestBodyWhenCreatedThenCorrect() throws UnirestException {
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
.body("{\"name\":\"Sam Baeldung\", \"city\":\"viena\"}")
.asJson();
assertEquals(201, jsonResponse.getStatus());
}
@Test
public void whenAysncRequestShouldReturnOk() throws InterruptedException, ExecutionException {
Future<HttpResponse<JsonNode>> future = Unirest.post("http://www.mocky.io/v2/5a9ce37b3100004f00ab5154?mocky-delay=10000ms")
.header("accept", "application/json")
.asJsonAsync(new Callback<JsonNode>() {
public void failed(UnirestException e) {
// Do something if the request failed
}
public void completed(HttpResponse<JsonNode> response) {
// Do something if the request is successful
}
public void cancelled() {
// Do something if the request is cancelled
}
});
assertEquals(200, future.get()
.getStatus());
}
@Test
public void givenArticleWhenCreatedThenCorrect() throws UnirestException {
Article article = new Article("ID1213", "Guide to Rest", "baeldung");
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
.body(article)
.asJson();
assertEquals(201, jsonResponse.getStatus());
}
// @Test
public void givenFileWhenUploadedThenCorrect() throws UnirestException {
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
.field("file", new File("/path/to/file"))
.asJson();
assertEquals(201, jsonResponse.getStatus());
}
// @Test
public void givenByteStreamWhenUploadedThenCorrect() throws IOException, UnirestException {
try (InputStream inputStream = new FileInputStream(new File("/path/to/file/artcile.txt"))) {
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
.field("file", bytes, "article.txt")
.asJson();
assertEquals(201, jsonResponse.getStatus());
}
}
// @Test
public void givenInputStreamWhenUploadedThenCorrect() throws UnirestException, IOException {
try (InputStream inputStream = new FileInputStream(new File("/path/to/file/artcile.txt"))) {
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
.field("file", inputStream, ContentType.APPLICATION_OCTET_STREAM, "article.txt")
.asJson();
assertEquals(201, jsonResponse.getStatus());
}
}
}

View File

@ -388,4 +388,4 @@
</extension>
</extensions>
</build>
</project>
</project>