From ade15ea80a84a21b17b52bf817b35437bf18b666 Mon Sep 17 00:00:00 2001 From: Dhrubajyoti Bhattacharjee Date: Tue, 13 Mar 2018 02:37:03 +0530 Subject: [PATCH] BAEL-1576 Guide to unirest (#3779) --- libraries/pom.xml | 6 + .../java/com/baeldung/unirest/Article.java | 39 ++++ .../com/baeldung/unirest/HttpClientTest.java | 172 ++++++++++++++++++ pom.xml | 2 +- 4 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 libraries/src/test/java/com/baeldung/unirest/Article.java create mode 100644 libraries/src/test/java/com/baeldung/unirest/HttpClientTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index beeba88ff1..e9bfecf527 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -627,6 +627,11 @@ org.milyn milyn-smooks-all ${smooks.version} + + + com.mashape.unirest + unirest-java + ${unirest.version} @@ -814,6 +819,7 @@ 8.5.24 2.2.0 9.1.5.Final + 1.4.9 diff --git a/libraries/src/test/java/com/baeldung/unirest/Article.java b/libraries/src/test/java/com/baeldung/unirest/Article.java new file mode 100644 index 0000000000..03fd4959b2 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/unirest/Article.java @@ -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; + } +} \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/unirest/HttpClientTest.java b/libraries/src/test/java/com/baeldung/unirest/HttpClientTest.java new file mode 100644 index 0000000000..3e919f031c --- /dev/null +++ b/libraries/src/test/java/com/baeldung/unirest/HttpClientTest.java @@ -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 readValue(String value, Class 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 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 headers = new HashMap(); + headers.put("accept", "application/json"); + headers.put("Authorization", "Bearer 5a9ce37b3100004f00ab5154"); + + Map fields = new HashMap(); + fields.put("name", "Sam Baeldung"); + fields.put("id", "PSP123"); + + HttpResponse 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 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> future = Unirest.post("http://www.mocky.io/v2/5a9ce37b3100004f00ab5154?mocky-delay=10000ms") + .header("accept", "application/json") + .asJsonAsync(new Callback() { + + public void failed(UnirestException e) { + // Do something if the request failed + } + + public void completed(HttpResponse 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 jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d") + .body(article) + .asJson(); + assertEquals(201, jsonResponse.getStatus()); + } + + // @Test + public void givenFileWhenUploadedThenCorrect() throws UnirestException { + + HttpResponse 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 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 jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d") + .field("file", inputStream, ContentType.APPLICATION_OCTET_STREAM, "article.txt") + .asJson(); + assertEquals(201, jsonResponse.getStatus()); + + } + } +} diff --git a/pom.xml b/pom.xml index 9b76ffd26d..6118e81288 100644 --- a/pom.xml +++ b/pom.xml @@ -388,4 +388,4 @@ - \ No newline at end of file +