diff --git a/cukes-core/src/main/java/lv/ctco/cukes/core/api/CoreSteps.java b/cukes-core/src/main/java/lv/ctco/cukes/core/api/CoreSteps.java new file mode 100644 index 00000000..b365c2a3 --- /dev/null +++ b/cukes-core/src/main/java/lv/ctco/cukes/core/api/CoreSteps.java @@ -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()); + } +} diff --git a/cukes-http/pom.xml b/cukes-http/pom.xml index 33b4a918..e89a2a03 100644 --- a/cukes-http/pom.xml +++ b/cukes-http/pom.xml @@ -37,6 +37,13 @@ awaitility 1.7.0 + + + + org.assertj + assertj-core + 3.8.0 + diff --git a/cukes-http/src/main/java/lv/ctco/cukes/http/HttpMethod.java b/cukes-http/src/main/java/lv/ctco/cukes/http/HttpMethod.java index 97a8238a..74d38b61 100644 --- a/cukes-http/src/main/java/lv/ctco/cukes/http/HttpMethod.java +++ b/cukes-http/src/main/java/lv/ctco/cukes/http/HttpMethod.java @@ -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"); } } diff --git a/cukes-http/src/main/java/lv/ctco/cukes/http/api/HttpSteps.java b/cukes-http/src/main/java/lv/ctco/cukes/http/api/HttpSteps.java new file mode 100644 index 00000000..0de101fb --- /dev/null +++ b/cukes-http/src/main/java/lv/ctco/cukes/http/api/HttpSteps.java @@ -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 stringStringMap = jsonParser.parsePathToValueMap(json); + for (Map.Entry 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> properties) { + Response response = httpResponseFacade.response(); + SoftAssertions softly = new SoftAssertions(); + + String path, expectedValue; + for (Map 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); + } +} diff --git a/cukes-ldap/src/main/java/lv/ctco/cukes/ldap/api/LdapSteps.java b/cukes-ldap/src/main/java/lv/ctco/cukes/ldap/api/LdapSteps.java new file mode 100644 index 00000000..cd20a45d --- /dev/null +++ b/cukes-ldap/src/main/java/lv/ctco/cukes/ldap/api/LdapSteps.java @@ -0,0 +1,273 @@ +package lv.ctco.cukes.ldap.api; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import cucumber.api.java.en.And; +import cucumber.api.java.en.Then; +import lv.ctco.cukes.core.CukesDocs; +import lv.ctco.cukes.core.facade.VariableFacade; +import lv.ctco.cukes.core.internal.context.InflateContext; +import lv.ctco.cukes.ldap.facade.EntityFacade; +import lv.ctco.cukes.ldap.facade.ModificationFacade; + +import java.util.List; +import java.util.Map; + +@Singleton +@InflateContext +public class LdapSteps { + @Inject + private EntityFacade entityFacade; + @Inject + private ModificationFacade modificationFacade; + @Inject + private VariableFacade variableFacade; + + //CREATE + @And("^LDAP entity is created using LDIF:$") + @CukesDocs("Creates LDAP entity from LDIF body") + public void importLdif(String ldif) { + entityFacade.importLdif(ldif); + } + + @And("^LDAP entity is created using LDIF from file \"([^\"]+)\"$") + @CukesDocs("Creates LDAP entity from separate LDIF file") + public void createEntityFromLdifFile(String ldifFile) { + entityFacade.importLdifFromFile(ldifFile); + } + + //READ + @And("^LDAP entity by DN \"([^\"]+)\" is retrieved$") + @CukesDocs("Gets LDAP entity for further processing") + public void readEntityByDn(String dn) { + entityFacade.readEntityByDn(dn); + } + + @And("^LDAP entities are searched within DN \"([^\"]+)\" by filter \"([^\"]+)\"$") + @CukesDocs("Gets LDAP entities search results for further processing") + public void searchEntitiesByFilter(String dn, String filter) { + entityFacade.searchByFilter(dn, filter); + } + + @And("^LDAP entity with index (\\d+) from search results is retrieved$") + @CukesDocs("Gets LDAP entities search results for further processing") + public void readEntityFromSearchResults(int index) { + entityFacade.takeEntityFromSearchResults(index - 1); + } + + @And("^LDAP entity by DN \"([^\"]+)\" attribute \"([^\"]+)\" value is saved to variable \"([^\"]+)\"$") + @CukesDocs("Creates variables with LDAP entity attribute value") + public void createVariableSetToLdapEntityAttributeValue(String dn, String attribute, String varName) throws Throwable { + entityFacade.readEntityByDn(dn); + String value = entityFacade.getNotNullAttribute(attribute).get().toString(); + this.variableFacade.setVariable(varName, value); + } + + //UPDATE + @And("^LDAP entity new modifications are prepared$") + @CukesDocs("Clears previous ldap entity modifications. " + + "First, you need to retrieve entity itself before this step.") + public void prepareNewModification() { + modificationFacade.reset(); + } + + @And("^LDAP entity attribute \"([^\"]+)\" value \"([^\"]+)\" is modified with operation \"(add|remove|replace)\"$") + @CukesDocs("Modifies LDAP entity attribute by adding value, replacing value or removing it. " + + "First, you need to retrieve entity itself before this step. " + + "Second, you need to update entity after this step.") + public void addModification(String attribute, String operation, String value) { + modificationFacade.add(attribute, operation, value); + } + + @And("^LDAP entity by DN \"([^\"]+)\" is updated using prepared modifications$") + @CukesDocs("Updates LDAP entity with prepared modifications. " + + "You need to retrieve and make entity modification before this step") + public void modifyEntityWithDn(String dn) { + modificationFacade.execute(dn); + } + + @And("^LDAP entity by DN \"([^\"]+)\" attribute \"([^\"]+)\" value \"([^\"]*)\" is modified with operation \"(add|remove|replace)\"$") + @CukesDocs("Updates LDAP entity attribute value with selected modication: add, remove or replace.") + public void updateEntityInLDAP(String dn, String attributeName, String attributeValue, String operation) { + modificationFacade.reset(); + modificationFacade.add(attributeName, operation, attributeValue); + modificationFacade.execute(dn); + } + + @And("^LDAP entity by DN \"([^\"]+)\" attributes are updated:$") + @CukesDocs("Updates LDAP entity with set of modications. Allowed operations: add, remove, replace.") + public void updateEntityInLDAP(String dn, List> attributeList) { + modificationFacade.reset(); + attributeList.forEach(attributeMap -> { + String attributeName = attributeMap.get("attribute"); + String attributeValue = attributeMap.get("value"); + String operation = attributeMap.get("operation") != null ? attributeMap.get("operation") : "replace"; + modificationFacade.add(attributeName, operation, attributeValue); + }); + modificationFacade.execute(dn); + } + + //DELETE + @And("^LDAP entity by DN \"([^\"]+)\" is deleted$") + @CukesDocs("Removes LDAP entity") + public void deleteEntityWithDn(String dn) { + entityFacade.deleteEntityByDn(dn); + } + + //ASSERT + @Then("^LDAP entity should( not|) exist$") + @CukesDocs("Asserts that preset LDAP entity exists or not. " + + "First, you need to retrieve entity itself before this step.") + public void checkEntityExists(String condition) { + boolean shouldExist = condition.isEmpty(); + if (shouldExist) { + entityFacade.entityExists(); + } else { + entityFacade.entityDoesNotExist(); + } + } + + @Then("^LDAP entity by DN \"([^\"]+)\" should( not|) exist$") + @CukesDocs("Asserts that LDAP entity exists or not.") + public void checkEntityByDnExists(String dn, String condition) { + entityFacade.readEntityByDn(dn); + checkEntityExists(condition); + } + + @Then("^LDAP entity should( not|) contain attribute \"([^\"]+)\"$") + @CukesDocs("Asserts that preset LDAP entity contains or not contains attribute. " + + "First, you need to retrieve entity itself before this step.") + public void checkEntityContainsAttribute(String condition, String attribute) { + boolean shouldContain = condition.isEmpty(); + if (shouldContain) { + entityFacade.entityContainsAttribute(attribute); + } else { + entityFacade.entityDoesNotContainAttribute(attribute); + } + } + + @Then("^LDAP entity by DN \"([^\"]+)\" should( not|) contain attribute \"([^\"]+)\"$") + @CukesDocs("Asserts that LDAP entity contains or not contains attribute.") + public void checkEntityByDnContainsAttribute(String dn, String condition, String attribute) { + entityFacade.readEntityByDn(dn); + checkEntityContainsAttribute(condition, attribute); + } + + @Then("^LDAP entity attribute \"([^\"]+)\" value should( not|) be equal to \"([^\"]*)\"$") + @CukesDocs("Asserts that preset LDAP entity attribute value is equal or not equal to certain value. " + + "First, you need to retrieve entity itself before this step.") + public void checkEntityHasAttributeWithValue(String attribute, String condition, String value) { + boolean shouldBeEqual = condition.isEmpty(); + if (shouldBeEqual) { + entityFacade.entityHasAttributeWithValue(attribute, value); + } else { + entityFacade.entityHasAttributeWithValueOtherThat(attribute, value); + } + } + + @Then("^LDAP entity by DN \"([^\"]+)\" attribute \"([^\"]+)\" value should( not|) be equal to \"([^\"]*)\"$") + @CukesDocs("Asserts that LDAP entity attribute value is equal or not equal to certain value.") + public void checkEntityByDnHasAttributeWithValue(String dn, String attribute, String condition, String value) { + entityFacade.readEntityByDn(dn); + checkEntityHasAttributeWithValue(attribute, condition, value); + } + + @Then("^LDAP entity attribute \"([^\"]+)\" should( not|) contain (\\d+) values?$") + @CukesDocs("Asserts that preset LDAP entity attribute should contain or not contain certain number of values. " + + "First, you need to retrieve entity itself before this step.") + public void checkEntityHasAttributeWithValueArray(String attribute, String condition, int size) { + boolean shouldContain = condition.isEmpty(); + if (shouldContain) { + entityFacade.entityHasAttributeAsArrayOfSize(attribute, "=", size); + } else { + entityFacade.entityHasAttributeAsArrayOfSize(attribute, "<>", size); + } + } + + @Then("^LDAP entity by DN \"([^\"]+)\" attribute \"([^\"]+)\" should( not|) contain (\\d+) values?$") + @CukesDocs("Asserts that LDAP entity attribute should contain or not contain certain number of values.") + public void checkEntityByDnHasAttributeWithValueArray(String dn, String attribute, String condition, int size) { + entityFacade.readEntityByDn(dn); + checkEntityHasAttributeWithValueArray(attribute, condition, size); + } + + @Then("^LDAP entity attribute \"([^\"]+)\" should contain (>=|>|<=|<) than (\\d+) values?$") + @CukesDocs("Asserts that preset LDAP entity attribute should contain more or equal, more, less or equal, less than certain number of values. " + + "First, you need to retrieve entity itself before this step.") + public void checkEntityHasAttributeAsArrayOfSize(String attribute, String operator, int size) { + entityFacade.entityHasAttributeAsArrayOfSize(attribute, operator, size); + } + + @Then("^LDAP entity by DN \"([^\"]+)\" attribute \"([^\"]+)\" should contain (>=|>|<=|<) than (\\d+) values?$") + @CukesDocs("Asserts that LDAP entity attribute should contain more or equal, more, less or equal, less than certain number of values.") + public void checkEntityByDnHasAttributeAsArrayOfSize(String dn, String attribute, String operator, int size) { + entityFacade.readEntityByDn(dn); + entityFacade.entityHasAttributeAsArrayOfSize(attribute, operator, size); + } + + @Then("^LDAP entity attribute \"([^\"]+)\" should( not|) match pattern \"([^\"]+)\"$") + @CukesDocs("Asserts that preset LDAP entity attribute should match or not match certain pattern. " + + "First, you need to retrieve entity itself before this step.") + public void checkEntityHasAttributeWithValueMatchingPattern(String attribute, String condition, String pattern) { + boolean shouldMatch = condition.isEmpty(); + if (shouldMatch) { + entityFacade.entityHasAttributeWithValueMatchingPattern(attribute, pattern); + } else { + entityFacade.entityHasAttributeWithValueNotMatchingPattern(attribute, pattern); + } + } + + @Then("^LDAP entity by DN \"([^\"]+)\" attribute \"([^\"]+)\" should( not|) match pattern \"([^\"]+)\"$") + @CukesDocs("Asserts that LDAP entity attribute should match or not match certain pattern.") + public void checkEntityBeDnHasAttributeWithValueMatchingPattern(String dn, String attribute, String condition, String pattern) { + entityFacade.readEntityByDn(dn); + checkEntityHasAttributeWithValueMatchingPattern(attribute, condition, pattern); + } + + @Then("^LDAP entity should match LDIF:$") + @CukesDocs("Asserts that preset LDAP entity attribute should match certain LDIF. " + + "First, you need to retrieve entity itself before this step.") + public void checkEntityMatchesLDIF(String ldif) { + entityFacade.entityMatchesLDIF(ldif); + } + + @Then("^LDAP entity by DN \"([^\"]+)\" should match LDIF:$") + @CukesDocs("Asserts that LDAP entity attribute should match certain LDIF.") + public void checkEntityByDnMatchesLDIF(String dn, String ldif) { + entityFacade.readEntityByDn(dn); + entityFacade.entityMatchesLDIF(ldif); + } + + @Then("^LDAP entities search result size should( not|) be equal to (\\d+)$") + @CukesDocs("Asserts that prefound LDAP entity search result size should be equal or not be equal to certain number. " + + "First, you need to search for LDAP entities before this step.") + public void checkSearchResultSizeEquals(String condition, int size) { + boolean shouldBeEqual = condition.isEmpty(); + if (shouldBeEqual) { + entityFacade.searchResultHasSize("=", size); + } else { + entityFacade.searchResultHasSize("<>", size); + } + } + + @Then("^LDAP entities search within DN \"([^\"]+)\" by filter \"([^\"]+)\" result size should( not|) be equal to (\\d+)$") + @CukesDocs("Asserts that LDAP entity search result size should be equal or not be equal to certain number.") + public void checkEntitiesByDnSearchResultSizeEquals(String dn, String filter, String condition, int size) { + entityFacade.searchByFilter(dn, filter); + checkSearchResultSizeEquals(condition, size); + } + + @Then("^LDAP entities search result size should be (>=|>|<=|<) than (\\d+)$") + @CukesDocs("Asserts that prefound LDAP entity search result size should be more or equal, more, less or equal, less than certain number. " + + "First, you need to search for LDAP entities before this step.") + public void checkEntitiesSearchResultSizeDiffers(String operator, int size) { + entityFacade.searchResultHasSize(operator, size); + } + + @Then("^LDAP entities search within DN \"([^\"]+)\" by filter \"([^\"]+)\" result size should be (>=|>|<=|<) than (\\d+)$") + @CukesDocs("Asserts that LDAP entity search result size should be more or equal, more, less or equal, less than certain number.") + public void checkEntitiesByDnSearchResultSizeDiffers(String dn, String filter, String operator, int size) { + entityFacade.searchByFilter(dn, filter); + checkEntitiesSearchResultSizeDiffers(operator, size); + } +} diff --git a/cukes-ldap/src/main/java/lv/ctco/cukes/ldap/facade/EntityFacade.java b/cukes-ldap/src/main/java/lv/ctco/cukes/ldap/facade/EntityFacade.java index 5972a4c7..f332a997 100644 --- a/cukes-ldap/src/main/java/lv/ctco/cukes/ldap/facade/EntityFacade.java +++ b/cukes-ldap/src/main/java/lv/ctco/cukes/ldap/facade/EntityFacade.java @@ -57,6 +57,14 @@ public void readEntityByDn(String dn) { entity = entityService.getEntityByDn(dn); } + public Attributes getEntityByDn(String dn) { + return entityService.getEntityByDn(dn); + } + + public Attributes getEntity() { + return entity; + } + public void entityExists() { assertThat(entity, notNullValue()); } diff --git a/cukes-rest/src/main/java/lv/ctco/cukes/rest/api/RestSteps.java b/cukes-rest/src/main/java/lv/ctco/cukes/rest/api/RestSteps.java new file mode 100644 index 00000000..f69b3c43 --- /dev/null +++ b/cukes-rest/src/main/java/lv/ctco/cukes/rest/api/RestSteps.java @@ -0,0 +1,65 @@ +package lv.ctco.cukes.rest.api; + +import com.google.inject.Inject; +import cucumber.api.java.en.And; +import groovy.lang.Singleton; +import groovy.util.logging.Slf4j; +import io.restassured.http.ContentType; +import lv.ctco.cukes.core.CukesDocs; +import lv.ctco.cukes.core.internal.context.GlobalWorldFacade; +import lv.ctco.cukes.core.internal.context.InflateContext; +import lv.ctco.cukes.http.facade.HttpRequestFacade; +import lv.ctco.cukes.http.facade.HttpResponseFacade; +import lv.ctco.cukes.rest.facade.RestRequestFacade; +import org.apache.commons.lang3.StringUtils; + +@Singleton +@Slf4j +@InflateContext +public class RestSteps { + + @Inject + private RestRequestFacade restRequestFacade; + @Inject + private HttpResponseFacade httpResponseFacade; + @Inject + private HttpRequestFacade httpRequestFacade; + @Inject + private GlobalWorldFacade world; + + + @And("^HTTP request body is set:$") + @CukesDocs("Setup request body for http request") + public void setHttpRequestBody(String body) { + restRequestFacade.setRequestBody(body); + } + + @And("^HTTP \"(POST|PUT|PATCH)\" request is sent to \"(.+)\":$") + @CukesDocs("Send http request with request body") + public void performHttpPostRequest(String httpMethod, String url, String body) throws Throwable { + restRequestFacade.setRequestBody(body); + world.put("requestBody", body); + httpResponseFacade.setResponsePrefix(StringUtils.EMPTY); + httpResponseFacade.doRequest(httpMethod, url); + } + + @And("^HTTP \"(POST|PUT|PATCH)\" request with (ANY|TEXT|JSON|XML|HTML|URLENC|BINARY) content type is sent to \"(.+)\":$") + @CukesDocs("Send http request with presetup content type and request body") + public void performHttpPostRequestWithJsonContentType(String httpMethod, ContentType contentType, String url, String body) throws Throwable { + httpRequestFacade.contentType(contentType.toString()); + restRequestFacade.setRequestBody(body); + world.put("requestBody", body); + httpResponseFacade.setResponsePrefix(StringUtils.EMPTY); + httpResponseFacade.doRequest(httpMethod, url); + } + + @And("^HTTP \"(POST|PUT|PATCH)\" request with content type \"([^\"]+)\" is sent to \"(.+)\":$") + @CukesDocs("Send http request with content type and request body") + public void performHttpPostRequestWithJsonContentType(String httpMethod, String contentType, String url, String body) throws Throwable { + httpRequestFacade.contentType(contentType); + restRequestFacade.setRequestBody(body); + world.put("requestBody", body); + httpResponseFacade.setResponsePrefix(StringUtils.EMPTY); + httpResponseFacade.doRequest(httpMethod, url); + } +}