Skip to content

Commit

Permalink
fix: Deprecate eachKeyLike methods as they actually act on the values #…
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Aug 7, 2024
1 parent cff891d commit 3030270
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package au.com.dius.pact.consumer.junit5;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.PactDslJsonRootValue;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.core.model.PactSpecVersion;
import au.com.dius.pact.core.model.V4Pact;
import au.com.dius.pact.core.model.annotations.Pact;
import org.apache.hc.client5.http.fluent.Request;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;

import static au.com.dius.pact.consumer.dsl.LambdaDsl.newJsonBody;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

// Issue #1813
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "eachkeylike_provider", pactVersion = PactSpecVersion.V4)
public class EachKeyLikeTest {

@Pact(consumer="eachkeylike__consumer")
public V4Pact createFragment(PactDslWithProvider builder) {
return builder
.uponReceiving("A request")
.path("/")
.method("POST")
.body(newJsonBody(body ->
body.object("a", aObj -> {
aObj.eachKeyLike("prop1", PactDslJsonRootValue.stringMatcher("prop\\d+", "prop1"));
aObj.eachKeyLike("prop1", propObj -> propObj.stringType("value", "x"));
})).build())
.willRespondWith()
.status(200)
.toPact(V4Pact.class);
}

@Test
void runTest(MockServer mockServer) throws IOException {
String json = "{\n" +
" \"a\": {\n" +
" \"prop1\": {\n" +
" \"value\": \"x\"\n" +
" },\n" +
" \"prop\": {\n" +
" \"value\": \"y\"\n" +
" }\n" +
" }\n" +
"}";
ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.post(mockServer.getUrl())
.body(new StringEntity(json, ContentType.APPLICATION_JSON))
.execute()
.returnResponse();
assertThat(httpResponse.getCode(), is(200));
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package au.com.dius.pact.consumer.dsl;

import au.com.dius.pact.core.matchers.UrlMatcherSupport;
import au.com.dius.pact.core.model.matchingrules.MatchingRule;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;
import java.util.function.Consumer;

import static au.com.dius.pact.consumer.dsl.Dsl.matcherKey;

public class LambdaDslObject {

private final PactDslJsonBody object;
Expand Down Expand Up @@ -949,8 +945,31 @@ public LambdaDslObject eachKeyMappedToAnArrayLike(String exampleKey, Consumer<La
* Accepts any key, and each key is mapped to a map that must match the following object definition.
*
* @param exampleKey Example key to use for generating bodies
* @deprecated Use eachValueLike instead
*/
@Deprecated
public LambdaDslObject eachKeyLike(String exampleKey, Consumer<LambdaDslObject> nestedObject) {
return eachValueLike(exampleKey, nestedObject);
}

/**
* Accepts any key, and each key is mapped to a map that must match the provided object definition
*
* @param exampleKey Example key to use for generating bodies
* @param value Value to use for matching and generated bodies
* @deprecated Use eachValueLike instead
*/
@Deprecated
public LambdaDslObject eachKeyLike(String exampleKey, PactDslJsonRootValue value) {
return eachValueLike(exampleKey, value);
}

/**
* Accepts any key in a map, and each key is mapped to a value that must match the following object definition.
*
* @param exampleKey Example key to use for generating bodies
*/
public LambdaDslObject eachValueLike(String exampleKey, Consumer<LambdaDslObject> nestedObject) {
final PactDslJsonBody objectLike = object.eachKeyLike(exampleKey);
final LambdaDslObject dslObject = new LambdaDslObject(objectLike);
nestedObject.accept(dslObject);
Expand All @@ -959,12 +978,12 @@ public LambdaDslObject eachKeyLike(String exampleKey, Consumer<LambdaDslObject>
}

/**
* Accepts any key, and each key is mapped to a map that must match the provided object definition
* Accepts any key, and each key is mapped to a value that must match the provided object definition
*
* @param exampleKey Example key to use for generating bodies
* @param value Value to use for matching and generated bodies
*/
public LambdaDslObject eachKeyLike(String exampleKey, PactDslJsonRootValue value) {
public LambdaDslObject eachValueLike(String exampleKey, PactDslJsonRootValue value) {
object.eachKeyLike(exampleKey, value);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,26 @@ open class PactDslJsonBody : DslPart {
* Accepts any key, and each key is mapped to a map that must match the following object definition
* @param exampleKey Example key to use for generating bodies
*/
@Deprecated("Use eachValueLike instead", ReplaceWith("eachValueLike(exampleKey)"))
fun eachKeyLike(exampleKey: String): PactDslJsonBody {
return eachValueLike(exampleKey)
}

/**
* Accepts any key, and each key is mapped to a map that must match the provided object definition
* @param exampleKey Example key to use for generating bodies
* @param value Value to use for matching and generated bodies
*/
@Deprecated("Use eachValueLike instead", ReplaceWith("eachValueLike(exampleKey, value)"))
fun eachKeyLike(exampleKey: String, value: PactDslJsonRootValue): PactDslJsonBody {
return eachValueLike(exampleKey, value)
}

/**
* Accepts any key, and each key is mapped to a value that must match the following object definition
* @param exampleKey Example key to use for generating bodies
*/
fun eachValueLike(exampleKey: String): PactDslJsonBody {
matchers.addRule(
if (rootPath.endsWith(".")) rootPath.substring(0, rootPath.length - 1) else rootPath, ValuesMatcher
)
Expand All @@ -1820,7 +1839,7 @@ open class PactDslJsonBody : DslPart {
* @param exampleKey Example key to use for generating bodies
* @param value Value to use for matching and generated bodies
*/
fun eachKeyLike(exampleKey: String, value: PactDslJsonRootValue): PactDslJsonBody {
fun eachValueLike(exampleKey: String, value: PactDslJsonRootValue): PactDslJsonBody {
when (val body = body) {
is JsonValue.Object -> body.add(exampleKey, value.body)
is JsonValue.Array -> {
Expand Down

0 comments on commit 3030270

Please sign in to comment.