Cucumber fix (#2765)

* Rest-assured fix

* Cucumber fix

* HystrixManualTest

* Reformat HystrixTimeoutManualTest
This commit is contained in:
Grzegorz Piwowarek 2017-10-19 22:10:21 +02:00 committed by GitHub
parent aed9c8bfa8
commit 8616b07506
6 changed files with 66 additions and 78 deletions

View File

@ -2,7 +2,6 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>hystrix</artifactId> <artifactId>hystrix</artifactId>
<version>1.0</version> <version>1.0</version>
<name>hystrix</name> <name>hystrix</name>

View File

@ -10,53 +10,53 @@ import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
public class HystrixTimeoutIntegrationTest { public class HystrixTimeoutManualTest {
@Test @Test
public void givenInputBobAndDefaultSettings_whenCommandExecuted_thenReturnHelloBob(){ public void givenInputBobAndDefaultSettings_whenCommandExecuted_thenReturnHelloBob() {
assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!")); assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!"));
} }
@Test @Test
public void givenSvcTimeoutOf100AndDefaultSettings_whenRemoteSvcExecuted_thenReturnSuccess() public void givenSvcTimeoutOf100AndDefaultSettings_whenRemoteSvcExecuted_thenReturnSuccess()
throws InterruptedException { throws InterruptedException {
HystrixCommand.Setter config = HystrixCommand HystrixCommand.Setter config = HystrixCommand
.Setter .Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup2")); .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup2"));
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(), assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(),
equalTo("Success")); equalTo("Success"));
} }
@Test(expected = HystrixRuntimeException.class) @Test(expected = HystrixRuntimeException.class)
public void givenSvcTimeoutOf10000AndDefaultSettings__whenRemoteSvcExecuted_thenExpectHRE() throws InterruptedException { public void givenSvcTimeoutOf10000AndDefaultSettings__whenRemoteSvcExecuted_thenExpectHRE() throws InterruptedException {
HystrixCommand.Setter config = HystrixCommand HystrixCommand.Setter config = HystrixCommand
.Setter .Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest3")); .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest3"));
new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute(); new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute();
} }
@Test @Test
public void givenSvcTimeoutOf5000AndExecTimeoutOf10000_whenRemoteSvcExecuted_thenReturnSuccess() public void givenSvcTimeoutOf5000AndExecTimeoutOf10000_whenRemoteSvcExecuted_thenReturnSuccess()
throws InterruptedException { throws InterruptedException {
HystrixCommand.Setter config = HystrixCommand HystrixCommand.Setter config = HystrixCommand
.Setter .Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest4")); .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest4"));
HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter();
commandProperties.withExecutionTimeoutInMilliseconds(10_000); commandProperties.withExecutionTimeoutInMilliseconds(10_000);
config.andCommandPropertiesDefaults(commandProperties); config.andCommandPropertiesDefaults(commandProperties);
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
equalTo("Success")); equalTo("Success"));
} }
@Test(expected = HystrixRuntimeException.class) @Test(expected = HystrixRuntimeException.class)
public void givenSvcTimeoutOf15000AndExecTimeoutOf5000__whenExecuted_thenExpectHRE() public void givenSvcTimeoutOf15000AndExecTimeoutOf5000__whenExecuted_thenExpectHRE()
throws InterruptedException { throws InterruptedException {
HystrixCommand.Setter config = HystrixCommand HystrixCommand.Setter config = HystrixCommand
.Setter .Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest5")); .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest5"));
HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter();
commandProperties.withExecutionTimeoutInMilliseconds(5_000); commandProperties.withExecutionTimeoutInMilliseconds(5_000);
config.andCommandPropertiesDefaults(commandProperties); config.andCommandPropertiesDefaults(commandProperties);
@ -65,45 +65,45 @@ public class HystrixTimeoutIntegrationTest {
@Test @Test
public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool__whenExecuted_thenReturnSuccess() public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool__whenExecuted_thenReturnSuccess()
throws InterruptedException { throws InterruptedException {
HystrixCommand.Setter config = HystrixCommand HystrixCommand.Setter config = HystrixCommand
.Setter .Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupThreadPool")); .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupThreadPool"));
HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter();
commandProperties.withExecutionTimeoutInMilliseconds(10_000); commandProperties.withExecutionTimeoutInMilliseconds(10_000);
config.andCommandPropertiesDefaults(commandProperties); config.andCommandPropertiesDefaults(commandProperties);
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withMaxQueueSize(10) .withMaxQueueSize(10)
.withCoreSize(3) .withCoreSize(3)
.withQueueSizeRejectionThreshold(10)); .withQueueSizeRejectionThreshold(10));
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
equalTo("Success")); equalTo("Success"));
} }
@Test @Test
public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess() public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess()
throws InterruptedException { throws InterruptedException {
HystrixCommand.Setter config = HystrixCommand HystrixCommand.Setter config = HystrixCommand
.Setter .Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupCircuitBreaker")); .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupCircuitBreaker"));
HystrixCommandProperties.Setter properties = HystrixCommandProperties.Setter(); HystrixCommandProperties.Setter properties = HystrixCommandProperties.Setter();
properties.withExecutionTimeoutInMilliseconds(1000); properties.withExecutionTimeoutInMilliseconds(1000);
properties.withCircuitBreakerSleepWindowInMilliseconds(4000); properties.withCircuitBreakerSleepWindowInMilliseconds(4000);
properties.withExecutionIsolationStrategy( properties.withExecutionIsolationStrategy(
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD); HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
properties.withCircuitBreakerEnabled(true); properties.withCircuitBreakerEnabled(true);
properties.withCircuitBreakerRequestVolumeThreshold(1); properties.withCircuitBreakerRequestVolumeThreshold(1);
config.andCommandPropertiesDefaults(properties); config.andCommandPropertiesDefaults(properties);
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withMaxQueueSize(1) .withMaxQueueSize(1)
.withCoreSize(1) .withCoreSize(1)
.withQueueSizeRejectionThreshold(1)); .withQueueSizeRejectionThreshold(1));
assertThat(this.invokeRemoteService(config, 10_000), equalTo(null)); assertThat(this.invokeRemoteService(config, 10_000), equalTo(null));
assertThat(this.invokeRemoteService(config, 10_000), equalTo(null)); assertThat(this.invokeRemoteService(config, 10_000), equalTo(null));
@ -111,19 +111,19 @@ public class HystrixTimeoutIntegrationTest {
Thread.sleep(5000); Thread.sleep(5000);
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
equalTo("Success")); equalTo("Success"));
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
equalTo("Success")); equalTo("Success"));
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
equalTo("Success")); equalTo("Success"));
} }
public String invokeRemoteService(HystrixCommand.Setter config, int timeout) public String invokeRemoteService(HystrixCommand.Setter config, int timeout)
throws InterruptedException { throws InterruptedException {
String response = null; String response = null;
try { try {
response = new RemoteServiceTestCommand(config, response = new RemoteServiceTestCommand(config,
new RemoteServiceTestSimulator(timeout)).execute(); new RemoteServiceTestSimulator(timeout)).execute();
} catch (HystrixRuntimeException ex) { } catch (HystrixRuntimeException ex) {
System.out.println("ex = " + ex); System.out.println("ex = " + ex);
} }

View File

@ -1,16 +0,0 @@
package com.baeldung;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
public class OtherDefsIntegrationTest extends SpringIntegrationTest {
@When("^the client calls /baeldung$")
public void the_client_issues_POST_hello() throws Throwable {
executePost("http://localhost:8082/baeldung");
}
@Given("^the client calls /hello$")
public void the_client_issues_GET_hello() throws Throwable {
executeGet("http://localhost:8082/hello");
}
}

View File

@ -11,23 +11,19 @@ public class ResponseResults {
private final ClientHttpResponse theResponse; private final ClientHttpResponse theResponse;
private final String body; private final String body;
protected ResponseResults(final ClientHttpResponse response) throws IOException { ResponseResults(final ClientHttpResponse response) throws IOException {
this.theResponse = response; this.theResponse = response;
final InputStream bodyInputStream = response.getBody(); final InputStream bodyInputStream = response.getBody();
if (null == bodyInputStream) { final StringWriter stringWriter = new StringWriter();
this.body = "{}"; IOUtils.copy(bodyInputStream, stringWriter);
} else { this.body = stringWriter.toString();
final StringWriter stringWriter = new StringWriter();
IOUtils.copy(bodyInputStream, stringWriter);
this.body = stringWriter.toString();
}
} }
protected ClientHttpResponse getTheResponse() { ClientHttpResponse getTheResponse() {
return theResponse; return theResponse;
} }
protected String getBody() { String getBody() {
return body; return body;
} }
} }

View File

@ -1,9 +1,5 @@
package com.baeldung; 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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationContextLoader; import org.springframework.boot.test.SpringApplicationContextLoader;
@ -12,20 +8,23 @@ import org.springframework.http.client.ClientHttpResponse;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.ResponseErrorHandler; import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
//@RunWith(SpringJUnit4ClassRunner.class) //@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class) @ContextConfiguration(classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class)
@WebAppConfiguration @WebAppConfiguration
@IntegrationTest @IntegrationTest
public class SpringIntegrationTest { public class SpringIntegrationTest {
protected static ResponseResults latestResponse = null; static ResponseResults latestResponse = null;
@Autowired @Autowired
protected RestTemplate restTemplate; protected RestTemplate restTemplate;
protected void executeGet(String url) throws IOException { void executeGet(String url) throws IOException {
final Map<String, String> headers = new HashMap<>(); final Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json"); headers.put("Accept", "application/json");
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers); final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
@ -39,10 +38,9 @@ public class SpringIntegrationTest {
return (new ResponseResults(response)); return (new ResponseResults(response));
} }
}); });
} }
protected void executePost(String url) throws IOException { void executePost() throws IOException {
final Map<String, String> headers = new HashMap<>(); final Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json"); headers.put("Accept", "application/json");
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers); final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
@ -53,14 +51,14 @@ public class SpringIntegrationTest {
} }
restTemplate.setErrorHandler(errorHandler); restTemplate.setErrorHandler(errorHandler);
latestResponse = restTemplate.execute(url, HttpMethod.POST, requestCallback, response -> { latestResponse = restTemplate
if (errorHandler.hadError) { .execute("http://localhost:8082/baeldung", HttpMethod.POST, requestCallback, response -> {
return (errorHandler.getResults()); if (errorHandler.hadError) {
} else { return (errorHandler.getResults());
return (new ResponseResults(response)); } else {
} return (new ResponseResults(response));
}); }
});
} }
private class ResponseResultErrorHandler implements ResponseErrorHandler { private class ResponseResultErrorHandler implements ResponseErrorHandler {

View File

@ -3,6 +3,7 @@ package com.baeldung;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import cucumber.api.java.en.Given;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import cucumber.api.java.en.And; import cucumber.api.java.en.And;
@ -11,6 +12,16 @@ import cucumber.api.java.en.When;
public class StepDefsIntegrationTest extends SpringIntegrationTest { 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$") @When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable { public void the_client_issues_GET_version() throws Throwable {
executeGet("http://localhost:8082/version"); executeGet("http://localhost:8082/version");