use of org.baeldung.web.dto.Foo in project tutorials by eugenp.
the class RestTemplateBasicLiveTest method givenFooService_whenPutExistingEntity_thenItIsUpdated.
// PUT
@Test
public void givenFooService_whenPutExistingEntity_thenItIsUpdated() {
final HttpHeaders headers = prepareBasicAuthHeaders();
final HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers);
// Create Resource
final ResponseEntity<Foo> createResponse = restTemplate.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class);
// Update Resource
final Foo updatedInstance = new Foo("newName");
updatedInstance.setId(createResponse.getBody().getId());
final String resourceUrl = fooResourceUrl + '/' + createResponse.getBody().getId();
final HttpEntity<Foo> requestUpdate = new HttpEntity<>(updatedInstance, headers);
restTemplate.exchange(resourceUrl, HttpMethod.PUT, requestUpdate, Void.class);
// Check that Resource was updated
final ResponseEntity<Foo> updateResponse = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
final Foo foo = updateResponse.getBody();
assertThat(foo.getName(), is(updatedInstance.getName()));
}
use of org.baeldung.web.dto.Foo in project tutorials by eugenp.
the class RestTemplateBasicLiveTest method givenResourceUrl_whenRetrievingResource_thenCorrect.
@Test
public void givenResourceUrl_whenRetrievingResource_thenCorrect() throws IOException {
final Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class);
assertThat(foo.getName(), notNullValue());
assertThat(foo.getId(), is(1L));
}
use of org.baeldung.web.dto.Foo in project tutorials by eugenp.
the class RestTemplateBasicLiveTest method givenFooService_whenCallDelete_thenEntityIsRemoved.
// DELETE
@Test
public void givenFooService_whenCallDelete_thenEntityIsRemoved() {
final Foo foo = new Foo("remove me");
final ResponseEntity<Foo> response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class);
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
final String entityUrl = fooResourceUrl + "/" + response.getBody().getId();
restTemplate.delete(entityUrl);
try {
restTemplate.getForEntity(entityUrl, Foo.class);
fail();
} catch (final HttpClientErrorException ex) {
assertThat(ex.getStatusCode(), is(HttpStatus.NOT_FOUND));
}
}
use of org.baeldung.web.dto.Foo in project tutorials by eugenp.
the class SpringHttpMessageConvertersLiveTest method givenConsumingXml_whenReadingTheFoo_thenCorrect.
@Test
public void givenConsumingXml_whenReadingTheFoo_thenCorrect() {
final String URI = BASE_URI + "foos/{id}";
final RestTemplate restTemplate = new RestTemplate();
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
final HttpEntity<String> entity = new HttpEntity<String>(headers);
final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1");
final Foo resource = response.getBody();
assertThat(resource, notNullValue());
}
use of org.baeldung.web.dto.Foo in project tutorials by eugenp.
the class SpringHttpMessageConvertersLiveTest method givenConsumingJson_whenReadingTheFoo_thenCorrect.
@Test
public void givenConsumingJson_whenReadingTheFoo_thenCorrect() {
final String URI = BASE_URI + "foos/{id}";
final RestTemplate restTemplate = new RestTemplate();
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
final HttpEntity<String> entity = new HttpEntity<String>(headers);
final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1");
final Foo resource = response.getBody();
assertThat(resource, notNullValue());
}
Aggregations