Skip to content

Commit

Permalink
Merge pull request #34 from christophd/issue/32/add-swagger-steps
Browse files Browse the repository at this point in the history
feat: #32 Add Swagger steps
  • Loading branch information
nicolaferraro authored Sep 17, 2019
2 parents 7bbc4e5 + c4a15e8 commit 01dc935
Show file tree
Hide file tree
Showing 19 changed files with 1,636 additions and 73 deletions.
7 changes: 7 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<kubernetes-client.version>4.3.0</kubernetes-client.version>
<postgresql.version>9.4.1212</postgresql.version>
<testcontainers.version>1.10.7</testcontainers.version>
<swagger.parser.version>1.0.34</swagger.parser.version>

<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
Expand Down Expand Up @@ -90,6 +91,11 @@
<artifactId>yaks-testing-http</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.yaks</groupId>
<artifactId>yaks-testing-swagger</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dev.yaks</groupId>
<artifactId>yaks-testing-standard</artifactId>
Expand Down Expand Up @@ -161,6 +167,7 @@
<module>yaks-testing-camel-k</module>
<module>yaks-testing-http</module>
<module>yaks-testing-jdbc</module>
<module>yaks-testing-swagger</module>
<module>yaks-testing-standard</module>
</modules>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.http.client.HttpClient;
import com.consol.citrus.http.message.HttpMessage;
import com.consol.citrus.variable.dictionary.DataDictionary;
import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
Expand All @@ -33,15 +34,15 @@
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.StringUtils;

/**
* @author Christoph Deppisch
*/
public class HttpClientSteps {
public class HttpClientSteps implements HttpSteps {

@CitrusResource
private TestRunner runner;
Expand All @@ -53,17 +54,21 @@ public class HttpClientSteps {

private String requestUrl;

private HttpMessage request;
private HttpMessage response;

private Map<String, String> requestHeaders = new HashMap<>();
private Map<String, String> responseHeaders = new HashMap<>();
private Map<String, String> requestParams = new HashMap<>();

private Map<String, String> bodyValidationExpressions = new HashMap<>();

private String requestMessageType;
private String responseMessageType;

private String requestBody;
private String responseBody;

private DataDictionary outboundDictionary;
private DataDictionary inboundDictionary;

@Before
public void before(Scenario scenario) {
if (httpClient == null && citrus.getApplicationContext().getBeansOfType(HttpClient.class).size() == 1L) {
Expand All @@ -76,11 +81,14 @@ public void before(Scenario scenario) {

requestHeaders = new HashMap<>();
responseHeaders = new HashMap<>();
request = new HttpMessage();
response = new HttpMessage();
requestParams = new HashMap<>();
requestMessageType = Citrus.DEFAULT_MESSAGE_TYPE;
responseMessageType = Citrus.DEFAULT_MESSAGE_TYPE;
requestBody = null;
responseBody = null;
bodyValidationExpressions = new HashMap<>();
outboundDictionary = null;
inboundDictionary = null;
}

@Given("^http-client \"([^\"\\s]+)\"$")
Expand Down Expand Up @@ -108,6 +116,10 @@ public void setUrl(String url) {

@Then("^(?:expect|verify) HTTP response header ([^\\s]+)(?:=| is )\"(.+)\"$")
public void addResponseHeader(String name, String value) {
if (name.equals(HttpHeaders.CONTENT_TYPE)) {
responseMessageType = getMessageType(value);
}

responseHeaders.put(name, value);
}

Expand All @@ -119,9 +131,18 @@ public void addResponseHeaders(DataTable headers) {

@Given("^HTTP request header ([^\\s]+)(?:=| is )\"(.+)\"$")
public void addRequestHeader(String name, String value) {
if (name.equals(HttpHeaders.CONTENT_TYPE)) {
requestMessageType = getMessageType(value);
}

requestHeaders.put(name, value);
}

@Given("^HTTP request query parameter ([^\\s]+)(?:=| is )\"(.+)\"$")
public void addRequestQueryParam(String name, String value) {
requestParams.put(name, value);
}

@Given("^HTTP request headers$")
public void addRequestHeaders(DataTable headers) {
Map<String, String> headerPairs = headers.asMap(String.class, String.class);
Expand Down Expand Up @@ -176,41 +197,15 @@ public void sendClientRequestMultilineBody(String method) {

@When("^send (GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|TRACE) ([^\"\\s]+)$")
public void sendClientRequest(String method, String path) {
request.method(HttpMethod.valueOf(method));

if (StringUtils.hasText(path)) {
request.path(path);
request.contextPath(path);
}

if (StringUtils.hasText(requestBody)) {
request.setPayload(requestBody);
}

for (Map.Entry<String, String> headerEntry : requestHeaders.entrySet()) {
request.setHeader(headerEntry.getKey(), headerEntry.getValue());
}

sendClientRequest(request);

sendClientRequest(createRequest(requestBody, requestHeaders, requestParams, method, path));
requestBody = null;
requestHeaders.clear();
requestParams.clear();
}

@Then("^receive HTTP (\\d+)(?: [^\\s]+)?$")
public void receiveClientResponse(Integer status) {
response.status(HttpStatus.valueOf(status));

if (StringUtils.hasText(responseBody)) {
response.setPayload(responseBody);
}

for (Map.Entry<String, String> headerEntry : responseHeaders.entrySet()) {
response.setHeader(headerEntry.getKey(), headerEntry.getValue());
}

receiveClientResponse(response);

receiveClientResponse(createResponse(responseBody, responseHeaders, status));
responseBody = null;
responseHeaders.clear();
}
Expand Down Expand Up @@ -247,6 +242,12 @@ private void sendClientRequest(HttpMessage request) {
if (StringUtils.hasText(requestUrl)) {
requestBuilder.uri(requestUrl);
}

requestBuilder.messageType(requestMessageType);

if (outboundDictionary != null) {
requestBuilder.dictionary(outboundDictionary);
}
};

runner.http(action);
Expand All @@ -266,6 +267,12 @@ private void receiveClientResponse(HttpMessage response) {
responseBuilder.validate(headerEntry.getKey(), headerEntry.getValue());
}
bodyValidationExpressions.clear();

responseBuilder.messageType(responseMessageType);

if (inboundDictionary != null) {
responseBuilder.dictionary(inboundDictionary);
}
});
}

Expand Down Expand Up @@ -300,4 +307,22 @@ private org.apache.http.client.HttpClient sslClient() {
throw new CitrusRuntimeException("Failed to create http client for ssl connection", e);
}
}

/**
* Specifies the inboundDictionary.
*
* @param inboundDictionary
*/
public void setInboundDictionary(DataDictionary inboundDictionary) {
this.inboundDictionary = inboundDictionary;
}

/**
* Specifies the outboundDictionary.
*
* @param outboundDictionary
*/
public void setOutboundDictionary(DataDictionary outboundDictionary) {
this.outboundDictionary = outboundDictionary;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import io.cucumber.datatable.DataTable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;

/**
* @author Christoph Deppisch
*/
public class HttpServerSteps {
public class HttpServerSteps implements HttpSteps {

@CitrusResource
private TestRunner runner;
Expand All @@ -38,14 +37,15 @@ public class HttpServerSteps {

private HttpServer httpServer;

private HttpMessage request;
private HttpMessage response;

private Map<String, String> requestHeaders = new HashMap<>();
private Map<String, String> responseHeaders = new HashMap<>();
private Map<String, String> requestParams = new HashMap<>();

private Map<String, String> bodyValidationExpressions = new HashMap<>();

private String requestMessageType;
private String responseMessageType;

private String requestBody;
private String responseBody;

Expand All @@ -61,8 +61,9 @@ public void before(Scenario scenario) {

requestHeaders = new HashMap<>();
responseHeaders = new HashMap<>();
request = new HttpMessage();
response = new HttpMessage();
requestParams = new HashMap<>();
requestMessageType = Citrus.DEFAULT_MESSAGE_TYPE;
responseMessageType = Citrus.DEFAULT_MESSAGE_TYPE;
requestBody = null;
responseBody = null;
bodyValidationExpressions = new HashMap<>();
Expand All @@ -79,6 +80,10 @@ public void setServer(String id) {

@Then("^(?:expect|verify) HTTP request header: ([^\\s]+)(?:=| is )\"(.+)\"$")
public void addRequestHeader(String name, String value) {
if (name.equals(HttpHeaders.CONTENT_TYPE)) {
requestMessageType = getMessageType(value);
}

requestHeaders.put(name, value);
}

Expand All @@ -88,8 +93,17 @@ public void addRequestHeaders(DataTable headers) {
headerPairs.forEach(this::addRequestHeader);
}

@Given("^(?:expect|verify) HTTP request query parameter ([^\\s]+)(?:=| is )\"(.+)\"$")
public void addRequestQueryParam(String name, String value) {
requestParams.put(name, value);
}

@Given("^HTTP response header: ([^\\s]+)(?:=| is )\"(.+)\"$")
public void addResponseHeader(String name, String value) {
if (name.equals(HttpHeaders.CONTENT_TYPE)) {
responseMessageType = getMessageType(value);
}

responseHeaders.put(name, value);
}

Expand Down Expand Up @@ -147,41 +161,15 @@ public void receiveServerRequestMultilineBody(String method) {

@When("^receive (GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS|TRACE) ([^\"\\s]+)$")
public void receiveServerRequest(String method, String path) {
request.method(HttpMethod.valueOf(method));

if (StringUtils.hasText(path)) {
request.path(path);
request.contextPath(path);
}

if (StringUtils.hasText(requestBody)) {
request.setPayload(requestBody);
}

for (Map.Entry<String, String> headerEntry : requestHeaders.entrySet()) {
request.setHeader(headerEntry.getKey(), headerEntry.getValue());
}

receiveServerRequest(request);

receiveServerRequest(createRequest(requestBody, requestHeaders, requestParams, method, path));
requestBody = null;
requestHeaders.clear();
requestParams.clear();
}

@Then("^send HTTP (\\d+)(?: [^\\s]+)?$")
public void sendServerResponse(Integer status) {
response.status(HttpStatus.valueOf(status));

if (StringUtils.hasText(responseBody)) {
response.setPayload(responseBody);
}

for (Map.Entry<String, String> headerEntry : responseHeaders.entrySet()) {
response.setHeader(headerEntry.getKey(), headerEntry.getValue());
}

sendServerResponse(response);

sendServerResponse(createResponse(responseBody, responseHeaders, status));
responseBody = null;
responseHeaders.clear();
}
Expand Down Expand Up @@ -219,6 +207,8 @@ private void receiveServerRequest(HttpMessage request) {
requestBuilder.validate(headerEntry.getKey(), headerEntry.getValue());
}
bodyValidationExpressions.clear();

requestBuilder.messageType(requestMessageType);
};

runner.http(action);
Expand All @@ -231,6 +221,7 @@ private void receiveServerRequest(HttpMessage request) {
private void sendServerResponse(HttpMessage response) {
runner.http(action -> action.server(httpServer).send()
.response(response.getStatusCode())
.messageType(responseMessageType)
.message(response));
}

Expand Down
Loading

0 comments on commit 01dc935

Please sign in to comment.