-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add a ProviderState injected test with integer values #1700
- Loading branch information
1 parent
b1806ab
commit 1b00f63
Showing
4 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...ider/junit5/src/test/java/au/com/dius/pact/provider/junit5/ProviderStateInjectedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
provider/junit5/src/test/resources/pacts/V3Consumer-ProviderStateService.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |