Skip to content

Unify Cukes step styling #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions cukes-core/src/main/java/lv/ctco/cukes/core/api/CoreSteps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package lv.ctco.cukes.core.api;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import cucumber.api.java.en.And;
import lv.ctco.cukes.core.CukesDocs;
import lv.ctco.cukes.core.facade.RandomGeneratorFacade;
import lv.ctco.cukes.core.facade.VariableFacade;
import lv.ctco.cukes.core.internal.context.InflateContext;
import org.apache.commons.lang3.RandomStringUtils;

import java.text.SimpleDateFormat;

@Singleton
@InflateContext
public class CoreSteps {

@Inject
private VariableFacade variableFacade;
@Inject
private RandomGeneratorFacade randomGeneratorFacade;

@And("^variable \"([^\"]+)\" is \"([^\"]+)\"$")
@CukesDocs("Assigns value to the variable")
public void createVariableSetToValue(String varName, String value) {
this.variableFacade.setVariable(varName, value);
}

@And("^variable \"([^\"]+)\" is random UUID$")
@CukesDocs("Generates random UUID and assigns it to a variable")
public void createVariableSetToRandomUUID(String varName) {
this.variableFacade.setUUIDToVariable(varName);
}

@And("^variable \"(\\S+)\" is random password by matching pattern \"([Aa0]+)\"$")
@CukesDocs("Generates random password by given pattern. Pattern may contain symbils a,A,0. " +
"So A is replaced with random capital letter, a - with random letter and 0 - with random number")
public void createVariableSetToRandomPasswordByPattern(String variableName, String pattern) {
this.variableFacade.setVariable(variableName, this.randomGeneratorFacade.byPattern(pattern));
}

@And("^variable \"(\\S+)\" is random password with length (\\d+)$")
@CukesDocs("Generates random password with given length")
public void createVariableSetToRandomPasswordByLength(String variableName, int length) {
this.variableFacade.setVariable(variableName, this.randomGeneratorFacade.withLength(length));
}

@And("^variable \"([^\"]+)\" is current timestamp$")
@CukesDocs("Assigns current timestamp to a variable")
public void createVariableSetToCurrentTimestamp(String varName) {
this.variableFacade.setCurrentTimestampToVariable(varName);
}

@And("^variable \"([^\"]+)\" is current time in format \"([^\"]+)\"$")
@CukesDocs("Assigns current timestamp in a defined format to a variable")
public void createVariableSetToTodayDate(String varName, String format) {
String value = new SimpleDateFormat(format).format(System.currentTimeMillis());
this.variableFacade.setVariable(varName, value);
}

@And("^variable \"([^\"]+)\" is \"([^\"]*)\" with (\\d+) random characters?$")
@CukesDocs("Generates random character line of required length and adds it to a variable value")
public void createVariableSetToValueWithRandomCharsPostfix(String varName, String value, int length) {
value += this.randomGeneratorFacade.withLength(length);
this.variableFacade.setVariable(varName, value);
}

@And("^variable \"([^\"]+)\" is \"([^\"]*)\" with (\\d+) chars? long random string$")
@CukesDocs("Generates random alphabetic character line of required length and adds it to a variable value")
public void createVariableSetToValueWithRandomStringPostfix(String varName, String value, int length) {
value += RandomStringUtils.randomAlphabetic(length);
this.variableFacade.setVariable(varName, value);
}

@And("^variable \"([^\"]+)\" is \"([^\"]*)\" with (\\d+) digits? long random number$")
@CukesDocs("Generates random digit line of required length and adds it to a variable value")
public void createVariableSetToValueWithRandomNumberPostfix(String varName, String value, int length) {
value += RandomStringUtils.randomNumeric(length);
this.variableFacade.setVariable(varName, value);
}

@And("^variable \"([^\"]+)\" is \"([^\"]*)\" set to lower case$")
@CukesDocs("Assigns value set to lower case to the variable")
public void createVariableSetToLowerCase(String varName, String value) {
this.variableFacade.setVariable(varName, value.toLowerCase());
}

@And("^variable \"([^\"]+)\" is \"([^\"]*)\" set to upper case$")
@CukesDocs("Assigns value set to upper case to the variable")
public void createVariableSetToUpperCase(String varName, String value) {
this.variableFacade.setVariable(varName, value.toUpperCase());
}
}
7 changes: 7 additions & 0 deletions cukes-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
<artifactId>awaitility</artifactId>
<version>1.7.0</version>
</dependency>

<!-- Soft Assertions -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public Response doRequest(RequestSpecification when, String url) {
case HEAD: return when.head(url);
case PATCH: return when.patch(url);
}
throw new CukesRuntimeException("This Http Method is nos supported");
throw new CukesRuntimeException("This Http Method is not supported");
}
}
216 changes: 216 additions & 0 deletions cukes-http/src/main/java/lv/ctco/cukes/http/api/HttpSteps.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package lv.ctco.cukes.http.api;

import com.google.inject.Inject;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Then;
import groovy.lang.Singleton;
import groovy.util.logging.Slf4j;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import lv.ctco.cukes.core.CukesDocs;
import lv.ctco.cukes.core.internal.context.ContextInflater;
import lv.ctco.cukes.core.internal.context.GlobalWorldFacade;
import lv.ctco.cukes.core.internal.context.InflateContext;
import lv.ctco.cukes.http.facade.HttpAssertionFacade;
import lv.ctco.cukes.http.facade.HttpRequestFacade;
import lv.ctco.cukes.http.facade.HttpResponseFacade;
import lv.ctco.cukes.http.json.JsonParser;
import org.assertj.core.api.SoftAssertions;
import org.junit.Assert;

import java.util.List;
import java.util.Map;

@Singleton
@Slf4j
@InflateContext
public class HttpSteps {

@Inject
private HttpRequestFacade httpRequestFacade;
@Inject
private HttpResponseFacade httpResponseFacade;
@Inject
private HttpAssertionFacade httpAssertionFacade;
@Inject
private JsonParser jsonParser;
@Inject
private ContextInflater inflater;
@Inject
private GlobalWorldFacade world;


@And("^HTTP response (header|property) \"([^\"]+)\" value is saved to variable \"([^\"]+)\"$")
@CukesDocs("Save response property or header value to variable")
public void saveHttpResponseHeaderToAVariable(String headerOrProperty, String attributeName, String varName) {
if (headerOrProperty.equals("header")) {
httpAssertionFacade.varAssignedFromHeader(varName, attributeName);
} else {
httpAssertionFacade.varAssignedFromProperty(varName, attributeName);
}
}

@And("^variable \"([^\"]+)\" is HTTP response (header|property) \"([^\"]+)\" value$")
@CukesDocs("Save response property or header value to variable")
public void setVariableToHttpResponseHeaderValue(String varName, String headerOrProperty, String attributeName) {
if (headerOrProperty.equals("header")) {
httpAssertionFacade.varAssignedFromHeader(varName, attributeName);
} else {
httpAssertionFacade.varAssignedFromProperty(varName, attributeName);
}
}

@And("^HTTP content type is \"([^\"]+)\"$")
@CukesDocs("Setup content type for http request")
public void setContentType(String contentType) {
httpRequestFacade.contentType(contentType);
}

@And("^HTTP content type is (ANY|TEXT|JSON|XML|HTML|URLENC|BINARY)$")
@CukesDocs("Select content type for http request")
public void setContentTypeJson(ContentType contentType) {
httpRequestFacade.contentType(contentType.toString());
}

@And("^HTTP \"(GET|POST|PUT|DELETE|OPTIONS|HEAD|PATCH)\" request is sent to \"(.+)\"$")
@CukesDocs("Send http request")
public void performHttpRequest(String httpMethod, String url) throws Throwable {
httpResponseFacade.setResponsePrefix("");
httpResponseFacade.doRequest(httpMethod, url);
}

//ASSERT
@Then("^HTTP response status code should( not|) be (\\d+)$")
@CukesDocs("Check http response status code")
public void checkHttpRsponseStatusCode(String condition, int expectedStatusCode) {
int actualStatusCode = httpResponseFacade.response().statusCode();
boolean shouldBeEqual = condition.isEmpty();
boolean isEqual = actualStatusCode == expectedStatusCode;
Assert.assertTrue(
"Status code should" + condition + " be " + expectedStatusCode + " but was " + actualStatusCode +
"\nAnd response body is:\n" + httpResponseFacade.response().body().prettyPrint(),
isEqual == shouldBeEqual);
}

@Then("^HTTP response property \"([^\"]+)\" value should( not|) be equal to \"([^\"]*)\"$")
@CukesDocs("Check http response property")
public void checkHttpResponsePropertyEquals(String path, String condition, String value) {
boolean shouldBeEqual = condition.isEmpty();
if (shouldBeEqual) {
httpAssertionFacade.bodyContainsPathWithValue(path, value);
} else {
httpAssertionFacade.bodyContainsPathWithOtherValue(path, value);
}
}

@Then("^HTTP response header \"([^\"]+)\" value should( not|) be equal to \"([^\"]+)\"$")
@CukesDocs("Check http response header")
public void checkHttpResponseHeaderEquals(String headerName, String condition, String value) {
boolean shouldBeEqual = condition.isEmpty();
if (shouldBeEqual) {
httpAssertionFacade.headerContains(headerName, value);
} else {
httpAssertionFacade.headerDoesNotContain(headerName, value);
}
}

@Then("^HTTP response should contain properties from json:$")
@CukesDocs("Check http response contains properties from certain json")
public void checkHttpResponseContainsProperty(String json) {
// httpAssertionFacade.bodyContainsPropertiesFromJson(json);
Response response = httpResponseFacade.response();
SoftAssertions softly = new SoftAssertions();
Map<String, String> stringStringMap = jsonParser.parsePathToValueMap(json);
for (Map.Entry<String, String> entry : stringStringMap.entrySet()) {
String path = entry.getKey();
String expectedValue = entry.getValue();
Object actualValue = response.body().path(path);
softly
.assertThat(actualValue)
.as("Value of property by path '%s'", path)
.isEqualTo(expectedValue);
}
if(!softly.wasSuccess()) {
softly.fail("Response was: \n" + response.body().prettyPrint());
}
softly.assertAll();
}

@Then("^HTTP response properties should match:$")
@CukesDocs("Check http response contains properties from table")
public void checkHttpResponsePropertiesMatch(List<Map<String, String>> properties) {
Response response = httpResponseFacade.response();
SoftAssertions softly = new SoftAssertions();

String path, expectedValue;
for (Map<String, String> propertyMap : properties) {
path = propertyMap.get("path");
expectedValue = inflater.inflate(propertyMap.get("value"));
Object actualValue = response.body().path(path);
softly
.assertThat(actualValue)
.as("Value of property by path '%s'", path)
.isEqualTo(expectedValue);
}
if(!softly.wasSuccess()) {
softly.fail("Response was: \n" + response.body().prettyPrint());
}
softly.assertAll();
}

@Then("^HTTP response property \"([^\"]+)\" array size should( not|) be (\\d+)$")
@CukesDocs("Check http response property size")
public void checkHttpResponsePropertySize(String path, String condition, Integer size) {
boolean shouldBeEqual = condition.isEmpty();
if(shouldBeEqual) {
httpAssertionFacade.bodyContainsArrayWithSize(path, size.toString());
} else {
httpAssertionFacade.bodyContainsArrayWithSize(path, "<>"+size);
}
}

@Then("^HTTP response property \"([^\"]+)\" array size should be (>=|>|<|<=) than (\\d+)$")
@CukesDocs("Check http response property size is different than certain number")
public void checkHttpResponsePropertySizeIsOtherThan(String path, String operator, Integer size) {
httpAssertionFacade.bodyContainsArrayWithSize(path, operator + size);
}

@Then("^HTTP response property \"([^\"]+)\" length should( not|) be (\\d+)$")
@CukesDocs("Check http response property length")
public void checkHttpResponsePropertySize(String path, String condition, int expectedLength) {
Response response = httpResponseFacade.response();
String property = response.body().path(path).toString();
int actualLength = property.length();
boolean shouldBeEqual = condition.isEmpty();
boolean isEqual = actualLength == expectedLength;
Assert.assertTrue(
"HTTP response property '" + path + "' should" + condition + " be " + expectedLength + " but was " + actualLength +
"\nAnd property value is " + property +
"\nAnd response body is:\n" + httpResponseFacade.response().body().prettyPrint() +
"\nAnd request body is:\n" + world.get("requestBody").orNull(),
isEqual == shouldBeEqual);
}

@Then("^HTTP response property \"([^\"]+)\" length should be (>=|>|<|<=) than (\\d+)$")
@CukesDocs("Check http response property length is different than certain number")
public void checkHttpResponsePropertySizeIsOtherThan(String path, String operation, int expectedLength) {
Response response = httpResponseFacade.response();
String property = response.body().path(path).toString();
int actualLength = property.length();
boolean result = false;
switch (operation) {
// @formatter:off
case ">=": result = actualLength >= expectedLength; break;
case ">": result = actualLength > expectedLength; break;
case "<=": result = actualLength <= expectedLength; break;
case "<": result = actualLength < expectedLength; break;
// @formatter:on
}
Assert.assertTrue(
"HTTP response property '" + path + "' should be " + operation + " than " + expectedLength + " but was " + actualLength +
"\nAnd property value is " + property +
"\nAnd response body is:\n" + httpResponseFacade.response().body().prettyPrint() +
"\nAnd request body is:\n" + world.get("requestBody").orNull(),
result);
}
}
Loading