testing work and minor doc cleanup

This commit is contained in:
eugenp 2014-01-02 16:45:51 +02:00
parent 3130bcdcd3
commit b08f53ce48
3 changed files with 8 additions and 35 deletions

View File

@ -4,3 +4,4 @@
### Relevant Articles:
- [Test a REST API with Java](http://www.baeldung.com/2011/10/13/integration-testing-a-rest-api/)

View File

@ -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 <T> String convertResourceToJson(final T resource) throws IOException {
Preconditions.checkNotNull(resource);
return new ObjectMapper().writeValueAsString(resource);
}
public static <T> T convertJsonToResource(final String json, final Class<T> clazzOfResource) throws IOException {
Preconditions.checkNotNull(json);
Preconditions.checkNotNull(clazzOfResource);
return new ObjectMapper().readValue(json, clazzOfResource);
}
}

View File

@ -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> T retrieveResourceFromResponse(final HttpResponse response, final Class<T> 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);
}
}