@PactTestFor(providerName = "SirenOrderProvider")
public class ContractTest
{
+ @Pact(consumer="SpringConsumer")
+ public RequestResponsePact getRoot(PactDslWithProvider builder)
+ {
+ return builder
+ .uponReceiving("get root")
+ .path("/")
+ .method("GET")
+ .willRespondWith()
+ .status(200)
+ .headers(Map.of("Content-Type", "application/vnd.siren+json"))
+ .body(LambdaDsl.newJsonBody(body ->
+ {
+ body.array("class", classArray ->
+ {
+ classArray.stringValue("representation");
+ });
+ body.array("links", linksArray ->
+ {
+ linksArray.object(object->
+ {
+ object.matchUrl2("href", "orders");
+ object.array("rel", relArray ->
+ {
+ relArray.stringValue("orders");
+ });
+ });
+ });
+ }).build())
+ .toPact();
+ }
+
+ @Test
+ @PactTestFor(pactMethod = "getRoot")
+ public void testGetRoot(MockServer mockServer)
+ {
+ RestTemplate restTemplate =
+ new RestTemplateBuilder()
+ .rootUri(mockServer.getUrl())
+ .build();
+ try
+ {
+ restTemplate.getForEntity("/", String.class);
+ }
+ catch (Exception e)
+ {
+ fail("Unexpected exception", e);
+ }
+ }
+
@Pact(consumer="SpringConsumer")
public RequestResponsePact getAllOrders(PactDslWithProvider builder)
{
},
"status": 200
}
+ },
+ {
+ "description": "get root",
+ "request": {
+ "method": "GET",
+ "path": "/"
+ },
+ "response": {
+ "body": {
+ "class": [
+ "representation"
+ ],
+ "links": [
+ {
+ "href": "http://localhost:8080/orders",
+ "rel": [
+ "orders"
+ ]
+ }
+ ]
+ },
+ "generators": {
+ "body": {
+ "$.links[0].href": {
+ "example": "http://localhost:8080/orders",
+ "regex": ".*\\/(\\Qorders\\E)$",
+ "type": "MockServerURL"
+ }
+ }
+ },
+ "headers": {
+ "Content-Type": "application/vnd.siren+json"
+ },
+ "matchingRules": {
+ "body": {
+ "$.links[0].href": {
+ "combine": "AND",
+ "matchers": [
+ {
+ "match": "regex",
+ "regex": ".*\\/(\\Qorders\\E)$"
+ }
+ ]
+ }
+ }
+ },
+ "status": 200
+ }
}
],
"metadata": {