0
votes

I have a restFul API http://localhost:8080/books/{id} which return a book with a particular ID.

ex:

GET http://localhost:8080/books/1

will return

{ "id" : 1, "pages" : 20, "price" : 100 }

GET http://localhost:8080/books/2

will return

{ "id" : 2, "pages" : 30, "price" : 120 }

I have written my consumer expectations using regex in place of {id} and have generated the PACT.

There is no data present in the provider now. (no book information in present in the database). When I run pact verfication on provider side it fails since it is not able to get any information by hitting the actual service to cross check the contract.

Can the contract be verified without data being present in the provider database? or is there any workaround for the same?

Also the DB of provider can change if we deploying the provider into multiple environments, how to handle this?

Appreciate the help. Thanks.

2
Basically how the Data is handled in PACT? How to give the right data to provider so that the response returned is 200 which we need. - Sam

2 Answers

0
votes

Why would you want to workaround that? The point of contract testing is so that you can verify that both sides of the contract are properly met!

You might want to look at Provider States [1] for this.

Which language are you using?

The article provided gives you some background. In JVM, you can look at the @State annotation https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-junit#example-of-http-test.

In your consumer, you would specify a state such as "Given a book with ID 1 exists". On the provider side, the framework will invoke the @State annotation corresponding to this expectation. This gives you the opportunity to ensure any state data exists (in this case, that book) before the test case runs.

[1] https://docs.pact.io/documentation/provider_states.html

-1
votes

First of all, none of the above URL works. & its so freaking difficult to use this framework.. none of the API has proper documentation and also bloody when some thing does not work, it does not give proper error message also.

In my case, I have been trying to send a Request Parameter called "business-date" which expects a date with format "yyyy-MM-dd" and none of the below options seems to be working --

matchQuery("business-date", "\\d{4}-\\d{2}-\\d{2}", "2020-05-18")

query("business-date="+getBusinessDate())

queryMatchingDate("business-date", "yyyy-MM-dd")

matchQuery("business-date", "\\d{4}-\\d{2}-\\d{2}")

queryMatchingDate("business-date", "yyyy-MM-dd")

queryMatchingISODate("business-date", "yyyy-MM-dd")

The exception looks like this

org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: [{ "error": "Unexpected request : \tmethod: GET\n\tpath: \/api\/batch_activities\/ACTIVITY\/STARTED\n\tquery: {}\n\theaders: {X-b3-traceid=[6c88f0a1a0ae288e], Accept=[application\/json], Connection=[ke... (443 bytes)]

Caused by: org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: [{ "error": "Unexpected request : \tmethod: GET\n\tpath: \/api\/batch_activities\/ACTIVITY\/STARTED\n\tquery: {}\n\theaders: {X-b3-traceid=[6c88f0a1a0ae288e], Accept=[application\/json], Connection=[ke... (443 bytes)]

Once instance of the DSL looks like below -

builder.given("Fetch Batch Activity by status")
        .uponReceiving("Receiving Batch activity status").headers(headers).method(HttpMethod.GET)
        .matchPath(pathRegex, path)
        .matchQuery("business-date", "\\d{4}-\\d{2}-\\d{2}")
        .willRespondWith().status(200).headers(headers).body(jsonUtil.getObjectAsString(
            formatBatchActivityForStatus(FeedExporterConstants.BATCH_STATUS_STARTED)))
        .toPact();