{"id":21528207,"url":"https://github.com/znsio/specmatic-google-pubsub-sample","last_synced_at":"2025-03-17T18:44:43.548Z","repository":{"id":233462589,"uuid":"758466396","full_name":"znsio/specmatic-google-pubsub-sample","owner":"znsio","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-24T05:38:18.000Z","size":594,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-02-24T06:33:38.687Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/znsio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-16T11:36:48.000Z","updated_at":"2025-02-24T05:38:21.000Z","dependencies_parsed_at":"2024-04-16T08:50:08.588Z","dependency_job_id":"90111f13-4b88-4848-9338-c7fc4b9a2d6b","html_url":"https://github.com/znsio/specmatic-google-pubsub-sample","commit_stats":null,"previous_names":["znsio/specmatic-google-pubsub-sample"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/znsio%2Fspecmatic-google-pubsub-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/znsio%2Fspecmatic-google-pubsub-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/znsio%2Fspecmatic-google-pubsub-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/znsio%2Fspecmatic-google-pubsub-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/znsio","download_url":"https://codeload.github.com/znsio/specmatic-google-pubsub-sample/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244091699,"owners_count":20396664,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T01:52:11.621Z","updated_at":"2025-03-17T18:44:43.516Z","avatar_url":"https://github.com/znsio.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Specmatic Google PubSub Sample\n\n* [Specmatic Website](https://specmatic.in)\n* [Specmatic Documenation](https://specmatic.in/documentation.html)\n\nThis sample project demonstrates how we can run contract tests against a service which interacts with a Google PubSub instance. \n\n## Pre-requisites\n* Gradle\n* JDK 17+\n* Docker\n\n## Run the tests\n1. Start Docker Desktop.\n2. Run the following command -\n```shell\n./gradlew clean test\n```\n\n## Application Architecture\nThe service under test is: **Order Service**.  \nIt listens for messages on a Google PubSub topic called **'place-order'**.  \nFor every message it receives, it sends a message on a Google PubSub topics: **process-order** and **notification**.  \nWe also have an async api specification defined for the Order Service in the **order_service.yaml** file.  \nThe **order_service.yaml** is defined as a 'test' contract in the [specmatic.json](https://specmatic.in/documentation/specmatic_json.html) file.\n\n![Order Service Production Architecture](assets/order_service_prod_architecture.gif)\n\n![Order Service Production Architecture](assets/order_service_contract_test.gif)\n\n## Contract Tests\nTo run contract tests, Specmatic leverages the \"named examples\" defined in the async api specification of the Order Service.  \n**Note**: As of now, contract tests will be generated only if there are named examples defined in the async api specification. \n\nHere's the structure of a contract test:\n- Specmatic will use the named example defined for the **'place-order'** topic and publish a message on the **'place-order'** topic.  \n  If the example message does not match the message schema of the the **'place-order'** topic, the test will fail.\n\n- Specmatic will wait for the Order Service to process the message and publish a message on the **'process-order'** and **'notification'** topics.\n\n- Specmatic will then assert that :\n  - Exactly one message each is received on the **process-order** and **'notification'** topics\n  - The message received on the **process-order** topic is the named example defined for the **'process-order'** topic.  \n    The message received should match the message schema for the **process-order** topic\n  - The message received on the **notification** topic is the named example defined for the **notification** topic.  \n    The message received should match the message schema for the **notification** topic\n\n## Running the tests with the Google PubSub Emulator\nThis project is setup to use the Google PubSub emulator by default.  \n\n1. Ensure that the PUBSUB_EMULATOR_HOST env variable is set appropriately while running tests. For this, add the following line in build.gradle.\n```groovy\ntest {\n  environment \"PUBSUB_EMULATOR_HOST\", \"localhost:8085\"\n}\n```\n\n2. Make use of the ```PubSubEmulator``` utility class to start the Google PubSub emulator in the contract tests. Here's how to create the pubsub emulator by specifying your **project id** and a list of topics:\n```kotlin\nprivate val pubSubEmulator = PubSubEmulator(\n            projectId = PROJECT_ID,\n            topics = listOf(PLACE_ORDER_TOPIC, PROCESS_ORDER_TOPIC, NOTIFICATION_TOPIC),\n        )\n```\n3. Start the emulator in your ```@BeforeAll``` method\n```kotlin\npubSubEmulator.start()\n```\n\n4. Stop the emulator in your ```@AfterAll``` method\n```kotlin\npubSubEmulator.stop()\n```\n\n## Running the tests with a live Google PubSub project\nYou can also run your tests against a real/live Google PubSub project.\nHere are the steps:\n- Create the following three topics in your project: \n  - **place-order**\n  - **process-order**\n  - **notification** \n\n- Update the following with your Google PubSub project id:\n  - **application.properties** (src/main/resources/)  \n   ```yaml\n  google.pubsub.projectId=pub-sub-demo-414308\n  ```\n  - **ContractTest**\n  ```kotlin\n   private const val projectId = \"pub-sub-demo-414308\"\n  ```\n- Remove the following line from the `test` section in **build.gradle**  \n  ```\n  environment \"PUBSUB_EMULATOR_HOST\", \"localhost:8085\"\n  ```\n- You would need to then set up some authentication mechanism using one of the options described [here](https://cloud.google.com/docs/authentication/provide-credentials-adc#how-to).  \n  - **Service Account with Key**  \n    If you are planning to use a service account key, add this line in **build.gradle** in the `test` section after `useJUnitPlatform()`:  \n    ```\n       environment \"GOOGLE_APPLICATION_CREDENTIALS\", \"\u003cpath to your key file\u003e\"\n    ```\n\n  ## Troubleshooting\n- If the contract tests fail due to expected messages not being received on the subscribe topics, try increasing the wait timeout of specmatic's ```GooglePubSubMock```. \n  \n  For example, to make the ```GooglePubSubMock``` wait for 15 seconds, we can provide the timeout as follows -\n  ```kotlin\n  GooglePubSubMock.connectWithBroker(PROJECT_ID, messageWaitTimeoutInMilliseconds = 15000)\n  ```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fznsio%2Fspecmatic-google-pubsub-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fznsio%2Fspecmatic-google-pubsub-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fznsio%2Fspecmatic-google-pubsub-sample/lists"}