Skip to content

Commit 3030270

Browse files
committed
fix: Deprecate eachKeyLike methods as they actually act on the values #1813
1 parent cff891d commit 3030270

File tree

3 files changed

+106
-7
lines changed

3 files changed

+106
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package au.com.dius.pact.consumer.junit5;
2+
3+
import au.com.dius.pact.consumer.MockServer;
4+
import au.com.dius.pact.consumer.dsl.PactDslJsonRootValue;
5+
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
6+
import au.com.dius.pact.core.model.PactSpecVersion;
7+
import au.com.dius.pact.core.model.V4Pact;
8+
import au.com.dius.pact.core.model.annotations.Pact;
9+
import org.apache.hc.client5.http.fluent.Request;
10+
import org.apache.hc.core5.http.ClassicHttpResponse;
11+
import org.apache.hc.core5.http.ContentType;
12+
import org.apache.hc.core5.http.io.entity.StringEntity;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.ExtendWith;
15+
16+
import java.io.IOException;
17+
18+
import static au.com.dius.pact.consumer.dsl.LambdaDsl.newJsonBody;
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
import static org.hamcrest.Matchers.is;
21+
22+
// Issue #1813
23+
@ExtendWith(PactConsumerTestExt.class)
24+
@PactTestFor(providerName = "eachkeylike_provider", pactVersion = PactSpecVersion.V4)
25+
public class EachKeyLikeTest {
26+
27+
@Pact(consumer="eachkeylike__consumer")
28+
public V4Pact createFragment(PactDslWithProvider builder) {
29+
return builder
30+
.uponReceiving("A request")
31+
.path("/")
32+
.method("POST")
33+
.body(newJsonBody(body ->
34+
body.object("a", aObj -> {
35+
aObj.eachKeyLike("prop1", PactDslJsonRootValue.stringMatcher("prop\\d+", "prop1"));
36+
aObj.eachKeyLike("prop1", propObj -> propObj.stringType("value", "x"));
37+
})).build())
38+
.willRespondWith()
39+
.status(200)
40+
.toPact(V4Pact.class);
41+
}
42+
43+
@Test
44+
void runTest(MockServer mockServer) throws IOException {
45+
String json = "{\n" +
46+
" \"a\": {\n" +
47+
" \"prop1\": {\n" +
48+
" \"value\": \"x\"\n" +
49+
" },\n" +
50+
" \"prop\": {\n" +
51+
" \"value\": \"y\"\n" +
52+
" }\n" +
53+
" }\n" +
54+
"}";
55+
ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.post(mockServer.getUrl())
56+
.body(new StringEntity(json, ContentType.APPLICATION_JSON))
57+
.execute()
58+
.returnResponse();
59+
assertThat(httpResponse.getCode(), is(200));
60+
}
61+
}

consumer/src/main/java/au/com/dius/pact/consumer/dsl/LambdaDslObject.java

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package au.com.dius.pact.consumer.dsl;
22

3-
import au.com.dius.pact.core.matchers.UrlMatcherSupport;
43
import au.com.dius.pact.core.model.matchingrules.MatchingRule;
54

65
import java.math.BigDecimal;
76
import java.time.Instant;
87
import java.time.LocalDate;
98
import java.time.ZonedDateTime;
10-
import java.util.Arrays;
119
import java.util.Date;
1210
import java.util.TimeZone;
1311
import java.util.UUID;
1412
import java.util.function.Consumer;
1513

16-
import static au.com.dius.pact.consumer.dsl.Dsl.matcherKey;
17-
1814
public class LambdaDslObject {
1915

2016
private final PactDslJsonBody object;
@@ -949,8 +945,31 @@ public LambdaDslObject eachKeyMappedToAnArrayLike(String exampleKey, Consumer<La
949945
* Accepts any key, and each key is mapped to a map that must match the following object definition.
950946
*
951947
* @param exampleKey Example key to use for generating bodies
948+
* @deprecated Use eachValueLike instead
952949
*/
950+
@Deprecated
953951
public LambdaDslObject eachKeyLike(String exampleKey, Consumer<LambdaDslObject> nestedObject) {
952+
return eachValueLike(exampleKey, nestedObject);
953+
}
954+
955+
/**
956+
* Accepts any key, and each key is mapped to a map that must match the provided object definition
957+
*
958+
* @param exampleKey Example key to use for generating bodies
959+
* @param value Value to use for matching and generated bodies
960+
* @deprecated Use eachValueLike instead
961+
*/
962+
@Deprecated
963+
public LambdaDslObject eachKeyLike(String exampleKey, PactDslJsonRootValue value) {
964+
return eachValueLike(exampleKey, value);
965+
}
966+
967+
/**
968+
* Accepts any key in a map, and each key is mapped to a value that must match the following object definition.
969+
*
970+
* @param exampleKey Example key to use for generating bodies
971+
*/
972+
public LambdaDslObject eachValueLike(String exampleKey, Consumer<LambdaDslObject> nestedObject) {
954973
final PactDslJsonBody objectLike = object.eachKeyLike(exampleKey);
955974
final LambdaDslObject dslObject = new LambdaDslObject(objectLike);
956975
nestedObject.accept(dslObject);
@@ -959,12 +978,12 @@ public LambdaDslObject eachKeyLike(String exampleKey, Consumer<LambdaDslObject>
959978
}
960979

961980
/**
962-
* Accepts any key, and each key is mapped to a map that must match the provided object definition
981+
* Accepts any key, and each key is mapped to a value that must match the provided object definition
963982
*
964983
* @param exampleKey Example key to use for generating bodies
965984
* @param value Value to use for matching and generated bodies
966985
*/
967-
public LambdaDslObject eachKeyLike(String exampleKey, PactDslJsonRootValue value) {
986+
public LambdaDslObject eachValueLike(String exampleKey, PactDslJsonRootValue value) {
968987
object.eachKeyLike(exampleKey, value);
969988
return this;
970989
}

consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt

+20-1
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,26 @@ open class PactDslJsonBody : DslPart {
18081808
* Accepts any key, and each key is mapped to a map that must match the following object definition
18091809
* @param exampleKey Example key to use for generating bodies
18101810
*/
1811+
@Deprecated("Use eachValueLike instead", ReplaceWith("eachValueLike(exampleKey)"))
18111812
fun eachKeyLike(exampleKey: String): PactDslJsonBody {
1813+
return eachValueLike(exampleKey)
1814+
}
1815+
1816+
/**
1817+
* Accepts any key, and each key is mapped to a map that must match the provided object definition
1818+
* @param exampleKey Example key to use for generating bodies
1819+
* @param value Value to use for matching and generated bodies
1820+
*/
1821+
@Deprecated("Use eachValueLike instead", ReplaceWith("eachValueLike(exampleKey, value)"))
1822+
fun eachKeyLike(exampleKey: String, value: PactDslJsonRootValue): PactDslJsonBody {
1823+
return eachValueLike(exampleKey, value)
1824+
}
1825+
1826+
/**
1827+
* Accepts any key, and each key is mapped to a value that must match the following object definition
1828+
* @param exampleKey Example key to use for generating bodies
1829+
*/
1830+
fun eachValueLike(exampleKey: String): PactDslJsonBody {
18121831
matchers.addRule(
18131832
if (rootPath.endsWith(".")) rootPath.substring(0, rootPath.length - 1) else rootPath, ValuesMatcher
18141833
)
@@ -1820,7 +1839,7 @@ open class PactDslJsonBody : DslPart {
18201839
* @param exampleKey Example key to use for generating bodies
18211840
* @param value Value to use for matching and generated bodies
18221841
*/
1823-
fun eachKeyLike(exampleKey: String, value: PactDslJsonRootValue): PactDslJsonBody {
1842+
fun eachValueLike(exampleKey: String, value: PactDslJsonRootValue): PactDslJsonBody {
18241843
when (val body = body) {
18251844
is JsonValue.Object -> body.add(exampleKey, value.body)
18261845
is JsonValue.Array -> {

0 commit comments

Comments
 (0)