Skip to content

znsio/specmatic-order-bff-grpc-kotlin

Repository files navigation

Specmatic Order BFF gRPC Sample

This sample project demonstrates Specmatic gRPC support which allows you to use your proto files for Contract Testing and Intelligent Service Virtualisation (stubbing/mocking). Specmatic gRPC support can also help you use your proto files for #nocode backward compatibility testing and more.

Specmatic gRPC Sample Project Architecture

The specmatic-order-bff-grpc-kotlin is a gRPC server application developed according to the following proto file, which can be found in the central contract repository:

This BFF project relies on the OrderAPI domain service which implements the following proto files:

The ContractTest class demonstrates how to use Specmatic to test specmatic-order-bff-grpc-kotlin gRPC server app using the above proto files.

Requirements

  • Java 17 or later

Project Setup

  1. Clone the repository

    git clone https://github.com/znsio/specmatic-order-bff-grpc-kotlin
  2. Initialize and update the specmatic-order-contracts submodule

    git submodule update --init --recursive --remote
  3. Enable automatic submodule updating when executing git pull

    git config submodule.recurse true

Running Contract Tests

Using Gradle

./gradlew clean test   

Using Docker

  1. Start the Specmatic gRPC stub server to emulate domain service:

    docker run -p 9090:9090 -v "$(pwd)/specmatic.yaml:/usr/src/app/specmatic.yaml" znsio/specmatic-grpc-trial virtualize --port=9090
  2. Build and run the BFF service (System Under Test) in a Docker container:

    docker build --no-cache -t specmatic-order-bff-grpc .
    docker run --network host -p 8085:8085 specmatic-order-bff-grpc

    Or run it using ./gradlew bootRun

  3. Finally, run Specmatic Contract on the BFF service (System Under Test):

    docker run --network host -v "$(pwd)/specmatic.yaml:/usr/src/app/specmatic.yaml" -v "$(pwd)/build/reports/specmatic:/usr/src/app/build/reports/specmatic" -e SPECMATIC_GENERATIVE_TESTS=true znsio/specmatic-grpc-trial test --port=8085 --host=host.docker.internal

Developer notes

The BFF service can be independently started with below command.

./gradlew bootRun

And now you can debug / test by using grpcurl to verify the setup.

grpcurl -plaintext -d '{"type": "OTHER", "pageSize": 10}' localhost:8085 com.store.order.bff.OrderService/findAvailableProducts

Which should give you results as shown below.

%  grpcurl -plaintext -d '{"type": "OTHER", "pageSize": 10}' localhost:8085 com.store.order.bff.OrderService/findAvailableProducts
{
  "products": [
    {
      "id": 608,
      "name": "PXDIO",
      "type": "GADGET",
      "inventory": 148
    }
  ]
}

Alternatively you can also use grpcui to debug the service.

grpcui -plaintext localhost:8085

Also observe corresponding logs in the Specmatic Stub Server which is emulating domain service to understand the interactions between BFF and Domain and Service.