11package org .baeldung .client ;
22
33import static org .apache .commons .codec .binary .Base64 .encodeBase64 ;
4+ import static org .baeldung .Consts .APPLICATION_PORT ;
45import static org .hamcrest .CoreMatchers .is ;
56import static org .hamcrest .CoreMatchers .not ;
67import static org .hamcrest .MatcherAssert .assertThat ;
4344
4445public 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 };
0 commit comments