BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
## Spring Cucumber
This module contains articles about Spring testing with Cucumber
### Relevant Articles:
- [Cucumber Spring Integration](https://www.baeldung.com/cucumber-spring-integration)
+61
View File
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-cucumber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cucumber</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>${cucumber.java.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.java.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.java.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.java.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
</dependencies>
<properties>
<cucumber.java.version>1.2.5</cucumber.java.version>
<commons-io.version>1.3.2</commons-io.version>
</properties>
</project>
@@ -0,0 +1,21 @@
package com.baeldung;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
@RestController
public class BaeldungController {
@GetMapping("/hello")
public String sayHello(HttpServletResponse response) {
return "hello";
}
@PostMapping("/baeldung")
public String sayHelloPost(HttpServletResponse response) {
return "hello";
}
}
@@ -0,0 +1,19 @@
package com.baeldung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class SpringDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
@@ -0,0 +1,14 @@
package com.baeldung;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class VersionController {
@RequestMapping(method = { RequestMethod.GET }, value = { "/version" })
public String getVersion() {
return "1.0";
}
}
@@ -0,0 +1 @@
server.port=8082
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -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));
}
}
@@ -0,0 +1,17 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.SpringDemoApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringDemoApplication.class)
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,17 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.SpringDemoApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringDemoApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,9 @@
Feature: the message can be retrieved
Scenario: client makes call to POST /baeldung
When the client calls /baeldung
Then the client receives status code of 200
And the client receives server version hello
Scenario: client makes call to GET /hello
Given the client calls /hello
When the client receives status code of 200
Then the client receives server version hello
@@ -0,0 +1,6 @@
Feature: the version can be retrieved
Scenario: client makes call to GET /version
When the client calls /version
Then the client receives status code of 200
And the client receives server version 1.0