[RestTemplate] Use constant for an application port value

- Introduce consistent test names
- Fix typo
This commit is contained in:
Dmitry Zinkevich 2015-09-18 14:48:56 +03:00
parent 2c393c059e
commit b0c30e70ec
3 changed files with 26 additions and 19 deletions

View File

@ -0,0 +1,5 @@
package org.baeldung;
public interface Consts {
int APPLICATION_PORT = 8080;
}

View File

@ -1,6 +1,7 @@
package org.baeldung.client; package org.baeldung.client;
import static org.apache.commons.codec.binary.Base64.encodeBase64; import static org.apache.commons.codec.binary.Base64.encodeBase64;
import static org.baeldung.Consts.APPLICATION_PORT;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
@ -43,9 +44,9 @@ import com.google.common.base.Charsets;
public class RestTemplateLiveTest { public class RestTemplateLiveTest {
RestTemplate restTemplate; private RestTemplate restTemplate;
private List<HttpMessageConverter<?>> messageConverters; private List<HttpMessageConverter<?>> messageConverters;
private static final String fooResourceUrl = "http://localhost:8080/spring-security-rest-full/foos"; private static final String fooResourceUrl = "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/foos";
@Before @Before
public void beforeTest() { public void beforeTest() {
@ -60,13 +61,13 @@ public class RestTemplateLiveTest {
} }
@Test @Test
public void givenValidEndpoint_whenSendGetForRequestEntity_thenStatusOk() throws IOException { public void givenResourceUrl_whenSendGetForRequestEntity_thenStatusOk() throws IOException {
ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class); ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class);
assertThat(response.getStatusCode(), is(HttpStatus.OK)); assertThat(response.getStatusCode(), is(HttpStatus.OK));
} }
@Test @Test
public void givenRepoEndpoint_whenSendGetForRestEntity_thenReceiveCorrectJson() throws IOException { public void givenResourceUrl_whenSendGetForRestEntity_thenReceiveCorrectJson() throws IOException {
ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class); ResponseEntity<String> response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class);
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
@ -80,7 +81,7 @@ public class RestTemplateLiveTest {
} }
@Test @Test
public void givenRepoEndpoint_whenSendGetForObject_thenReturnsRepoObject() { public void givenResourceUrl_whenSendGetForObject_thenReturnsRepoObject() {
restTemplate.setMessageConverters(messageConverters); restTemplate.setMessageConverters(messageConverters);
Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class); Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class);
assertThat(foo.getName(), is("bar")); assertThat(foo.getName(), is("bar"));
@ -106,21 +107,21 @@ public class RestTemplateLiveTest {
} }
@Test @Test
public void givenResource_whenCallHeadForHeaders_thenReceiveAllHeadersForThatResource() { public void givenFooService_whenCallHeadForHeaders_thenReceiveAllHeadersForThatResource() {
HttpHeaders httpHeaders = restTemplate.headForHeaders(fooResourceUrl); HttpHeaders httpHeaders = restTemplate.headForHeaders(fooResourceUrl);
assertTrue(httpHeaders.getContentType().includes(MediaType.APPLICATION_JSON)); assertTrue(httpHeaders.getContentType().includes(MediaType.APPLICATION_JSON));
assertTrue(httpHeaders.get("bar").contains("baz")); assertTrue(httpHeaders.get("bar").contains("baz"));
} }
@Test @Test
public void givenResource_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() { public void givenFooService_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() {
Set<HttpMethod> optionsForAllow = restTemplate.optionsForAllow(fooResourceUrl); Set<HttpMethod> optionsForAllow = restTemplate.optionsForAllow(fooResourceUrl);
HttpMethod[] supportedMethods = {HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE}; HttpMethod[] supportedMethods = {HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE};
assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods))); assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods)));
} }
@Test @Test
public void givenRestService_whenPostResource_thenResourceIsCreated() { public void givenFooService_whenPostResource_thenResourceIsCreated() {
RestTemplate template = new RestTemplate(); RestTemplate template = new RestTemplate();
HttpHeaders headers = prepareBasicAuthHeaders(); HttpHeaders headers = prepareBasicAuthHeaders();
@ -134,7 +135,7 @@ public class RestTemplateLiveTest {
} }
@Test @Test
public void givenResource_whenPutExistingEntity_thenItIsUpdated() { public void givenFooService_whenPutExistingEntity_thenItIsUpdated() {
RestTemplate template = new RestTemplate(); RestTemplate template = new RestTemplate();
HttpHeaders headers = prepareBasicAuthHeaders(); HttpHeaders headers = prepareBasicAuthHeaders();
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers); HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers);
@ -156,7 +157,7 @@ public class RestTemplateLiveTest {
} }
@Test @Test
public void givenRestService_whenCallDelete_thenEntityIsRemoved() { public void givenFooService_whenCallDelete_thenEntityIsRemoved() {
Foo foo = new Foo("remove me"); Foo foo = new Foo("remove me");
ResponseEntity<Foo> response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class); ResponseEntity<Foo> response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class);
assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
@ -193,7 +194,7 @@ public class RestTemplateLiveTest {
.setSocketTimeout(timeout * 1000).build(); .setSocketTimeout(timeout * 1000).build();
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), credentialsProvider.setCredentials(new AuthScope("localhost", APPLICATION_PORT, AuthScope.ANY_REALM),
new UsernamePasswordCredentials("user1", "user1Pass")); new UsernamePasswordCredentials("user1", "user1Pass"));
CloseableHttpClient client = HttpClientBuilder.create() CloseableHttpClient client = HttpClientBuilder.create()
@ -216,10 +217,10 @@ public class RestTemplateLiveTest {
return new String(authHeaderBytes, Charsets.US_ASCII); return new String(authHeaderBytes, Charsets.US_ASCII);
} }
private RequestCallback requestCallback(Foo updatedInstace) { private RequestCallback requestCallback(Foo updatedInstance) {
return clientHttpRequest -> { return clientHttpRequest -> {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(clientHttpRequest.getBody(), updatedInstace); mapper.writeValue(clientHttpRequest.getBody(), updatedInstance);
clientHttpRequest.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); clientHttpRequest.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
clientHttpRequest.getHeaders().add(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedLogPass()); clientHttpRequest.getHeaders().add(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedLogPass());
}; };

View File

@ -1,15 +1,16 @@
package org.baeldung.common.web; package org.baeldung.common.web;
import java.io.Serializable;
import org.baeldung.test.IMarshaller;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.net.HttpHeaders; import com.google.common.net.HttpHeaders;
import com.jayway.restassured.RestAssured; import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response; import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification; import com.jayway.restassured.specification.RequestSpecification;
import org.baeldung.test.IMarshaller;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import static org.baeldung.Consts.APPLICATION_PORT;
public abstract class AbstractLiveTest<T extends Serializable> { public abstract class AbstractLiveTest<T extends Serializable> {
@ -55,7 +56,7 @@ public abstract class AbstractLiveTest<T extends Serializable> {
// //
protected String getURL() { protected String getURL() {
return "http://localhost:8080/spring-security-rest-full/foos"; return "http://localhost:" + APPLICATION_PORT + "/spring-security-rest-full/foos";
} }
protected final RequestSpecification givenAuth() { protected final RequestSpecification givenAuth() {