Skip to content

Commit 8284c82

Browse files
committed
Added Jackson realistic @test for ObjectMapper (dep only for test scope)
1 parent 4957d6b commit 8284c82

File tree

4 files changed

+115
-77
lines changed

4 files changed

+115
-77
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<properties>
4444
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4545
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
46+
<jackson.version>2.6.0</jackson.version>
4647
</properties>
4748

4849
<build>
@@ -132,5 +133,11 @@
132133
<version>2.4</version>
133134
<scope>test</scope>
134135
</dependency>
136+
<dependency>
137+
<groupId>com.fasterxml.jackson.core</groupId>
138+
<artifactId>jackson-databind</artifactId>
139+
<version>${jackson.version}</version>
140+
<scope>test</scope>
141+
</dependency>
135142
</dependencies>
136143
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.mashape.unirest.http;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
6+
@JsonIgnoreProperties(ignoreUnknown = true)
7+
public class GetResponse {
8+
9+
@JsonProperty("url")
10+
private String url;
11+
12+
public String getUrl() {
13+
return url;
14+
}
15+
16+
public void setUrl(String url) {
17+
this.url = url;
18+
}
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mashape.unirest.http;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
5+
import java.io.IOException;
6+
7+
public class JacksonObjectMapper implements ObjectMapper {
8+
9+
com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper
10+
= new com.fasterxml.jackson.databind.ObjectMapper();
11+
12+
public <T> T readValue(String value, Class<T> valueType) {
13+
try {
14+
return jacksonObjectMapper.readValue(value, valueType);
15+
} catch (IOException e) {
16+
throw new RuntimeException(e);
17+
}
18+
}
19+
20+
public String writeValue(Object value) {
21+
try {
22+
return jacksonObjectMapper.writeValueAsString(value);
23+
} catch (JsonProcessingException e) {
24+
throw new RuntimeException(e);
25+
}
26+
}
27+
}

src/test/java/com/mashape/unirest/test/http/UnirestTest.java

Lines changed: 62 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,12 @@ a copy of this software and associated documentation files (the
2525

2626
package com.mashape.unirest.test.http;
2727

28-
import static org.junit.Assert.assertEquals;
29-
import static org.junit.Assert.assertFalse;
30-
import static org.junit.Assert.assertNotNull;
31-
import static org.junit.Assert.assertTrue;
32-
import static org.junit.Assert.fail;
33-
34-
import java.io.File;
35-
import java.io.FileInputStream;
36-
import java.io.FileNotFoundException;
37-
import java.io.IOException;
38-
import java.io.InputStream;
39-
import java.net.InetAddress;
40-
import java.net.URISyntaxException;
41-
import java.net.UnknownHostException;
42-
import java.util.Arrays;
43-
import java.util.Locale;
44-
import java.util.concurrent.CountDownLatch;
45-
import java.util.concurrent.ExecutionException;
46-
import java.util.concurrent.ExecutorService;
47-
import java.util.concurrent.Executors;
48-
import java.util.concurrent.Future;
49-
import java.util.concurrent.TimeUnit;
50-
import java.util.concurrent.atomic.AtomicInteger;
51-
28+
import com.mashape.unirest.http.*;
29+
import com.mashape.unirest.http.async.Callback;
30+
import com.mashape.unirest.http.exceptions.UnirestException;
31+
import com.mashape.unirest.http.options.Options;
32+
import com.mashape.unirest.request.GetRequest;
33+
import com.mashape.unirest.request.HttpRequest;
5234
import org.apache.commons.io.IOUtils;
5335
import org.apache.http.entity.ContentType;
5436
import org.apache.http.impl.client.HttpClientBuilder;
@@ -59,15 +41,15 @@ a copy of this software and associated documentation files (the
5941
import org.junit.Before;
6042
import org.junit.Test;
6143

62-
import com.mashape.unirest.http.HttpResponse;
63-
import com.mashape.unirest.http.JsonNode;
64-
import com.mashape.unirest.http.ObjectMapper;
65-
import com.mashape.unirest.http.Unirest;
66-
import com.mashape.unirest.http.async.Callback;
67-
import com.mashape.unirest.http.exceptions.UnirestException;
68-
import com.mashape.unirest.http.options.Options;
69-
import com.mashape.unirest.request.GetRequest;
70-
import com.mashape.unirest.request.HttpRequest;
44+
import java.io.*;
45+
import java.net.InetAddress;
46+
import java.net.URISyntaxException;
47+
import java.net.UnknownHostException;
48+
import java.util.Arrays;
49+
import java.util.concurrent.*;
50+
import java.util.concurrent.atomic.AtomicInteger;
51+
52+
import static org.junit.Assert.*;
7153

7254
public class UnirestTest {
7355

@@ -248,34 +230,34 @@ public void testAsyncCallback() throws JSONException, InterruptedException, Exec
248230
.field("param2","bye")
249231
.asJsonAsync(new Callback<JsonNode>() {
250232

251-
public void failed(UnirestException e) {
252-
fail();
253-
}
233+
public void failed(UnirestException e) {
234+
fail();
235+
}
254236

255-
public void completed(HttpResponse<JsonNode> jsonResponse) {
256-
assertTrue(jsonResponse.getHeaders().size() > 0);
257-
assertTrue(jsonResponse.getBody().toString().length() > 0);
258-
assertFalse(jsonResponse.getRawBody() == null);
259-
assertEquals(200, jsonResponse.getStatus());
237+
public void completed(HttpResponse<JsonNode> jsonResponse) {
238+
assertTrue(jsonResponse.getHeaders().size() > 0);
239+
assertTrue(jsonResponse.getBody().toString().length() > 0);
240+
assertFalse(jsonResponse.getRawBody() == null);
241+
assertEquals(200, jsonResponse.getStatus());
260242

261-
JsonNode json = jsonResponse.getBody();
262-
assertFalse(json.isArray());
263-
assertNotNull(json.getObject());
264-
assertNotNull(json.getArray());
265-
assertEquals(1, json.getArray().length());
266-
assertNotNull(json.getArray().get(0));
243+
JsonNode json = jsonResponse.getBody();
244+
assertFalse(json.isArray());
245+
assertNotNull(json.getObject());
246+
assertNotNull(json.getArray());
247+
assertEquals(1, json.getArray().length());
248+
assertNotNull(json.getArray().get(0));
267249

268-
assertEquals("value1", json.getObject().getJSONObject("form").getString("param1"));
269-
assertEquals("bye", json.getObject().getJSONObject("form").getString("param2"));
250+
assertEquals("value1", json.getObject().getJSONObject("form").getString("param1"));
251+
assertEquals("bye", json.getObject().getJSONObject("form").getString("param2"));
270252

271-
status = true;
272-
lock.countDown();
273-
}
253+
status = true;
254+
lock.countDown();
255+
}
274256

275-
public void cancelled() {
276-
fail();
277-
}
278-
});
257+
public void cancelled() {
258+
fail();
259+
}
260+
});
279261

280262
lock.await(10, TimeUnit.SECONDS);
281263
assertTrue(status);
@@ -831,33 +813,36 @@ public void setTimeoutsAndCustomClient() {
831813
}
832814

833815
@Test
834-
public void testObjectMapper() throws UnirestException, IOException {
835-
final String responseJson = "{\"locale\": \"english\"}";
816+
public void testObjectMapperRead() throws UnirestException, IOException {
817+
Unirest.setObjectMapper(new JacksonObjectMapper());
836818

837-
Unirest.setObjectMapper(new ObjectMapper() {
838-
public Object readValue(String ignored) {
839-
return Locale.ENGLISH;
840-
}
819+
GetResponse getResponseMock = new GetResponse();
820+
getResponseMock.setUrl("http://httpbin.org/get");
841821

842-
public String writeValue(Object ignored) {
843-
return responseJson;
844-
}
845-
});
822+
HttpResponse<GetResponse> getResponse = Unirest.get(getResponseMock.getUrl())
823+
.asObject(GetResponse.class);
846824

847-
HttpResponse<Locale> getResponse = Unirest.get("http://httpbin.org/get").asObject(Locale.class);
848825
assertEquals(200, getResponse.getStatus());
849-
assertEquals(getResponse.getBody(), Locale.ENGLISH);
826+
assertEquals(getResponse.getBody().getUrl(), getResponseMock.getUrl());
827+
}
850828

851-
HttpResponse<JsonNode> postResponse = Unirest.post("http://httpbin.org/post")
852-
.header("accept", "application/json")
853-
.header("Content-Type", "application/json")
854-
.body(Locale.ENGLISH)
855-
.asJson();
829+
@Test
830+
public void testObjectMapperWrite() throws UnirestException, IOException {
831+
Unirest.setObjectMapper(new JacksonObjectMapper());
832+
833+
GetResponse postResponseMock = new GetResponse();
834+
postResponseMock.setUrl("http://httpbin.org/post");
835+
836+
HttpResponse<JsonNode> postResponse = Unirest.post(postResponseMock.getUrl())
837+
.header("accept", "application/json")
838+
.header("Content-Type", "application/json")
839+
.body(postResponseMock)
840+
.asJson();
841+
842+
assertEquals(200, postResponse.getStatus());
843+
assertEquals(postResponse.getBody().getObject().getString("data"), "{\"url\":\"http://httpbin.org/post\"}");
844+
}
856845

857-
assertEquals(200, postResponse.getStatus());
858-
assertEquals(postResponse.getBody().getObject().getString("data"), responseJson);
859-
}
860-
861846
@Test
862847
public void testPostProvidesSortedParams() throws IOException {
863848
// Verify that fields are encoded into the body in sorted order.

0 commit comments

Comments
 (0)