Microcks is a tool for API Mocking and Testing. In this presentation an overview of the support in Microcks for asynchronous APIs - the event publishing and consuming behavior of services and applications
2. Classificatie: vertrouwelijk
The Interface of a Service
API
HTTP REST/JSON
Code Café Microcks - January 2022
µ
HTTP/2 gRPC/ProtoBuf
HTTP SOAP/XML
Kafka
MQTT
WebSocket
HTTP
JMS
AMQP
STOMP
NATS, SNS, SQS,
Redis, RabbitMQ
Kafka
MQTT
WebSocket
HTTP
JMS
AMQP
STOMP
NATS, SNS, SQS,
Redis, RabbitMQ
2
3. Classificatie: vertrouwelijk
AsyncAPI
• Design Time
• Event Driven Architecture
• Pub/Sub
• Asynchronous Interactions
• API Design
• Interface Definition/Contract
Code Café Microcks - January 2022 3
4. Classificatie: vertrouwelijk
AsyncAPI
• Compare OpenAPI
• Describes an API (in yaml or json) to consumers
• invitation to publish events on a certain topic
on which the API is subscribed and from which it consumes
• invitation to subscribe to a certain topic (that the API publishes to)
• the message structure that is actually published or consumed
• Used to generate code for publishing or consuming
• and provide documentation for developers and testers
• Used to drive Mock-tools to publish the event said to be consumed
• Used to drive Test-tools-tools to listen for the events claimed to be published
• Sponsored by Solace, Postman, IBM, SalesForce, MuleSoft, IQVia, Tibco,
SAP, Slack
Code Café Microcks - January 2022 4
5. Classificatie: vertrouwelijk
AsynchAPI Interface Definition
• Define for an API
• the Channel (queue, topic, …) the API subscribes to
• the message type the API expects to consume
• the Channel the API publishes to
• the message type the API publishes
• the Servers on which the API connects
• Kafka Broker, HTTP url endpoint, RabbitMQ Broker endpoint
• link between Channel and Server(s) (channel binding)
• Developer and tools can interpret this definition
• generate code, stubs, mocks, tests
Code Café Microcks - January 2022 5
PUBLISH
SUBSCRIBE
6. Classificatie: vertrouwelijk
Testing and Mocking Async APIs with Microcks
Code Café Microcks - January 2022 6
load & interpret AsyncAPI definitions
Mock implementation
that publishes events
to channel (just as the
real API would do)
Test that subscribes
and consumes
messages that API
promises to publish
Kafka
MQTT
WebSocket
Kafka
MQTT
WebSocket
7. Classificatie: vertrouwelijk
application.properties
Running Microcks with Docker Compose
(with async-api enabled)
Code Café Microcks - January 2022 7
Kafka
docker-compose.yml docker-compose-async-addon.yml kafdrop-addon.yml
Kaf
Drop
Mongo
DB
Key
Cloak
microcks-
postman
microcks
async-
minion
Zoo
keeper
UI
UI
UI
features.properties
9. Classificatie: vertrouwelijk
Microcks and Async APIs
• Prepare AsyncAPI interface definition
• message definitions, channel name
• examples (used for mocked messages) – including function expressions
such as {{now(yyyy-mm-dd)}}, {{uuid()}}, {{randomFirstName()}}
• Import API contract in Microcks
• Microcks will automatically
• validate interface and exposes API documentation
• start publishing mock messages to default channel binding (i.e. Kafka)
• when other channel bindings are declared, then these too (WS, MQTT)
• Consume mock messages in event client application under development
• …
Code Café Microcks - January 2022 10
10. Classificatie: vertrouwelijk
WS
MQTT
Application can Consume Correct Messages through Real
Channel published by generated Mock
Code Café Microcks - January 2022 11
load & interpret AsyncAPI definitions
Mock implementation that
publishes events to channel
(just as the real API would do)
Kafka
Kaf
Drop
UI
Application
under Development
subscribe & consume
Event Client can be
generated from Async
API interface definition
Mock
API
implemen-
tation
11. Classificatie: vertrouwelijk
Code Café Microcks - January 2022 12
Translated by Microcks to topic
CodeCafeAdvancedStreetlightsAPI-1.1-smartylighting-
codecafe-streetlights-event-lighting-measured
20. Classificatie: vertrouwelijk
Microcks and Async APIs
• Prepare AsyncAPI interface definition
• message definitions, channel name
• Import API contract in Microcks
• Microcks automatically
• validates interface and exposes API documentation
• starts publishing mock messages to default channel binding (i.e. Kafka)
• when other channel bindings are declared, then these too (WS, MQTT)
• Consume mock messages in event client application under development
• Create Test in Microcks: validate messages published by application under
test to a specific endpoint
• messages to validate can be consumed from topic on Kafka Broker,
MQTT Broker or WebSocket Server
Code Café Microcks - January 2022 21
21. Classificatie: vertrouwelijk
Microcks can generate and run tests on the messages
published by producing application
Code Café Microcks - January 2022 22
load & interpret AsyncAPI definitions
Kafka
Application
under Development
MQTT
Web
Socket
Test that subscribes and
consumes messages that
API promises to publish
subscribe
&
consume
Event producing code can
be generated from Async
API interface definition
22. Classificatie: vertrouwelijk
Validate the example messages produced by Mock
Code Café Microcks - January 2022 23
load & interpret AsyncAPI definitions
Kafka
Test that subscribes and
consumes messages that
API promises to publish
load & interpret AsyncAPI definitions
Mock
API
implemen-
tation
Validate correctness of
Mock-produced events
24. Classificatie: vertrouwelijk
Validate the example messages produced by Mock
Code Café Microcks - January 2022 25
Change message
schema in
AysyncAPI
Examples do not
comply
Test fails
load &
interpret
AsyncAPI
definitions
Reload AsyncAPI
definition
26. Classificatie: vertrouwelijk
Conclusion on Microcks & Async APIs
• AsyncAPI is standard for defining API interface for consuming or producing messages
asynchronously
• via queue | event broker | web socket
• Microcks understands AsyncAPI contracts – but only SUBSCRIBE operations
• generates Mock implementation of API that produces messages
• generates Tests that validate events produced against contract
• support for WS, Kafka, MQTT
• No support in Microcks for
• PUBLISH operation (that invites API clients to publish their events)
• No Mock Event Consumers and no Tests on “proper consumption”
• how useful would this be?
• other Async channels (JMS, AMQP, STOMP, NATS, SNS, SQS, Redis, RabbitMQ,…)
• Microcks installs with out of the box integrated Kafka broker and WebSocket server
Code Café Microcks - January 2022 27
27. Classificatie: vertrouwelijk
Steps
• Create Kafka Topic streetlight-dropbox
• Configure publishTopic in Kafka Binding
entry in Dapr components.yaml
Code Café Microcks - January 2022 28
out
in
app-id(s) to route to
28. Classificatie: vertrouwelijk
Steps (2)
• Run a Dapr application with the components.yaml
dapr run –components-path .
• Remember the HTTP port at which Dapr is listening
• Publish an event through Dapr
curl -X POST http://localhost:37919/v1.0/bindings/advanced-street-light-
destination
-H "Content-Type: application/json"
-d '{"data": {"streetlightId": "dev99",.... } , "operation": "create"}'
Code Café Microcks - January 2022 29
29. Classificatie: vertrouwelijk
Steps (3)
• Check the event in Kafdrop
• Run test in Microcks UI on endpoint kafka://kafka:19092/streetlight-dropbox
• Publish an(other) event (for the test to consume)
Code Café Microcks - January 2022 30
31. Classificatie: vertrouwelijk
To create Kafka consuming application with Dapr
• Create Kafka Binding entry in components.yaml
• name of the binding
• endpoints for one or more Kafka brokers
• topic(s) to subscribe to
• consumergroup name
• the applications to route
incoming messages to
Code Café Microcks - January 2022 32
32. Classificatie: vertrouwelijk
Create generic application
that can receive POST request (from Dapr side car)
• Incoming messages are posted to application from side car at path equal to /binding-name
in components.yaml (in this case: /advanced-street-light-destination)
Code Café Microcks - January 2022 33
33. Classificatie: vertrouwelijk
Run Consuming application as Daprized application
dapr run --app-id street-light-consumer --app-port 3000 node app.js -
-components-path .
• Run this command in the directory that contains both app.js and components.yaml
• Make sure that:
• app-port corresponds with the port on which the application listens
• app-id corresponds with the value used in the scopes element in the
components.yaml file
Code Café Microcks - January 2022 34