11package org .baeldung .client ;
22
3- import com .fasterxml .jackson .databind .JsonNode ;
4- import com .fasterxml .jackson .databind .ObjectMapper ;
5- import com .google .common .base .Charsets ;
3+ import static org .apache .commons .codec .binary .Base64 .encodeBase64 ;
4+ import static org .hamcrest .CoreMatchers .is ;
5+ import static org .hamcrest .CoreMatchers .not ;
6+ import static org .hamcrest .MatcherAssert .assertThat ;
7+ import static org .hamcrest .Matchers .notNullValue ;
8+ import static org .junit .Assert .assertTrue ;
9+ import static org .junit .Assert .fail ;
10+
11+ import java .io .IOException ;
12+ import java .util .ArrayList ;
13+ import java .util .Arrays ;
14+ import java .util .List ;
15+ import java .util .Set ;
16+
617import org .apache .http .auth .AuthScope ;
718import org .apache .http .auth .UsernamePasswordCredentials ;
819import org .apache .http .client .config .RequestConfig ;
920import org .apache .http .impl .client .BasicCredentialsProvider ;
1021import org .apache .http .impl .client .CloseableHttpClient ;
1122import org .apache .http .impl .client .HttpClientBuilder ;
1223import org .baeldung .persistence .model .Foo ;
13- import org .baeldung .persistence .service .IFooService ;
14- import org .baeldung .spring .PersistenceConfig ;
1524import org .junit .Before ;
1625import org .junit .Test ;
17- import org .junit .runner .RunWith ;
18- import org .springframework .beans .factory .annotation .Autowired ;
19- import org .springframework .http .*;
26+ import org .springframework .http .HttpEntity ;
27+ import org .springframework .http .HttpHeaders ;
28+ import org .springframework .http .HttpMethod ;
29+ import org .springframework .http .HttpStatus ;
30+ import org .springframework .http .MediaType ;
31+ import org .springframework .http .ResponseEntity ;
2032import org .springframework .http .client .ClientHttpRequestFactory ;
2133import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
2234import org .springframework .http .converter .HttpMessageConverter ;
2335import org .springframework .http .converter .json .MappingJackson2HttpMessageConverter ;
24- import org .springframework .test .context .ContextConfiguration ;
25- import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
2636import org .springframework .web .client .HttpClientErrorException ;
2737import org .springframework .web .client .RequestCallback ;
2838import org .springframework .web .client .RestTemplate ;
2939
30- import java .io .IOException ;
31- import java .util .ArrayList ;
32- import java .util .Arrays ;
33- import java .util .List ;
34- import java .util .Set ;
35-
36- import static org .apache .commons .codec .binary .Base64 .encodeBase64 ;
37- import static org .hamcrest .CoreMatchers .is ;
38- import static org .hamcrest .CoreMatchers .not ;
39- import static org .hamcrest .MatcherAssert .assertThat ;
40- import static org .hamcrest .Matchers .notNullValue ;
41- import static org .junit .Assert .assertTrue ;
42- import static org .junit .Assert .fail ;
40+ import com .fasterxml .jackson .databind .JsonNode ;
41+ import com .fasterxml .jackson .databind .ObjectMapper ;
42+ import com .google .common .base .Charsets ;
4343
44- @ RunWith (SpringJUnit4ClassRunner .class )
45- @ ContextConfiguration (classes = {PersistenceConfig .class })
4644public class RestTemplateLiveTest {
4745
4846 RestTemplate restTemplate ;
4947 private List <HttpMessageConverter <?>> messageConverters ;
5048 private static final String fooResourceUrl = "http://localhost:8080/spring-security-rest-full/foos" ;
5149
52- @ Autowired
53- private IFooService fooService ;
54-
5550 @ Before
5651 public void beforeTest () {
5752 restTemplate = new RestTemplate (getClientHttpRequestFactory ());
@@ -114,7 +109,7 @@ public void givenFooService_whenPostFor2Objects_thenNewObjectIsCreatedEachTime()
114109 public void givenResource_whenCallHeadForHeaders_thenReceiveAllHeadersForThatResource () {
115110 HttpHeaders httpHeaders = restTemplate .headForHeaders (fooResourceUrl );
116111 assertTrue (httpHeaders .getContentType ().includes (MediaType .APPLICATION_JSON ));
117- assertTrue (httpHeaders .get ("foo " ).contains ("bar " ));
112+ assertTrue (httpHeaders .get ("bar " ).contains ("baz " ));
118113 }
119114
120115 @ Test
@@ -162,7 +157,11 @@ public void givenResource_whenPutExistingEntity_thenItIsUpdated() {
162157
163158 @ Test
164159 public void givenRestService_whenCallDelete_thenEntityIsRemoved () {
165- String entityUrl = fooResourceUrl + "/1" ;
160+ Foo foo = new Foo ("remove me" );
161+ ResponseEntity <Foo > response = restTemplate .postForEntity (fooResourceUrl , foo , Foo .class );
162+ assertThat (response .getStatusCode (), is (HttpStatus .CREATED ));
163+
164+ String entityUrl = fooResourceUrl + "/" + response .getBody ().getId ();
166165 restTemplate .delete (entityUrl );
167166 try {
168167 restTemplate .getForEntity (entityUrl , Foo .class );
@@ -174,9 +173,16 @@ public void givenRestService_whenCallDelete_thenEntityIsRemoved() {
174173
175174 private void ensureOneEntityExists () {
176175 Foo instance = new Foo ("bar" );
177- if (fooService .findOne (1L ) == null ) {
178- fooService .create (instance );
176+ instance .setId (1L );
177+
178+ try {
179+ restTemplate .getForEntity (fooResourceUrl + "/1" , Foo .class );
180+ } catch (HttpClientErrorException ex ) {
181+ if (ex .getStatusCode () == HttpStatus .NOT_FOUND ) {
182+ restTemplate .postForEntity (fooResourceUrl , instance , Foo .class );
183+ }
179184 }
185+
180186 }
181187
182188 private ClientHttpRequestFactory getClientHttpRequestFactory () {
0 commit comments