Skip to content

Commit

Permalink
feat: add PactDslJsonArray eachLike/minArrayLike/maxArrayLike methods…
Browse files Browse the repository at this point in the history
… that can take a DSLPart #1286
  • Loading branch information
Ronald Holshausen committed Jan 20, 2021
1 parent 37d483e commit 57a8541
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public DslPart(String rootPath, String rootName) {
/**
* Array element where each element of the array must match the provided object
*/
public abstract PactDslJsonBody eachLike(DslPart object);
public abstract PactDslJsonArray eachLike(DslPart object);

/**
* Array field where each element must match the following object
Expand Down Expand Up @@ -118,7 +118,7 @@ public DslPart(String rootPath, String rootName) {
* Array element with a minumum size and each element of the array must match the provided object
* @param size minimum size
*/
public abstract PactDslJsonBody minArrayLike(Integer size, DslPart object);
public abstract PactDslJsonArray minArrayLike(Integer size, DslPart object);

/**
* Array field with a minumum size and each element must match the following object
Expand Down Expand Up @@ -159,7 +159,7 @@ public DslPart(String rootPath, String rootName) {
* Array element with a maximum size and each element of the array must match the provided object
* @param size minimum size
*/
public abstract PactDslJsonBody maxArrayLike(Integer size, DslPart object);
public abstract PactDslJsonArray maxArrayLike(Integer size, DslPart object);

/**
* Array field with a maximum size and each element must match the following object
Expand Down Expand Up @@ -204,7 +204,7 @@ public DslPart(String rootPath, String rootName) {
* @param minSize minimum size
* @param maxSize maximum size
*/
public abstract PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object);
public abstract PactDslJsonArray minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object);

/**
* Array field with a minimum and maximum size and each element must match the following object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ public PactDslJsonBody eachLike(String name) {

@Override
public PactDslJsonBody eachLike(String name, DslPart object) {
return null;
throw new UnsupportedOperationException("use the eachLike(DslPart object) form");
}

@Override
public PactDslJsonBody eachLike(String name, int numberExamples) {
throw new UnsupportedOperationException("use the eachLike(numberExamples) form");
}
public PactDslJsonBody eachLike(String name, int numberExamples) {
throw new UnsupportedOperationException("use the eachLike(numberExamples) form");
}

/**
* Element that is an array where each item must match the following example
Expand All @@ -125,8 +125,18 @@ public PactDslJsonBody eachLike() {
}

@Override
public PactDslJsonBody eachLike(DslPart object) {
return null;
public PactDslJsonArray eachLike(DslPart object) {
matchers.addRule(rootPath + appendArrayIndex(1), matchMin(0));
PactDslJsonArray parent = new PactDslJsonArray(rootPath, "", this, true);
parent.setNumberExamples(numberExamples);

if (object instanceof PactDslJsonBody) {
parent.putObject(object);
} else if (object instanceof PactDslJsonArray) {
parent.putArray(object);
}

return parent.closeArray().asArray();
}

/**
Expand Down Expand Up @@ -157,12 +167,22 @@ public PactDslJsonBody minArrayLike(Integer size) {

@Override
public PactDslJsonBody minArrayLike(String name, Integer size, DslPart object) {
return null;
throw new UnsupportedOperationException("use the minArrayLike(Integer size, DslPart object) form");
}

@Override
public PactDslJsonBody minArrayLike(Integer size, DslPart object) {
return null;
public PactDslJsonArray minArrayLike(Integer size, DslPart object) {
matchers.addRule(rootPath + appendArrayIndex(1), matchMin(size));
PactDslJsonArray parent = new PactDslJsonArray(rootPath, "", this, true);
parent.setNumberExamples(size);

if (object instanceof PactDslJsonBody) {
parent.putObject(object);
} else if (object instanceof PactDslJsonArray) {
parent.putArray(object);
}

return parent.closeArray().asArray();
}

@Override
Expand Down Expand Up @@ -203,12 +223,21 @@ public PactDslJsonBody maxArrayLike(Integer size) {

@Override
public PactDslJsonBody maxArrayLike(String name, Integer size, DslPart object) {
return null;
throw new UnsupportedOperationException("use the maxArrayLike(Integer size, DslPart object) form");
}

@Override
public PactDslJsonBody maxArrayLike(Integer size, DslPart object) {
return null;
public PactDslJsonArray maxArrayLike(Integer size, DslPart object) {
matchers.addRule(rootPath + appendArrayIndex(1), matchMax(size));
PactDslJsonArray parent = new PactDslJsonArray(rootPath, "", this, true);

if (object instanceof PactDslJsonBody) {
parent.putObject(object);
} else if (object instanceof PactDslJsonArray) {
parent.putArray(object);
}

return parent.closeArray().asArray();
}

@Override
Expand Down Expand Up @@ -1132,7 +1161,7 @@ public PactDslJsonBody minMaxArrayLike(String name, Integer minSize, Integer max

@Override
public PactDslJsonBody minMaxArrayLike(String name, Integer minSize, Integer maxSize, DslPart object) {
return null;
throw new UnsupportedOperationException("use the minMaxArrayLike(minSize, maxSize, object) form");
}

@Override
Expand All @@ -1141,8 +1170,18 @@ public PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize) {
}

@Override
public PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
return null;
public PactDslJsonArray minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
matchers.addRule(rootPath + appendArrayIndex(1), matchMinMax(minSize, maxSize));
PactDslJsonArray parent = new PactDslJsonArray(rootPath, "", this, true);
parent.setNumberExamples(minSize);

if (object instanceof PactDslJsonBody) {
parent.putObject(object);
} else if (object instanceof PactDslJsonArray) {
parent.putArray(object);
}

return parent.closeArray().asArray();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public PactDslJsonBody eachLike() {
}

@Override
public PactDslJsonBody eachLike(DslPart object) {
public PactDslJsonArray eachLike(DslPart object) {
throw new UnsupportedOperationException("use the eachLike(String name, DslPart object) form");
}

Expand Down Expand Up @@ -831,7 +831,7 @@ public PactDslJsonBody minArrayLike(String name, Integer size, DslPart object) {
}

@Override
public PactDslJsonBody minArrayLike(Integer size, DslPart object) {
public PactDslJsonArray minArrayLike(Integer size, DslPart object) {
throw new UnsupportedOperationException("use the minArrayLike(String name, Integer size, DslPart object) form");
}

Expand Down Expand Up @@ -918,7 +918,7 @@ public PactDslJsonBody maxArrayLike(String name, Integer size, DslPart object) {
}

@Override
public PactDslJsonBody maxArrayLike(Integer size, DslPart object) {
public PactDslJsonArray maxArrayLike(Integer size, DslPart object) {
throw new UnsupportedOperationException("use the maxArrayLike(String name, Integer size, DslPart object) form");
}

Expand Down Expand Up @@ -1284,7 +1284,7 @@ public PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize) {
}

@Override
public PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
public PactDslJsonArray minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
throw new UnsupportedOperationException("use the minMaxArrayLike(String name, Integer minSize, Integer maxSize, DslPart object) form");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public PactDslJsonBody eachLike() {

@Override
@Deprecated
public PactDslJsonBody eachLike(DslPart object) {
public PactDslJsonArray eachLike(DslPart object) {
throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS);
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public PactDslJsonBody minArrayLike(String name, Integer size, DslPart object) {

@Override
@Deprecated
public PactDslJsonBody minArrayLike(Integer size, DslPart object) {
public PactDslJsonArray minArrayLike(Integer size, DslPart object) {
throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS);
}

Expand Down Expand Up @@ -225,7 +225,7 @@ public PactDslJsonBody maxArrayLike(String name, Integer size, DslPart object) {

@Override
@Deprecated
public PactDslJsonBody maxArrayLike(Integer size, DslPart object) {
public PactDslJsonArray maxArrayLike(Integer size, DslPart object) {
throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS);
}

Expand Down Expand Up @@ -273,7 +273,7 @@ public PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize) {

@Override
@Deprecated
public PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
public PactDslJsonArray minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public PactDslJsonBody eachLike() {
}

@Override
public PactDslJsonBody eachLike(DslPart object) {
public PactDslJsonArray eachLike(DslPart object) {
throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS);
}

Expand All @@ -147,7 +147,7 @@ public PactDslJsonBody minArrayLike(String name, Integer size, DslPart object) {
}

@Override
public PactDslJsonBody minArrayLike(Integer size, DslPart object) {
public PactDslJsonArray minArrayLike(Integer size, DslPart object) {
throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS);
}

Expand Down Expand Up @@ -189,7 +189,7 @@ public PactDslJsonBody maxArrayLike(String name, Integer size, DslPart object) {
}

@Override
public PactDslJsonBody maxArrayLike(Integer size, DslPart object) {
public PactDslJsonArray maxArrayLike(Integer size, DslPart object) {
throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS);
}

Expand Down Expand Up @@ -231,7 +231,7 @@ public PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize) {
}

@Override
public PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
public PactDslJsonArray minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
throw new UnsupportedOperationException(USE_PACT_DSL_JSON_ARRAY_FOR_ARRAYS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DslPartSpec extends Specification {
PactDslJsonBody eachLike() { null }

@Override
PactDslJsonBody eachLike(DslPart object) {
PactDslJsonArray eachLike(DslPart object) {
null
}

Expand All @@ -60,7 +60,7 @@ class DslPartSpec extends Specification {
}

@Override
PactDslJsonBody minArrayLike(Integer size, DslPart object) {
PactDslJsonArray minArrayLike(Integer size, DslPart object) {
null
}

Expand All @@ -82,7 +82,7 @@ class DslPartSpec extends Specification {
}

@Override
PactDslJsonBody maxArrayLike(Integer size, DslPart object) {
PactDslJsonArray maxArrayLike(Integer size, DslPart object) {
null
}

Expand Down Expand Up @@ -156,7 +156,7 @@ class DslPartSpec extends Specification {
}

@Override
PactDslJsonBody minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
PactDslJsonArray minMaxArrayLike(Integer minSize, Integer maxSize, DslPart object) {
null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,63 @@ class PactDslJsonArraySpec extends Specification {
'$[2]': [type: 'DateTime', format: "yyyy-MM-dd'T'HH:mm:ss", expression: 'today + 1 hour']]]
}

def 'each like with DSLPart'() {
given:
PactDslJsonArray body = new PactDslJsonArray()
.eachLike()
.stringType('messageId', 'test')
.stringType('date', 'test')
.stringType('contractVersion', 'test')
.closeArray()
PactDslJsonBody message = new PactDslJsonBody()
.stringType('messageId', 'test')
.stringType('date', 'test')
.stringType('contractVersion', 'test')
PactDslJsonArray body2 = new PactDslJsonArray()
.eachLike(message)

expect:
body.body.toString() == body2.body.toString()
body.matchers == body2.matchers
}

def 'min like with DSLPart'() {
given:
PactDslJsonArray body = new PactDslJsonArray()
.minArrayLike(1)
.stringType('messageId', 'test')
.stringType('date', 'test')
.stringType('contractVersion', 'test')
.closeArray()
PactDslJsonBody message = new PactDslJsonBody()
.stringType('messageId', 'test')
.stringType('date', 'test')
.stringType('contractVersion', 'test')
PactDslJsonArray body2 = new PactDslJsonArray()
.minArrayLike(1, message)

expect:
body.body.toString() == body2.body.toString()
body.matchers == body2.matchers
}

def 'max like with DSLPart'() {
given:
PactDslJsonArray body = new PactDslJsonArray()
.maxArrayLike(10)
.stringType('messageId', 'test')
.stringType('date', 'test')
.stringType('contractVersion', 'test')
.closeArray()
PactDslJsonBody message = new PactDslJsonBody()
.stringType('messageId', 'test')
.stringType('date', 'test')
.stringType('contractVersion', 'test')
PactDslJsonArray body2 = new PactDslJsonArray()
.maxArrayLike(10, message)

expect:
body.body.toString() == body2.body.toString()
body.matchers == body2.matchers
}
}

0 comments on commit 57a8541

Please sign in to comment.