Skip to content

Commit e83b4b8

Browse files
author
eugenp
committed
formatting work
1 parent 5244509 commit e83b4b8

9 files changed

+117
-128
lines changed

spring-cucumber/src/main/java/com/baeldung/BaeldungController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
@RestController
1010
public class BaeldungController {
11-
12-
@RequestMapping(method={RequestMethod.GET},value={"/hello"})
13-
public String sayHello(HttpServletResponse response){
11+
12+
@RequestMapping(method = { RequestMethod.GET }, value = { "/hello" })
13+
public String sayHello(HttpServletResponse response) {
1414
return "hello";
1515
}
16-
17-
@RequestMapping(method={RequestMethod.POST},value={"/baeldung"})
18-
public String sayHelloPost(HttpServletResponse response){
16+
17+
@RequestMapping(method = { RequestMethod.POST }, value = { "/baeldung" })
18+
public String sayHelloPost(HttpServletResponse response) {
1919
return "hello";
2020
}
2121

spring-cucumber/src/main/java/com/baeldung/SpringDemoApplication.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import org.springframework.boot.context.web.SpringBootServletInitializer;
77

88
@SpringBootApplication
9-
public class SpringDemoApplication extends SpringBootServletInitializer{
9+
public class SpringDemoApplication extends SpringBootServletInitializer {
1010

11-
public static void main(String[] args) {
12-
SpringApplication.run(SpringDemoApplication.class, args);
13-
}
14-
15-
@Override
16-
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
11+
public static void main(String[] args) {
12+
SpringApplication.run(SpringDemoApplication.class, args);
13+
}
14+
15+
@Override
16+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
1717
return application.sources(SpringDemoApplication.class);
1818
}
1919
}

spring-cucumber/src/main/java/com/baeldung/VersionController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
@RestController
88
public class VersionController {
9-
10-
@RequestMapping(method={RequestMethod.GET},value={"/version"})
11-
public String getVersion(){
12-
return "1.0";
13-
}
9+
10+
@RequestMapping(method = { RequestMethod.GET }, value = { "/version" })
11+
public String getVersion() {
12+
return "1.0";
13+
}
1414
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import cucumber.api.junit.Cucumber;
55
import org.junit.runner.RunWith;
66

7-
87
@RunWith(Cucumber.class)
98
@CucumberOptions(features = "src/test/resources")
10-
public class CucumberTest{
9+
public class CucumberTest {
1110
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,27 @@
77
import java.io.IOException;
88
import java.util.Map;
99

10-
11-
public class HeaderSettingRequestCallback implements RequestCallback{
12-
final Map<String,String> requestHeaders;
10+
public class HeaderSettingRequestCallback implements RequestCallback {
11+
final Map<String, String> requestHeaders;
1312

1413
private String body;
1514

16-
public HeaderSettingRequestCallback(final Map<String, String> headers){
15+
public HeaderSettingRequestCallback(final Map<String, String> headers) {
1716
this.requestHeaders = headers;
1817
}
1918

20-
public void setBody(final String postBody ){
19+
public void setBody(final String postBody) {
2120
this.body = postBody;
2221
}
2322

2423
@Override
25-
public void doWithRequest(ClientHttpRequest request) throws IOException{
24+
public void doWithRequest(ClientHttpRequest request) throws IOException {
2625
final HttpHeaders clientHeaders = request.getHeaders();
27-
for( final Map.Entry<String,String> entry : requestHeaders.entrySet() ){
28-
clientHeaders.add(entry.getKey(),entry.getValue());
26+
for (final Map.Entry<String, String> entry : requestHeaders.entrySet()) {
27+
clientHeaders.add(entry.getKey(), entry.getValue());
2928
}
30-
if( null != body ){
31-
request.getBody().write( body.getBytes() );
29+
if (null != body) {
30+
request.getBody().write(body.getBytes());
3231
}
3332
}
3433
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
import cucumber.api.java.en.Given;
44
import cucumber.api.java.en.When;
55

6-
7-
public class OtherDefs extends SpringIntegrationTest{
6+
public class OtherDefs extends SpringIntegrationTest {
87
@When("^the client calls /baeldung$")
9-
public void the_client_issues_POST_hello() throws Throwable{
8+
public void the_client_issues_POST_hello() throws Throwable {
109
executePost("http://localhost:8080/baeldung");
1110
}
12-
11+
1312
@Given("^the client calls /hello$")
14-
public void the_client_issues_GET_hello() throws Throwable{
13+
public void the_client_issues_GET_hello() throws Throwable {
1514
executeGet("http://localhost:8080/hello");
1615
}
1716
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,27 @@
77
import org.apache.commons.io.IOUtils;
88
import org.springframework.http.client.ClientHttpResponse;
99

10-
11-
public class ResponseResults{
10+
public class ResponseResults {
1211
private final ClientHttpResponse theResponse;
1312
private final String body;
1413

15-
protected ResponseResults(final ClientHttpResponse response) throws IOException{
14+
protected ResponseResults(final ClientHttpResponse response) throws IOException {
1615
this.theResponse = response;
1716
final InputStream bodyInputStream = response.getBody();
18-
if (null == bodyInputStream){
17+
if (null == bodyInputStream) {
1918
this.body = "{}";
20-
}else{
19+
} else {
2120
final StringWriter stringWriter = new StringWriter();
2221
IOUtils.copy(bodyInputStream, stringWriter);
2322
this.body = stringWriter.toString();
2423
}
2524
}
2625

27-
protected ClientHttpResponse getTheResponse(){
26+
protected ClientHttpResponse getTheResponse() {
2827
return theResponse;
2928
}
3029

31-
protected String getBody(){
30+
protected String getBody() {
3231
return body;
3332
}
3433
}

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

Lines changed: 71 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -16,87 +16,80 @@
1616
import org.springframework.web.client.ResponseExtractor;
1717
import org.springframework.web.client.RestTemplate;
1818

19-
2019
//@RunWith(SpringJUnit4ClassRunner.class)
2120
@ContextConfiguration(classes = SpringDemoApplication.class, loader = SpringApplicationContextLoader.class)
2221
@WebAppConfiguration
2322
@IntegrationTest
2423
public class SpringIntegrationTest {
25-
protected static ResponseResults latestResponse = null;
26-
27-
protected RestTemplate restTemplate = null;
28-
29-
protected void executeGet(String url) throws IOException{
30-
final Map<String,String> headers = new HashMap<>();
31-
headers.put("Accept","application/json");
32-
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
33-
final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
34-
35-
if (restTemplate == null){
36-
restTemplate = new RestTemplate();
37-
}
38-
39-
restTemplate.setErrorHandler(errorHandler);
40-
latestResponse = restTemplate.execute(url,
41-
HttpMethod.GET,
42-
requestCallback,
43-
new ResponseExtractor<ResponseResults>(){
44-
@Override
45-
public ResponseResults extractData(ClientHttpResponse response) throws IOException {
46-
if (errorHandler.hadError){
47-
return (errorHandler.getResults());
48-
} else{
49-
return (new ResponseResults(response));
50-
}
51-
}
52-
});
53-
54-
}
55-
56-
protected void executePost(String url) throws IOException{
57-
final Map<String,String> headers = new HashMap<>();
58-
headers.put("Accept","application/json");
59-
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
60-
final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
61-
62-
if (restTemplate == null){
63-
restTemplate = new RestTemplate();
64-
}
65-
66-
restTemplate.setErrorHandler(errorHandler);
67-
latestResponse = restTemplate.execute(url,
68-
HttpMethod.POST,
69-
requestCallback,
70-
new ResponseExtractor<ResponseResults>(){
71-
@Override
72-
public ResponseResults extractData(ClientHttpResponse response) throws IOException {
73-
if (errorHandler.hadError){
74-
return (errorHandler.getResults());
75-
} else{
76-
return (new ResponseResults(response));
77-
}
78-
}
79-
});
80-
81-
}
82-
83-
private class ResponseResultErrorHandler implements ResponseErrorHandler{
84-
private ResponseResults results = null;
85-
private Boolean hadError = false;
86-
87-
private ResponseResults getResults(){
88-
return results;
89-
}
90-
91-
@Override
92-
public boolean hasError(ClientHttpResponse response) throws IOException{
93-
hadError = response.getRawStatusCode() >= 400;
94-
return hadError;
95-
}
96-
97-
@Override
98-
public void handleError(ClientHttpResponse response) throws IOException {
99-
results = new ResponseResults(response);
100-
}
101-
}
24+
protected static ResponseResults latestResponse = null;
25+
26+
protected RestTemplate restTemplate = null;
27+
28+
protected void executeGet(String url) throws IOException {
29+
final Map<String, String> headers = new HashMap<>();
30+
headers.put("Accept", "application/json");
31+
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
32+
final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
33+
34+
if (restTemplate == null) {
35+
restTemplate = new RestTemplate();
36+
}
37+
38+
restTemplate.setErrorHandler(errorHandler);
39+
latestResponse = restTemplate.execute(url, HttpMethod.GET, requestCallback, new ResponseExtractor<ResponseResults>() {
40+
@Override
41+
public ResponseResults extractData(ClientHttpResponse response) throws IOException {
42+
if (errorHandler.hadError) {
43+
return (errorHandler.getResults());
44+
} else {
45+
return (new ResponseResults(response));
46+
}
47+
}
48+
});
49+
50+
}
51+
52+
protected void executePost(String url) throws IOException {
53+
final Map<String, String> headers = new HashMap<>();
54+
headers.put("Accept", "application/json");
55+
final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback(headers);
56+
final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler();
57+
58+
if (restTemplate == null) {
59+
restTemplate = new RestTemplate();
60+
}
61+
62+
restTemplate.setErrorHandler(errorHandler);
63+
latestResponse = restTemplate.execute(url, HttpMethod.POST, requestCallback, new ResponseExtractor<ResponseResults>() {
64+
@Override
65+
public ResponseResults extractData(ClientHttpResponse response) throws IOException {
66+
if (errorHandler.hadError) {
67+
return (errorHandler.getResults());
68+
} else {
69+
return (new ResponseResults(response));
70+
}
71+
}
72+
});
73+
74+
}
75+
76+
private class ResponseResultErrorHandler implements ResponseErrorHandler {
77+
private ResponseResults results = null;
78+
private Boolean hadError = false;
79+
80+
private ResponseResults getResults() {
81+
return results;
82+
}
83+
84+
@Override
85+
public boolean hasError(ClientHttpResponse response) throws IOException {
86+
hadError = response.getRawStatusCode() >= 400;
87+
return hadError;
88+
}
89+
90+
@Override
91+
public void handleError(ClientHttpResponse response) throws IOException {
92+
results = new ResponseResults(response);
93+
}
94+
}
10295
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
import static org.hamcrest.MatcherAssert.assertThat;
99
import static org.hamcrest.Matchers.is;
1010

11-
public class StepDefs extends SpringIntegrationTest{
12-
13-
@When("^the client calls /version$")
14-
public void the_client_issues_GET_version() throws Throwable{
11+
public class StepDefs extends SpringIntegrationTest {
12+
13+
@When("^the client calls /version$")
14+
public void the_client_issues_GET_version() throws Throwable {
1515
executeGet("http://localhost:8080/version");
1616
}
1717

1818
@Then("^the client receives status code of (\\d+)$")
19-
public void the_client_receives_status_code_of(int statusCode) throws Throwable{
19+
public void the_client_receives_status_code_of(int statusCode) throws Throwable {
2020
final HttpStatus currentStatusCode = latestResponse.getTheResponse().getStatusCode();
21-
assertThat("status code is incorrect : "+ latestResponse.getBody(), currentStatusCode.value(), is(statusCode) );
21+
assertThat("status code is incorrect : " + latestResponse.getBody(), currentStatusCode.value(), is(statusCode));
2222
}
2323

2424
@And("^the client receives server version (.+)$")
25-
public void the_client_receives_server_version_body(String version) throws Throwable{
26-
assertThat(latestResponse.getBody(), is(version)) ;
25+
public void the_client_receives_server_version_body(String version) throws Throwable {
26+
assertThat(latestResponse.getBody(), is(version));
2727
}
2828
}

0 commit comments

Comments
 (0)