Search in sources :

Example 1 with Foo

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()));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Foo(org.baeldung.web.dto.Foo) Test(org.junit.Test)

Example 2 with Foo

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));
}
Also used : Foo(org.baeldung.web.dto.Foo) Test(org.junit.Test)

Example 3 with Foo

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));
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) Foo(org.baeldung.web.dto.Foo) Test(org.junit.Test)

Example 4 with Foo

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());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Foo(org.baeldung.web.dto.Foo) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Example 5 with Foo

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());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) Foo(org.baeldung.web.dto.Foo) RestTemplate(org.springframework.web.client.RestTemplate) Test(org.junit.Test)

Aggregations

Foo (org.baeldung.web.dto.Foo)14 Test (org.junit.Test)14 HttpEntity (org.springframework.http.HttpEntity)10 HttpHeaders (org.springframework.http.HttpHeaders)8 RestTemplate (org.springframework.web.client.RestTemplate)6 URI (java.net.URI)1 KryoHttpMessageConverter (org.baeldung.config.converter.KryoHttpMessageConverter)1 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)1 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1