Skip to content

Commit 7ab70a9

Browse files
committedFeb 27, 2020
Fix off-by-one valueFromProviderState index
`PactDslJsonArray.valueFromProviderState()` assigns an index one less than it should. Before appending to the `body` array, the method invokes `appendArrayIndex`, which in turn emits an array index value of `body.length()-1`. Since the `body` array is yet to be updated the calculated index is off by one (the most obvious sign of this is a negative index of `-1`). This PR moves the append to `body` first.
1 parent 7e81778 commit 7ab70a9

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed
 

‎consumer/pact-jvm-consumer-junit/src/test/java/au/com/dius/pact/consumer/MatcherTestUtils.java

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package au.com.dius.pact.consumer;
22

3+
import au.com.dius.pact.core.model.PactSpecVersion;
34
import au.com.dius.pact.core.model.RequestResponsePact;
5+
import au.com.dius.pact.core.model.generators.Generators;
46
import au.com.dius.pact.core.model.matchingrules.MatchingRuleGroup;
57
import au.com.dius.pact.core.model.matchingrules.MatchingRules;
68
import au.com.dius.pact.core.model.messaging.MessagePact;
@@ -37,6 +39,13 @@ public static void assertResponseMatcherKeysEqualTo(RequestResponsePact pact, St
3739
assertEquals(asSet(matcherKeys), new TreeSet<>(matchers.keySet()));
3840
}
3941

42+
@SuppressWarnings("unchecked")
43+
public static void assertResponseGeneratorKeysEqualTo(RequestResponsePact pact, String category, String... matcherKeys) {
44+
Generators generators = pact.getInteractions().get(0).getResponse().getGenerators();
45+
Map<String, Object> categoryMap = (Map<String, Object>) generators.toMap(PactSpecVersion.V3).get(category);
46+
assertEquals(asSet(matcherKeys), new TreeSet<>(categoryMap.keySet()));
47+
}
48+
4049
public static void assertResponseKeysEqualTo(RequestResponsePact pact, String... keys) {
4150
String body = pact.getInteractions().get(0).getResponse().getBody().valueAsString();
4251
Map hashMap = null;

‎consumer/pact-jvm-consumer-junit/src/test/java/au/com/dius/pact/consumer/junit/PactDslJsonArrayTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ protected RequestResponsePact createPact(PactDslWithProvider builder) {
2727
.stringValue("name", "Cat in the Hat")
2828
.timestamp()
2929
.date("dob", "MM/dd/yyyy")
30+
.array("things")
31+
.valueFromProviderState("thingName", "Thing 1")
32+
.closeArray()
3033
.closeObject();
3134
RequestResponsePact pact = builder
3235
.uponReceiving("java test interaction with a DSL array body")
@@ -46,9 +49,19 @@ protected RequestResponsePact createPact(PactDslWithProvider builder) {
4649
"$[2].v1",
4750
"$[3].id",
4851
"$[3].timestamp",
49-
"$[3].dob"
52+
"$[3].dob",
53+
"$[3].things[0]"
5054
);
5155

56+
MatcherTestUtils.assertResponseGeneratorKeysEqualTo(pact, "body",
57+
"$[2].id",
58+
"$[2].timestamp",
59+
"$[2].dob",
60+
"$[3].id",
61+
"$[3].timestamp",
62+
"$[3].dob",
63+
"$[3].things[0]");
64+
5265
return pact;
5366
}
5467

‎consumer/pact-jvm-consumer/src/main/java/au/com/dius/pact/consumer/dsl/PactDslJsonArray.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1232,8 +1232,8 @@ public PactDslJsonArray eachArrayWithMinMaxLike(int numberExamples, Integer minS
12321232
* @param example Example value to be used in the consumer test
12331233
*/
12341234
public PactDslJsonArray valueFromProviderState(String expression, Object example) {
1235-
generators.addGenerator(Category.BODY, rootPath + appendArrayIndex(0), new ProviderStateGenerator(expression));
12361235
body.put(example);
1236+
generators.addGenerator(Category.BODY, rootPath + appendArrayIndex(0), new ProviderStateGenerator(expression));
12371237
matchers.addRule(rootPath + appendArrayIndex(0), TypeMatcher.INSTANCE);
12381238
return this;
12391239
}

0 commit comments

Comments
 (0)
Please sign in to comment.