This repository contains documentation for the MOCK Schema Registry API and embedded Broker, which can assist you in your Unit/Integration or System tests.
When testing Java applications that interact with Kafka, incorporating a mock schema registry is crucial for several reasons:
-
Isolation: It enables tests to run independently of external dependencies, ensuring that changes in the schema registry or Kafka don't impact test outcomes.
-
Controlled Environment: Developers can establish controlled testing environments without relying on a live registry or Kafka broker, allowing for precise testing of different scenarios.
-
Speed: Mock schema registries facilitate faster test execution by eliminating the need for network communication with a real registry or Kafka broker.
-
Flexibility: Developers can simulate various responses and behaviors from the schema registry, enabling comprehensive testing of different edge cases and error scenarios.
-
Cost Reduction: Utilizing mock schema registries reduces development and testing costs by eliminating the need to access and maintain real Kafka infrastructure.
The Schema Registry API is used to register schemas for topics within the system.
To include the required dependency in your Maven project, add the following lines to your pom.xml
file:
<dependency>
<If you have dependency for you avro you can add as maven>
</dependency>
To register a schema, make a POST request to the following endpoint:
http://127.0.0.1:10000/api/v2/schema-registry/register
The request body should be in JSON format and include the following parameters:
key
: The key for the schema.value
: The value representing the schema.topicName
: The name of the topic associated with the schema.
Example Request Body:
{
"key": "nl.nightcrawler.spring.kafkastandalone.avro.model.UUID",
"value": "nl.nightcrawler.spring.kafkastandalone.avro.model.Customer",
"topicName": "topic5"
}
To retrieve schema mappings, make a GET request to the following endpoint:
http://127.0.0.1:10000/api/v2/schema-registry/mappings
This endpoint returns a list of mappings between schema keys and values.
To register a topic, make a POST request to the following endpoint:
http://127.0.0.1:10000/api/v2/broker-registry/register-topic/topic5
This endpoint allows you to register a new topic named topic5
.
If other applications are consuming the registration endpoints, they should be aware of the following configurations:
- Port:
10002
- Port:
10004
Make sure the consuming applications are configured to use these ports for communication.
The following are the default schema names that are registered when the application starts:
ota-exp-topic1
To build a Docker image with a specific version number from the project root, run the following commands:
- Run Maven clean install:
mvn clean install
- Build the Docker image:
docker build -t kafka-standalone:v1.0 .
This command builds a Docker image named kafka-standalone
with version v1.0
from the project's root directory.
To run the Docker image with the specified ports mapped, use the following command:
docker run -p 10000:10000 -p 10003:10003 -p 10004:10004 --name kafka-standalone -it kafka-standalone:v1.0
This command runs the Docker image named kafka-standalone
with version v1.0
and maps ports 10000
, 10003
, and 10004
to the corresponding ports inside the container.
This project is licensed under the MIT License.