Skip to content

Commit 8616b07

Browse files
authored
Cucumber fix (eugenp#2765)
* Rest-assured fix * Cucumber fix * HystrixManualTest * Reformat HystrixTimeoutManualTest
1 parent aed9c8b commit 8616b07

6 files changed

Lines changed: 66 additions & 78 deletions

File tree

hystrix/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
33

44
<modelVersion>4.0.0</modelVersion>
5-
<groupId>com.baeldung</groupId>
65
<artifactId>hystrix</artifactId>
76
<version>1.0</version>
87
<name>hystrix</name>

hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutIntegrationTest.java renamed to hystrix/src/test/java/com/baeldung/hystrix/HystrixTimeoutManualTest.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,53 @@
1010
import static org.hamcrest.MatcherAssert.assertThat;
1111
import static org.hamcrest.Matchers.equalTo;
1212

13-
public class HystrixTimeoutIntegrationTest {
13+
public class HystrixTimeoutManualTest {
1414

1515
@Test
16-
public void givenInputBobAndDefaultSettings_whenCommandExecuted_thenReturnHelloBob(){
16+
public void givenInputBobAndDefaultSettings_whenCommandExecuted_thenReturnHelloBob() {
1717
assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!"));
1818
}
1919

2020
@Test
2121
public void givenSvcTimeoutOf100AndDefaultSettings_whenRemoteSvcExecuted_thenReturnSuccess()
22-
throws InterruptedException {
22+
throws InterruptedException {
2323
HystrixCommand.Setter config = HystrixCommand
24-
.Setter
25-
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup2"));
24+
.Setter
25+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroup2"));
2626

2727
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(100)).execute(),
28-
equalTo("Success"));
28+
equalTo("Success"));
2929
}
3030

3131
@Test(expected = HystrixRuntimeException.class)
3232
public void givenSvcTimeoutOf10000AndDefaultSettings__whenRemoteSvcExecuted_thenExpectHRE() throws InterruptedException {
3333
HystrixCommand.Setter config = HystrixCommand
34-
.Setter
35-
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest3"));
34+
.Setter
35+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest3"));
3636
new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(10_000)).execute();
3737
}
3838

3939
@Test
4040
public void givenSvcTimeoutOf5000AndExecTimeoutOf10000_whenRemoteSvcExecuted_thenReturnSuccess()
41-
throws InterruptedException {
41+
throws InterruptedException {
4242

4343
HystrixCommand.Setter config = HystrixCommand
44-
.Setter
45-
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest4"));
44+
.Setter
45+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest4"));
4646
HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter();
4747
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
4848
config.andCommandPropertiesDefaults(commandProperties);
4949

5050
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
51-
equalTo("Success"));
51+
equalTo("Success"));
5252
}
5353

5454
@Test(expected = HystrixRuntimeException.class)
5555
public void givenSvcTimeoutOf15000AndExecTimeoutOf5000__whenExecuted_thenExpectHRE()
56-
throws InterruptedException {
56+
throws InterruptedException {
5757
HystrixCommand.Setter config = HystrixCommand
58-
.Setter
59-
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest5"));
58+
.Setter
59+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupTest5"));
6060
HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter();
6161
commandProperties.withExecutionTimeoutInMilliseconds(5_000);
6262
config.andCommandPropertiesDefaults(commandProperties);
@@ -65,65 +65,65 @@ public void givenSvcTimeoutOf15000AndExecTimeoutOf5000__whenExecuted_thenExpectH
6565

6666
@Test
6767
public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool__whenExecuted_thenReturnSuccess()
68-
throws InterruptedException {
68+
throws InterruptedException {
6969

7070
HystrixCommand.Setter config = HystrixCommand
71-
.Setter
72-
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupThreadPool"));
71+
.Setter
72+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupThreadPool"));
7373
HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter();
7474
commandProperties.withExecutionTimeoutInMilliseconds(10_000);
7575
config.andCommandPropertiesDefaults(commandProperties);
7676
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
77-
.withMaxQueueSize(10)
78-
.withCoreSize(3)
79-
.withQueueSizeRejectionThreshold(10));
77+
.withMaxQueueSize(10)
78+
.withCoreSize(3)
79+
.withQueueSizeRejectionThreshold(10));
8080

8181
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
82-
equalTo("Success"));
82+
equalTo("Success"));
8383
}
8484

8585
@Test
8686
public void givenCircuitBreakerSetup__whenRemoteSvcCmdExecuted_thenReturnSuccess()
87-
throws InterruptedException {
87+
throws InterruptedException {
8888

8989
HystrixCommand.Setter config = HystrixCommand
90-
.Setter
91-
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupCircuitBreaker"));
90+
.Setter
91+
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RemoteServiceGroupCircuitBreaker"));
9292
HystrixCommandProperties.Setter properties = HystrixCommandProperties.Setter();
9393
properties.withExecutionTimeoutInMilliseconds(1000);
9494

9595
properties.withCircuitBreakerSleepWindowInMilliseconds(4000);
9696
properties.withExecutionIsolationStrategy(
97-
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
97+
HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
9898
properties.withCircuitBreakerEnabled(true);
9999
properties.withCircuitBreakerRequestVolumeThreshold(1);
100100

101101
config.andCommandPropertiesDefaults(properties);
102102

103103
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
104-
.withMaxQueueSize(1)
105-
.withCoreSize(1)
106-
.withQueueSizeRejectionThreshold(1));
104+
.withMaxQueueSize(1)
105+
.withCoreSize(1)
106+
.withQueueSizeRejectionThreshold(1));
107107

108108
assertThat(this.invokeRemoteService(config, 10_000), equalTo(null));
109109
assertThat(this.invokeRemoteService(config, 10_000), equalTo(null));
110110
assertThat(this.invokeRemoteService(config, 10_000), equalTo(null));
111111
Thread.sleep(5000);
112112

113113
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
114-
equalTo("Success"));
114+
equalTo("Success"));
115115
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
116-
equalTo("Success"));
116+
equalTo("Success"));
117117
assertThat(new RemoteServiceTestCommand(config, new RemoteServiceTestSimulator(500)).execute(),
118-
equalTo("Success"));
118+
equalTo("Success"));
119119
}
120120

121121
public String invokeRemoteService(HystrixCommand.Setter config, int timeout)
122-
throws InterruptedException {
122+
throws InterruptedException {
123123
String response = null;
124124
try {
125125
response = new RemoteServiceTestCommand(config,
126-
new RemoteServiceTestSimulator(timeout)).execute();
126+
new RemoteServiceTestSimulator(timeout)).execute();
127127
} catch (HystrixRuntimeException ex) {
128128
System.out.println("ex = " + ex);
129129
}

spring-cucumber/src/test/java/com/baeldung/OtherDefsIntegrationTest.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

spring-cucumber/src/test/java/com/baeldung/ResponseResults.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,19 @@ public class ResponseResults {
1111
private final ClientHttpResponse theResponse;
1212
private final String body;
1313

14-
protected ResponseResults(final ClientHttpResponse response) throws IOException {
14+
ResponseResults(final ClientHttpResponse response) throws IOException {
1515
this.theResponse = response;
1616
final InputStream bodyInputStream = response.getBody();
17-
if (null == bodyInputStream) {
18-
this.body = "{}";
19-
} else {
20-
final StringWriter stringWriter = new StringWriter();
21-
IOUtils.copy(bodyInputStream, stringWriter);
22-
this.body = stringWriter.toString();
23-
}
17+
final StringWriter stringWriter = new StringWriter();
18+
IOUtils.copy(bodyInputStream, stringWriter);
19+
this.body = stringWriter.toString();
2420
}
2521

26-
protected ClientHttpResponse getTheResponse() {
22+
ClientHttpResponse getTheResponse() {
2723
return theResponse;
2824
}
2925

30-
protected String getBody() {
26+
String getBody() {
3127
return body;
3228
}
3329
}

spring-cucumber/src/test/java/com/baeldung/SpringIntegrationTest.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.baeldung;
22

3-
import java.io.IOException;
4-
import java.util.HashMap;
5-
import java.util.Map;
6-
73
import org.springframework.beans.factory.annotation.Autowired;
84
import org.springframework.boot.test.IntegrationTest;
95
import org.springframework.boot.test.SpringApplicationContextLoader;
@@ -12,20 +8,23 @@
128
import org.springframework.test.context.ContextConfiguration;
139
import org.springframework.test.context.web.WebAppConfiguration;
1410
import org.springframework.web.client.ResponseErrorHandler;
15-
import org.springframework.web.client.ResponseExtractor;
1611
import org.springframework.web.client.RestTemplate;
1712

13+
import java.io.IOException;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
1817
//@RunWith(SpringJUnit4ClassRunner.class)
1918
@ContextConfiguration(classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class)
2019
@WebAppConfiguration
2120
@IntegrationTest
2221
public class SpringIntegrationTest {
23-
protected static ResponseResults latestResponse = null;
22+
static ResponseResults latestResponse = null;
2423

2524
@Autowired
2625
protected RestTemplate restTemplate;
2726

28-
protected void executeGet(String url) throws IOException {
27+
void executeGet(String url) throws IOException {
2928
final Map<String, String> headers = new HashMap<>();
3029
headers.put("Accept", "application/json");
3130
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
@@ -39,10 +38,9 @@ protected void executeGet(String url) throws IOException {
3938
return (new ResponseResults(response));
4039
}
4140
});
42-
4341
}
4442

45-
protected void executePost(String url) throws IOException {
43+
void executePost() throws IOException {
4644
final Map<String, String> headers = new HashMap<>();
4745
headers.put("Accept", "application/json");
4846
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
@@ -53,14 +51,14 @@ protected void executePost(String url) throws IOException {
5351
}
5452

5553
restTemplate.setErrorHandler(errorHandler);
56-
latestResponse = restTemplate.execute(url, HttpMethod.POST, requestCallback, response -> {
57-
if (errorHandler.hadError) {
58-
return (errorHandler.getResults());
59-
} else {
60-
return (new ResponseResults(response));
61-
}
62-
});
63-
54+
latestResponse = restTemplate
55+
.execute("http://localhost:8082/baeldung", HttpMethod.POST, requestCallback, response -> {
56+
if (errorHandler.hadError) {
57+
return (errorHandler.getResults());
58+
} else {
59+
return (new ResponseResults(response));
60+
}
61+
});
6462
}
6563

6664
private class ResponseResultErrorHandler implements ResponseErrorHandler {

spring-cucumber/src/test/java/com/baeldung/StepDefsIntegrationTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
55

6+
import cucumber.api.java.en.Given;
67
import org.springframework.http.HttpStatus;
78

89
import cucumber.api.java.en.And;
@@ -11,6 +12,16 @@
1112

1213
public class StepDefsIntegrationTest extends SpringIntegrationTest {
1314

15+
@When("^the client calls /baeldung$")
16+
public void the_client_issues_POST_hello() throws Throwable {
17+
executePost();
18+
}
19+
20+
@Given("^the client calls /hello$")
21+
public void the_client_issues_GET_hello() throws Throwable {
22+
executeGet("http://localhost:8082/hello");
23+
}
24+
1425
@When("^the client calls /version$")
1526
public void the_client_issues_GET_version() throws Throwable {
1627
executeGet("http://localhost:8082/version");

0 commit comments

Comments
 (0)