|
| 1 | +package org.baeldung.web.test; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.baeldung.web.dto.Foo; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | +import org.springframework.http.HttpEntity; |
| 11 | +import org.springframework.http.HttpHeaders; |
| 12 | +import org.springframework.http.HttpMethod; |
| 13 | +import org.springframework.http.MediaType; |
| 14 | +import org.springframework.http.ResponseEntity; |
| 15 | +import org.springframework.http.converter.HttpMessageConverter; |
| 16 | +import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; |
| 17 | +import org.springframework.oxm.xstream.XStreamMarshaller; |
| 18 | +import org.springframework.web.client.RestTemplate; |
| 19 | + |
| 20 | +/** |
| 21 | + * Integration Test class. Tests methods hits the server's rest services. |
| 22 | + */ |
| 23 | +public class SpringHttpMessageConvertersIntegrationTestsCase { |
| 24 | + |
| 25 | + private static String BASE_URI = "http://localhost:8080/spring-rest/"; |
| 26 | + |
| 27 | + /** |
| 28 | + * Without specifying Accept Header, uses the default response from the |
| 29 | + * server (in this case json) |
| 30 | + */ |
| 31 | + @Test |
| 32 | + public void testGetFoo() { |
| 33 | + final String URI = BASE_URI + "foos/{id}"; |
| 34 | + |
| 35 | + final RestTemplate restTemplate = new RestTemplate(); |
| 36 | + final Foo resource = restTemplate.getForObject(URI, Foo.class, "1"); |
| 37 | + |
| 38 | + Assert.assertEquals(1l, resource.getId()); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Specifying Accept Header with application/xml for getting the xml response from the server. |
| 43 | + */ |
| 44 | + @Test |
| 45 | + public void testGetFooAcceptXML() { |
| 46 | + final String URI = BASE_URI + "foos/{id}"; |
| 47 | + |
| 48 | + final RestTemplate restTemplate = new RestTemplate(); |
| 49 | + restTemplate.setMessageConverters(getMessageConverters()); |
| 50 | + |
| 51 | + final HttpHeaders headers = new HttpHeaders(); |
| 52 | + headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML)); |
| 53 | + final HttpEntity<String> entity = new HttpEntity<String>(headers); |
| 54 | + |
| 55 | + final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1"); |
| 56 | + final Foo resource = response.getBody(); |
| 57 | + |
| 58 | + Assert.assertEquals(1l, resource.getId()); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Specifying Accept Header with application/xml for getting the xml response from the server. |
| 64 | + */ |
| 65 | + @Test |
| 66 | + public void testPUTFooXML() { |
| 67 | + final String URI = BASE_URI + "foos/{id}"; |
| 68 | + final RestTemplate restTemplate = new RestTemplate(); |
| 69 | + restTemplate.setMessageConverters(getMessageConverters()); |
| 70 | + |
| 71 | + final Foo resource = new Foo(4, "andres"); |
| 72 | + final HttpHeaders headers = new HttpHeaders(); |
| 73 | + headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML)); |
| 74 | + headers.setContentType((MediaType.APPLICATION_XML)); |
| 75 | + final HttpEntity<Foo> entity = new HttpEntity<Foo>(resource, headers); |
| 76 | + |
| 77 | + final ResponseEntity<Foo> response = restTemplate.exchange(URI, HttpMethod.PUT, entity, Foo.class, resource.getId()); |
| 78 | + final Foo fooResponse = response.getBody(); |
| 79 | + |
| 80 | + Assert.assertEquals(resource.getId(), fooResponse.getId()); |
| 81 | + } |
| 82 | + |
| 83 | + // UTIL |
| 84 | + |
| 85 | + /** |
| 86 | + * Configures Message Converters. |
| 87 | + */ |
| 88 | + private List<HttpMessageConverter<?>> getMessageConverters() { |
| 89 | + final List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); |
| 90 | + // adds XML converter using XStreamMarshaller |
| 91 | + final XStreamMarshaller marshaller = new XStreamMarshaller(); |
| 92 | + marshaller.setAnnotatedClasses(Foo.class); |
| 93 | + |
| 94 | + final MarshallingHttpMessageConverter marshallingConverter = new MarshallingHttpMessageConverter(marshaller); |
| 95 | + converters.add(marshallingConverter); |
| 96 | + |
| 97 | + return converters; |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments