diff --git a/consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslJsonRootValue.java b/consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslJsonRootValue.java deleted file mode 100644 index 44e05f81b..000000000 --- a/consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslJsonRootValue.java +++ /dev/null @@ -1,962 +0,0 @@ -package au.com.dius.pact.consumer.dsl; - -import au.com.dius.pact.consumer.InvalidMatcherException; -import au.com.dius.pact.core.matchers.UrlMatcherSupport; -import au.com.dius.pact.core.model.generators.Category; -import au.com.dius.pact.core.model.generators.DateGenerator; -import au.com.dius.pact.core.model.generators.DateTimeGenerator; -import au.com.dius.pact.core.model.generators.ProviderStateGenerator; -import au.com.dius.pact.core.model.generators.RandomDecimalGenerator; -import au.com.dius.pact.core.model.generators.RandomHexadecimalGenerator; -import au.com.dius.pact.core.model.generators.RandomIntGenerator; -import au.com.dius.pact.core.model.generators.RandomStringGenerator; -import au.com.dius.pact.core.model.generators.RegexGenerator; -import au.com.dius.pact.core.model.generators.TimeGenerator; -import au.com.dius.pact.core.model.generators.UuidGenerator; -import au.com.dius.pact.core.model.matchingrules.MatchingRule; -import au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup; -import au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher; -import au.com.dius.pact.core.model.matchingrules.RegexMatcher; -import au.com.dius.pact.core.model.matchingrules.RuleLogic; -import au.com.dius.pact.core.model.matchingrules.TypeMatcher; -import au.com.dius.pact.core.support.Json; -import au.com.dius.pact.core.support.expressions.DataType; -import au.com.dius.pact.core.support.json.JsonValue; -import com.mifmif.common.regex.Generex; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.commons.lang3.time.FastDateFormat; -import org.json.JSONObject; - -import java.math.BigDecimal; -import java.util.Arrays; -import java.util.Date; -import java.util.UUID; - -public class PactDslJsonRootValue extends DslPart { - - private static final String USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS = "Use PactDslJsonArray for arrays"; - private static final String USE_PACT_DSL_JSON_BODY_FOR_OBJECTS = "Use PactDslJsonBody for objects"; - private static final String EXAMPLE = "Example \""; - - private Object value; - - public PactDslJsonRootValue() { - super("", ""); - } - - @Override - public void putObjectPrivate(DslPart object) { - throw new UnsupportedOperationException(); - } - - @Override - public void putArrayPrivate(DslPart object) { - throw new UnsupportedOperationException(); - } - - @Override - public JsonValue getBody() { - return Json.toJson(value); - } - - @Override - public void setBody(JsonValue body) { - value = body; - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray array(String name) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Deprecated - @Override - public PactDslJsonArray array() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public DslPart closeArray() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody eachLike(String name) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - @Deprecated - public PactDslJsonBody eachLike(String name, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody eachLike(int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody eachLike(String name, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody eachLike() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - @Deprecated - public PactDslJsonArray eachLike(DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody minArrayLike(String name, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody minArrayLike(int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - @Deprecated - public PactDslJsonBody minArrayLike(String name, int size, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - @Deprecated - public PactDslJsonArray minArrayLike(int size, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody minArrayLike(String name, int size, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody minArrayLike(int size, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody maxArrayLike(String name, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody maxArrayLike(int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - @Deprecated - public PactDslJsonBody maxArrayLike(String name, int size, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - @Deprecated - public PactDslJsonArray maxArrayLike(int size, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody maxArrayLike(String name, int size, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody maxArrayLike(int size, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody minMaxArrayLike(String name, int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - @Deprecated - public PactDslJsonBody minMaxArrayLike(String name, int minSize, int maxSize, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody minMaxArrayLike(int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - @Deprecated - public PactDslJsonArray minMaxArrayLike(int minSize, int maxSize, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody minMaxArrayLike(String name, int minSize, int maxSize, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonBody minMaxArrayLike(int minSize, int maxSize, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonBody for objects - */ - @Override - @Deprecated - public PactDslJsonBody object(String name) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS); - } - - /** - * @deprecated Use PactDslJsonBody for objects - */ - @Override - @Deprecated - public PactDslJsonBody object() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS); - } - - /** - * @deprecated Use PactDslJsonBody for objects - */ - @Override - @Deprecated - public DslPart closeObject() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS); - } - - @Override - public DslPart close() { - getMatchers().applyMatcherRootPrefix("$"); - getGenerators().applyRootPrefix("$"); - return this; - } - - /** - * Value that can be any string - */ - public static PactDslJsonRootValue stringType() { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomStringGenerator(20)); - value.setValue("string"); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that can be any string - * - * @param example example value to use for generated bodies - */ - public static PactDslJsonRootValue stringType(String example) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(example); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that can be any number - */ - public static PactDslJsonRootValue numberType() { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomIntGenerator(0, Integer.MAX_VALUE)); - value.setValue(100); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that can be any number - * @param number example number to use for generated bodies - */ - public static PactDslJsonRootValue numberType(Number number) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(number); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that must be an integer - */ - public static PactDslJsonRootValue integerType() { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomIntGenerator(0, Integer.MAX_VALUE)); - value.setValue(100); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)); - return value; - } - - /** - * Value that must be an integer - * @param number example integer value to use for generated bodies - */ - public static PactDslJsonRootValue integerType(Long number) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(number); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)); - return value; - } - - /** - * Value that must be an integer - * @param number example integer value to use for generated bodies - */ - public static PactDslJsonRootValue integerType(int number) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(number); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)); - return value; - } - - /** - * Value that must be a decimal value - */ - public static PactDslJsonRootValue decimalType() { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomDecimalGenerator(10)); - value.setValue(100); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)); - return value; - } - - /** - * Value that must be a decimalType value - * @param number example decimalType value - */ - public static PactDslJsonRootValue decimalType(BigDecimal number) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(number); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)); - return value; - } - - /** - * Value that must be a decimalType value - * @param number example decimalType value - */ - public static PactDslJsonRootValue decimalType(Double number) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(number); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)); - return value; - } - - /** - * Value that must be a boolean - */ - public static PactDslJsonRootValue booleanType() { - return booleanType(true); - } - - /** - * Value that must be a boolean - * @param example example boolean to use for generated bodies - */ - public static PactDslJsonRootValue booleanType(Boolean example) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(example); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that must match the regular expression - * @param regex regular expression - * @param value example value to use for generated bodies - */ - public static PactDslJsonRootValue stringMatcher(String regex, String value) { - if (!value.matches(regex)) { - throw new InvalidMatcherException(EXAMPLE + value + "\" does not match regular expression \"" + - regex + "\""); - } - PactDslJsonRootValue rootValue = new PactDslJsonRootValue(); - rootValue.setValue(value); - rootValue.setMatcher(new RegexMatcher(regex, value)); - return rootValue; - } - - /** - * Value that must match the regular expression - * @param regex regular expression - * @deprecated Use the version that takes an example value - */ - @Deprecated - public static PactDslJsonRootValue stringMatcher(String regex) { - PactDslJsonRootValue rootValue = new PactDslJsonRootValue(); - rootValue.getGenerators().addGenerator(Category.BODY, "", new RegexGenerator(regex)); - rootValue.setValue(new Generex(regex).random()); - rootValue.setMatcher(rootValue.regexp(regex)); - return rootValue; - } - - /** - * Value that must be an ISO formatted timestamp - */ - public static PactDslJsonRootValue timestamp() { - return timestamp(DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()); - } - - /** - * Value that must match the given timestamp format - * @param format timestamp format - */ - public static PactDslJsonRootValue timestamp(String format) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new DateTimeGenerator(format)); - FastDateFormat instance = FastDateFormat.getInstance(format); - value.setValue(instance.format(new Date(DATE_2000))); - value.setMatcher(value.matchTimestamp(format)); - return value; - } - - /** - * Value that must match the given timestamp format - * @param format timestamp format - * @param example example date and time to use for generated bodies - */ - public static PactDslJsonRootValue timestamp(String format, Date example) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(instance.format(example)); - value.setMatcher(value.matchTimestamp(format)); - return value; - } - - /** - * Value that must be formatted as an ISO date - */ - public static PactDslJsonRootValue date() { - return date(DateFormatUtils.ISO_DATE_FORMAT.getPattern()); - } - - /** - * Value that must match the provided date format - * @param format date format to match - */ - public static PactDslJsonRootValue date(String format) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new DateGenerator(format)); - value.setValue(instance.format(new Date(DATE_2000))); - value.setMatcher(value.matchDate(format)); - return value; - } - - /** - * Value that must match the provided date format - * @param format date format to match - * @param example example date to use for generated values - */ - public static PactDslJsonRootValue date(String format, Date example) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(instance.format(example)); - value.setMatcher(value.matchDate(format)); - return value; - } - - /** - * Value that must be an ISO formatted time - */ - public static PactDslJsonRootValue time() { - return time(DateFormatUtils.ISO_TIME_FORMAT.getPattern()); - } - - /** - * Value that must match the given time format - * @param format time format to match - */ - public static PactDslJsonRootValue time(String format) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new TimeGenerator(format)); - value.setValue(instance.format(new Date(DATE_2000))); - value.setMatcher(value.matchTime(format)); - return value; - } - - /** - * Value that must match the given time format - * @param format time format to match - * @param example example time to use for generated bodies - */ - public static PactDslJsonRootValue time(String format, Date example) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(instance.format(example)); - value.setMatcher(value.matchTime(format)); - return value; - } - - /** - * Value that must be an IP4 address - */ - public static PactDslJsonRootValue ipAddress() { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue("127.0.0.1"); - value.setMatcher(value.regexp("(\\d{1,3}\\.)+\\d{1,3}")); - return value; - } - - /** - * Value that must be a numeric identifier - */ - public static PactDslJsonRootValue id() { - return numberType(); - } - - /** - * Value that must be a numeric identifier - * @param id example id to use for generated bodies - */ - public static PactDslJsonRootValue id(Long id) { - return numberType(id); - } - - /** - * Value that must be encoded as a hexadecimal value - */ - public static PactDslJsonRootValue hexValue() { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomHexadecimalGenerator(10)); - value.setValue("1234a"); - value.setMatcher(value.regexp("[0-9a-fA-F]+")); - return value; - } - - /** - * Value that must be encoded as a hexadecimal value - * @param hexValue example value to use for generated bodies - */ - public static PactDslJsonRootValue hexValue(String hexValue) { - if (!hexValue.matches(DslPart.Companion.getHEXADECIMAL().getPattern())) { - throw new InvalidMatcherException(EXAMPLE + hexValue + "\" is not a hexadecimal value"); - } - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(hexValue); - value.setMatcher(value.regexp("[0-9a-fA-F]+")); - return value; - } - - /** - * Value that must be encoded as an UUID - */ - public static PactDslJsonRootValue uuid() { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", UuidGenerator.INSTANCE); - value.setValue("e2490de5-5bd3-43d5-b7c4-526e33f71304"); - value.setMatcher(value.regexp(DslPart.Companion.getUUID_REGEX().getPattern())); - return value; - } - - /** - * Value that must be encoded as an UUID - * @param uuid example UUID to use for generated bodies - */ - public static PactDslJsonRootValue uuid(UUID uuid) { - return uuid(uuid.toString()); - } - - /** - * Value that must be encoded as an UUID - * @param uuid example UUID to use for generated bodies - */ - public static PactDslJsonRootValue uuid(String uuid) { - if (!uuid.matches(DslPart.Companion.getUUID_REGEX().getPattern())) { - throw new InvalidMatcherException(EXAMPLE + uuid + "\" is not an UUID"); - } - - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(uuid); - value.setMatcher(value.regexp(DslPart.Companion.getUUID_REGEX().getPattern())); - return value; - } - - public void setValue(Object value) { - this.value = value; - } - - public void setMatcher(MatchingRule matcher) { - getMatchers().addRule(matcher); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayLike(String name) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayLike(int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMaxLike(String name, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMaxLike(int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMaxLike(String name, int numberExamples, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMaxLike(int numberExamples, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMinLike(String name, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMinLike(int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMinLike(String name, int numberExamples, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMinLike(int numberExamples, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMinMaxLike(String name, int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMinMaxLike(int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMinMaxLike(String name, int numberExamples, int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayWithMinMaxLike(int numberExamples, int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayLike(String name, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - @Deprecated - public PactDslJsonArray eachArrayLike() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * Combine all the matchers using AND - * @param example Attribute example value - * @param rules Matching rules to apply - */ - public static PactDslJsonRootValue and(Object example, MatchingRule... rules) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - if (example != null) { - value.setValue(example); - } else { - value.setValue(JSONObject.NULL); - } - value.getMatchers().setRules("", new MatchingRuleGroup(Arrays.asList(rules), RuleLogic.AND)); - return value; - } - - /** - * Combine all the matchers using OR - * @param example Attribute name - * @param rules Matching rules to apply - */ - public static PactDslJsonRootValue or(Object example, MatchingRule... rules) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - if (example != null) { - value.setValue(example); - } else { - value.setValue(JSONObject.NULL); - } - value.getMatchers().setRules("", new MatchingRuleGroup(Arrays.asList(rules), RuleLogic.OR)); - return value; - } - - /** - * Matches a URL that is composed of a base path and a sequence of path expressions - * @param basePath The base path for the URL (like "http://localhost:8080/") which will be excluded from the matching - * @param pathFragments Series of path fragments to match on. These can be strings or regular expressions. - */ - public PactDslJsonRootValue matchUrl(String basePath, Object... pathFragments) { - UrlMatcherSupport urlMatcher = new UrlMatcherSupport(basePath, Arrays.asList(pathFragments)); - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.setValue(urlMatcher.getExampleValue()); - value.setMatcher(value.regexp(urlMatcher.getRegexExpression())); - return value; - } - - @Override - public DslPart matchUrl(String name, String basePath, Object... pathFragments) { - throw new UnsupportedOperationException( - "URL matcher with an attribute name is not supported. " + - "Use matchUrl(String basePath, Object... pathFragments)"); - } - - @Override - public PactDslJsonBody matchUrl2(String name, Object... pathFragments) { - throw new UnsupportedOperationException( - "URL matcher with an attribute name is not supported. " + - "Use matchUrl2(Object... pathFragments)"); - } - - /** - * Matches a URL that is composed of a base path and a sequence of path expressions. Base path from the mock server - * will be used. - * @param pathFragments Series of path fragments to match on. These can be strings or regular expressions. - */ - @Override - public DslPart matchUrl2(Object... pathFragments) { - return matchUrl(null, pathFragments); - } - - /** - * Adds a value that will have it's value injected from the provider state - * @param expression Expression to be evaluated from the provider state - * @param example Example value to be used in the consumer test - */ - public static PactDslJsonRootValue valueFromProviderState(String expression, Object example) { - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new ProviderStateGenerator(expression, DataType.from(example))); - value.setValue(example); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Date value generated from an expression. The date will be formatted as an ISO date. - * @param expression Date expression - */ - public static PactDslJsonRootValue dateExpression(String expression) { - return dateExpression(expression, DateFormatUtils.ISO_DATE_FORMAT.getPattern()); - } - - /** - * Date value generated from an expression. - * @param expression Date expression - * @param format Date format to use - */ - public static PactDslJsonRootValue dateExpression(String expression, String format) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new DateGenerator(format, expression)); - value.setValue(instance.format(new Date(DATE_2000))); - value.setMatcher(value.matchDate(format)); - return value; - } - - /** - * Time value generated from an expression. The time will be formatted as an ISO time. - * @param expression Date expression - */ - public static PactDslJsonRootValue timeExpression(String expression) { - return timeExpression(expression, DateFormatUtils.ISO_TIME_NO_T_FORMAT.getPattern()); - } - - /** - * Time value generated from an expression. - * @param expression Time expression - * @param format Time format to use - */ - public static PactDslJsonRootValue timeExpression(String expression, String format) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new TimeGenerator(format, expression)); - value.setValue(instance.format(new Date(DATE_2000))); - value.setMatcher(value.matchTime(format)); - return value; - } - - /** - * Datetime value generated from an expression. The datetime will be formatted as an ISO datetime. - * @param expression Datetime expression - */ - public static PactDslJsonRootValue datetimeExpression(String expression) { - return datetimeExpression(expression, DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()); - } - - /** - * Datetime value generated from an expression. - * @param expression Datetime expression - * @param format Datetime format to use - */ - public static PactDslJsonRootValue datetimeExpression(String expression, String format) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslJsonRootValue value = new PactDslJsonRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new DateTimeGenerator(format, expression)); - value.setValue(instance.format(new Date(DATE_2000))); - value.setMatcher(value.matchTimestamp(format)); - return value; - } -} diff --git a/consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslRootValue.java b/consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslRootValue.java deleted file mode 100644 index 878a0d189..000000000 --- a/consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslRootValue.java +++ /dev/null @@ -1,817 +0,0 @@ -package au.com.dius.pact.consumer.dsl; - -import au.com.dius.pact.consumer.InvalidMatcherException; -import au.com.dius.pact.core.model.generators.Category; -import au.com.dius.pact.core.model.generators.DateGenerator; -import au.com.dius.pact.core.model.generators.DateTimeGenerator; -import au.com.dius.pact.core.model.generators.ProviderStateGenerator; -import au.com.dius.pact.core.model.generators.RandomDecimalGenerator; -import au.com.dius.pact.core.model.generators.RandomHexadecimalGenerator; -import au.com.dius.pact.core.model.generators.RandomIntGenerator; -import au.com.dius.pact.core.model.generators.RandomStringGenerator; -import au.com.dius.pact.core.model.generators.TimeGenerator; -import au.com.dius.pact.core.model.generators.UuidGenerator; -import au.com.dius.pact.core.model.matchingrules.MatchingRule; -import au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup; -import au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher; -import au.com.dius.pact.core.model.matchingrules.RuleLogic; -import au.com.dius.pact.core.model.matchingrules.TypeMatcher; -import au.com.dius.pact.core.support.Json; -import au.com.dius.pact.core.support.expressions.DataType; -import au.com.dius.pact.core.support.json.JsonValue; -import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.commons.lang3.time.FastDateFormat; -import org.json.JSONObject; - -import java.math.BigDecimal; -import java.util.Arrays; -import java.util.Date; -import java.util.UUID; - -/** - * Matcher to create a plain root matching strategy. Used with text/plain to match regex responses - */ -public class PactDslRootValue extends DslPart { - - private static final String USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS = "Use PactDslJsonArray for arrays"; - private static final String USE_PACT_DSL_JSON_BODY_FOR_OBJECTS = "Use PactDslJsonBody for objects"; - private static final String EXAMPLE = "Example \""; - - private JsonValue value; - private boolean encodeJson = false; - - public PactDslRootValue() { - super("", ""); - } - - @Override - public void putObjectPrivate(DslPart object) { - throw new UnsupportedOperationException(); - } - - @Override - public void putArrayPrivate(DslPart object) { - throw new UnsupportedOperationException(); - } - - @Override - public JsonValue getBody() { - return value; - } - - @Override - public void setBody(JsonValue body) { - value = body; - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray array(String name) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray array() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public DslPart closeArray() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody eachLike(String name) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - public PactDslJsonBody eachLike(String name, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody eachLike(int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody eachLike(String name, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody eachLike() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - public PactDslJsonArray eachLike(DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody minArrayLike(String name, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody minArrayLike(int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - public PactDslJsonBody minArrayLike(String name, int size, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - public PactDslJsonArray minArrayLike(int size, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody minArrayLike(String name, int size, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody minArrayLike(int size, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody maxArrayLike(String name, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody maxArrayLike(int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - public PactDslJsonBody maxArrayLike(String name, int size, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - public PactDslJsonArray maxArrayLike(int size, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody maxArrayLike(String name, int size, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody maxArrayLike(int size, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody minMaxArrayLike(String name, int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - public PactDslJsonBody minMaxArrayLike(String name, int minSize, int maxSize, DslPart object) { - return null; - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody minMaxArrayLike(int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - @Override - public PactDslJsonArray minMaxArrayLike(int minSize, int maxSize, DslPart object) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody minMaxArrayLike(String name, int minSize, int maxSize, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonBody minMaxArrayLike(int minSize, int maxSize, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonBody for objects - */ - @Override - public PactDslJsonBody object(String name) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS); - } - - /** - * @deprecated Use PactDslJsonBody for objects - */ - @Override - public PactDslJsonBody object() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS); - } - - /** - * @deprecated Use PactDslJsonBody for objects - */ - @Override - public DslPart closeObject() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS); - } - - @Override - public DslPart close() { - getMatchers().applyMatcherRootPrefix("$"); - getGenerators().applyRootPrefix("$"); - return this; - } - - /** - * Value that can be any string - */ - public static PactDslRootValue stringType() { - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomStringGenerator(20)); - value.setValue("string"); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that can be any string - * - * @param example example value to use for generated bodies - */ - public static PactDslRootValue stringType(String example) { - PactDslRootValue value = new PactDslRootValue(); - value.setValue(example); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that can be any number - */ - public static PactDslRootValue numberType() { - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomIntGenerator(0, Integer.MAX_VALUE)); - value.setValue(100); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that can be any number - * @param number example number to use for generated bodies - */ - public static PactDslRootValue numberType(Number number) { - PactDslRootValue value = new PactDslRootValue(); - value.setValue(number); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that must be an integer - */ - public static PactDslRootValue integerType() { - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomIntGenerator(0, Integer.MAX_VALUE)); - value.setValue(100); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)); - return value; - } - - /** - * Value that must be an integer - * @param number example integer value to use for generated bodies - */ - public static PactDslRootValue integerType(Long number) { - PactDslRootValue value = new PactDslRootValue(); - value.setValue(number); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)); - return value; - } - - /** - * Value that must be an integer - * @param number example integer value to use for generated bodies - */ - public static PactDslRootValue integerType(int number) { - PactDslRootValue value = new PactDslRootValue(); - value.setValue(number); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)); - return value; - } - - /** - * Value that must be a decimal value - */ - public static PactDslRootValue decimalType() { - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomDecimalGenerator(10)); - value.setValue(100); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)); - return value; - } - - /** - * Value that must be a decimalType value - * @param number example decimalType value - */ - public static PactDslRootValue decimalType(BigDecimal number) { - PactDslRootValue value = new PactDslRootValue(); - value.setValue(number); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)); - return value; - } - - /** - * Value that must be a decimalType value - * @param number example decimalType value - */ - public static PactDslRootValue decimalType(Double number) { - PactDslRootValue value = new PactDslRootValue(); - value.setValue(number); - value.setMatcher(new NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)); - return value; - } - - /** - * Value that must be a boolean - */ - public static PactDslRootValue booleanType() { - return booleanType(true); - } - - /** - * Value that must be a boolean - * @param example example boolean to use for generated bodies - */ - public static PactDslRootValue booleanType(Boolean example) { - PactDslRootValue value = new PactDslRootValue(); - value.setValue(example); - value.setMatcher(TypeMatcher.INSTANCE); - return value; - } - - /** - * Value that must match the regular expression - * @param regex regular expression - * @param value example value to use for generated bodies - */ - public static PactDslRootValue stringMatcher(String regex, String value) { - if (!value.matches(regex)) { - throw new InvalidMatcherException(EXAMPLE + value + "\" does not match regular expression \"" + - regex + "\""); - } - PactDslRootValue rootValue = new PactDslRootValue(); - rootValue.setValue(value); - rootValue.setMatcher(rootValue.regexp(regex)); - return rootValue; - } - - /** - * Value that must be an ISO formatted timestamp - */ - public static PactDslRootValue timestamp() { - return timestamp(DateFormatUtils.ISO_DATETIME_FORMAT.getPattern()); - } - - /** - * Value that must match the given timestamp format - * @param format timestamp format - */ - public static PactDslRootValue timestamp(String format) { - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new DateTimeGenerator(format)); - FastDateFormat instance = FastDateFormat.getInstance(format); - value.setValue(instance.format(new Date(DATE_2000))); - value.setMatcher(value.matchTimestamp(format)); - return value; - } - - /** - * Value that must match the given timestamp format - * @param format timestamp format - * @param example example date and time to use for generated bodies - */ - public static PactDslRootValue timestamp(String format, Date example) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslRootValue value = new PactDslRootValue(); - value.setValue(instance.format(example)); - value.setMatcher(value.matchTimestamp(format)); - return value; - } - - /** - * Value that must be formatted as an ISO date - */ - public static PactDslRootValue date() { - return date(DateFormatUtils.ISO_DATE_FORMAT.getPattern()); - } - - /** - * Value that must match the provided date format - * @param format date format to match - */ - public static PactDslRootValue date(String format) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new DateGenerator(format)); - value.setValue(instance.format(new Date(DATE_2000))); - value.setMatcher(value.matchDate(format)); - return value; - } - - /** - * Value that must match the provided date format - * @param format date format to match - * @param example example date to use for generated values - */ - public static PactDslRootValue date(String format, Date example) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslRootValue value = new PactDslRootValue(); - value.setValue(instance.format(example)); - value.setMatcher(value.matchDate(format)); - return value; - } - - /** - * Value that must be an ISO formatted time - */ - public static PactDslRootValue time() { - return time(DateFormatUtils.ISO_TIME_FORMAT.getPattern()); - } - - /** - * Value that must match the given time format - * @param format time format to match - */ - public static PactDslRootValue time(String format) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new TimeGenerator(format)); - value.setValue(instance.format(new Date(DATE_2000))); - value.setMatcher(value.matchTime(format)); - return value; - } - - /** - * Value that must match the given time format - * @param format time format to match - * @param example example time to use for generated bodies - */ - public static PactDslRootValue time(String format, Date example) { - FastDateFormat instance = FastDateFormat.getInstance(format); - PactDslRootValue value = new PactDslRootValue(); - value.setValue(instance.format(example)); - value.setMatcher(value.matchTime(format)); - return value; - } - - /** - * Value that must be an IP4 address - */ - public static PactDslRootValue ipAddress() { - PactDslRootValue value = new PactDslRootValue(); - value.setValue("127.0.0.1"); - value.setMatcher(value.regexp("(\\d{1,3}\\.)+\\d{1,3}")); - return value; - } - - /** - * Value that must be a numeric identifier - */ - public static PactDslRootValue id() { - return numberType(); - } - - /** - * Value that must be a numeric identifier - * @param id example id to use for generated bodies - */ - public static PactDslRootValue id(Long id) { - return numberType(id); - } - - /** - * Value that must be encoded as a hexadecimal value - */ - public static PactDslRootValue hexValue() { - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new RandomHexadecimalGenerator(10)); - value.setValue("1234a"); - value.setMatcher(value.regexp("[0-9a-fA-F]+")); - return value; - } - - /** - * Value that must be encoded as a hexadecimal value - * @param hexValue example value to use for generated bodies - */ - public static PactDslRootValue hexValue(String hexValue) { - if (!hexValue.matches(DslPart.Companion.getHEXADECIMAL().getPattern())) { - throw new InvalidMatcherException(EXAMPLE + hexValue + "\" is not a hexadecimal value"); - } - PactDslRootValue value = new PactDslRootValue(); - value.setValue(hexValue); - value.setMatcher(value.regexp("[0-9a-fA-F]+")); - return value; - } - - /** - * Value that must be encoded as an UUID - */ - public static PactDslRootValue uuid() { - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", UuidGenerator.INSTANCE); - value.setValue("e2490de5-5bd3-43d5-b7c4-526e33f71304"); - value.setMatcher(value.regexp(DslPart.Companion.getUUID_REGEX().getPattern())); - return value; - } - - /** - * Value that must be encoded as an UUID - * @param uuid example UUID to use for generated bodies - */ - public static PactDslRootValue uuid(UUID uuid) { - return uuid(uuid.toString()); - } - - /** - * Value that must be encoded as an UUID - * @param uuid example UUID to use for generated bodies - */ - public static PactDslRootValue uuid(String uuid) { - if (!uuid.matches(DslPart.Companion.getUUID_REGEX().getPattern())) { - throw new InvalidMatcherException(EXAMPLE + uuid + "\" is not an UUID"); - } - - PactDslRootValue value = new PactDslRootValue(); - value.setValue(uuid); - value.setMatcher(value.regexp(DslPart.Companion.getUUID_REGEX().getPattern())); - return value; - } - - public void setValue(Object value) { - this.value = Json.toJson(value); - } - - public void setMatcher(MatchingRule matcher) { - getMatchers().addRule(matcher); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayLike(String name) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayLike(int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMaxLike(String name, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMaxLike(int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMaxLike(String name, int numberExamples, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMaxLike(int numberExamples, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMinLike(String name, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMinLike(int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMinLike(String name, int numberExamples, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMinLike(int numberExamples, int size) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMinMaxLike(String name, int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMinMaxLike(int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMinMaxLike(String name, int numberExamples, int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayWithMinMaxLike(int numberExamples, int minSize, int maxSize) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayLike(String name, int numberExamples) { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * @deprecated Use PactDslJsonArray for arrays - */ - @Override - public PactDslJsonArray eachArrayLike() { - throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS); - } - - /** - * Combine all the matchers using AND - * @param example Attribute example value - * @param rules Matching rules to apply - */ - public static PactDslRootValue and(Object example, MatchingRule... rules) { - PactDslRootValue value = new PactDslRootValue(); - if (example != null) { - value.setValue(example); - } else { - value.setValue(JSONObject.NULL); - } - value.getMatchers().setRules("", new MatchingRuleGroup(Arrays.asList(rules), RuleLogic.AND)); - return value; - } - - /** - * Combine all the matchers using OR - * @param example Attribute name - * @param rules Matching rules to apply - */ - public static PactDslRootValue or(Object example, MatchingRule... rules) { - PactDslRootValue value = new PactDslRootValue(); - if (example != null) { - value.setValue(example); - } else { - value.setValue(JSONObject.NULL); - } - value.getMatchers().setRules("", new MatchingRuleGroup(Arrays.asList(rules), RuleLogic.OR)); - return value; - } - - /** - * Adds a value that will have it's value injected from the provider state - * @param expression Expression to be evaluated from the provider state - * @param example Example value to be used in the consumer test - */ - public static PactDslRootValue valueFromProviderState(String expression, Object example) { - PactDslRootValue value = new PactDslRootValue(); - value.getGenerators().addGenerator(Category.BODY, "", new ProviderStateGenerator(expression, DataType.from(example))); - value.setValue(example); - return value; - } - - @Override - public DslPart matchUrl(String name, String basePath, Object... pathFragments) { - throw new UnsupportedOperationException("matchUrl is not currently supported for PactDslRootValue"); - } - - @Override - public DslPart matchUrl(String basePath, Object... pathFragments) { - throw new UnsupportedOperationException("matchUrl is not currently supported for PactDslRootValue"); - } - - @Override - public DslPart matchUrl2(String name, Object... pathFragments) { - throw new UnsupportedOperationException("matchUrl2 is not currently supported for PactDslRootValue"); - } - - @Override - public DslPart matchUrl2(Object... pathFragments) { - throw new UnsupportedOperationException("matchUrl2 is not currently supported for PactDslRootValue"); - } -} diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt index f3d6377e9..e12eaccb6 100755 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt @@ -44,7 +44,7 @@ import java.util.regex.Pattern * DSL to define a JSON Object */ @Suppress("LargeClass", "TooManyFunctions", "SpreadOperator", "ImplicitDefaultLocale") -class PactDslJsonBody : DslPart { +open class PactDslJsonBody : DslPart { override var body: JsonValue /** @@ -60,7 +60,7 @@ class PactDslJsonBody : DslPart { * @param rootName Name to associate this object as in the parent * @param parent Parent to attach to */ - constructor(rootPath: String?, rootName: String?, parent: DslPart?) : super(parent, rootPath!!, rootName!!) { + constructor(rootPath: String, rootName: String, parent: DslPart?) : super(parent, rootPath, rootName) { body = JsonValue.Object() } @@ -71,7 +71,8 @@ class PactDslJsonBody : DslPart { * @param parent Parent to attach to * @param body Body to copy values from */ - constructor(rootPath: String?, rootName: String?, parent: DslPart?, body: PactDslJsonBody) : super(parent, rootPath!!, rootName!!) { + constructor(rootPath: String, rootName: String, parent: DslPart?, body: PactDslJsonBody) + : super(parent, rootPath, rootName) { this.body = body.body matchers = body.matchers.copyWithUpdatedMatcherRootPrefix(rootPath) generators = body.generators.copyWithUpdatedMatcherRootPrefix(rootPath) diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonRootValue.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonRootValue.kt new file mode 100644 index 000000000..21399090e --- /dev/null +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonRootValue.kt @@ -0,0 +1,814 @@ +package au.com.dius.pact.consumer.dsl + +import au.com.dius.pact.consumer.InvalidMatcherException +import au.com.dius.pact.core.matchers.UrlMatcherSupport +import au.com.dius.pact.core.model.generators.Category +import au.com.dius.pact.core.model.generators.DateGenerator +import au.com.dius.pact.core.model.generators.DateTimeGenerator +import au.com.dius.pact.core.model.generators.ProviderStateGenerator +import au.com.dius.pact.core.model.generators.RandomDecimalGenerator +import au.com.dius.pact.core.model.generators.RandomHexadecimalGenerator +import au.com.dius.pact.core.model.generators.RandomIntGenerator +import au.com.dius.pact.core.model.generators.RandomStringGenerator +import au.com.dius.pact.core.model.generators.RegexGenerator +import au.com.dius.pact.core.model.generators.TimeGenerator +import au.com.dius.pact.core.model.generators.UuidGenerator +import au.com.dius.pact.core.model.matchingrules.MatchingRule +import au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup +import au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher +import au.com.dius.pact.core.model.matchingrules.RegexMatcher +import au.com.dius.pact.core.model.matchingrules.RuleLogic +import au.com.dius.pact.core.model.matchingrules.TypeMatcher +import au.com.dius.pact.core.support.Json.toJson +import au.com.dius.pact.core.support.expressions.DataType.Companion.from +import au.com.dius.pact.core.support.json.JsonValue +import com.mifmif.common.regex.Generex +import org.apache.commons.lang3.time.DateFormatUtils +import org.apache.commons.lang3.time.FastDateFormat +import org.json.JSONObject +import java.math.BigDecimal +import java.util.Date +import java.util.UUID + +@Suppress("TooManyFunctions", "SpreadOperator") +open class PactDslJsonRootValue : DslPart("", "") { + var value: Any? = null + + override fun putObjectPrivate(obj: DslPart) { + throw UnsupportedOperationException() + } + + override fun putArrayPrivate(obj: DslPart) { + throw UnsupportedOperationException() + } + + override var body: JsonValue + get() = toJson(value) + set(body) { + value = body + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun array(name: String): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun array(): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun closeArray(): DslPart? { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(name: String): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(name: String, obj: DslPart): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(name: String, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(`object`: DslPart): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(name: String, size: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(size: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(name: String, size: Int, obj: DslPart): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(size: Int, obj: DslPart): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(name: String, size: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(size: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(name: String, size: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(size: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(name: String, size: Int, obj: DslPart): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(size: Int, obj: DslPart): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(name: String, size: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(size: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(name: String, minSize: Int, maxSize: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(name: String, minSize: Int, maxSize: Int, obj: DslPart): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(minSize: Int, maxSize: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(minSize: Int, maxSize: Int, obj: DslPart): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(name: String, minSize: Int, maxSize: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(minSize: Int, maxSize: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonBody for objects") + override fun `object`(name: String): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS) + } + + @Deprecated("Use PactDslJsonBody for objects") + override fun `object`(): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS) + } + + @Deprecated("Use PactDslJsonBody for objects") + override fun closeObject(): DslPart? { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS) + } + + override fun close(): DslPart { + matchers.applyMatcherRootPrefix("$") + generators.applyRootPrefix("$") + return this + } + + fun setMatcher(matcher: MatchingRule) { + matchers.addRule(matcher) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayLike(name: String): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayLike(numberExamples: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMaxLike(name: String, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMaxLike(size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMaxLike(name: String, numberExamples: Int, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMaxLike(numberExamples: Int, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinLike(name: String, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinLike(size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinLike(name: String, numberExamples: Int, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinLike(numberExamples: Int, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinMaxLike(name: String, minSize: Int, maxSize: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinMaxLike(minSize: Int, maxSize: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinMaxLike( + name: String, + numberExamples: Int, + minSize: Int, + maxSize: Int + ): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinMaxLike(numberExamples: Int, minSize: Int, maxSize: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayLike(name: String, numberExamples: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayLike(): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + /** + * Matches a URL that is composed of a base path and a sequence of path expressions + * @param basePath The base path for the URL (like "http://localhost:8080/") which will be excluded from the matching + * @param pathFragments Series of path fragments to match on. These can be strings or regular expressions. + */ + override fun matchUrl(basePath: String?, vararg pathFragments: Any): PactDslJsonRootValue { + val urlMatcher = UrlMatcherSupport(basePath!!, listOf(*pathFragments)) + val value = PactDslJsonRootValue() + val exampleValue = urlMatcher.getExampleValue() + value.value = exampleValue + val regexExpression = urlMatcher.getRegexExpression() + value.setMatcher(value.regexp(regexExpression)) + return value + } + + override fun matchUrl(name: String, basePath: String?, vararg pathFragments: Any): DslPart { + throw UnsupportedOperationException( + "URL matcher with an attribute name is not supported. " + + "Use matchUrl(String basePath, Object... pathFragments)") + } + + override fun matchUrl2(name: String, vararg pathFragments: Any): PactDslJsonBody { + throw UnsupportedOperationException( + "URL matcher with an attribute name is not supported. " + + "Use matchUrl2(Object... pathFragments)") + } + + /** + * Matches a URL that is composed of a base path and a sequence of path expressions. Base path from the mock server + * will be used. + * @param pathFragments Series of path fragments to match on. These can be strings or regular expressions. + */ + override fun matchUrl2(vararg pathFragments: Any): DslPart { + return matchUrl(null, *pathFragments) + } + + companion object { + private const val USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS = "Use PactDslJsonArray for arrays" + private const val USE_PACT_DSL_JSON_BODY_FOR_OBJECTS = "Use PactDslJsonBody for objects" + + /** + * Value that can be any string + */ + @JvmStatic + fun stringType(): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", RandomStringGenerator(20)) + value.value = "string" + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that can be any string + * + * @param example example value to use for generated bodies + */ + @JvmStatic + fun stringType(example: String): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.value = example + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that can be any number + */ + @JvmStatic + fun numberType(): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", RandomIntGenerator(0, Int.MAX_VALUE)) + value.value = 100 + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that can be any number + * @param number example number to use for generated bodies + */ + @JvmStatic + fun numberType(number: Number): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.value = number + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that must be an integer + */ + @JvmStatic + fun integerType(): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", RandomIntGenerator(0, Int.MAX_VALUE)) + value.value = 100 + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)) + return value + } + + /** + * Value that must be an integer + * @param number example integer value to use for generated bodies + */ + @JvmStatic + fun integerType(number: Long): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.value = number + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)) + return value + } + + /** + * Value that must be an integer + * @param number example integer value to use for generated bodies + */ + @JvmStatic + fun integerType(number: Int): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.value = number + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)) + return value + } + + /** + * Value that must be a decimal value + */ + @JvmStatic + fun decimalType(): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", RandomDecimalGenerator(10)) + value.value = 100 + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)) + return value + } + + /** + * Value that must be a decimalType value + * @param number example decimalType value + */ + @JvmStatic + fun decimalType(number: BigDecimal): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.value = number + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)) + return value + } + + /** + * Value that must be a decimalType value + * @param number example decimalType value + */ + @JvmStatic + fun decimalType(number: Double): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.value = number + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)) + return value + } + + /** + * Value that must be a boolean + * @param example example boolean to use for generated bodies + */ + @JvmOverloads + @JvmStatic + fun booleanType(example: Boolean = true): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.value = example + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that must match the regular expression + * @param regex regular expression + * @param value example value to use for generated bodies + */ + @JvmStatic + fun stringMatcher(regex: String, value: String): PactDslJsonRootValue { + if (!value.matches(Regex(regex))) { + throw InvalidMatcherException("Example \"$value\" does not match regular expression \"$regex\"") + } + val rootValue = PactDslJsonRootValue() + rootValue.value = value + rootValue.setMatcher(RegexMatcher(regex, value)) + return rootValue + } + + /** + * Value that must match the regular expression + * @param regex regular expression + */ + @Deprecated("Use the version that takes an example value") + @JvmStatic + fun stringMatcher(regex: String): PactDslJsonRootValue { + val rootValue = PactDslJsonRootValue() + rootValue.generators.addGenerator(Category.BODY, "", RegexGenerator(regex)) + rootValue.value = Generex(regex).random() + rootValue.setMatcher(rootValue.regexp(regex)) + return rootValue + } + + /** + * Value that must match the given timestamp format + * @param format timestamp format + */ + @JvmOverloads + @Deprecated("use datetime") + @JvmStatic + fun timestamp(format: String = DateFormatUtils.ISO_DATETIME_FORMAT.pattern): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", DateTimeGenerator(format)) + val instance = FastDateFormat.getInstance(format) + value.value = instance.format(Date(DATE_2000)) + value.setMatcher(value.matchTimestamp(format)) + return value + } + + /** + * Value that must match the given timestamp format + * @param format timestamp format + * @param example example date and time to use for generated bodies + */ + @Deprecated("use datetime") + @JvmStatic + fun timestamp(format: String, example: Date): PactDslJsonRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslJsonRootValue() + value.value = instance.format(example) + value.setMatcher(value.matchTimestamp(format)) + return value + } + + /** + * Value that must match the given timestamp format + * @param format timestamp format + */ + @JvmOverloads + @JvmStatic + fun datetime(format: String = DateFormatUtils.ISO_DATETIME_FORMAT.pattern): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", DateTimeGenerator(format)) + val instance = FastDateFormat.getInstance(format) + value.value = instance.format(Date(DATE_2000)) + value.setMatcher(value.matchTimestamp(format)) + return value + } + + /** + * Value that must match the given timestamp format + * @param format timestamp format + * @param example example date and time to use for generated bodies + */ + @JvmStatic + fun datetime(format: String, example: Date): PactDslJsonRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslJsonRootValue() + value.value = instance.format(example) + value.setMatcher(value.matchTimestamp(format)) + return value + } + + /** + * Value that must match the provided date format + * @param format date format to match + */ + @JvmOverloads + @JvmStatic + fun date(format: String = DateFormatUtils.ISO_DATE_FORMAT.pattern): PactDslJsonRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", DateGenerator(format)) + value.value = instance.format(Date(DATE_2000)) + value.setMatcher(value.matchDate(format)) + return value + } + + /** + * Value that must match the provided date format + * @param format date format to match + * @param example example date to use for generated values + */ + @JvmStatic + fun date(format: String, example: Date): PactDslJsonRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslJsonRootValue() + value.value = instance.format(example) + value.setMatcher(value.matchDate(format)) + return value + } + + /** + * Value that must match the given time format + * @param format time format to match + */ + @JvmOverloads + @JvmStatic + fun time(format: String = DateFormatUtils.ISO_TIME_FORMAT.pattern): PactDslJsonRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", TimeGenerator(format)) + value.value = instance.format(Date(DATE_2000)) + value.setMatcher(value.matchTime(format)) + return value + } + + /** + * Value that must match the given time format + * @param format time format to match + * @param example example time to use for generated bodies + */ + @JvmStatic + fun time(format: String, example: Date): PactDslJsonRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslJsonRootValue() + value.value = instance.format(example) + value.setMatcher(value.matchTime(format)) + return value + } + + /** + * Value that must be an IP4 address + */ + @JvmStatic + fun ipAddress(): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.value = "127.0.0.1" + value.setMatcher(value.regexp("(\\d{1,3}\\.)+\\d{1,3}")) + return value + } + + /** + * Value that must be a numeric identifier + */ + @JvmStatic + fun id(): PactDslJsonRootValue { + return numberType() + } + + /** + * Value that must be a numeric identifier + * @param id example id to use for generated bodies + */ + @JvmStatic + fun id(id: Long): PactDslJsonRootValue { + return numberType(id) + } + + /** + * Value that must be encoded as a hexadecimal value + */ + @JvmStatic + fun hexValue(): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", RandomHexadecimalGenerator(10)) + value.value = "1234a" + value.setMatcher(value.regexp("[0-9a-fA-F]+")) + return value + } + + /** + * Value that must be encoded as a hexadecimal value + * @param hexValue example value to use for generated bodies + */ + @JvmStatic + fun hexValue(hexValue: String): PactDslJsonRootValue { + if (!hexValue.matches(HEXADECIMAL)) { + throw InvalidMatcherException("Example \"$hexValue\" is not a hexadecimal value") + } + val value = PactDslJsonRootValue() + value.value = hexValue + value.setMatcher(value.regexp("[0-9a-fA-F]+")) + return value + } + + /** + * Value that must be encoded as an UUID + */ + @JvmStatic + fun uuid(): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", UuidGenerator) + value.value = "e2490de5-5bd3-43d5-b7c4-526e33f71304" + value.setMatcher(value.regexp(UUID_REGEX.pattern)) + return value + } + + /** + * Value that must be encoded as an UUID + * @param uuid example UUID to use for generated bodies + */ + @JvmStatic + fun uuid(uuid: UUID): PactDslJsonRootValue { + return uuid(uuid.toString()) + } + + /** + * Value that must be encoded as an UUID + * @param uuid example UUID to use for generated bodies + */ + @JvmStatic + fun uuid(uuid: String): PactDslJsonRootValue { + if (!uuid.matches(UUID_REGEX)) { + throw InvalidMatcherException("Example \"$uuid\" is not an UUID") + } + val value = PactDslJsonRootValue() + value.value = uuid + value.setMatcher(value.regexp(UUID_REGEX.pattern)) + return value + } + + /** + * Combine all the matchers using AND + * @param example Attribute example value + * @param rules Matching rules to apply + */ + @JvmStatic + fun and(example: Any?, vararg rules: MatchingRule): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + if (example != null) { + value.value = example + } else { + value.value = JSONObject.NULL + } + value.matchers.setRules("", MatchingRuleGroup(mutableListOf(*rules), RuleLogic.AND)) + return value + } + + /** + * Combine all the matchers using OR + * @param example Attribute name + * @param rules Matching rules to apply + */ + @JvmStatic + fun or(example: Any?, vararg rules: MatchingRule): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + if (example != null) { + value.value = example + } else { + value.value = JSONObject.NULL + } + value.matchers.setRules("", MatchingRuleGroup(mutableListOf(*rules), RuleLogic.OR)) + return value + } + + /** + * Adds a value that will have it's value injected from the provider state + * @param expression Expression to be evaluated from the provider state + * @param example Example value to be used in the consumer test + */ + @JvmStatic + fun valueFromProviderState(expression: String, example: Any?): PactDslJsonRootValue { + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", ProviderStateGenerator(expression, from(example))) + value.value = example + value.setMatcher(TypeMatcher) + return value + } + + /** + * Date value generated from an expression. + * @param expression Date expression + * @param format Date format to use + */ + @JvmOverloads + @JvmStatic + fun dateExpression( + expression: String, + format: String = DateFormatUtils.ISO_DATE_FORMAT.pattern + ): PactDslJsonRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", DateGenerator(format, expression)) + value.value = instance.format(Date(DATE_2000)) + value.setMatcher(value.matchDate(format)) + return value + } + + /** + * Time value generated from an expression. + * @param expression Time expression + * @param format Time format to use + */ + @JvmOverloads + @JvmStatic + fun timeExpression( + expression: String, + format: String = DateFormatUtils.ISO_TIME_NO_T_FORMAT.pattern + ): PactDslJsonRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", TimeGenerator(format, expression)) + value.value = instance.format(Date(DATE_2000)) + value.setMatcher(value.matchTime(format)) + return value + } + + /** + * Datetime value generated from an expression. + * @param expression Datetime expression + * @param format Datetime format to use + */ + @JvmOverloads + @JvmStatic + fun datetimeExpression( + expression: String, + format: String = DateFormatUtils.ISO_DATETIME_FORMAT.pattern + ): PactDslJsonRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslJsonRootValue() + value.generators.addGenerator(Category.BODY, "", DateTimeGenerator(format, expression)) + value.value = instance.format(Date(DATE_2000)) + value.setMatcher(value.matchTimestamp(format)) + return value + } + } +} diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRootValue.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRootValue.kt new file mode 100644 index 000000000..6cc36c078 --- /dev/null +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslRootValue.kt @@ -0,0 +1,688 @@ +package au.com.dius.pact.consumer.dsl + +import au.com.dius.pact.consumer.InvalidMatcherException +import au.com.dius.pact.core.model.generators.Category +import au.com.dius.pact.core.model.generators.DateGenerator +import au.com.dius.pact.core.model.generators.DateTimeGenerator +import au.com.dius.pact.core.model.generators.ProviderStateGenerator +import au.com.dius.pact.core.model.generators.RandomDecimalGenerator +import au.com.dius.pact.core.model.generators.RandomHexadecimalGenerator +import au.com.dius.pact.core.model.generators.RandomIntGenerator +import au.com.dius.pact.core.model.generators.RandomStringGenerator +import au.com.dius.pact.core.model.generators.TimeGenerator +import au.com.dius.pact.core.model.generators.UuidGenerator +import au.com.dius.pact.core.model.matchingrules.MatchingRule +import au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup +import au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher +import au.com.dius.pact.core.model.matchingrules.RuleLogic +import au.com.dius.pact.core.model.matchingrules.TypeMatcher +import au.com.dius.pact.core.support.Json.toJson +import au.com.dius.pact.core.support.expressions.DataType.Companion.from +import au.com.dius.pact.core.support.json.JsonValue +import org.apache.commons.lang3.time.DateFormatUtils +import org.apache.commons.lang3.time.FastDateFormat +import org.json.JSONObject +import java.math.BigDecimal +import java.util.Date +import java.util.UUID + +/** + * Matcher to create a plain root matching strategy. Used with text/plain to match regex responses + */ +@Suppress("TooManyFunctions", "SpreadOperator") +open class PactDslRootValue : DslPart("", "") { + override var body: JsonValue = JsonValue.Null + + override fun putObjectPrivate(obj: DslPart) { + throw UnsupportedOperationException() + } + + override fun putArrayPrivate(obj: DslPart) { + throw UnsupportedOperationException() + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun array(name: String): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun array(): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun closeArray(): DslPart? { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(name: String): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(name: String, obj: DslPart): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(name: String, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachLike(obj: DslPart): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(name: String, size: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(size: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(name: String, size: Int, obj: DslPart): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(size: Int, obj: DslPart): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(name: String, size: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minArrayLike(size: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(name: String, size: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(size: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(name: String, size: Int, obj: DslPart): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(size: Int, obj: DslPart): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(name: String, size: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun maxArrayLike(size: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(name: String, minSize: Int, maxSize: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(name: String, minSize: Int, maxSize: Int, obj: DslPart): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(minSize: Int, maxSize: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(minSize: Int, maxSize: Int, obj: DslPart): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(name: String, minSize: Int, maxSize: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun minMaxArrayLike(minSize: Int, maxSize: Int, numberExamples: Int): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonBody for objects") + override fun `object`(name: String): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS) + } + + @Deprecated("Use PactDslJsonBody for objects") + override fun `object`(): PactDslJsonBody { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS) + } + + @Deprecated("Use PactDslJsonBody for objects") + override fun closeObject(): DslPart? { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_BODY_FOR_OBJECTS) + } + + override fun close(): DslPart { + matchers.applyMatcherRootPrefix("$") + generators.applyRootPrefix("$") + return this + } + + fun setValue(value: Any?) { + body = toJson(value) + } + + fun setMatcher(matcher: MatchingRule) { + matchers.addRule(matcher) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayLike(name: String): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayLike(numberExamples: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMaxLike(name: String, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMaxLike(size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMaxLike(name: String, numberExamples: Int, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMaxLike(numberExamples: Int, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinLike(name: String, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinLike(size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinLike(name: String, numberExamples: Int, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinLike(numberExamples: Int, size: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinMaxLike(name: String, minSize: Int, maxSize: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinMaxLike(minSize: Int, maxSize: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinMaxLike( + name: String, + numberExamples: Int, + minSize: Int, + maxSize: Int + ): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayWithMinMaxLike(numberExamples: Int, minSize: Int, maxSize: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayLike(name: String, numberExamples: Int): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + @Deprecated("Use PactDslJsonArray for arrays") + override fun eachArrayLike(): PactDslJsonArray { + throw UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS) + } + + override fun matchUrl(name: String, basePath: String?, vararg pathFragments: Any): DslPart { + throw UnsupportedOperationException("matchUrl is not currently supported for PactDslRootValue") + } + + override fun matchUrl(basePath: String?, vararg pathFragments: Any): DslPart { + throw UnsupportedOperationException("matchUrl is not currently supported for PactDslRootValue") + } + + override fun matchUrl2(name: String, vararg pathFragments: Any): DslPart { + throw UnsupportedOperationException("matchUrl2 is not currently supported for PactDslRootValue") + } + + override fun matchUrl2(vararg pathFragments: Any): DslPart { + throw UnsupportedOperationException("matchUrl2 is not currently supported for PactDslRootValue") + } + + companion object { + private const val USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS = "Use PactDslJsonArray for arrays" + private const val USE_PACT_DSL_JSON_BODY_FOR_OBJECTS = "Use PactDslJsonBody for objects" + + /** + * Value that can be any string + */ + @JvmStatic + fun stringType(): PactDslRootValue { + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", RandomStringGenerator(20)) + value.setValue("string") + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that can be any string + * + * @param example example value to use for generated bodies + */ + @JvmStatic + fun stringType(example: String): PactDslRootValue { + val value = PactDslRootValue() + value.setValue(example) + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that can be any number + */ + @JvmStatic + fun numberType(): PactDslRootValue { + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", RandomIntGenerator(0, Int.MAX_VALUE)) + value.setValue(100) + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that can be any number + * @param number example number to use for generated bodies + */ + @JvmStatic + fun numberType(number: Number): PactDslRootValue { + val value = PactDslRootValue() + value.setValue(number) + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that must be an integer + */ + @JvmStatic + fun integerType(): PactDslRootValue { + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", RandomIntGenerator(0, Int.MAX_VALUE)) + value.setValue(100) + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)) + return value + } + + /** + * Value that must be an integer + * @param number example integer value to use for generated bodies + */ + @JvmStatic + fun integerType(number: Long): PactDslRootValue { + val value = PactDslRootValue() + value.setValue(number) + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)) + return value + } + + /** + * Value that must be an integer + * @param number example integer value to use for generated bodies + */ + @JvmStatic + fun integerType(number: Int): PactDslRootValue { + val value = PactDslRootValue() + value.setValue(number) + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.INTEGER)) + return value + } + + /** + * Value that must be a decimal value + */ + @JvmStatic + fun decimalType(): PactDslRootValue { + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", RandomDecimalGenerator(10)) + value.setValue(100) + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)) + return value + } + + /** + * Value that must be a decimalType value + * @param number example decimalType value + */ + @JvmStatic + fun decimalType(number: BigDecimal): PactDslRootValue { + val value = PactDslRootValue() + value.setValue(number) + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)) + return value + } + + /** + * Value that must be a decimalType value + * @param number example decimalType value + */ + @JvmStatic + fun decimalType(number: Double): PactDslRootValue { + val value = PactDslRootValue() + value.setValue(number) + value.setMatcher(NumberTypeMatcher(NumberTypeMatcher.NumberType.DECIMAL)) + return value + } + + /** + * Value that must be a boolean + * @param example example boolean to use for generated bodies + */ + @JvmOverloads + @JvmStatic + fun booleanType(example: Boolean = true): PactDslRootValue { + val value = PactDslRootValue() + value.setValue(example) + value.setMatcher(TypeMatcher) + return value + } + + /** + * Value that must match the regular expression + * @param regex regular expression + * @param value example value to use for generated bodies + */ + @JvmStatic + fun stringMatcher(regex: String, value: String): PactDslRootValue { + if (!value.matches(Regex(regex))) { + throw InvalidMatcherException("Example \"$value\" does not match regular expression \"$regex\"") + } + val rootValue = PactDslRootValue() + rootValue.setValue(value) + rootValue.setMatcher(rootValue.regexp(regex)) + return rootValue + } + + /** + * Value that must match the given timestamp format + * @param format timestamp format + */ + @JvmOverloads + @JvmStatic + fun timestamp(format: String = DateFormatUtils.ISO_DATETIME_FORMAT.pattern): PactDslRootValue { + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", DateTimeGenerator(format)) + val instance = FastDateFormat.getInstance(format) + value.setValue(instance.format(Date(DATE_2000))) + value.setMatcher(value.matchTimestamp(format)) + return value + } + + /** + * Value that must match the given timestamp format + * @param format timestamp format + * @param example example date and time to use for generated bodies + */ + @JvmStatic + fun timestamp(format: String, example: Date): PactDslRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslRootValue() + value.setValue(instance.format(example)) + value.setMatcher(value.matchTimestamp(format)) + return value + } + + /** + * Value that must match the provided date format + * @param format date format to match + */ + @JvmOverloads + @JvmStatic + fun date(format: String = DateFormatUtils.ISO_DATE_FORMAT.pattern): PactDslRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", DateGenerator(format)) + value.setValue(instance.format(Date(DATE_2000))) + value.setMatcher(value.matchDate(format)) + return value + } + + /** + * Value that must match the provided date format + * @param format date format to match + * @param example example date to use for generated values + */ + @JvmStatic + fun date(format: String, example: Date): PactDslRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslRootValue() + value.setValue(instance.format(example)) + value.setMatcher(value.matchDate(format)) + return value + } + + /** + * Value that must match the given time format + * @param format time format to match + */ + @JvmOverloads + @JvmStatic + fun time(format: String = DateFormatUtils.ISO_TIME_FORMAT.pattern): PactDslRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", TimeGenerator(format)) + value.setValue(instance.format(Date(DATE_2000))) + value.setMatcher(value.matchTime(format)) + return value + } + + /** + * Value that must match the given time format + * @param format time format to match + * @param example example time to use for generated bodies + */ + @JvmStatic + fun time(format: String, example: Date): PactDslRootValue { + val instance = FastDateFormat.getInstance(format) + val value = PactDslRootValue() + value.setValue(instance.format(example)) + value.setMatcher(value.matchTime(format)) + return value + } + + /** + * Value that must be an IP4 address + */ + @JvmStatic + fun ipAddress(): PactDslRootValue { + val value = PactDslRootValue() + value.setValue("127.0.0.1") + value.setMatcher(value.regexp("(\\d{1,3}\\.)+\\d{1,3}")) + return value + } + + /** + * Value that must be a numeric identifier + */ + @JvmStatic + fun id(): PactDslRootValue { + return numberType() + } + + /** + * Value that must be a numeric identifier + * @param id example id to use for generated bodies + */ + @JvmStatic + fun id(id: Long): PactDslRootValue { + return numberType(id) + } + + /** + * Value that must be encoded as a hexadecimal value + */ + @JvmStatic + fun hexValue(): PactDslRootValue { + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", RandomHexadecimalGenerator(10)) + value.setValue("1234a") + value.setMatcher(value.regexp("[0-9a-fA-F]+")) + return value + } + + /** + * Value that must be encoded as a hexadecimal value + * @param hexValue example value to use for generated bodies + */ + @JvmStatic + fun hexValue(hexValue: String): PactDslRootValue { + if (!hexValue.matches(HEXADECIMAL)) { + throw InvalidMatcherException("Example \"$hexValue\" is not a hexadecimal value") + } + val value = PactDslRootValue() + value.setValue(hexValue) + value.setMatcher(value.regexp("[0-9a-fA-F]+")) + return value + } + + /** + * Value that must be encoded as an UUID + */ + @JvmStatic + fun uuid(): PactDslRootValue { + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", UuidGenerator) + value.setValue("e2490de5-5bd3-43d5-b7c4-526e33f71304") + value.setMatcher(value.regexp(UUID_REGEX.pattern)) + return value + } + + /** + * Value that must be encoded as an UUID + * @param uuid example UUID to use for generated bodies + */ + @JvmStatic + fun uuid(uuid: UUID): PactDslRootValue { + return uuid(uuid.toString()) + } + + /** + * Value that must be encoded as an UUID + * @param uuid example UUID to use for generated bodies + */ + @JvmStatic + fun uuid(uuid: String): PactDslRootValue { + if (!uuid.matches(UUID_REGEX)) { + throw InvalidMatcherException("Example \"$uuid\" is not an UUID") + } + val value = PactDslRootValue() + value.setValue(uuid) + value.setMatcher(value.regexp(UUID_REGEX.pattern)) + return value + } + + /** + * Combine all the matchers using AND + * @param example Attribute example value + * @param rules Matching rules to apply + */ + @JvmStatic + fun and(example: Any?, vararg rules: MatchingRule): PactDslRootValue { + val value = PactDslRootValue() + if (example != null) { + value.setValue(example) + } else { + value.setValue(JSONObject.NULL) + } + value.matchers.setRules("", MatchingRuleGroup(mutableListOf(*rules), RuleLogic.AND)) + return value + } + + /** + * Combine all the matchers using OR + * @param example Attribute name + * @param rules Matching rules to apply + */ + @JvmStatic + fun or(example: Any?, vararg rules: MatchingRule): PactDslRootValue { + val value = PactDslRootValue() + if (example != null) { + value.setValue(example) + } else { + value.setValue(JSONObject.NULL) + } + value.matchers.setRules("", MatchingRuleGroup(mutableListOf(*rules), RuleLogic.OR)) + return value + } + + /** + * Adds a value that will have it's value injected from the provider state + * @param expression Expression to be evaluated from the provider state + * @param example Example value to be used in the consumer test + */ + @JvmStatic + fun valueFromProviderState(expression: String, example: Any?): PactDslRootValue { + val value = PactDslRootValue() + value.generators.addGenerator(Category.BODY, "", ProviderStateGenerator(expression, from(example))) + value.setValue(example) + return value + } + } +}