Skip to content

Commit

Permalink
chore: add a ProviderState injected test with integer values #1700
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Aug 23, 2023
1 parent b1806ab commit 1b00f63
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> defaultState(Map<String, Object> 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<String, Object> map = new HashMap<>();
map.put("userId", 666554433);
return map;
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}

0 comments on commit 1b00f63

Please sign in to comment.