Skip to content

Commit ade15ea

Browse files
dhruba619pivovarit
authored andcommitted
BAEL-1576 Guide to unirest (eugenp#3779)
1 parent d0b42d1 commit ade15ea

4 files changed

Lines changed: 218 additions & 1 deletion

File tree

‎libraries/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,11 @@
627627
<groupId>org.milyn</groupId>
628628
<artifactId>milyn-smooks-all</artifactId>
629629
<version>${smooks.version}</version>
630+
</dependency>
631+
<dependency>
632+
<groupId>com.mashape.unirest</groupId>
633+
<artifactId>unirest-java</artifactId>
634+
<version>${unirest.version}</version>
630635
</dependency>
631636
</dependencies>
632637
<repositories>
@@ -814,6 +819,7 @@
814819
<tomcat.version>8.5.24</tomcat.version>
815820
<async.http.client.version>2.2.0</async.http.client.version>
816821
<infinispan.version>9.1.5.Final</infinispan.version>
822+
<unirest.version>1.4.9</unirest.version>
817823
</properties>
818824

819825
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.unirest;
2+
3+
public class Article {
4+
5+
private String id;
6+
private String title;
7+
private String author;
8+
9+
public Article(String id, String title, String author) {
10+
super();
11+
this.id = id;
12+
this.title = title;
13+
this.author = author;
14+
}
15+
16+
public String getId() {
17+
return id;
18+
}
19+
20+
public void setId(String id) {
21+
this.id = id;
22+
}
23+
24+
public String getTitle() {
25+
return title;
26+
}
27+
28+
public void setTitle(String title) {
29+
this.title = title;
30+
}
31+
32+
public String getAuthor() {
33+
return author;
34+
}
35+
36+
public void setAuthor(String author) {
37+
this.author = author;
38+
}
39+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package com.baeldung.unirest;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
6+
import java.io.File;
7+
import java.io.FileInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
import java.util.concurrent.ExecutionException;
13+
import java.util.concurrent.Future;
14+
15+
import org.apache.http.entity.ContentType;
16+
import org.junit.AfterClass;
17+
import org.junit.BeforeClass;
18+
import org.junit.Test;
19+
20+
import com.baeldung.unirest.Article;
21+
import com.fasterxml.jackson.core.JsonProcessingException;
22+
import com.mashape.unirest.http.HttpResponse;
23+
import com.mashape.unirest.http.JsonNode;
24+
import com.mashape.unirest.http.ObjectMapper;
25+
import com.mashape.unirest.http.Unirest;
26+
import com.mashape.unirest.http.async.Callback;
27+
import com.mashape.unirest.http.exceptions.UnirestException;
28+
29+
public class HttpClientTest {
30+
31+
@BeforeClass
32+
public static void setup() {
33+
// Unirest.setProxy(new HttpHost("localhost", 8080));
34+
Unirest.setTimeouts(20000, 15000);
35+
Unirest.setDefaultHeader("X-app-name", "baeldung-unirest");
36+
Unirest.setDefaultHeader("X-request-id", "100004f00ab5");
37+
Unirest.setConcurrency(20, 5);
38+
Unirest.setObjectMapper(new ObjectMapper() {
39+
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
40+
41+
public String writeValue(Object value) {
42+
try {
43+
return mapper.writeValueAsString(value);
44+
} catch (JsonProcessingException e) {
45+
throw new RuntimeException(e);
46+
}
47+
48+
}
49+
50+
public <T> T readValue(String value, Class<T> valueType) {
51+
52+
try {
53+
return mapper.readValue(value, valueType);
54+
} catch (Exception e) {
55+
throw new RuntimeException(e);
56+
}
57+
58+
}
59+
});
60+
}
61+
62+
@AfterClass
63+
public static void tearDown() throws IOException {
64+
Unirest.clearDefaultHeaders();
65+
Unirest.shutdown();
66+
}
67+
68+
@Test
69+
public void shouldReturnStatusOkay() throws UnirestException {
70+
HttpResponse<JsonNode> jsonResponse = Unirest.get("http://www.mocky.io/v2/5a9ce37b3100004f00ab5154")
71+
.header("accept", "application/json")
72+
.queryString("apiKey", "123")
73+
.asJson();
74+
assertNotNull(jsonResponse.getBody());
75+
assertEquals(200, jsonResponse.getStatus());
76+
}
77+
78+
@Test
79+
public void shouldReturnStatusAccepted() throws UnirestException {
80+
81+
Map<String, String> headers = new HashMap<String, String>();
82+
headers.put("accept", "application/json");
83+
headers.put("Authorization", "Bearer 5a9ce37b3100004f00ab5154");
84+
85+
Map<String, Object> fields = new HashMap<String, Object>();
86+
fields.put("name", "Sam Baeldung");
87+
fields.put("id", "PSP123");
88+
89+
HttpResponse<JsonNode> jsonResponse = Unirest.put("http://www.mocky.io/v2/5a9ce7853100002a00ab515e")
90+
.headers(headers)
91+
.fields(fields)
92+
.asJson();
93+
assertNotNull(jsonResponse.getBody());
94+
assertEquals(202, jsonResponse.getStatus());
95+
}
96+
97+
@Test
98+
public void givenRequestBodyWhenCreatedThenCorrect() throws UnirestException {
99+
100+
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
101+
.body("{\"name\":\"Sam Baeldung\", \"city\":\"viena\"}")
102+
.asJson();
103+
assertEquals(201, jsonResponse.getStatus());
104+
}
105+
106+
@Test
107+
public void whenAysncRequestShouldReturnOk() throws InterruptedException, ExecutionException {
108+
Future<HttpResponse<JsonNode>> future = Unirest.post("http://www.mocky.io/v2/5a9ce37b3100004f00ab5154?mocky-delay=10000ms")
109+
.header("accept", "application/json")
110+
.asJsonAsync(new Callback<JsonNode>() {
111+
112+
public void failed(UnirestException e) {
113+
// Do something if the request failed
114+
}
115+
116+
public void completed(HttpResponse<JsonNode> response) {
117+
// Do something if the request is successful
118+
}
119+
120+
public void cancelled() {
121+
// Do something if the request is cancelled
122+
}
123+
124+
});
125+
assertEquals(200, future.get()
126+
.getStatus());
127+
128+
}
129+
130+
@Test
131+
public void givenArticleWhenCreatedThenCorrect() throws UnirestException {
132+
Article article = new Article("ID1213", "Guide to Rest", "baeldung");
133+
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
134+
.body(article)
135+
.asJson();
136+
assertEquals(201, jsonResponse.getStatus());
137+
}
138+
139+
// @Test
140+
public void givenFileWhenUploadedThenCorrect() throws UnirestException {
141+
142+
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
143+
.field("file", new File("/path/to/file"))
144+
.asJson();
145+
assertEquals(201, jsonResponse.getStatus());
146+
}
147+
148+
// @Test
149+
public void givenByteStreamWhenUploadedThenCorrect() throws IOException, UnirestException {
150+
try (InputStream inputStream = new FileInputStream(new File("/path/to/file/artcile.txt"))) {
151+
byte[] bytes = new byte[inputStream.available()];
152+
inputStream.read(bytes);
153+
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
154+
.field("file", bytes, "article.txt")
155+
.asJson();
156+
assertEquals(201, jsonResponse.getStatus());
157+
}
158+
159+
}
160+
161+
// @Test
162+
public void givenInputStreamWhenUploadedThenCorrect() throws UnirestException, IOException {
163+
try (InputStream inputStream = new FileInputStream(new File("/path/to/file/artcile.txt"))) {
164+
165+
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://www.mocky.io/v2/5a9ce7663100006800ab515d")
166+
.field("file", inputStream, ContentType.APPLICATION_OCTET_STREAM, "article.txt")
167+
.asJson();
168+
assertEquals(201, jsonResponse.getStatus());
169+
170+
}
171+
}
172+
}

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,4 @@
388388
</extension>
389389
</extensions>
390390
</build>
391-
</project>
391+
</project>

0 commit comments

Comments
 (0)