https://github.com/bewaremypower/testcontainers-ksn
The testcontainers module for Kafka-on-StreamNative (KSN)
https://github.com/bewaremypower/testcontainers-ksn
Last synced: 11 months ago
JSON representation
The testcontainers module for Kafka-on-StreamNative (KSN)
- Host: GitHub
- URL: https://github.com/bewaremypower/testcontainers-ksn
- Owner: BewareMyPower
- License: apache-2.0
- Created: 2023-10-24T11:00:15.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-24T12:13:09.000Z (over 2 years ago)
- Last Synced: 2025-06-16T22:06:05.914Z (12 months ago)
- Language: Java
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# testcontainers-ksn
The testcontainers module for Kafka-on-StreamNative (KSN), which is a private version of [KoP](https://github.com/streamnative/kop).
## Get Started
### Install and import this dependency
This project requires JDK 8 or higher.
Currently, this project is not published to Maven Central repository, so you have to install this dependency to your local repository manually:
```bash
mvn clean install -DskipTests
```
Then, you can import this dependency as well as the `testcontainers` dependency:
```xml
org.testcontainers
testcontainers
1.19.1
io.github.bewaremypower
testcontainers-ksn
0.1.0-SNAPSHOT
```
> **NOTE**:
>
> For simplicity, you can just fork this repository and add tests under [the tests directory](./src/test/java/io/github/bewaremypower).
### Start and stop the KoP cluster
After importing the dependency, you can set up your test like:
```java
// Start the KSN cluster. By default, 1 ZK node, 2 bookie nodes and 2 broker nodes will be deployed.
final KsnCluster cluster = new KsnCluster();
/* TODO: add your tests... */
// Stop the KSN cluster.
cluster.close();
```
You can customize the configs by passing a customized `KsnClusterConfig` object to the `KsnCluster`'s constructor.
```java
final KsnCluster cluster = new KsnCluster(new KsnClusterConfig()
.setNumBrokers(1)
.addConfig("kafkaTenant", "my-tenant"));
```
See [`KsnClusterConfig`](./src/main/java/io/github/bewaremypower/testcontainers/KsnClusterConfig.java) for more details.
### Create a Kafka client to connect a `KsnCluster`
`KsnCluster#getBootstrapServers()` returns the bootstrap servers config to create a Kafka client.
Example:
```java
// Create a Kafka admin
final Properties adminProps = new Properties();
adminProps.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, cluster.getBootstrapServers());
final AdminClient client = AdminClient.create(adminProps);
// Create a Kafka producer
final Properties producerProps = new Properties();
producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, cluster.getBootstrapServers());
producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
final KafkaProducer producer = new KafkaProducer<>(producerProps);
// Create a Kafka consumer
final Properties consumerProps = new Properties();
consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, cluster.getBootstrapServers());
consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "group");
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
final KafkaConsumer consumer = new KafkaConsumer<>(consumerProps);
```