From b08f53ce48345954137cf5fca5166672b541ff57 Mon Sep 17 00:00:00 2001 From: eugenp Date: Thu, 2 Jan 2014 16:45:51 +0200 Subject: [PATCH] testing work and minor doc cleanup --- rest-testing/README.md | 1 + .../java/org/baeldung/rest/ConvertUtil.java | 23 ------------------- .../java/org/baeldung/rest/RetrieveUtil.java | 19 ++++++--------- 3 files changed, 8 insertions(+), 35 deletions(-) delete mode 100644 rest-testing/src/test/java/org/baeldung/rest/ConvertUtil.java diff --git a/rest-testing/README.md b/rest-testing/README.md index cc476d097b..db7f0c8a86 100644 --- a/rest-testing/README.md +++ b/rest-testing/README.md @@ -4,3 +4,4 @@ ### Relevant Articles: +- [Test a REST API with Java](http://www.baeldung.com/2011/10/13/integration-testing-a-rest-api/) diff --git a/rest-testing/src/test/java/org/baeldung/rest/ConvertUtil.java b/rest-testing/src/test/java/org/baeldung/rest/ConvertUtil.java deleted file mode 100644 index a8b325d9a0..0000000000 --- a/rest-testing/src/test/java/org/baeldung/rest/ConvertUtil.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.baeldung.rest; - -import java.io.IOException; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.base.Preconditions; - -public class ConvertUtil { - - public static String convertResourceToJson(final T resource) throws IOException { - Preconditions.checkNotNull(resource); - - return new ObjectMapper().writeValueAsString(resource); - } - - public static T convertJsonToResource(final String json, final Class clazzOfResource) throws IOException { - Preconditions.checkNotNull(json); - Preconditions.checkNotNull(clazzOfResource); - - return new ObjectMapper().readValue(json, clazzOfResource); - } - -} diff --git a/rest-testing/src/test/java/org/baeldung/rest/RetrieveUtil.java b/rest-testing/src/test/java/org/baeldung/rest/RetrieveUtil.java index 61506d4ba4..75ec3c842c 100644 --- a/rest-testing/src/test/java/org/baeldung/rest/RetrieveUtil.java +++ b/rest-testing/src/test/java/org/baeldung/rest/RetrieveUtil.java @@ -2,25 +2,20 @@ package org.baeldung.rest; import java.io.IOException; -import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; +import org.apache.http.util.EntityUtils; -import com.google.common.base.Preconditions; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; public class RetrieveUtil { - public static String retrieveJsonFromResponse(final HttpResponse response) throws IOException { - Preconditions.checkNotNull(response); - - return IOUtils.toString(response.getEntity().getContent()); - } + // API public static T retrieveResourceFromResponse(final HttpResponse response, final Class clazz) throws IOException { - Preconditions.checkNotNull(response); - Preconditions.checkNotNull(clazz); - - final String jsonFromResponse = retrieveJsonFromResponse(response); - return ConvertUtil.convertJsonToResource(jsonFromResponse, clazz); + final String jsonFromResponse = EntityUtils.toString(response.getEntity()); + final ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return mapper.readValue(jsonFromResponse, clazz); } }