Example Java Kafka Producer

Source Code

https://github.com/pactflow/example-provider-java-kafka

badge__2_.svg

can--i--deploy-master_to_production-brightgreen.svg: https://testdemo.pactflow.io/pacticipants/pactflow-example-bi-directional-provider-readyapi/branches/master/latest-version/can-i-deploy/to-environment/production/badge

pactflow--example--consumer--java--kafka__pact-verified-brightgreen.svg: https://testdemo.pactflow.io/pacticipants/pactflow-example-bi-directional-provider-readyapi/branches/master/latest-version/can-i-deploy/to-environment/production/badge (latest pact)

pactflow--example--consumer--js--kafka_pact-verified-brightgreen.svg: https://testdemo.pactflow.io/pacticipants/pactflow-example-bi-directional-provider-readyapi/branches/master/latest-version/can-i-deploy/to-environment/production/badge

pactflow--example--consumer--java--kafka__pact-verified-brightgreen.svg: https://testdemo.pactflow.io/pacticipants/pactflow-example-bi-directional-provider-readyapi/branches/master/latest-version/can-i-deploy/to-environment/production/badge (master/master pact)

pactflow--example--consumer--js--kafka_pact-verified-brightgreen.svg: https://testdemo.pactflow.io/pacts/provider/pactflow-example-provider-java-kafka/consumer/pactflow-example-consumer-js-kafka/latest/master (master/master pact)

This is an example of a Java Spring Boot Kafka Producer that uses Pact, API Hub for Contract Testing and GitHub Actions to ensure that it is compatible with the expectations its consumers have of it.

See the canonical provider example here: https://github.com/pactflow/example-provider See also the full API Hub for Contract Testing CI/CD Workshop for which this can be substituted in as the "provider".

In the following diagram, we'll be testing the "Event API", a service that publishes product events to a Kafka stream on the product topic.

kafka.png

Pre-requisites

Software:

  • Tools listed

  • Java (11+)

  • A pactflow.io account with a valid API Token

Environment variables

To be able to run some of the commands locally, you will need to export the following environment variables into your shell:

  • PACT_BROKER_TOKEN: a valid API Token for API Hub for Contract Testing

  • PACT_BROKER_BASE_URL: a fully qualified domain name with protocol to your pact broker e.g., https://dius.pactflow.io

  • PACT_BROKER_HOST: a fully qualified domain name without protocol to your pact broker e.g., dius.pactflow.io

Usage

  • Running tests: ./gradlew clean test

  • Start a Kafka cluster, and setup the producer endpoint (enables POST to localhost:8081/products): make start

  • Create a new event (manually):

    curl -X POST -H"Content-Type: application/json" localhost:8081/products -d '{
      "id": "7e54c13c-e28e-41fc-b34d-99de62db4666",
      "name": "Unbranded Plastic Tuna",
      "type": "BACON",
      "event": "UPDATED",
      "version": "v1"
    }'

To disable test data generation that puts random events onto product topic, prefix with SEND_TEST_EVENTS=false. e.g. SEND_TEST_EVENTS=false make start

Publication date: