Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,11 @@
package com.baeldung;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources")
public class CucumberIntegrationTest extends SpringIntegrationTest{
}
@@ -0,0 +1,33 @@
package com.baeldung;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.web.client.RequestCallback;
import java.io.IOException;
import java.util.Map;
public class HeaderSettingRequestCallback implements RequestCallback {
final Map<String, String> requestHeaders;
private String body;
public HeaderSettingRequestCallback(final Map<String, String> headers) {
this.requestHeaders = headers;
}
public void setBody(final String postBody) {
this.body = postBody;
}
@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
final HttpHeaders clientHeaders = request.getHeaders();
for (final Map.Entry<String, String> entry : requestHeaders.entrySet()) {
clientHeaders.add(entry.getKey(), entry.getValue());
}
if (null != body) {
request.getBody().write(body.getBytes());
}
}
}
@@ -0,0 +1,29 @@
package com.baeldung;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import org.apache.commons.io.IOUtils;
import org.springframework.http.client.ClientHttpResponse;
public class ResponseResults {
private final ClientHttpResponse theResponse;
private final String body;
ResponseResults(final ClientHttpResponse response) throws IOException {
this.theResponse = response;
final InputStream bodyInputStream = response.getBody();
final StringWriter stringWriter = new StringWriter();
IOUtils.copy(bodyInputStream, stringWriter);
this.body = stringWriter.toString();
}
ClientHttpResponse getTheResponse() {
return theResponse;
}
String getBody() {
return body;
}
}
@@ -0,0 +1,81 @@
package com.baeldung;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
//@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringDemoApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT)
@ContextConfiguration
public class SpringIntegrationTest {
static ResponseResults latestResponse = null;
@Autowired
protected RestTemplate restTemplate;
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);
latestResponse = restTemplate.execute(url, HttpMethod.GET, requestCallback, response -> {
if (errorHandler.hadError) {
return (errorHandler.getResults());
} else {
return (new ResponseResults(response));
}
});
}
void executePost() 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);
latestResponse = restTemplate
.execute("http://localhost:8082/baeldung", HttpMethod.POST, requestCallback, response -> {
if (errorHandler.hadError) {
return (errorHandler.getResults());
} else {
return (new ResponseResults(response));
}
});
}
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);
}
}
}
@@ -0,0 +1,40 @@
package com.baeldung;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import cucumber.api.java.en.Given;
import org.springframework.http.HttpStatus;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class StepDefsIntegrationTest extends SpringIntegrationTest {
@When("^the client calls /baeldung$")
public void the_client_issues_POST_hello() throws Throwable {
executePost();
}
@Given("^the client calls /hello$")
public void the_client_issues_GET_hello() throws Throwable {
executeGet("http://localhost:8082/hello");
}
@When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable {
executeGet("http://localhost:8082/version");
}
@Then("^the client receives status code of (\\d+)$")
public void the_client_receives_status_code_of(int statusCode) throws Throwable {
final HttpStatus currentStatusCode = latestResponse.getTheResponse().getStatusCode();
assertThat("status code is incorrect : " + latestResponse.getBody(), currentStatusCode.value(), is(statusCode));
}
@And("^the client receives server version (.+)$")
public void the_client_receives_server_version_body(String version) throws Throwable {
assertThat(latestResponse.getBody(), is(version));
}
}