Kafka Testing

Applies to ReadyAPI 3.56, last modified on November 21, 2024

About Kafka and asynchronous APIs

Apache Kafka is an event-streaming platform. It is a scalable, reliable, and secure way to create asynchronous (or event-driven) APIs.

When a client application sends a request to a RESTful, GraphQL, SOAP, or other synchronous API, it waits for a response. When you work with asynchronous APIs, the client does not need a response, and the server does not send it. Such APIs implement the event-driven architecture.

A typical process looks like this:

  • A publisher (or producer, in Kafka terms) posts an event (sends a request) to a broker. An event is a piece of data to transmit. It can be a JSON object containing information about a transaction or details of a newly created object in a database, and so on.

    To separate one type of events from another, a producer sends it to one of the channels (or topics) specified within a broker.

  • If another application needs events related to some topic, it subscribes to the needed channel. Once a publisher posts an event to that channel, the subscriber (or consumer) receives the event and can work with it as it is needed by a business logic:

Kafka support in ReadyAPI: Event-streaming pipeline

Click the image to enlarge it.

Imagine you have a web store. When a user logs in, a client application posts a login event. When a user creates an order, the client application posts another event with the order details. The client application does not bother about it anymore. In its time, some internal application can get the order’s data from the broker and handle it in some way. For example, store it in a database or send you an email.

Note: Here, we use terms used in the AsyncAPI initiative. Kafka uses slightly different terminology: publishers are called producers, subscribers are called consumers, and channels are called topics. Despite their names, they mean the same.

Requirements

  • To add a Kafka API to the project, you need to have any ReadyAPI license.

  • To test Kafka APIs, you use the API Connection test step. To add it to a test case, you will need a ReadyAPI Test Pro license. If you do not have it, try a ReadyAPI trial.

Working with Kafka in ReadyAPI

To test Kafka-based services in ReadyAPI, you use the API Connection test step. This test step is linked to either the Publish or Subscribe Kafka operation. Depending on a base operation, the test step works as a producer or as a consumer.

Event serialization and schema registry

Kafka provides a way to serialize and deserialize events by using schemas. There are several reasons to do it. First, serialization translates the event data to an array of bytes, so its size is relatively small and requires fewer resources to store and transmit it. Second, the schema guarantees that a producer and a consumer understand each other.

When you use a schema, you bind a schema to a topic, so all the events posted to that topic must comply with that schema.

Schema registry is a dedicated service for storing schemas. The service provides you with schema versioning, and, depending on certain registry settings, it may check compatibility with the previous schema versions.

To serialize events in ReadyAPI:

  1. Open the API Connection test step.

  2. Select the needed format in the drop-down box. Select Custom to specify an Avro or Protobuf schema by using a file.

  3. If you use a schema registry, open Connection Settings and specify the Schema Registry URL and Registry Authorization if needed.

    If you use a file, select the needed file in the subsequent dialog and click Add:

Click the image to enlarge it.

Click the image to enlarge it.

Property list

Besides the test step editor, you can adjust test step behavior by using its properties in the Custom Kafka Test Step Properties panel in the Navigator.

ReadyAPI now supports custom Kafka test step properties. This tab contains:

Name Description
connectionSettings

The connectionSettings property must adhere to the following structure:

propertyName > propertyValue

Support for this property should be added for both Kafka Publish and Kafka Subscribe test step.

Channel

The Channel property should be a string that specifies the name of the channel selected for the test step.

This property must be set during the execution of the test step due to property expansion.

Endpoint

The Endpoint property should be a string that specifies the address of the Kafka broker.

This property must be set during the execution of the test step due to property expansion.

Data

The Data property should be a list containing elements structured as follows:

  • metadata: List of headers associated with the request/response.

  • message: Body of the Kafka message associated with the request/response.

This property should be updated each time a Kafka message is sent or received by the Kafka Publish or Subscribe test step. With each new execution, the current state of Data is cleared and replaced with new data.

Schemas

This field contains key and value schemas used by the test step. We support Protobuf and Avro schemas.

Next Steps

See Also

AMQP Testing
Testing Internet of Things (IoT)

Highlight search results