testing work

This commit is contained in:
eugenp 2014-01-02 16:12:53 +02:00
parent afbb4c6005
commit 3130bcdcd3
6 changed files with 151 additions and 90 deletions

View File

@ -15,7 +15,13 @@
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>${guava.version}</version> <version>${guava.version}</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
@ -35,6 +41,14 @@
<version>${httpcore.version}</version> <version>${httpcore.version}</version>
</dependency> </dependency>
<!-- marshalling -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- logging --> <!-- logging -->
<dependency> <dependency>
@ -125,11 +139,9 @@
<properties> <properties>
<!-- Spring --> <!-- Spring -->
<org.springframework.version>4.0.0.RELEASE</org.springframework.version> <org.springframework.version>4.0.0.RELEASE</org.springframework.version>
<org.springframework.security.version>3.2.0.RELEASE</org.springframework.security.version>
<!-- persistence --> <!-- marshalling -->
<hibernate.version>4.3.0.Final</hibernate.version> <jackson.version>2.3.0</jackson.version>
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>
<!-- logging --> <!-- logging -->
<org.slf4j.version>1.7.5</org.slf4j.version> <org.slf4j.version>1.7.5</org.slf4j.version>

View File

@ -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 <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

@ -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;
}
}

View File

@ -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()));
}
}

View File

@ -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());
}
}

View File

@ -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> 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);
}
}