Testing an event application locally

Overview

This guide demonstrates how to test an event type Connect application on your local machine using sample data.

GCP event message payload

Please note that all Google Cloud Platform event payload message.data is base64 encoded.

{
"message": {
"attributes": {
"key": "value"
},
"data": "SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ==",
"messageId": "2070443601311540",
"message_id": "2070443601311540",
"publishTime": "2021-02-26T19:13:55.749Z",
"publish_time": "2021-02-26T19:13:55.749Z"
},
"subscription": "projects/myproject/subscriptions/mysubscription"
}

Requirements

Your application must expose an http server on port 8080.

Create the sample payload

The event application will be called via POST request and the input is provided as the body payload.

For messages received by our application, there are two types of messages: Platform or CloudEvent. This type is defined when you Create the Subscription.

Format type Platform

The following example represents a new Cart being created using the PlatformFormat.

{
"notificationType": "Message",
"projectKey": "{projectKey}",
"id": "ef97dde9-1d51-4017-b1f5-42dfad118aa5",
"version": 1,
"sequenceNumber": 1,
"resource": {
"typeId": "cart",
"id": "5202b49c-9cd4-46a0-b700-063869352a63"
},
"resourceVersion": 1,
"type": "CartCreated",
"totalPrice": {
"type": "centPrecision",
"currencyCode": "EUR",
"centAmount": 4000,
"fractionDigits": 2
},
"lineItemCount": 1,
"discountCodes": [],
"createdAt": "2023-06-05T15:30:23.587Z",
"lastModifiedAt": "2023-06-05T15:30:23.587Z"
}

The following example represents an existing Cart being updated using the PlatformFormat.

{
"notificationType": "CartCreated",
"projectKey": "{projectKey}",
"resource": {
"typeId": "cart",
"id": "41389b00-ef83-4b88-864f-435ba3a520c0"
},
"version": 7,
"oldVersion": 4,
"modifiedAt": "2023-06-05T15:55:46.747Z"
}

Format type CloudEvent version 1.0

The following example represents a new Cart being created using the CloudEventsFormat.

{
"id": "ef97dde9-1d51-4017-b1f5-42dfad118aa5",
"source": "/{projectKey}/carts",
"specversion": "1.0",
"type": "com.commercetools.cart.message.CartCreated",
"subject": "5202b49c-9cd4-46a0-b700-063869352a63",
"time": "2023-06-05T15:30:23.587Z",
"dataref": "/{projectKey}/messages/ef97dde9-1d51-4017-b1f5-42dfad118aa5",
"sequence": "1",
"sequencetype": "Integer",
"data": {
"notificationType": "Message",
"projectKey": "{projectKey}",
"id": "ef97dde9-1d51-4017-b1f5-42dfad118aa5",
"version": 1,
"sequenceNumber": 1,
"resource": {
"typeId": "cart",
"id": "5202b49c-9cd4-46a0-b700-063869352a63"
},
"resourceVersion": 1,
"type": "CartCreated",
"totalPrice": {
"type": "centPrecision",
"currencyCode": "EUR",
"centAmount": 4000,
"fractionDigits": 2
},
"lineItemCount": 1,
"discountCodes": [],
"createdAt": "2023-06-05T15:30:23.587Z",
"lastModifiedAt": "2023-06-05T15:30:23.587Z"
}
}

The following example represents an existing Cart being updated using the CloudEventsFormat.

{
"id": "41389b00-ef83-4b88-864f-435ba3a520c0-u7",
"source": "/{projectKey}/carts",
"specversion": "1.0",
"type": "com.commercetools.cart.change.ResourceUpdated",
"subject": "41389b00-ef83-4b88-864f-435ba3a520c0",
"time": "2023-06-05T15:55:46.747Z",
"data": {
"notificationType": "ResourceUpdated",
"projectKey": "{projectKey}",
"resource": {
"typeId": "cart",
"id": "41389b00-ef83-4b88-864f-435ba3a520c0"
},
"version": 7,
"oldVersion": 4,
"modifiedAt": "2023-06-05T15:55:46.747Z"
}
}

Call your application

Call your application's endpoint with the message payload.

curl -X POST http://localhost:8080/{endpoint} \
-H 'Content-Type: application/json' \
-d '<message-payload>'
Example call with ResourceUpdated datashell
curl -X POST http://localhost:8080/{endpoint} \
-H 'Content-Type: application/json' \
-d '{"id":"41389b00-ef83-4b88-864f-435ba3a520c0-u7","source":"/{projectKey}/carts","specversion":"1.0","type":"com.commercetools.cart.change.ResourceUpdated","subject":"41389b00-ef83-4b88-864f-435ba3a520c0","time":"2023-06-05T15:55:46.747Z","data":{"notificationType":"ResourceUpdated","projectKey":"{projectKey}","resource":{"typeId":"cart","id":"41389b00-ef83-4b88-864f-435ba3a520c0"},"version":7,"oldVersion":4,"modifiedAt":"2023-06-05T15:55:46.747Z"}}'

Review the results

Now that your application has been called, you should check your logs and/or your third party APIs to ensure your business logic was applied. Your application must reply with a 200 HTTP status code.