Skip to content

Commit b0c30e7

Browse files
author
Dmitry Zinkevich
committed
[RestTemplate] Use constant for an application port value
- Introduce consistent test names - Fix typo
1 parent 2c393c0 commit b0c30e7

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.baeldung;
2+
3+
public interface Consts {
4+
int APPLICATION_PORT = 8080;
5+
}

spring-security-rest-full/src/test/java/org/baeldung/client/RestTemplateLiveTest.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.baeldung.client;
22

33
import static org.apache.commons.codec.binary.Base64.encodeBase64;
4+
import static org.baeldung.Consts.APPLICATION_PORT;
45
import static org.hamcrest.CoreMatchers.is;
56
import static org.hamcrest.CoreMatchers.not;
67
import static org.hamcrest.MatcherAssert.assertThat;
@@ -43,9 +44,9 @@
4344

4445
public class RestTemplateLiveTest {
4546

46-
RestTemplate restTemplate;
47+
private RestTemplate restTemplate;
4748
private List<HttpMessageConverter<?>> messageConverters;
48-
private static final String fooResourceUrl = "http://localhost:8080/spring-security-rest-full/foos";
49+
private static final String fooResourceUrl = "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/foos";
4950

5051
@Before
5152
public void beforeTest() {
@@ -60,13 +61,13 @@ public void beforeTest() {
6061
}
6162

6263
@Test
63-
public void givenValidEndpoint_whenSendGetForRequestEntity_thenStatusOk() throws IOException {
64+
public void givenResourceUrl_whenSendGetForRequestEntity_thenStatusOk() throws IOException {
6465
ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class);
6566
assertThat(response.getStatusCode(), is(HttpStatus.OK));
6667
}
6768

6869
@Test
69-
public void givenRepoEndpoint_whenSendGetForRestEntity_thenReceiveCorrectJson() throws IOException {
70+
public void givenResourceUrl_whenSendGetForRestEntity_thenReceiveCorrectJson() throws IOException {
7071
ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class);
7172

7273
ObjectMapper mapper = new ObjectMapper();
@@ -80,7 +81,7 @@ public void givenRepoEndpoint_whenSendGetForRestEntity_thenReceiveCorrectJson()
8081
}
8182

8283
@Test
83-
public void givenRepoEndpoint_whenSendGetForObject_thenReturnsRepoObject() {
84+
public void givenResourceUrl_whenSendGetForObject_thenReturnsRepoObject() {
8485
restTemplate.setMessageConverters(messageConverters);
8586
Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class);
8687
assertThat(foo.getName(), is("bar"));
@@ -106,21 +107,21 @@ public void givenFooService_whenPostFor2Objects_thenNewObjectIsCreatedEachTime()
106107
}
107108

108109
@Test
109-
public void givenResource_whenCallHeadForHeaders_thenReceiveAllHeadersForThatResource() {
110+
public void givenFooService_whenCallHeadForHeaders_thenReceiveAllHeadersForThatResource() {
110111
HttpHeaders httpHeaders = restTemplate.headForHeaders(fooResourceUrl);
111112
assertTrue(httpHeaders.getContentType().includes(MediaType.APPLICATION_JSON));
112113
assertTrue(httpHeaders.get("bar").contains("baz"));
113114
}
114115

115116
@Test
116-
public void givenResource_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() {
117+
public void givenFooService_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() {
117118
Set<HttpMethod> optionsForAllow = restTemplate.optionsForAllow(fooResourceUrl);
118119
HttpMethod[] supportedMethods = {HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE};
119120
assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods)));
120121
}
121122

122123
@Test
123-
public void givenRestService_whenPostResource_thenResourceIsCreated() {
124+
public void givenFooService_whenPostResource_thenResourceIsCreated() {
124125
RestTemplate template = new RestTemplate();
125126

126127
HttpHeaders headers = prepareBasicAuthHeaders();
@@ -134,7 +135,7 @@ public void givenRestService_whenPostResource_thenResourceIsCreated() {
134135
}
135136

136137
@Test
137-
public void givenResource_whenPutExistingEntity_thenItIsUpdated() {
138+
public void givenFooService_whenPutExistingEntity_thenItIsUpdated() {
138139
RestTemplate template = new RestTemplate();
139140
HttpHeaders headers = prepareBasicAuthHeaders();
140141
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers);
@@ -156,7 +157,7 @@ public void givenResource_whenPutExistingEntity_thenItIsUpdated() {
156157
}
157158

158159
@Test
159-
public void givenRestService_whenCallDelete_thenEntityIsRemoved() {
160+
public void givenFooService_whenCallDelete_thenEntityIsRemoved() {
160161
Foo foo = new Foo("remove me");
161162
ResponseEntity<Foo> response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class);
162163
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
@@ -193,7 +194,7 @@ private ClientHttpRequestFactory getClientHttpRequestFactory() {
193194
.setSocketTimeout(timeout * 1000).build();
194195

195196
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
196-
credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM),
197+
credentialsProvider.setCredentials(new AuthScope("localhost", APPLICATION_PORT, AuthScope.ANY_REALM),
197198
new UsernamePasswordCredentials("user1", "user1Pass"));
198199

199200
CloseableHttpClient client = HttpClientBuilder.create()
@@ -216,10 +217,10 @@ private String getBase64EncodedLogPass() {
216217
return new String(authHeaderBytes, Charsets.US_ASCII);
217218
}
218219

219-
private RequestCallback requestCallback(Foo updatedInstace) {
220+
private RequestCallback requestCallback(Foo updatedInstance) {
220221
return clientHttpRequest -> {
221222
ObjectMapper mapper = new ObjectMapper();
222-
mapper.writeValue(clientHttpRequest.getBody(), updatedInstace);
223+
mapper.writeValue(clientHttpRequest.getBody(), updatedInstance);
223224
clientHttpRequest.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
224225
clientHttpRequest.getHeaders().add(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedLogPass());
225226
};

spring-security-rest-full/src/test/java/org/baeldung/common/web/AbstractLiveTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package org.baeldung.common.web;
22

3-
import java.io.Serializable;
4-
5-
import org.baeldung.test.IMarshaller;
6-
import org.springframework.beans.factory.annotation.Autowired;
7-
83
import com.google.common.base.Preconditions;
94
import com.google.common.net.HttpHeaders;
105
import com.jayway.restassured.RestAssured;
116
import com.jayway.restassured.response.Response;
127
import com.jayway.restassured.specification.RequestSpecification;
8+
import org.baeldung.test.IMarshaller;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
11+
import java.io.Serializable;
12+
13+
import static org.baeldung.Consts.APPLICATION_PORT;
1314

1415
public abstract class AbstractLiveTest<T extends Serializable> {
1516

@@ -55,7 +56,7 @@ final Response createAsResponse(final T resource) {
5556
//
5657

5758
protected String getURL() {
58-
return "http://localhost:8080/spring-security-rest-full/foos";
59+
return "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/foos";
5960
}
6061

6162
protected final RequestSpecification givenAuth() {

0 commit comments

Comments
 (0)