Example Provider Implementation Guide

Published date: April 15, 2024, Version: 1.0

Overview

The implementation and swagger specifications must be aligned during provider side contract testing. Whenever there is a drift, we must fix it.

SwaggerHub serves as a single source of truth for all swagger specifications. The Swagger specification will be shared with FED and other teams via SwaggerHub.

Getting Started

Follow these step-by-step instructions to setup provider side contract testing:

  1. Install the latest version of @cantire-corp/cds.lib.swagger-to-pacts library in the repo.

  2. Install dredd library globally by using npm i -g dredd

    Dredd library should not be installed as a dev dependency. It will cause unnecessary problems in CI/CD pipelines.

  3. Add the following scripts in the package.json file

4. Create a file called dredd.yml in the root directory of the repo with the following content

5. Create a folder called contract-test inside /test directory

a .Create a file called /output/report.md inside /test/contract-test/ directory

b. Create a file called hooks.js inside /test/contract-test/ directory

c. In the hooks.js file you can modify the request and response based on the need.

  1. Let’s say, your endpoint returning 404 not found response when you pass invalid id. To handle this case, you can use hooks.

  2. You can also skip some tractions that you don’t want to cover as part of provider side contract testing.

  3. Here is the hooks.js file in category-experience-api

Running dredd

Make the following changes to the repo on your local machine, but don't commit them to Github.

  1. Open tsoa.json file, set yaml to true

  2. Change the blueprint path in dredd.yml file.

Now, we made necessary changes into the repo. Let’s run the dredd.

Open a terminal and start the service by npm start command. Once the service started, open another terminal and issue npm run dredd command.

Or if you want to run all in single command run npm run contract-test:provider command.

Dredd will run api testing and the results will be updated in /test/contract-test/output/report.md file. Make sure all the dredd test cases are passed like below.

Publishing provider-side contracts into Pactflow

Once, we publish the specification into SwaggerHub, its time to publish provider side contracts into Pactflow. For that please run the following command

npm run contract-test:provider:publish

 

Publishing provider side contracts into Pactflow will be done on the developer's local machine in CDS.

CI/CD configuration

In .github/workflows/build.yml file, add the following configuration at the end