From 3130bcdcd3082aa226c1ba8feeed0c8e69c8b99d Mon Sep 17 00:00:00 2001 From: eugenp Date: Thu, 2 Jan 2014 16:12:53 +0200 Subject: [PATCH] testing work --- rest-testing/pom.xml | 22 +++-- .../java/org/baeldung/rest/ConvertUtil.java | 23 +++++ .../java/org/baeldung/rest/GitHubUser.java | 21 +++++ .../baeldung/rest/GithubBasicLiveTest.java | 64 ++++++++++++++ .../rest/HttpClientBasicLiveTest.java | 85 ------------------- .../java/org/baeldung/rest/RetrieveUtil.java | 26 ++++++ 6 files changed, 151 insertions(+), 90 deletions(-) create mode 100644 rest-testing/src/test/java/org/baeldung/rest/ConvertUtil.java create mode 100644 rest-testing/src/test/java/org/baeldung/rest/GitHubUser.java create mode 100644 rest-testing/src/test/java/org/baeldung/rest/GithubBasicLiveTest.java delete mode 100644 rest-testing/src/test/java/org/baeldung/rest/HttpClientBasicLiveTest.java create mode 100644 rest-testing/src/test/java/org/baeldung/rest/RetrieveUtil.java diff --git a/rest-testing/pom.xml b/rest-testing/pom.xml index bd3133df3b..a789c57816 100644 --- a/rest-testing/pom.xml +++ b/rest-testing/pom.xml @@ -15,7 +15,13 @@ guava ${guava.version} - + + + commons-io + commons-io + 2.4 + + org.apache.commons commons-lang3 @@ -35,6 +41,14 @@ ${httpcore.version} + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + @@ -125,11 +139,9 @@ 4.0.0.RELEASE - 3.2.0.RELEASE - - 4.3.0.Final - 5.1.27 + + 2.3.0 1.7.5 diff --git a/rest-testing/src/test/java/org/baeldung/rest/ConvertUtil.java b/rest-testing/src/test/java/org/baeldung/rest/ConvertUtil.java new file mode 100644 index 0000000000..a8b325d9a0 --- /dev/null +++ b/rest-testing/src/test/java/org/baeldung/rest/ConvertUtil.java @@ -0,0 +1,23 @@ +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/GitHubUser.java b/rest-testing/src/test/java/org/baeldung/rest/GitHubUser.java new file mode 100644 index 0000000000..73c9677377 --- /dev/null +++ b/rest-testing/src/test/java/org/baeldung/rest/GitHubUser.java @@ -0,0 +1,21 @@ +package org.baeldung.rest; + +public class GitHubUser { + + private String login; + + public GitHubUser() { + super(); + } + + // API + + public String getLogin() { + return login; + } + + public void setLogin(final String login) { + this.login = login; + } + +} diff --git a/rest-testing/src/test/java/org/baeldung/rest/GithubBasicLiveTest.java b/rest-testing/src/test/java/org/baeldung/rest/GithubBasicLiveTest.java new file mode 100644 index 0000000000..acac82c8f4 --- /dev/null +++ b/rest-testing/src/test/java/org/baeldung/rest/GithubBasicLiveTest.java @@ -0,0 +1,64 @@ +package org.baeldung.rest; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import java.io.IOException; + +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.entity.ContentType; +import org.apache.http.impl.client.HttpClientBuilder; +import org.hamcrest.Matchers; +import org.junit.Test; + +public class GithubBasicLiveTest { + + // simple request - response + + @Test + public void givenUserDoesNotExists_whenUserInfoIsRetrieved_then404IsReceived() throws ClientProtocolException, IOException { + // Given + final String name = randomAlphabetic(8); + final HttpUriRequest request = new HttpGet("https://api.github.com/users/" + name); + + // When + final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); + + // Then + assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_NOT_FOUND)); + } + + @Test + public void givenRequestWithNoAcceptHeader_whenRequestIsExecuted_thenDefaultResponseContentTypeIsJson() throws ClientProtocolException, IOException { + // Given + final String jsonMimeType = "application/json"; + final HttpUriRequest request = new HttpGet("https://api.github.com/users/eugenp"); + + // When + final HttpResponse response = HttpClientBuilder.create().build().execute(request); + + // Then + final String mimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); + assertEquals(jsonMimeType, mimeType); + } + + @Test + public void givenUserExists_whenUserInformationIsRetrieved_thenRetrievedResourceIsCorrect() throws ClientProtocolException, IOException { + // Given + final HttpUriRequest request = new HttpGet("https://api.github.com/users/eugenp"); + + // When + final HttpResponse response = HttpClientBuilder.create().build().execute(request); + + // Then + final GitHubUser resource = RetrieveUtil.retrieveResourceFromResponse(response, GitHubUser.class); + assertThat("eugenp", Matchers.is(resource.getLogin())); + } + +} diff --git a/rest-testing/src/test/java/org/baeldung/rest/HttpClientBasicLiveTest.java b/rest-testing/src/test/java/org/baeldung/rest/HttpClientBasicLiveTest.java deleted file mode 100644 index bc0702118d..0000000000 --- a/rest-testing/src/test/java/org/baeldung/rest/HttpClientBasicLiveTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package org.baeldung.rest; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; - -import java.io.IOException; -import java.io.InputStream; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpStatus; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.entity.ContentType; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.util.EntityUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -public class HttpClientBasicLiveTest { - - private static final String SAMPLE_URL = "http://www.github.com"; - - private CloseableHttpClient instance; - - private CloseableHttpResponse response; - - @Before - public final void before() { - instance = HttpClientBuilder.create().build(); - } - - @After - public final void after() throws IllegalStateException, IOException { - if (response == null) { - return; - } - - try { - final HttpEntity entity = response.getEntity(); - if (entity != null) { - final InputStream instream = entity.getContent(); - instream.close(); - } - } finally { - response.close(); - } - } - - // tests - - // simple request - response - - @Test - public final void whenExecutingBasicGetRequest_thenNoExceptions() throws ClientProtocolException, IOException { - response = instance.execute(new HttpGet(SAMPLE_URL)); - } - - @Test - public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException { - response = instance.execute(new HttpGet(SAMPLE_URL)); - final int statusCode = response.getStatusLine().getStatusCode(); - assertThat(statusCode, equalTo(HttpStatus.SC_OK)); - } - - @Test - public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectMimeType() throws ClientProtocolException, IOException { - response = instance.execute(new HttpGet(SAMPLE_URL)); - final String contentMimeType = ContentType.getOrDefault(response.getEntity()).getMimeType(); - - assertThat(contentMimeType, equalTo(ContentType.TEXT_HTML.getMimeType())); - } - - @Test - public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectBody() throws ClientProtocolException, IOException { - response = instance.execute(new HttpGet(SAMPLE_URL)); - final String bodyAsString = EntityUtils.toString(response.getEntity()); - - assertThat(bodyAsString, notNullValue()); - } - -} diff --git a/rest-testing/src/test/java/org/baeldung/rest/RetrieveUtil.java b/rest-testing/src/test/java/org/baeldung/rest/RetrieveUtil.java new file mode 100644 index 0000000000..61506d4ba4 --- /dev/null +++ b/rest-testing/src/test/java/org/baeldung/rest/RetrieveUtil.java @@ -0,0 +1,26 @@ +package org.baeldung.rest; + +import java.io.IOException; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; + +import com.google.common.base.Preconditions; + +public class RetrieveUtil { + + public static String retrieveJsonFromResponse(final HttpResponse response) throws IOException { + Preconditions.checkNotNull(response); + + return IOUtils.toString(response.getEntity().getContent()); + } + + 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); + } + +}