From 8616b075066e62d969b8d8c6ca7c989f0df6ab15 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Thu, 19 Oct 2017 22:10:21 +0200 Subject: [PATCH] Cucumber fix (#2765) * Rest-assured fix * Cucumber fix * HystrixManualTest * Reformat HystrixTimeoutManualTest --- hystrix/pom.xml | 1 - ...est.java => HystrixTimeoutManualTest.java} | 68 +++++++++---------- .../baeldung/OtherDefsIntegrationTest.java | 16 ----- .../java/com/baeldung/ResponseResults.java | 16 ++--- .../com/baeldung/SpringIntegrationTest.java | 32 ++++----- .../com/baeldung/StepDefsIntegrationTest.java | 11 +++ 6 files changed, 66 insertions(+), 78 deletions(-) rename hystrix/src/test/java/com/baeldung/hystrix/{HystrixTimeoutIntegrationTest.java => HystrixTimeoutManualTest.java} (72%) delete mode 100644 spring-cucumber/src/test/java/com/baeldung/OtherDefsIntegrationTest.java diff --git a/hystrix/pom.xml b/hystrix/pom.xml index 7e9a217dba..58e09816ea 100644 --- a/hystrix/pom.xml +++ b/hystrix/pom.xml @@ -2,7 +2,6 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - com.baeldung hystrix 1.0 hystrix diff --git a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutIntegrationTest.java b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutManualTest.java similarity index 72% rename from hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutIntegrationTest.java rename to hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutManualTest.java index bf0c542980..ed89104c05 100644 --- a/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutIntegrationTest.java +++ b/hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutManualTest.java @@ -10,53 +10,53 @@ import org.junit.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; -public class HystrixTimeoutIntegrationTest { +public class HystrixTimeoutManualTest { @Test - public void givenInputBobAndDefaultSettings_whenCommandExecuted_thenReturnHelloBob(){ + public void givenInputBobAndDefaultSettings_whenCommandExecuted_thenReturnHelloBob() { assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!")); } @Test public void givenSvcTimeoutOf100AndDefaultSettings_whenRemoteSvcExecuted_thenReturnSuccess() - throws InterruptedException { + throws InterruptedException { HystrixCommand.Setter config = HystrixCommand - .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup2")); + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup2")); assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(), - equalTo("Success")); + equalTo("Success")); } @Test(expected = HystrixRuntimeException.class) public void givenSvcTimeoutOf10000AndDefaultSettings__whenRemoteSvcExecuted_thenExpectHRE() throws InterruptedException { HystrixCommand.Setter config = HystrixCommand - .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest3")); + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest3")); new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute(); } @Test public void givenSvcTimeoutOf5000AndExecTimeoutOf10000_whenRemoteSvcExecuted_thenReturnSuccess() - throws InterruptedException { + throws InterruptedException { HystrixCommand.Setter config = HystrixCommand - .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest4")); + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest4")); HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); commandProperties.withExecutionTimeoutInMilliseconds(10_000); config.andCommandPropertiesDefaults(commandProperties); assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), - equalTo("Success")); + equalTo("Success")); } @Test(expected = HystrixRuntimeException.class) public void givenSvcTimeoutOf15000AndExecTimeoutOf5000__whenExecuted_thenExpectHRE() - throws InterruptedException { + throws InterruptedException { HystrixCommand.Setter config = HystrixCommand - .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest5")); + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest5")); HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); commandProperties.withExecutionTimeoutInMilliseconds(5_000); config.andCommandPropertiesDefaults(commandProperties); @@ -65,45 +65,45 @@ public class HystrixTimeoutIntegrationTest { @Test public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool__whenExecuted_thenReturnSuccess() - throws InterruptedException { + throws InterruptedException { HystrixCommand.Setter config = HystrixCommand - .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupThreadPool")); + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupThreadPool")); HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter(); commandProperties.withExecutionTimeoutInMilliseconds(10_000); config.andCommandPropertiesDefaults(commandProperties); config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() - .withMaxQueueSize(10) - .withCoreSize(3) - .withQueueSizeRejectionThreshold(10)); + .withMaxQueueSize(10) + .withCoreSize(3) + .withQueueSizeRejectionThreshold(10)); assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), - equalTo("Success")); + equalTo("Success")); } @Test public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess() - throws InterruptedException { + throws InterruptedException { HystrixCommand.Setter config = HystrixCommand - .Setter - .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupCircuitBreaker")); + .Setter + .withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupCircuitBreaker")); HystrixCommandProperties.Setter properties = HystrixCommandProperties.Setter(); properties.withExecutionTimeoutInMilliseconds(1000); properties.withCircuitBreakerSleepWindowInMilliseconds(4000); properties.withExecutionIsolationStrategy( - HystrixCommandProperties.ExecutionIsolationStrategy.THREAD); + HystrixCommandProperties.ExecutionIsolationStrategy.THREAD); properties.withCircuitBreakerEnabled(true); properties.withCircuitBreakerRequestVolumeThreshold(1); config.andCommandPropertiesDefaults(properties); config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() - .withMaxQueueSize(1) - .withCoreSize(1) - .withQueueSizeRejectionThreshold(1)); + .withMaxQueueSize(1) + .withCoreSize(1) + .withQueueSizeRejectionThreshold(1)); 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); assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), - equalTo("Success")); + equalTo("Success")); assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), - equalTo("Success")); + equalTo("Success")); assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(), - equalTo("Success")); + equalTo("Success")); } public String invokeRemoteService(HystrixCommand.Setter config, int timeout) - throws InterruptedException { + throws InterruptedException { String response = null; try { response = new RemoteServiceTestCommand(config, - new RemoteServiceTestSimulator(timeout)).execute(); + new RemoteServiceTestSimulator(timeout)).execute(); } catch (HystrixRuntimeException ex) { System.out.println("ex = " + ex); } diff --git a/spring-cucumber/src/test/java/com/baeldung/OtherDefsIntegrationTest.java b/spring-cucumber/src/test/java/com/baeldung/OtherDefsIntegrationTest.java deleted file mode 100644 index 400452c3fa..0000000000 --- a/spring-cucumber/src/test/java/com/baeldung/OtherDefsIntegrationTest.java +++ /dev/null @@ -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"); - } -} \ No newline at end of file diff --git a/spring-cucumber/src/test/java/com/baeldung/ResponseResults.java b/spring-cucumber/src/test/java/com/baeldung/ResponseResults.java index 4c0125e9b4..0f5bcbb824 100644 --- a/spring-cucumber/src/test/java/com/baeldung/ResponseResults.java +++ b/spring-cucumber/src/test/java/com/baeldung/ResponseResults.java @@ -11,23 +11,19 @@ public class ResponseResults { private final ClientHttpResponse theResponse; private final String body; - protected ResponseResults(final ClientHttpResponse response) throws IOException { + ResponseResults(final ClientHttpResponse response) throws IOException { this.theResponse = response; final InputStream bodyInputStream = response.getBody(); - if (null == bodyInputStream) { - this.body = "{}"; - } else { - final StringWriter stringWriter = new StringWriter(); - IOUtils.copy(bodyInputStream, stringWriter); - this.body = stringWriter.toString(); - } + final StringWriter stringWriter = new StringWriter(); + IOUtils.copy(bodyInputStream, stringWriter); + this.body = stringWriter.toString(); } - protected ClientHttpResponse getTheResponse() { + ClientHttpResponse getTheResponse() { return theResponse; } - protected String getBody() { + String getBody() { return body; } } \ No newline at end of file diff --git a/spring-cucumber/src/test/java/com/baeldung/SpringIntegrationTest.java b/spring-cucumber/src/test/java/com/baeldung/SpringIntegrationTest.java index 159ff5dc21..9fbaeb348d 100644 --- a/spring-cucumber/src/test/java/com/baeldung/SpringIntegrationTest.java +++ b/spring-cucumber/src/test/java/com/baeldung/SpringIntegrationTest.java @@ -1,9 +1,5 @@ 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.IntegrationTest; 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.web.WebAppConfiguration; import org.springframework.web.client.ResponseErrorHandler; -import org.springframework.web.client.ResponseExtractor; import org.springframework.web.client.RestTemplate; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + //@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class) @WebAppConfiguration @IntegrationTest public class SpringIntegrationTest { - protected static ResponseResults latestResponse = null; + static ResponseResults latestResponse = null; @Autowired protected RestTemplate restTemplate; - protected void executeGet(String url) throws IOException { + void executeGet(String url) throws IOException { final Map headers = new HashMap<>(); headers.put("Accept", "application/json"); final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers); @@ -39,10 +38,9 @@ public class SpringIntegrationTest { return (new ResponseResults(response)); } }); - } - protected void executePost(String url) throws IOException { + void executePost() throws IOException { final Map headers = new HashMap<>(); headers.put("Accept", "application/json"); final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers); @@ -53,14 +51,14 @@ public class SpringIntegrationTest { } restTemplate.setErrorHandler(errorHandler); - latestResponse = restTemplate.execute(url, HttpMethod.POST, requestCallback, response -> { - if (errorHandler.hadError) { - return (errorHandler.getResults()); - } else { - return (new ResponseResults(response)); - } - }); - + 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 { diff --git a/spring-cucumber/src/test/java/com/baeldung/StepDefsIntegrationTest.java b/spring-cucumber/src/test/java/com/baeldung/StepDefsIntegrationTest.java index 42fb8a44ee..e1b6e370c7 100644 --- a/spring-cucumber/src/test/java/com/baeldung/StepDefsIntegrationTest.java +++ b/spring-cucumber/src/test/java/com/baeldung/StepDefsIntegrationTest.java @@ -3,6 +3,7 @@ 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; @@ -11,6 +12,16 @@ 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");