|
| 1 | +package org.baeldung.client; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import org.apache.http.auth.AuthScope; |
| 6 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 7 | +import org.apache.http.client.config.RequestConfig; |
| 8 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 9 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 10 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 11 | +import org.baeldung.persistence.model.Foo; |
| 12 | +import org.baeldung.persistence.model.Repository; |
| 13 | +import org.baeldung.spring.PersistenceConfig; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.springframework.http.HttpEntity; |
| 18 | +import org.springframework.http.HttpStatus; |
| 19 | +import org.springframework.http.ResponseEntity; |
| 20 | +import org.springframework.http.client.ClientHttpRequestFactory; |
| 21 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 22 | +import org.springframework.http.converter.HttpMessageConverter; |
| 23 | +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
| 24 | +import org.springframework.test.context.ContextConfiguration; |
| 25 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 26 | +import org.springframework.web.client.RestTemplate; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.hamcrest.CoreMatchers.is; |
| 33 | +import static org.hamcrest.CoreMatchers.not; |
| 34 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 35 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | + |
| 37 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 38 | +@ContextConfiguration(classes = { PersistenceConfig.class }) |
| 39 | +public class RestTemplateLiveTest { |
| 40 | + |
| 41 | + RestTemplate restTemplate; |
| 42 | + private List<HttpMessageConverter<?>> messageConverters; |
| 43 | + private final String userReposUrl = "https://api.github.com/users/eugenp/repos"; |
| 44 | + private final String repoUrl = "https://api.github.com/repos/eugenp/tutorials"; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void beforeTest() { |
| 48 | + restTemplate = new RestTemplate(); |
| 49 | + |
| 50 | + messageConverters = new ArrayList<>(); |
| 51 | + MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter(); |
| 52 | + jsonMessageConverter.setObjectMapper(new ObjectMapper()); |
| 53 | + messageConverters.add(jsonMessageConverter); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenValidEndpoint_whenSendGetForRequestEntity_thenStatusOk() throws IOException { |
| 58 | + ResponseEntity<String> response = restTemplate.getForEntity(userReposUrl, String.class); |
| 59 | + assertThat(response.getStatusCode(), is(HttpStatus.OK)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenRepoEndpoint_whenSendGetForRestEntity_thenReceiveCorrectRepoJson() throws IOException { |
| 64 | + ResponseEntity<String> response = restTemplate.getForEntity(repoUrl, String.class); |
| 65 | + |
| 66 | + ObjectMapper mapper = new ObjectMapper(); |
| 67 | + JsonNode root = mapper.readTree(response.getBody()); |
| 68 | + |
| 69 | + JsonNode name = root.path("name"); |
| 70 | + assertThat(name.asText(), is("tutorials")); |
| 71 | + |
| 72 | + JsonNode owner = root.path("owner").path("login"); |
| 73 | + assertThat(owner.asText(), is("eugenp")); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenRepoEndpoint_whenSendGetForObject_thenReturnsRepoObject() { |
| 78 | + restTemplate.setMessageConverters(messageConverters); |
| 79 | + String repoUrl = "https://api.github.com/repos/eugenp/tutorials"; |
| 80 | + Repository repository = restTemplate.getForObject(repoUrl, Repository.class); |
| 81 | + |
| 82 | + assertThat(repository.getName(), is("tutorials")); |
| 83 | + assertThat(repository.getOwner().getLogin(), is("eugenp")); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void givenFooService_whenPostForObject_thenCreatedObjectIsReturned() { |
| 88 | + String fooService = "http://localhost:8080/spring-security-rest-full/foos"; |
| 89 | + |
| 90 | + ClientHttpRequestFactory requestFactory = getClientHttpRequestFactory(); |
| 91 | + RestTemplate restTemplate = new RestTemplate(requestFactory); |
| 92 | + |
| 93 | + HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar")); |
| 94 | + Foo foo = restTemplate.postForObject(fooService, request, Foo.class); |
| 95 | + assertThat(foo, notNullValue()); |
| 96 | + assertThat(foo.getName(), is("bar")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void givenFooService_whenPostFor2Objects_thenNewObjectIsCreatedEachTime() { |
| 101 | + String fooService = "http://localhost:8080/spring-security-rest-full/foos"; |
| 102 | + |
| 103 | + ClientHttpRequestFactory requestFactory = getClientHttpRequestFactory(); |
| 104 | + RestTemplate restTemplate = new RestTemplate(requestFactory); |
| 105 | + |
| 106 | + HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar")); |
| 107 | + Foo firstInstance = restTemplate.postForObject(fooService, request, Foo.class); |
| 108 | + Foo secondInstance = restTemplate.postForObject(fooService, request, Foo.class); |
| 109 | + assertThat(firstInstance, notNullValue()); |
| 110 | + assertThat(secondInstance, notNullValue()); |
| 111 | + assertThat(firstInstance, not(secondInstance)); |
| 112 | + } |
| 113 | + |
| 114 | + private ClientHttpRequestFactory getClientHttpRequestFactory() { |
| 115 | + int timeout = 5; |
| 116 | + RequestConfig config = RequestConfig.custom() |
| 117 | + .setConnectTimeout(timeout * 1000) |
| 118 | + .setConnectionRequestTimeout(timeout * 1000) |
| 119 | + .setSocketTimeout(timeout * 1000).build(); |
| 120 | + |
| 121 | + BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
| 122 | + credentialsProvider.setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), |
| 123 | + new UsernamePasswordCredentials("user1", "user1Pass")); |
| 124 | + |
| 125 | + CloseableHttpClient client = HttpClientBuilder.create() |
| 126 | + .setDefaultRequestConfig(config) |
| 127 | + .setDefaultCredentialsProvider(credentialsProvider).build(); |
| 128 | + |
| 129 | + return new HttpComponentsClientHttpRequestFactory(client); |
| 130 | + } |
| 131 | +} |
0 commit comments