Skip to content

Commit 3130bcd

Browse files
author
eugenp
committed
testing work
1 parent afbb4c6 commit 3130bcd

File tree

6 files changed

+151
-90
lines changed

6 files changed

+151
-90
lines changed

rest-testing/pom.xml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
<artifactId>guava</artifactId>
1616
<version>${guava.version}</version>
1717
</dependency>
18-
18+
19+
<dependency>
20+
<groupId>commons-io</groupId>
21+
<artifactId>commons-io</artifactId>
22+
<version>2.4</version>
23+
</dependency>
24+
1925
<dependency>
2026
<groupId>org.apache.commons</groupId>
2127
<artifactId>commons-lang3</artifactId>
@@ -35,6 +41,14 @@
3541
<version>${httpcore.version}</version>
3642
</dependency>
3743

44+
<!-- marshalling -->
45+
46+
<dependency>
47+
<groupId>com.fasterxml.jackson.core</groupId>
48+
<artifactId>jackson-databind</artifactId>
49+
<version>${jackson.version}</version>
50+
</dependency>
51+
3852
<!-- logging -->
3953

4054
<dependency>
@@ -125,11 +139,9 @@
125139
<properties>
126140
<!-- Spring -->
127141
<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
128-
<org.springframework.security.version>3.2.0.RELEASE</org.springframework.security.version>
129142

130-
<!-- persistence -->
131-
<hibernate.version>4.3.0.Final</hibernate.version>
132-
<mysql-connector-java.version>5.1.27</mysql-connector-java.version>
143+
<!-- marshalling -->
144+
<jackson.version>2.3.0</jackson.version>
133145

134146
<!-- logging -->
135147
<org.slf4j.version>1.7.5</org.slf4j.version>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.rest;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.google.common.base.Preconditions;
7+
8+
public class ConvertUtil {
9+
10+
public static <T> String convertResourceToJson(final T resource) throws IOException {
11+
Preconditions.checkNotNull(resource);
12+
13+
return new ObjectMapper().writeValueAsString(resource);
14+
}
15+
16+
public static <T> T convertJsonToResource(final String json, final Class<T> clazzOfResource) throws IOException {
17+
Preconditions.checkNotNull(json);
18+
Preconditions.checkNotNull(clazzOfResource);
19+
20+
return new ObjectMapper().readValue(json, clazzOfResource);
21+
}
22+
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.rest;
2+
3+
public class GitHubUser {
4+
5+
private String login;
6+
7+
public GitHubUser() {
8+
super();
9+
}
10+
11+
// API
12+
13+
public String getLogin() {
14+
return login;
15+
}
16+
17+
public void setLogin(final String login) {
18+
this.login = login;
19+
}
20+
21+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.baeldung.rest;
2+
3+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertThat;
7+
8+
import java.io.IOException;
9+
10+
import org.apache.http.HttpResponse;
11+
import org.apache.http.HttpStatus;
12+
import org.apache.http.client.ClientProtocolException;
13+
import org.apache.http.client.methods.HttpGet;
14+
import org.apache.http.client.methods.HttpUriRequest;
15+
import org.apache.http.entity.ContentType;
16+
import org.apache.http.impl.client.HttpClientBuilder;
17+
import org.hamcrest.Matchers;
18+
import org.junit.Test;
19+
20+
public class GithubBasicLiveTest {
21+
22+
// simple request - response
23+
24+
@Test
25+
public void givenUserDoesNotExists_whenUserInfoIsRetrieved_then404IsReceived() throws ClientProtocolException, IOException {
26+
// Given
27+
final String name = randomAlphabetic(8);
28+
final HttpUriRequest request = new HttpGet("https://api.github.com/users/" + name);
29+
30+
// When
31+
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
32+
33+
// Then
34+
assertThat(httpResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_NOT_FOUND));
35+
}
36+
37+
@Test
38+
public void givenRequestWithNoAcceptHeader_whenRequestIsExecuted_thenDefaultResponseContentTypeIsJson() throws ClientProtocolException, IOException {
39+
// Given
40+
final String jsonMimeType = "application/json";
41+
final HttpUriRequest request = new HttpGet("https://api.github.com/users/eugenp");
42+
43+
// When
44+
final HttpResponse response = HttpClientBuilder.create().build().execute(request);
45+
46+
// Then
47+
final String mimeType = ContentType.getOrDefault(response.getEntity()).getMimeType();
48+
assertEquals(jsonMimeType, mimeType);
49+
}
50+
51+
@Test
52+
public void givenUserExists_whenUserInformationIsRetrieved_thenRetrievedResourceIsCorrect() throws ClientProtocolException, IOException {
53+
// Given
54+
final HttpUriRequest request = new HttpGet("https://api.github.com/users/eugenp");
55+
56+
// When
57+
final HttpResponse response = HttpClientBuilder.create().build().execute(request);
58+
59+
// Then
60+
final GitHubUser resource = RetrieveUtil.retrieveResourceFromResponse(response, GitHubUser.class);
61+
assertThat("eugenp", Matchers.is(resource.getLogin()));
62+
}
63+
64+
}

rest-testing/src/test/java/org/baeldung/rest/HttpClientBasicLiveTest.java

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.rest;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.commons.io.IOUtils;
6+
import org.apache.http.HttpResponse;
7+
8+
import com.google.common.base.Preconditions;
9+
10+
public class RetrieveUtil {
11+
12+
public static String retrieveJsonFromResponse(final HttpResponse response) throws IOException {
13+
Preconditions.checkNotNull(response);
14+
15+
return IOUtils.toString(response.getEntity().getContent());
16+
}
17+
18+
public static <T> T retrieveResourceFromResponse(final HttpResponse response, final Class<T> clazz) throws IOException {
19+
Preconditions.checkNotNull(response);
20+
Preconditions.checkNotNull(clazz);
21+
22+
final String jsonFromResponse = retrieveJsonFromResponse(response);
23+
return ConvertUtil.convertJsonToResource(jsonFromResponse, clazz);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)