/** * Element that is a JSON object */ public PactDslJsonBody object() { return new PactDslJsonBody(".", "", this); }
/** * Attributes that can be any string * @param names attribute names */ public PactDslJsonBody stringType(String... names) { for (String name: names) { stringType(name); } return this; }
public LambdaDslObject object(final String name, final Consumer<LambdaDslObject> nestedObject) { final PactDslJsonBody pactObject = object.object(name); final LambdaDslObject dslObject = new LambdaDslObject(pactObject); nestedObject.accept(dslObject); pactObject.closeObject(); return this; }
@Override public PactDslJsonBody minMaxArrayLike(String name, Integer minSize, Integer maxSize, int numberExamples) { validateMinAndMaxAndExamples(minSize, maxSize, numberExamples); matchers.addRule(matcherKey(name), matchMinMax(minSize, maxSize)); PactDslJsonArray parent = new PactDslJsonArray(matcherKey(name), "", this, true); parent.setNumberExamples(numberExamples); return new PactDslJsonBody(".", "", parent); }
@Pact(provider = "userservice", consumer = "userclient") public MessagePact userCreatedMessagePact(MessagePactBuilder builder) { PactDslJsonBody body = new PactDslJsonBody(); body.stringType("messageUuid"); body.object("user") .numberType("id", 42L) .stringType("name", "Zaphod Beeblebrox") .closeObject(); // @formatter:off return builder .expectsToReceive("a user created message") .withContent(body) .toPact(); // @formatter:on }
/** * Attribute that is an array where each item must match the following example * @param name field name * @param numberExamples number of examples to generate */ @Override public PactDslJsonBody eachLike(String name, int numberExamples) { matchers.addRule(matcherKey(name), matchMin(0)); PactDslJsonArray parent = new PactDslJsonArray(matcherKey(name), "", this, true); parent.setNumberExamples(numberExamples); return new PactDslJsonBody(".", ".", parent); }
/** * Attribute that is an array with a maximum size where each item must match the following example * @param name field name * @param size maximum size of the array * @param numberExamples number of examples to generate */ @Override public PactDslJsonBody maxArrayLike(String name, Integer size, int numberExamples) { if (numberExamples > size) { throw new IllegalArgumentException(String.format("Number of example %d is more than the maximum size of %d", numberExamples, size)); } matchers.addRule(matcherKey(name), matchMax(size)); PactDslJsonArray parent = new PactDslJsonArray(matcherKey(name), "", this, true); parent.setNumberExamples(numberExamples); return new PactDslJsonBody(".", "", parent); }
/** * Attribute that is a JSON object defined from a DSL part * @param name field name * @param value DSL Part to set the value as */ public PactDslJsonBody object(String name, DslPart value) { String base = rootPath + name; if (!name.matches(Parser$.MODULE$.FieldRegex().toString())) { base = StringUtils.substringBeforeLast(rootPath, ".") + "['" + name + "']"; } if (value instanceof PactDslJsonBody) { PactDslJsonBody object = new PactDslJsonBody(base, "", this, (PactDslJsonBody) value); putObject(object); } else if (value instanceof PactDslJsonArray) { PactDslJsonArray object = new PactDslJsonArray(base, "", this, (PactDslJsonArray) value); putArray(object); } return this; }
@Pact(state = "person 42 exists", provider = "userservice", consumer = "userclient") RequestResponsePact updatePersonPact(PactDslWithProvider builder) { // @formatter:off return builder .given("person 42 exists") .uponReceiving("a request to PUT a person") .path("/user-service/users/42") .method("PUT") .willRespondWith() .status(200) .matchHeader("Content-Type", "application/json") .body(new PactDslJsonBody() .stringType("firstName", "Zaphod") .stringType("lastName", "Beeblebrox")) .toPact(); // @formatter:on }
/** * Attribute that can be any string * @param name attribute name */ public PactDslJsonBody stringType(String name) { generators.addGenerator(Category.BODY, matcherKey(name), new RandomStringGenerator(20)); return stringType(name, "string"); }
/** * Attribute that is an array where each item must match the following example * @param name field name * @deprecated use eachLike */ @Override @Deprecated public PactDslJsonBody arrayLike(String name) { matchers.addRule(matcherKey(name), TypeMatcher.INSTANCE); return new PactDslJsonBody(".", ".", new PactDslJsonArray(matcherKey(name), "", this, true)); }
/** * Accepts any key, and each key is mapped to a list of items that must match the following object definition * Note: this needs the Java system property "pact.matching.wildcard" set to value "true" when the pact file is verified. * @param exampleKey Example key to use for generating bodies */ public PactDslJsonBody eachKeyMappedToAnArrayLike(String exampleKey) { if (FeatureToggles.isFeatureSet(Feature.UseMatchValuesMatcher)) { matchers.addRule(rootPath.endsWith(".") ? rootPath.substring(0, rootPath.length() - 1) : rootPath, ValuesMatcher.INSTANCE); } else { matchers.addRule(rootPath + "*", matchMin(0)); } PactDslJsonArray parent = new PactDslJsonArray(rootPath + "*", exampleKey, this, true); return new PactDslJsonBody(".", "", parent); }
/** * Attributes that must be a decimal values * @param names attribute names */ public PactDslJsonBody decimalType(String... names) { for (String name: names) { decimalType(name); } return this; }
/** * Attribute named 'date' that must be formatted as an ISO date */ public PactDslJsonBody date() { return date("date"); }
/** * Closes the current array */ @Override public DslPart closeArray() { if (parent instanceof PactDslJsonArray) { closeObject(); return parent.closeArray(); } else { throw new UnsupportedOperationException("can't call closeArray on an Object"); } }
/** * Attribute that can be any number * @param name attribute name */ public PactDslJsonBody numberType(String name) { generators.addGenerator(Category.BODY, matcherKey(name), new RandomIntGenerator(0, Integer.MAX_VALUE)); return numberType(name, 100); }
@Pact(state = "provider accepts a new person", provider = "userservice", consumer = "userclient") RequestResponsePact createPersonPact(PactDslWithProvider builder) { // @formatter:off return builder .given("provider accepts a new person") .uponReceiving("a request to POST a person") .path("/user-service/users") .method("POST") .willRespondWith() .status(201) .matchHeader("Content-Type", "application/json") .body(new PactDslJsonBody() .integerType("id", 42)) .toPact(); // @formatter:on }
/** * Attributes that can be any number * @param names attribute names */ public PactDslJsonBody numberType(String... names) { for (String name: names) { numberType(name); } return this; }
/** * Attribute that must be a boolean * @param name attribute name */ public PactDslJsonBody booleanType(String name) { return booleanType(name, true); }
/** * Attribute that must be a decimal value * @param name attribute name */ public PactDslJsonBody decimalType(String name) { generators.addGenerator(Category.BODY, matcherKey(name), new RandomDecimalGenerator(10)); return decimalType(name, 100.0); }