All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.pactfoundation.consumer.dsl.LambdaDslJsonArray Maven / Gradle / Ivy

Go to download

# pact-jvm-consumer-java8 Provides a Java8 lambda based DSL for use with Junit to build consumer tests. ## Dependency The library is available on maven central using: * group-id = `au.com.dius.pact.consumer` * artifact-id = `java8` * version-id = `4.1.x` # A Lambda DSL for Pact This is an extension for the pact DSL provided by [consumer](../consumer). The difference between the default pact DSL and this lambda DSL is, as the name suggests, the usage of lambdas. The use of lambdas makes the code much cleaner. ## Why a new DSL implementation? The lambda DSL solves the following two main issues. Both are visible in the following code sample: ```java new PactDslJsonArray() .array() # open an array .stringValue("a1") # choose the method that is valid for arrays .stringValue("a2") # choose the method that is valid for arrays .closeArray() # close the array .array() # open an array .numberValue(1) # choose the method that is valid for arrays .numberValue(2) # choose the method that is valid for arrays .closeArray() # close the array .array() # open an array .object() # now we work with an object .stringValue("foo", "Foo") # choose the method that is valid for objects .closeObject() # close the object and we're back in the array .closeArray() # close the array ``` ### The existing DSL is quite error-prone Methods may only be called in certain states. For example `object()` may only be called when you're currently working on an array whereas `object(name)` is only allowed to be called when working on an object. But both of the methods are available. You'll find out at runtime if you're using the correct method. Finally, the need for opening and closing objects and arrays makes usage cumbersome. The lambda DSL has no ambiguous methods and there's no need to close objects and arrays as all the work on such an object is wrapped in a lamda call. ### The existing DSL is hard to read When formatting your source code with an IDE the code becomes hard to read as there's no indentation possible. Of course, you could do it by hand but we want auto formatting! Auto formatting works great for the new DSL! ```java array.object((o) -> { o.stringValue("foo", "Foo"); # an attribute o.stringValue("bar", "Bar"); # an attribute o.object("tar", (tarObject) -> { # an attribute with a nested object tarObject.stringValue("a", "A"); # attribute of the nested object tarObject.stringValue("b", "B"); # attribute of the nested object }) }); ``` ## Installation ### Maven ``` <dependency> <groupId>au.com.dius.pact.consumer</groupId> <artifactId>java8</artifactId> <version>${pact.version}</version> </dependency> ``` ## Usage Start with a static import of `LambdaDsl`. This class contains factory methods for the lambda dsl extension. When you come accross the `body()` method of `PactDslWithProvider` builder start using the new extensions. The call to `LambdaDsl` replaces the call to instance `new PactDslJsonArray()` and `new PactDslJsonBody()` of the pact library. ```java io.pactfoundation.consumer.dsl.LambdaDsl.* ``` ### Response body as json array ```java import static io.pactfoundation.consumer.dsl.LambdaDsl.newJsonArray; ... PactDslWithProvider builder = ... builder.given("some state") .uponReceiving("a request") .path("/my-app/my-service") .method("GET") .willRespondWith() .status(200) .body(newJsonArray((a) -> { a.stringValue("a1"); a.stringValue("a2"); }).build()); ``` ### Response body as json object ```java import static io.pactfoundation.consumer.dsl.LambdaDsl.newJsonBody; ... PactDslWithProvider builder = ... builder.given("some state") .uponReceiving("a request") .path("/my-app/my-service") .method("GET") .willRespondWith() .status(200) .body(newJsonBody((o) -> { o.stringValue("foo", "Foo"); o.stringValue("bar", "Bar"); }).build()); ``` ### Examples #### Simple Json object When creating simple json structures the difference between the two approaches isn't big. ##### JSON ```json { "bar": "Bar", "foo": "Foo" } ``` ##### Pact DSL ```java new PactDslJsonBody() .stringValue("foo", "Foo") .stringValue("bar", "Bar") ``` ##### Lambda DSL ```java newJsonBody((o) -> { o.stringValue("foo", "Foo"); o.stringValue("bar", "Bar"); }).build(); ``` #### An array of arrays When we come to more complex constructs with arrays and nested objects the beauty of lambdas become visible! ##### JSON ```json [ ["a1", "a2"], [1, 2], [{"foo": "Foo"}] ] ``` ##### Pact DSL ```java new PactDslJsonArray() .array() .stringValue("a1") .stringValue("a2") .closeArray() .array() .numberValue(1) .numberValue(2) .closeArray() .array() .object() .stringValue("foo", "Foo") .closeObject() .closeArray(); ``` ##### Lambda DSL ```java newJsonArray((rootArray) -> { rootArray.array((a) -> a.stringValue("a1").stringValue("a2")); rootArray.array((a) -> a.numberValue(1).numberValue(2)); rootArray.array((a) -> a.object((o) -> o.stringValue("foo", "Foo"))); }).build(); ``` ##### Kotlin Lambda DSL ```kotlin newJsonArray { newArray { stringValue("a1") stringValue("a2") } newArray { numberValue(1) numberValue(2) } newArray { newObject { stringValue("foo", "Foo") } } } ``` # Test Analytics We are tracking anonymous analytics to gather important usage statistics like JVM version and operating system. To disable tracking, set the 'pact_do_not_track' system property or environment variable to 'true'.

The newest version!
package io.pactfoundation.consumer.dsl;

import au.com.dius.pact.consumer.dsl.DslPart;
import au.com.dius.pact.consumer.dsl.PactDslJsonArray;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
import au.com.dius.pact.consumer.dsl.PactDslJsonRootValue;
import au.com.dius.pact.core.model.matchingrules.MatchingRule;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Date;
import java.util.function.Consumer;

public class LambdaDslJsonArray {

    private final PactDslJsonArray pactArray;

    LambdaDslJsonArray(final PactDslJsonArray pactArray) {
        this.pactArray = pactArray;
    }

    /**
     * Get the raw {@link PactDslJsonArray} which is abstracted with {@link PactDslJsonArray}
     */
    public PactDslJsonArray getPactDslJsonArray() {
        return pactArray;
    }

    /**
     * Element that is a JSON object
     */
    public LambdaDslJsonArray object(final Consumer o) {
        final PactDslJsonBody pactObject = pactArray.object();
        LambdaDslObject object = new LambdaDslObject(pactObject);
        o.accept(object);
        pactObject.closeObject();
        return this;
    }

    /**
     * Element that is a JSON array
     */
    public LambdaDslJsonArray array(final Consumer a) {
        final PactDslJsonArray pactArray = this.pactArray.array();
        LambdaDslJsonArray array = new LambdaDslJsonArray(pactArray);
        a.accept(array);
        pactArray.closeArray();
        return this;
    }

    /**
     * Element that must be the specified value
     *
     * @param value string value
     */
    public LambdaDslJsonArray stringValue(final String value) {
        pactArray.stringValue(value);
        return this;
    }

    /**
     * Element that can be any string
     *
     * @param example example value to use for generated bodies
     */
    public LambdaDslJsonArray stringType(final String example) {
        pactArray.stringType(example);
        return this;
    }

    /**
     * Element that must match the regular expression
     *
     * @param regex   regular expression
     * @param example example value to use for generated bodies
     */
    public LambdaDslJsonArray stringMatcher(final String regex, final String example) {
        pactArray.stringMatcher(regex, example);
        return this;
    }

    /**
     * Element that must be the specified number
     *
     * @param value number value
     */
    public LambdaDslJsonArray numberValue(final Number value) {
        pactArray.numberValue(value);
        return this;
    }

    /**
     * Element that can be any number
     *
     * @param example example number to use for generated bodies
     */
    public LambdaDslJsonArray numberType(final Number example) {
        pactArray.numberType(example);
        return this;
    }

    /**
     * Element that must be an integer
     */
    public LambdaDslJsonArray integerType() {
        pactArray.integerType();
        return this;
    }

    /**
     * Element that must be an integer
     *
     * @param example example integer value to use for generated bodies
     */
    public LambdaDslJsonArray integerType(final Long example) {
        pactArray.integerType(example);
        return this;
    }

    /**
     * Element that must be a decimal value
     */
    public LambdaDslJsonArray decimalType() {
        pactArray.decimalType();
        return this;
    }

    /**
     * Element that must be a decimalType value
     *
     * @param example example decimalType value
     */
    public LambdaDslJsonArray decimalType(final BigDecimal example) {
        pactArray.decimalType(example);
        return this;
    }

    /**
     * Attribute that must be a decimalType value
     *
     * @param example example decimalType value
     */
    public LambdaDslJsonArray decimalType(final Double example) {
        pactArray.decimalType(example);
        return this;
    }

    /**
     * Element that must be the specified value
     *
     * @param value boolean value
     */
    public LambdaDslJsonArray booleanValue(final Boolean value) {
        pactArray.booleanValue(value);
        return this;
    }

    /**
     * Element that must be a boolean
     *
     * @param example example boolean to use for generated bodies
     */
    public LambdaDslJsonArray booleanType(final Boolean example) {
        pactArray.booleanType(example);
        return this;
    }

    /**
     * Element that must be formatted as an ISO date
     */
    public LambdaDslJsonArray date() {
        pactArray.date();
        return this;
    }

    /**
     * Element that must match the provided date format
     *
     * @param format date format to match
     */
    public LambdaDslJsonArray date(final String format) {
        pactArray.date(format);
        return this;
    }

    /**
     * Element that must match the provided date format
     *
     * @param format  date format to match
     * @param example example date to use for generated values
     */
    public LambdaDslJsonArray date(final String format, final Date example) {
        pactArray.date(format, example);
        return this;
    }

    /**
     * Element that must be an ISO formatted time
     */
    public LambdaDslJsonArray time() {
        pactArray.time();
        return this;
    }

    /**
     * Element that must match the given time format
     *
     * @param format time format to match
     */
    public LambdaDslJsonArray time(final String format) {
        pactArray.time(format);
        return this;
    }

    /**
     * Element that must match the given time format
     *
     * @param format  time format to match
     * @param example example time to use for generated bodies
     */
    public LambdaDslJsonArray time(final String format, final Date example) {
        pactArray.time(format, example);
        return this;
    }

    /**
     * Element that must be an ISO formatted timestamp
     */
    public LambdaDslJsonArray timestamp() {
        pactArray.timestamp();
        return this;
    }

    /**
     * Element that must match the given timestamp format
     *
     * @param format timestamp format
     */
    public LambdaDslJsonArray timestamp(final String format) {
        pactArray.timestamp(format);
        return this;
    }

    /**
     * Element that must match the given timestamp format
     *
     * @param format  timestamp format
     * @param example example date and time to use for generated bodies
     */
    public LambdaDslJsonArray timestamp(final String format, final Date example) {
        pactArray.timestamp(format, example);
        return this;
    }

    /**
     * Element that must match the given timestamp format
     *
     * @param format  timestamp format
     * @param example example date and time to use for generated bodies
     */
    public LambdaDslJsonArray timestamp(final String format, final Instant example) {
        pactArray.timestamp(format, example);
        return this;
    }

    /**
     * Element that must be a numeric identifier
     */
    public LambdaDslJsonArray id() {
        pactArray.id();
        return this;
    }

    /**
     * Element that must be a numeric identifier
     *
     * @param example example id to use for generated bodies
     */
    public LambdaDslJsonArray id(final Long example) {
        pactArray.id(example);
        return this;
    }

    /**
     * Element that must be encoded as an UUID
     */
    public LambdaDslJsonArray uuid() {
        pactArray.uuid();
        return this;
    }

    /**
     * Element that must be encoded as an UUID
     *
     * @param example example UUID to use for generated bodies
     */
    public LambdaDslJsonArray uuid(final String example) {
        pactArray.uuid(example);
        return this;
    }

    /**
     * Element that must be encoded as a hexadecimal value
     */
    public LambdaDslJsonArray hexValue() {
        pactArray.hexValue();
        return this;
    }

    /**
     * Element that must be encoded as a hexadecimal value
     *
     * @param example example value to use for generated bodies
     */
    public LambdaDslJsonArray hexValue(final String example) {
        pactArray.hexValue(example);
        return this;
    }

    /**
     * Element that must be an IP4 address
     */
    public LambdaDslJsonArray ipV4Address() {
        pactArray.ipAddress();
        return this;
    }

    /**
     * Combine all the matchers using AND
     *
     * @param value Attribute example value
     * @param rules Matching rules to apply
     */
    public LambdaDslJsonArray and(Object value, MatchingRule... rules) {
        pactArray.and(value, rules);
        return this;
    }

    /**
     * Combine all the matchers using OR
     *
     * @param value Attribute example value
     * @param rules Matching rules to apply
     */
    public LambdaDslJsonArray or(Object value, MatchingRule... rules) {
        pactArray.or(value, rules);
        return this;
    }

    /**
     * Element that is an array where each item must match the following example
     */
    public LambdaDslJsonArray eachLike(Consumer nestedObject) {
        final PactDslJsonBody arrayLike = pactArray.eachLike();
        final LambdaDslJsonBody dslBody = new LambdaDslJsonBody(arrayLike);
        nestedObject.accept(dslBody);
        arrayLike.closeArray();
        return this;
    }

    /**
     * Element that is an array where each item must match the following example
     *
     * @param value Value that each item in the array must match
     */
    public LambdaDslJsonArray eachLike(PactDslJsonRootValue value) {
        pactArray.eachLike(value);
        return this;
    }

    /**
     * Element that is an array where each item must match the following example
     *
     * @param value          Value that each item in the array must match
     * @param numberExamples Number of examples to generate
     */
    public LambdaDslJsonArray eachLike(PactDslJsonRootValue value, int numberExamples) {
        pactArray.eachLike(value, numberExamples);
        return this;
    }

    /**
     * Element that is an array where each item must match the following example
     *
     * @param numberExamples Number of examples to generate
     */
    public LambdaDslJsonArray eachLike(int numberExamples, Consumer nestedObject) {
        final PactDslJsonBody arrayLike = pactArray.eachLike(numberExamples);
        final LambdaDslJsonBody dslBody = new LambdaDslJsonBody(arrayLike);
        nestedObject.accept(dslBody);
        arrayLike.closeArray();
        return this;
    }

    /**
     * Element that is an array with a minimum size where each item must match the following example
     *
     * @param size minimum size of the array
     */
    public LambdaDslJsonArray minArrayLike(Integer size, Consumer nestedObject) {
        final PactDslJsonBody arrayLike = pactArray.minArrayLike(size);
        final LambdaDslJsonBody dslBody = new LambdaDslJsonBody(arrayLike);
        nestedObject.accept(dslBody);
        arrayLike.closeArray();
        return this;
    }

    /**
     * Element that is an array with a minimum size where each item must match the following example
     *
     * @param size           minimum size of the array
     * @param numberExamples number of examples to generate
     */
    public LambdaDslJsonArray minArrayLike(Integer size, int numberExamples,
                                           Consumer nestedObject) {
        final PactDslJsonBody arrayLike = pactArray.minArrayLike(size, numberExamples);
        final LambdaDslJsonBody dslBody = new LambdaDslJsonBody(arrayLike);
        nestedObject.accept(dslBody);
        arrayLike.closeArray();
        return this;
    }

    /**
     * Element that is an array with a maximum size where each item must match the following example
     *
     * @param size maximum size of the array
     */
    public LambdaDslJsonArray maxArrayLike(Integer size, Consumer nestedObject) {
        final PactDslJsonBody arrayLike = pactArray.maxArrayLike(size);
        final LambdaDslJsonBody dslBody = new LambdaDslJsonBody(arrayLike);
        nestedObject.accept(dslBody);
        arrayLike.closeArray();
        return this;
    }

    /**
     * Element that is an array with a maximum size where each item must match the following example
     *
     * @param size           maximum size of the array
     * @param numberExamples number of examples to generate
     */
    public LambdaDslJsonArray maxArrayLike(Integer size, int numberExamples,
                                           Consumer nestedObject) {
        final PactDslJsonBody arrayLike = pactArray.maxArrayLike(size, numberExamples);
        final LambdaDslJsonBody dslBody = new LambdaDslJsonBody(arrayLike);
        nestedObject.accept(dslBody);
        arrayLike.closeArray();
        return this;
    }

    /**
     * Element that is an array with a minimum and maximum size where each item must match the following example
     *
     * @param minSize minimum size of the array
     * @param maxSize maximum size of the array
     */
    public LambdaDslJsonArray minMaxArrayLike(Integer minSize, Integer maxSize, Consumer nestedObject) {
        final PactDslJsonBody arrayLike = pactArray.minMaxArrayLike(minSize, maxSize);
        final LambdaDslJsonBody dslBody = new LambdaDslJsonBody(arrayLike);
        nestedObject.accept(dslBody);
        arrayLike.closeArray();
        return this;
    }

    /**
     * Element that is an array with a minimum and maximum size where each item must match the following example
     *
     * @param minSize        minimum size of the array
     * @param maxSize        maximum size of the array
     * @param numberExamples number of examples to generate
     */
    public LambdaDslJsonArray minMaxArrayLike(Integer minSize, Integer maxSize, int numberExamples,
                                              Consumer nestedObject) {
        final PactDslJsonBody arrayLike = pactArray.minMaxArrayLike(minSize, maxSize, numberExamples);
        final LambdaDslJsonBody dslBody = new LambdaDslJsonBody(arrayLike);
        nestedObject.accept(dslBody);
        arrayLike.closeArray();
        return this;
    }

    /**
     * Adds a null value to the list
     */
    public LambdaDslJsonArray nullValue() {
        pactArray.nullValue();
        return this;
    }

    /**
     * Array element where each element of the array is an array and must match the following object
     */
    public LambdaDslJsonArray eachArrayLike(Consumer nestedArray) {
        final PactDslJsonArray arrayLike = pactArray.eachArrayLike();
        final LambdaDslJsonArray dslArray = new LambdaDslJsonArray(arrayLike);
        nestedArray.accept(dslArray);
        arrayLike.closeArray().closeArray();
        return this;
    }

    /**
     * Array element where each element of the array is an array and must match the following object
     *
     * @param numberExamples number of examples to generate
     */
    public LambdaDslJsonArray eachArrayLike(int numberExamples, Consumer nestedArray) {
        final PactDslJsonArray arrayLike = pactArray.eachArrayLike(numberExamples);
        final LambdaDslJsonArray dslArray = new LambdaDslJsonArray(arrayLike);
        nestedArray.accept(dslArray);
        arrayLike.closeArray().closeArray();
        return this;
    }

    /**
     * Array element where each element of the array is an array and must match the following object.
     * This will generate 1 example value, if you want to change that number use {@link #eachArrayWithMaxLike(int, Integer, Consumer)}
     *
     * @param size Maximum size of the outer array
     */
    public LambdaDslJsonArray eachArrayWithMaxLike(Integer size, Consumer nestedArray) {
        final PactDslJsonArray arrayLike = pactArray.eachArrayWithMaxLike(size);
        final LambdaDslJsonArray dslArray = new LambdaDslJsonArray(arrayLike);
        nestedArray.accept(dslArray);
        arrayLike.closeArray().closeArray();
        return this;
    }

    /**
     * Array element where each element of the array is an array and must match the following object
     *
     * @param numberExamples number of examples to generate
     * @param size           Maximum size of the outer array
     */
    public LambdaDslJsonArray eachArrayWithMaxLike(int numberExamples, Integer size,
                                                   Consumer nestedArray) {
        final PactDslJsonArray arrayLike = pactArray.eachArrayWithMaxLike(numberExamples, size);
        final LambdaDslJsonArray dslArray = new LambdaDslJsonArray(arrayLike);
        nestedArray.accept(dslArray);
        arrayLike.closeArray().closeArray();
        return this;
    }

    /**
     * Array element where each element of the array is an array and must match the following object.
     * This will generate 1 example value, if you want to change that number use {@link #eachArrayWithMinLike(int, Integer, Consumer)}
     *
     * @param size Minimum size of the outer array
     */
    public LambdaDslJsonArray eachArrayWithMinLike(Integer size, Consumer nestedArray) {
        final PactDslJsonArray arrayLike = pactArray.eachArrayWithMinLike(size);
        final LambdaDslJsonArray dslArray = new LambdaDslJsonArray(arrayLike);
        nestedArray.accept(dslArray);
        arrayLike.closeArray().closeArray();
        return this;
    }

    /**
     * Array element where each element of the array is an array and must match the following object
     *
     * @param numberExamples number of examples to generate
     * @param size           Minimum size of the outer array
     */
    public LambdaDslJsonArray eachArrayWithMinLike(int numberExamples, Integer size,
                                                   Consumer nestedArray) {
        final PactDslJsonArray arrayLike = pactArray.eachArrayWithMinLike(numberExamples, size);
        final LambdaDslJsonArray dslArray = new LambdaDslJsonArray(arrayLike);
        nestedArray.accept(dslArray);
        arrayLike.closeArray().closeArray();
        return this;
    }

    /**
     * Array element where each element of the array is an array and must match the following object.
     * This will generate 1 example value, if you want to change that number use {@link #eachArrayWithMinMaxLike(Integer, Integer, int, Consumer)}
     *
     * @param minSize minimum size
     * @param maxSize maximum size
     */
    public LambdaDslJsonArray eachArrayWithMinMaxLike(Integer minSize, Integer maxSize, Consumer nestedArray) {
        final PactDslJsonArray arrayLike = pactArray.eachArrayWithMinMaxLike(minSize, maxSize);
        final LambdaDslJsonArray dslArray = new LambdaDslJsonArray(arrayLike);
        nestedArray.accept(dslArray);
        arrayLike.closeArray().closeArray();
        return this;
    }

    /**
     * Array element where each element of the array is an array and must match the following object
     *
     * @param minSize minimum size
     * @param maxSize maximum size
     */
    public LambdaDslJsonArray eachArrayWithMinMaxLike(Integer minSize, Integer maxSize, int numberExamples,
                                                      Consumer nestedArray) {
        final PactDslJsonArray arrayLike = pactArray.eachArrayWithMinMaxLike(numberExamples, minSize, maxSize);
        final LambdaDslJsonArray dslArray = new LambdaDslJsonArray(arrayLike);
        nestedArray.accept(dslArray);
        arrayLike.closeArray().closeArray();
        return this;
    }

    /**
     * Date value generated from the provided expression. Will use an ISO format.
     *
     * @param expression Date expression
     */
    public LambdaDslJsonArray dateExpression(String expression) {
        pactArray.dateExpression(expression);
        return this;
    }

    /**
     * Date value generated from the provided expression
     *
     * @param expression Date expression
     * @param format     Date format to use for values
     */
    public LambdaDslJsonArray dateExpression(String expression, String format) {
        pactArray.dateExpression(expression, format);
        return this;
    }

    /**
     * Time value generated from the provided expression. Will use an ISO format.
     *
     * @param expression Time expression
     */
    public LambdaDslJsonArray timeExpression(String expression) {
        pactArray.timeExpression(expression);
        return this;
    }

    /**
     * Time value generated from the provided expression
     *
     * @param expression Time expression
     * @param format     Time format to use for values
     */
    public LambdaDslJsonArray timeExpression(String expression, String format) {
        pactArray.timeExpression(expression, format);
        return this;
    }

    /**
     * Datetime generated from the provided expression. Will use an ISO format.
     *
     * @param expression Datetime expression
     */
    public LambdaDslJsonArray datetimeExpression(String expression) {
        pactArray.datetimeExpression(expression);
        return this;
    }

    /**
     * Datetime generated from the provided expression
     *
     * @param expression Datetime expression
     * @param format     Datetime format to use for values
     */
    public LambdaDslJsonArray datetimeExpression(String expression, String format) {
        pactArray.datetimeExpression(expression, format);
        return this;
    }

    public DslPart build() {
        return pactArray;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy