diff --git a/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/ProviderStateInjectedPactTest.groovy b/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/ProviderStateInjectedPactTest.groovy index fb5aad549..129b53b07 100644 --- a/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/ProviderStateInjectedPactTest.groovy +++ b/consumer/junit5/src/test/groovy/au/com/dius/pact/consumer/junit5/ProviderStateInjectedPactTest.groovy @@ -24,6 +24,10 @@ class ProviderStateInjectedPactTest { .uponReceiving('a request') .path('/values') .method('POST') + .body( + new PactDslJsonBody() + .valueFromProviderState('userId', 'userId', 100) + ) .willRespondWith() .headerFromProviderState('LOCATION', 'http://server/users/${userId}', 'http://server/users/666') .status(200) @@ -46,7 +50,8 @@ class ProviderStateInjectedPactTest { @Test void testArticles(MockServer mockServer) { HttpResponse httpResponse = Request.post("${mockServer.url}/values") - .bodyString(JsonOutput.toJson([userName: 'Test', userClass: 'Shoddy']), ContentType.APPLICATION_JSON) + .bodyString(JsonOutput.toJson([userId: 12345]), + ContentType.APPLICATION_JSON) .execute().returnResponse() assert httpResponse.code == 200 assert httpResponse.entity.content.text == '{"userId":100,"userName":"Test"}' diff --git a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt index 5a80172f4..2c6d64e52 100755 --- a/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt +++ b/consumer/src/main/kotlin/au/com/dius/pact/consumer/dsl/PactDslJsonBody.kt @@ -2067,7 +2067,7 @@ open class PactDslJsonBody : DslPart { } /** - * Adds an attribute that will have it's value injected from the provider state + * Adds an attribute that will have its value injected from the provider state * @param name Attribute name * @param expression Expression to be evaluated from the provider state * @param example Example value to be used in the consumer test diff --git a/provider/junit5/src/test/java/au/com/dius/pact/provider/junit5/ProviderStateInjectedTest.java b/provider/junit5/src/test/java/au/com/dius/pact/provider/junit5/ProviderStateInjectedTest.java new file mode 100644 index 000000000..7b07343bc --- /dev/null +++ b/provider/junit5/src/test/java/au/com/dius/pact/provider/junit5/ProviderStateInjectedTest.java @@ -0,0 +1,74 @@ +package au.com.dius.pact.provider.junit5; + +import au.com.dius.pact.provider.junitsupport.Provider; +import au.com.dius.pact.provider.junitsupport.State; +import au.com.dius.pact.provider.junitsupport.loader.PactFolder; +import com.github.tomakehurst.wiremock.WireMockServer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ru.lanwen.wiremock.ext.WiremockResolver; +import ru.lanwen.wiremock.ext.WiremockUriResolver; + +import java.math.BigInteger; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import static java.lang.String.format; +import static java.lang.String.valueOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +@Provider("ProviderStateService") +@PactFolder("pacts") +@ExtendWith({ + WiremockResolver.class, + WiremockUriResolver.class +}) +public class ProviderStateInjectedTest { + private static final Logger LOGGER = LoggerFactory.getLogger(ProviderStateInjectedTest.class); + + @TestTemplate + @ExtendWith(PactVerificationInvocationContextProvider.class) + void testTemplate(PactVerificationContext context) { + context.verifyInteraction(); + } + + @BeforeEach + void before(PactVerificationContext context, + @WiremockResolver.Wiremock WireMockServer server, + @WiremockUriResolver.WiremockUri String uri) throws MalformedURLException { + LOGGER.info("BeforeEach - " + uri); + + context.setTarget(HttpTestTarget.fromUrl(new URL(uri))); + + server.stubFor( + post(urlPathEqualTo("/values")) + .willReturn(aResponse() + .withStatus(200) + .withHeader("Location", "http://server/users/666554433") + .withHeader("content-type", "application/json") + .withBody("{\"userId\": 666554433,\"userName\": \"Test\"}") + ) + ); + } + + @State("a provider state with injectable values") + public Map defaultState(Map params) { + LOGGER.info("Default state: " + params); + LOGGER.info("valueB: " + params.get("valueB") + " {" + params.get("valueB").getClass() + "}"); + assertThat(params.get("valueB"), is(equalTo(BigInteger.valueOf(100)))); + + HashMap map = new HashMap<>(); + map.put("userId", 666554433); + return map; + } +} diff --git a/provider/junit5/src/test/resources/pacts/V3Consumer-ProviderStateService.json b/provider/junit5/src/test/resources/pacts/V3Consumer-ProviderStateService.json new file mode 100644 index 000000000..49a548b43 --- /dev/null +++ b/provider/junit5/src/test/resources/pacts/V3Consumer-ProviderStateService.json @@ -0,0 +1,111 @@ +{ + "consumer": { + "name": "V3Consumer" + }, + "interactions": [ + { + "description": "a request", + "providerStates": [ + { + "name": "a provider state with injectable values", + "params": { + "valueA": "A", + "valueB": 100 + } + } + ], + "request": { + "body": { + "userId": 100 + }, + "generators": { + "body": { + "$.userId": { + "dataType": "INTEGER", + "expression": "userId", + "type": "ProviderState" + } + } + }, + "headers": { + "Content-Type": "application/json; charset=UTF-8" + }, + "matchingRules": { + "body": { + "$.userId": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + } + } + }, + "method": "POST", + "path": "/values" + }, + "response": { + "body": { + "userId": 100, + "userName": "Test" + }, + "generators": { + "body": { + "$.userId": { + "dataType": "INTEGER", + "expression": "userId", + "type": "ProviderState" + } + }, + "header": { + "LOCATION": { + "dataType": "STRING", + "expression": "http://server/users/${userId}", + "type": "ProviderState" + } + } + }, + "headers": { + "Content-Type": "application/json; charset=UTF-8", + "LOCATION": "http://server/users/666" + }, + "matchingRules": { + "body": { + "$.userId": { + "combine": "AND", + "matchers": [ + { + "match": "type" + } + ] + } + }, + "header": { + "Content-Type": { + "combine": "AND", + "matchers": [ + { + "match": "regex", + "regex": "application/json(;\\s?charset=[\\w\\-]+)?" + } + ] + } + } + }, + "status": 200 + } + } + ], + "metadata": { + "pact-jvm": { + "version": "4.6.3" + }, + "pactSpecification": { + "version": "3.0.0" + } + }, + "provider": { + "name": "ProviderStateService" + } +}