Skip to content

Commit

Permalink
[DMS-376] Adding readme files, cleanup code, appsettings.json
Browse files Browse the repository at this point in the history
Signed-off-by: Moises Siles <[email protected]>
  • Loading branch information
msilesgap committed Nov 5, 2024
1 parent 68e4e72 commit 1e13fec
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Consumer Tests

Pact is a consumer-driven contract testing tool, meaning the API consumer
defines a test outlining its expectations and requirements from the API
provider(s). By unit testing the API client with Pact, we generate a contract
that can be shared with the provider to validate these expectations and help
prevent breaking changes.

## Running The Consumer Tests

Run the tests how you run any other test suite. For example:

- Visual Studio Test Explorer
- from `/src/config/frontend/...ConsumerTests/tests` run: `dotnet test`.
- Once the test are executed you will find a new file inside the `/pacts/`
folder, this folder contains a json file which is the contract used for the
provider tests

## Generate the Contract

PactNet will automatically generate a Pact file after running the test,
typically saved to the `/pacts/` folder. This Pact file is the contract your
consumer expects from the provider.

## Share the Pact file with the Provider

Share the generated Pact file with the provider for their own verification,
often by storing it in a Pact Broker or a shared repository.

This approach allows you to create a consumer-driven contract that the provider
can validate to prevent any breaking changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"consumer": {
"name": "DMS API Consumer"
"name": "ConfigurationService-API"
},
"interactions": [
{
Expand Down Expand Up @@ -96,6 +96,6 @@
}
},
"provider": {
"name": "DMS Configuration Service API"
"name": "DMS-API"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ConsumerIdentityTest
[SetUp]
public void Setup()
{
pact = Pact.V3("DMS API Consumer", "DMS Configuration Service API").WithHttpInteractions();
pact = Pact.V3("ConfigurationService-API", "DMS-API").WithHttpInteractions();
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Provider Tests

Provider tests in contract testing verify that the provider (API server) meets
the expectations defined by the consumer in the contract (Pact file). These
tests ensure the provider adheres to the contract, helping prevent breaking
changes for the consumer.

## Running The Provider Tests

Run the tests how you run any other test suite. For example:

- Visual Studio Test Explorer
- from `/src/config/frontend/...ProviderTests/tests` run: `dotnet test`.

> [!IMPORTANT]
Pact contract verification includes a single test in the code, using
> only one .verify method from Pact to run the contract-based tests. If any
> failures occur, an error will be displayed in the terminal, and the test
> framework will show a single failure result. To view the details of these
> failures, you’ll need to scroll up in the terminal output.
## Key Steps in contract testing

### Setup Pact Verification

The provider test will read the consumer's Pact file, usually stored in a Pact
Broker or as a local file, to retrieve the expected interactions. This file
defines the requests that the provider should handle and the corresponding
responses the consumer expects.

### Configure the provider Test

In the provider tests, configure the Pact verifier to:

- Specify the provider details (e.g., name and base URL).
- Specify the Pact source, currently we are not using the Pact Broker feature.
Pact File should be specified in the Provider Test.
- Specify the provider states url, make sure url matches the PactBase URL.

### Running the Provider Test and Verify Interactions

The Pact verifier sends the requests to the provider based on the interactions
in the Pact file and checks that the provider responds with the expected
responses. Each test run will verify that the provider's responses match the
expected responses, as defined in the contract.

## Share the Pact file with the Provider

Share the generated Pact file with the provider for their own verification, in
our case we are storing the Pact File in the repo.

This approach allows you to create a consumer-driven contract that the provider
can validate to prevent any breaking changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using PactNet;
using Microsoft.Extensions.Logging;
using PactNet.Infrastructure.Outputters;
using Microsoft.Extensions.Configuration;

namespace EdFi.DmsConfigurationService.Frontend.AspNetCore.ContractTest.Provider.Tests
{
Expand All @@ -24,10 +25,24 @@ public class ProviderIdentityTest : IDisposable

private IHost _host;
private IPactVerifier verifier;
private Uri pactURL = new Uri("http://localhost:5120");

private readonly Uri? pactURL;
private readonly string? pactFilePath;
private readonly string? providerStatePath;

public ProviderIdentityTest()
{
// Load configuration from appsettings.json
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) // Set the base path for your appsettings.json
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();

// Get values from the configuration file
pactURL = new Uri(configuration["Pact:PactURL"]!);
pactFilePath = configuration["Pact:PactFilePath"];
providerStatePath = configuration["Pact:ProviderStatePath"];

_host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>
{
Expand Down Expand Up @@ -64,19 +79,10 @@ public ProviderIdentityTest()
[Test]
public void Verify()
{
string pactFile = Path.Combine("..",
"..",
"..",
"..",
"EdFi.DmsConfigurationService.Frontend.AspNetCore.ContractTest.ConsumerTests",
"pacts",
"DMS API Consumer-DMS Configuration Service API.json");

verifier!.ServiceProvider("DMS Configuration Service API", pactURL)
.WithFileSource(new FileInfo(pactFile))
.WithProviderStateUrl(new Uri(pactURL + "provider-states"))
.WithFileSource(new FileInfo(pactFilePath!))
.WithProviderStateUrl(new Uri(pactURL + providerStatePath))
.Verify();

}

#region IDisposable Support
Expand All @@ -92,7 +98,7 @@ protected virtual void Dispose(bool disposing)

if (disposing)
{
//server.Dispose();
_host.Dispose();
}

_disposed = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@
}
}
]
}
},
"Pact": {
"PactURL": "http://localhost:5120",
"PactFilePath": "../../../../EdFi.DmsConfigurationService.Frontend.AspNetCore.ContractTest.ConsumerTests/pacts/ConfigurationService-API-DMS-API.json",
"ProviderStatePath": "provider-states"
}
}

0 comments on commit 1e13fec

Please sign in to comment.