/** * 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 boolean */ public static PactDslJsonRootValue booleanType() { return booleanType(true); }
/** * 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 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 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 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; }
/** * 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.matchers.setRules("", new MatchingRuleGroup(Arrays.asList(rules), RuleLogic.OR)); return value; }
@Override public DslPart close() { getMatchers().applyMatcherRootPrefix("$"); getGenerators().applyRootPrefix("$"); return this; }
/** * 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 formatted as an ISO date */ public static PactDslJsonRootValue date() { return date(DateFormatUtils.ISO_DATE_FORMAT.getPattern()); }
/** * Accepts any key, and each key is mapped to a map that must match the provided object definition * Note: this needs the Java system property "pact.matching.wildcard" set to value "true" when the pact file is verified. * @param exampleKey Example key to use for generating bodies * @param value Value to use for matching and generated bodies */ public PactDslJsonBody eachKeyLike(String exampleKey, PactDslJsonRootValue value) { body.put(exampleKey, value.getBody()); if (FeatureToggles.isFeatureSet(Feature.UseMatchValuesMatcher)) { matchers.addRule(rootPath.endsWith(".") ? rootPath.substring(0, rootPath.length() - 1) : rootPath, ValuesMatcher.INSTANCE); } for(String matcherName: value.matchers.getMatchingRules().keySet()) { matchers.addRules(rootPath + "*" + matcherName, value.matchers.getMatchingRules().get(matcherName).getRules()); } return this; }
/** * 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 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 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; }
/** * 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.matchers.setRules("", new MatchingRuleGroup(Arrays.asList(rules), RuleLogic.OR)); return value; }
@Override public DslPart close() { getMatchers().applyMatcherRootPrefix("$"); getGenerators().applyRootPrefix("$"); return this; }
/** * 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 formatted as an ISO date */ public static PactDslJsonRootValue date() { return date(DateFormatUtils.ISO_DATE_FORMAT.getPattern()); }
/** * Accepts any key, and each key is mapped to a map that must match the provided object definition * Note: this needs the Java system property "pact.matching.wildcard" set to value "true" when the pact file is verified. * @param exampleKey Example key to use for generating bodies * @param value Value to use for matching and generated bodies */ public PactDslJsonBody eachKeyLike(String exampleKey, PactDslJsonRootValue value) { body.put(exampleKey, value.getBody()); if (FeatureToggles.isFeatureSet(Feature.UseMatchValuesMatcher)) { matchers.addRule(rootPath.endsWith(".") ? rootPath.substring(0, rootPath.length() - 1) : rootPath, ValuesMatcher.INSTANCE); } for(String matcherName: value.matchers.getMatchingRules().keySet()) { matchers.addRules(rootPath + "*" + matcherName, value.matchers.getMatchingRules().get(matcherName).getRules()); } return this; }
/** * 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; }