2016-07-27 13:19:46 +03:00
|
|
|
package com.baeldung;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2016-08-03 16:20:49 -05:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2016-07-27 13:19:46 +03:00
|
|
|
import org.springframework.boot.test.IntegrationTest;
|
|
|
|
import org.springframework.boot.test.SpringApplicationContextLoader;
|
|
|
|
import org.springframework.http.HttpMethod;
|
|
|
|
import org.springframework.http.client.ClientHttpResponse;
|
|
|
|
import org.springframework.test.context.ContextConfiguration;
|
|
|
|
import org.springframework.test.context.web.WebAppConfiguration;
|
|
|
|
import org.springframework.web.client.ResponseErrorHandler;
|
|
|
|
import org.springframework.web.client.ResponseExtractor;
|
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
|
|
//@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
|
@ContextConfiguration(classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class)
|
|
|
|
@WebAppConfiguration
|
|
|
|
@IntegrationTest
|
|
|
|
public class SpringIntegrationTest {
|
2016-08-02 12:34:08 +03:00
|
|
|
protected static ResponseResults latestResponse = null;
|
|
|
|
|
2016-08-03 16:20:49 -05:00
|
|
|
@Autowired
|
|
|
|
protected RestTemplate restTemplate;
|
2016-08-02 12:34:08 +03:00
|
|
|
|
|
|
|
protected void executeGet(String url) throws IOException {
|
|
|
|
final Map<String, String> headers = new HashMap<>();
|
|
|
|
headers.put("Accept", "application/json");
|
|
|
|
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
|
|
|
|
final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
|
|
|
|
|
|
|
|
restTemplate.setErrorHandler(errorHandler);
|
2017-10-19 17:51:31 +02:00
|
|
|
latestResponse = restTemplate.execute(url, HttpMethod.GET, requestCallback, response -> {
|
|
|
|
if (errorHandler.hadError) {
|
|
|
|
return (errorHandler.getResults());
|
|
|
|
} else {
|
|
|
|
return (new ResponseResults(response));
|
2016-08-02 12:34:08 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void executePost(String url) throws IOException {
|
|
|
|
final Map<String, String> headers = new HashMap<>();
|
|
|
|
headers.put("Accept", "application/json");
|
|
|
|
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
|
|
|
|
final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
|
|
|
|
|
|
|
|
if (restTemplate == null) {
|
|
|
|
restTemplate = new RestTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
restTemplate.setErrorHandler(errorHandler);
|
2017-10-19 17:51:31 +02:00
|
|
|
latestResponse = restTemplate.execute(url, HttpMethod.POST, requestCallback, response -> {
|
|
|
|
if (errorHandler.hadError) {
|
|
|
|
return (errorHandler.getResults());
|
|
|
|
} else {
|
|
|
|
return (new ResponseResults(response));
|
2016-08-02 12:34:08 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ResponseResultErrorHandler implements ResponseErrorHandler {
|
|
|
|
private ResponseResults results = null;
|
|
|
|
private Boolean hadError = false;
|
|
|
|
|
|
|
|
private ResponseResults getResults() {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasError(ClientHttpResponse response) throws IOException {
|
|
|
|
hadError = response.getRawStatusCode() >= 400;
|
|
|
|
return hadError;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handleError(ClientHttpResponse response) throws IOException {
|
|
|
|
results = new ResponseResults(response);
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 09:43:09 -05:00
|
|
|
}
|