[RestTemplate] Create unit tests for exchange() and execute() methods
This commit is contained in:
parent
7f78499bd7
commit
7dc0bafa12
|
@ -2,6 +2,7 @@ package org.baeldung.client;
|
|||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.base.Charsets;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
|
@ -14,26 +15,26 @@ import org.baeldung.spring.PersistenceConfig;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.web.client.RequestCallback;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import static org.apache.commons.codec.binary.Base64.encodeBase64;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class })
|
||||
public class RestTemplateLiveTest {
|
||||
|
@ -42,6 +43,7 @@ public class RestTemplateLiveTest {
|
|||
private List<HttpMessageConverter<?>> messageConverters;
|
||||
private final String userReposUrl = "https://api.github.com/users/eugenp/repos";
|
||||
private final String repoUrl = "https://api.github.com/repos/eugenp/tutorials";
|
||||
private final String fooService = "http://localhost:8080/spring-security-rest-full/foos";
|
||||
|
||||
@Before
|
||||
public void beforeTest() {
|
||||
|
@ -108,7 +110,7 @@ public class RestTemplateLiveTest {
|
|||
Foo secondInstance = restTemplate.postForObject(fooService, request, Foo.class);
|
||||
assertThat(firstInstance, notNullValue());
|
||||
assertThat(secondInstance, notNullValue());
|
||||
assertThat(firstInstance, not(secondInstance));
|
||||
assertThat(firstInstance.getId(), not(secondInstance.getId()));
|
||||
}
|
||||
|
||||
private ClientHttpRequestFactory getClientHttpRequestFactory() {
|
||||
|
@ -128,4 +130,86 @@ public class RestTemplateLiveTest {
|
|||
|
||||
return new HttpComponentsClientHttpRequestFactory(client);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResource_whenCallHeadForHeaders_thenReceiveAllHeadersForThatResource() {
|
||||
String repoUrl = "https://api.github.com/repos/eugenp/{repoName}";
|
||||
RestTemplate template = new RestTemplate();
|
||||
|
||||
Map<String, Object> uriVariables = new HashMap<>();
|
||||
uriVariables.put("repoName", "tutorials");
|
||||
|
||||
HttpHeaders httpHeaders = template.headForHeaders(repoUrl, uriVariables);
|
||||
assertTrue(httpHeaders.getContentType().includes(MediaType.APPLICATION_JSON));
|
||||
assertThat(httpHeaders.get("X-RateLimit-Limit"), contains("60"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResource_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() {
|
||||
RestTemplate template = new RestTemplate(getClientHttpRequestFactory());
|
||||
Set<HttpMethod> optionsForAllow = template.optionsForAllow(fooService);
|
||||
HttpMethod[] supportedMethods = {HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE};
|
||||
assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRestService_whenPostResource_thenResourceIsCreated() {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
HttpHeaders headers = prepareBasicAuthHeaders();
|
||||
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers);
|
||||
|
||||
ResponseEntity<Foo> response = restTemplate.exchange(fooService, HttpMethod.POST, request, Foo.class);
|
||||
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
|
||||
Foo foo = response.getBody();
|
||||
assertThat(foo, notNullValue());
|
||||
assertThat(foo.getName(), is("bar"));
|
||||
assertThat(foo.getId(), is(1L));
|
||||
}
|
||||
|
||||
private HttpHeaders prepareBasicAuthHeaders() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
String encodedLogPass = getBase64EncodedLogPass();
|
||||
headers.add(HttpHeaders.AUTHORIZATION, "Basic " + encodedLogPass);
|
||||
return headers;
|
||||
}
|
||||
|
||||
private String getBase64EncodedLogPass() {
|
||||
String logPass = "user1:user1Pass";
|
||||
byte[] authHeaderBytes = encodeBase64(logPass.getBytes(Charsets.US_ASCII));
|
||||
return new String(authHeaderBytes, Charsets.US_ASCII);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenResource_whenPutExistingEntity_thenItIsUpdated() {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
HttpHeaders headers = prepareBasicAuthHeaders();
|
||||
HttpEntity<Foo> request = new HttpEntity<>(new Foo("bar"), headers);
|
||||
|
||||
//Create entity
|
||||
ResponseEntity<Foo> response = restTemplate.exchange(fooService, HttpMethod.POST, request, Foo.class);
|
||||
assertThat(response.getStatusCode(), is(HttpStatus.CREATED));
|
||||
assertThat(response.getBody().getId(), is(1L));
|
||||
|
||||
//Update entity
|
||||
Foo updatedInstance = new Foo("newName");
|
||||
updatedInstance.setId(1);
|
||||
String resourceUrl = fooService + "/1";
|
||||
restTemplate.execute(resourceUrl, HttpMethod.PUT, requestCallback(updatedInstance), clientHttpResponse -> null);
|
||||
|
||||
//Check that entity was updated
|
||||
response = restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class);
|
||||
Foo foo = response.getBody();
|
||||
assertThat(foo.getId(), is(1L));
|
||||
assertThat(foo.getName(), is(updatedInstance.getName()));
|
||||
}
|
||||
|
||||
private RequestCallback requestCallback(Foo updatedInstace) {
|
||||
return clientHttpRequest -> {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.writeValue(clientHttpRequest.getBody(), updatedInstace);
|
||||
clientHttpRequest.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
clientHttpRequest.getHeaders().add(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedLogPass());
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue