Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test module for CLI #276

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ include(
":streams-bootstrap-test",
":streams-bootstrap-large-messages",
":streams-bootstrap-cli",
":streams-bootstrap-cli-test",
)
6 changes: 6 additions & 0 deletions streams-bootstrap-cli-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description = "Utils for testing your Kafka Streams Application"

dependencies {
api(project(":streams-bootstrap-test"))
api(project(":streams-bootstrap-cli"))
}
3 changes: 3 additions & 0 deletions streams-bootstrap-cli-test/lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This file is generated by the 'io.freefair.lombok' Gradle plugin
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* MIT License
*
* Copyright (c) 2025 bakdata
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.bakdata.kafka;

import com.bakdata.fluent_kafka_streams_tests.TestTopology;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public final class TestApplicationHelper {

private final @NonNull TestEnvironment environment;

public ConfiguredStreamsApp<? extends StreamsApp> createConfiguredApp(
final KafkaStreamsApplication<? extends StreamsApp> app) {
this.configure(app);
app.prepareRun();
return app.createConfiguredApp();
}

public <K, V> TestTopology<K, V> createTopology(final KafkaStreamsApplication<? extends StreamsApp> app) {
final ConfiguredStreamsApp<? extends StreamsApp> configuredApp = this.createConfiguredApp(app);
final TestTopologyFactory testTopologyFactory = this.createTestTopologyFactory();
return testTopologyFactory.createTopology(configuredApp);
}

public <K, V> TestTopology<K, V> createTopologyExtension(final KafkaStreamsApplication<? extends StreamsApp> app) {
final ConfiguredStreamsApp<? extends StreamsApp> configuredApp = this.createConfiguredApp(app);
final TestTopologyFactory testTopologyFactory = this.createTestTopologyFactory();
return testTopologyFactory.createTopologyExtension(configuredApp);
}

public void configure(final KafkaStreamsApplication<? extends StreamsApp> app) {
app.setSchemaRegistryUrl(this.environment.getSchemaRegistryUrl());
}

private TestTopologyFactory createTestTopologyFactory() {
return new TestTopologyFactory(this.environment);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* MIT License
*
* Copyright (c) 2025 bakdata
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.bakdata.kafka;

import static java.util.Collections.emptyList;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.List;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public final class TestApplicationRunner {

private final @NonNull String bootstrapServers;
private final @NonNull TestEnvironment environment;

public void run(final KafkaStreamsApplication<? extends StreamsApp> app, final String[] args) {
final String[] newArgs = this.setupArgs(args, emptyList());
final Thread thread = new Thread(() -> KafkaApplication.startApplicationWithoutExit(app, newArgs));
thread.start();
}

public int clean(final KafkaStreamsApplication<? extends StreamsApp> app, final String[] args) {
final String[] newArgs = this.setupArgs(args, List.of("clean"));
return KafkaApplication.startApplicationWithoutExit(app, newArgs);
}

public int reset(final KafkaStreamsApplication<? extends StreamsApp> app, final String[] args) {
final String[] newArgs = this.setupArgs(args, List.of("reset"));
return KafkaApplication.startApplicationWithoutExit(app, newArgs);
}

public Thread run(final KafkaStreamsApplication<? extends StreamsApp> app) {
this.prepareExecution(app);
final Thread thread = new Thread(app);
final UncaughtExceptionHandler handler = new CapturingUncaughtExceptionHandler();
thread.setUncaughtExceptionHandler(handler);
thread.start();
return thread;
}

public void clean(final KafkaStreamsApplication<? extends StreamsApp> app) {
this.prepareExecution(app);
app.clean();
}

public void reset(final KafkaStreamsApplication<? extends StreamsApp> app) {
this.prepareExecution(app);
app.reset();
}

public void prepareExecution(final KafkaStreamsApplication<? extends StreamsApp> app) {
this.configure(app);
app.onApplicationStart();
}

public ConsumerGroupVerifier verify(final KafkaStreamsApplication<? extends StreamsApp> app) {
this.configure(app);
final KafkaEndpointConfig endpointConfig = app.getEndpointConfig();
final KafkaTestClient testClient = new KafkaTestClient(endpointConfig);
try (final ConfiguredStreamsApp<? extends StreamsApp> configuredApp = app.createConfiguredApp()) {
final String uniqueAppId = configuredApp.getUniqueAppId();
return new ConsumerGroupVerifier(uniqueAppId, testClient::admin);
}
}

public KafkaTestClient newTestClient() {
return new KafkaTestClient(KafkaEndpointConfig.builder()
.bootstrapServers(this.bootstrapServers)
.schemaRegistryUrl(this.environment.getSchemaRegistryUrl())
.build());
}

public void configure(final KafkaStreamsApplication<? extends StreamsApp> app) {
app.setBootstrapServers(this.bootstrapServers);
app.setSchemaRegistryUrl(this.environment.getSchemaRegistryUrl());
}

private String[] setupArgs(final String[] args, final Iterable<String> command) {
final Builder<String> argBuilder = ImmutableList.<String>builder()
.add(args)
.add("--bootstrap-servers", this.bootstrapServers);
if (this.environment.getSchemaRegistryUrl() != null) {
argBuilder.add("--schema-registry-url", this.environment.getSchemaRegistryUrl());
}
final List<String> newArgs = argBuilder
.addAll(command)
.build();
return newArgs.toArray(new String[0]);
}

}
1 change: 1 addition & 0 deletions streams-bootstrap-cli/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
testImplementation(group = "org.mockito", name = "mockito-core", version = mockitoVersion)
testImplementation(group = "org.mockito", name = "mockito-junit-jupiter", version = mockitoVersion)
testImplementation(testFixtures(project(":streams-bootstrap-core")))
testImplementation(project(":streams-bootstrap-cli-test"))
testImplementation(group = "com.ginsberg", name = "junit5-system-exit", version = "1.1.2")
val confluentVersion: String by project
testImplementation(group = "io.confluent", name = "kafka-streams-avro-serde", version = confluentVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,36 +257,36 @@ public final CleanableApp<CR> createCleanableApp() {
return cleanableApp;
}

/**
* Create a new {@code ConfiguredApp} that will be executed according to the given config.
*
* @param app app to configure.
* @param configuration configuration for app
* @return {@code ConfiguredApp}
*/
protected abstract CA createConfiguredApp(final A app, AppConfiguration<T> configuration);

/**
* Called before starting the application, e.g., invoking {@link #run()}
*/
protected void onApplicationStart() {
public void onApplicationStart() {
// do nothing by default
}

/**
* Called before running the application, i.e., invoking {@link #run()}
*/
protected void prepareRun() {
public void prepareRun() {
// do nothing by default
}

/**
* Called before cleaning the application, i.e., invoking {@link #clean()}
*/
protected void prepareClean() {
public void prepareClean() {
// do nothing by default
}

/**
* Create a new {@code ConfiguredApp} that will be executed according to the given config.
*
* @param app app to configure.
* @param configuration configuration for app
* @return {@code ConfiguredApp}
*/
protected abstract CA createConfiguredApp(final A app, AppConfiguration<T> configuration);

private void startApplication() {
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
this.onApplicationStart();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2024 bakdata
* Copyright (c) 2025 bakdata
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -154,7 +154,7 @@ public final ConfiguredStreamsApp<T> createConfiguredApp(final T app,
* Called before cleaning the application, i.e., invoking {@link #clean()} or {@link #reset()}
*/
@Override
protected void prepareClean() {
public void prepareClean() {
super.prepareClean();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@

package com.bakdata.kafka.integration;

import static com.bakdata.kafka.TestEnvironment.withoutSchemaRegistry;
import static org.assertj.core.api.Assertions.assertThat;

import com.bakdata.kafka.KafkaStreamsApplication;
import com.bakdata.kafka.KafkaTest;
import com.bakdata.kafka.KafkaTestClient;
import com.bakdata.kafka.SenderBuilder.SimpleProducerRecord;
import com.bakdata.kafka.SimpleKafkaStreamsApplication;
import com.bakdata.kafka.TestApplicationRunner;
import com.bakdata.kafka.TestTopologyFactory;
import com.bakdata.kafka.test_applications.Mirror;
import java.nio.file.Path;
Expand All @@ -53,12 +55,10 @@ void shouldRunApp() {
final KafkaTestClient testClient = this.newTestClient();
testClient.createTopic(output);
try (final KafkaStreamsApplication<?> app = new SimpleKafkaStreamsApplication<>(Mirror::new)) {
app.setBootstrapServers(this.getBootstrapServers());
app.setKafkaConfig(TestTopologyFactory.createStreamsTestConfig(this.stateDir));
app.setInputTopics(List.of(input));
app.setOutputTopic(output);
// run in Thread because the application blocks indefinitely
new Thread(app).start();
new TestApplicationRunner(this.getBootstrapServers(), withoutSchemaRegistry()).run(app);
testClient.send()
.with(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class)
.with(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
package com.bakdata.kafka.integration;


import static com.bakdata.kafka.TestEnvironment.withoutSchemaRegistry;

import com.bakdata.kafka.CloseFlagApp;
import com.bakdata.kafka.KafkaStreamsApplication;
import com.bakdata.kafka.KafkaTest;
import com.bakdata.kafka.KafkaTestClient;
import com.bakdata.kafka.SenderBuilder.SimpleProducerRecord;
import com.bakdata.kafka.SimpleKafkaStreamsApplication;
import com.bakdata.kafka.TestApplicationRunner;
import com.bakdata.kafka.TestTopologyFactory;
import com.bakdata.kafka.test_applications.WordCount;
import com.bakdata.kafka.util.ImprovedAdminClient;
Expand Down Expand Up @@ -84,7 +87,7 @@ void shouldClean() {

// Wait until all stream applications are completely stopped before triggering cleanup
this.awaitClosed(app.createExecutableApp());
app.clean();
this.clean(app);

try (final ImprovedAdminClient admin = testClient.admin()) {
this.softly.assertThat(admin.getTopicClient().exists(app.getOutputTopic()))
Expand Down Expand Up @@ -120,7 +123,7 @@ void shouldReset() {

// Wait until all stream applications are completely stopped before triggering cleanup
this.awaitClosed(app.createExecutableApp());
app.reset();
this.reset(app);

try (final ImprovedAdminClient admin = testClient.admin()) {
this.softly.assertThat(admin.getTopicClient().exists(app.getOutputTopic()))
Expand All @@ -141,26 +144,37 @@ void shouldCallClose() {
this.newTestClient().createTopic(app.getInputTopics().get(0));
this.softly.assertThat(app.isClosed()).isFalse();
this.softly.assertThat(app.isAppClosed()).isFalse();
app.clean();
this.clean(app);
this.softly.assertThat(app.isAppClosed()).isTrue();
app.setAppClosed(false);
app.reset();
this.reset(app);
this.softly.assertThat(app.isAppClosed()).isTrue();
}
}

private void clean(final KafkaStreamsApplication<?> app) {
this.createTestRunner().clean(app);
}

private void reset(final KafkaStreamsApplication<?> app) {
this.createTestRunner().reset(app);
}

private void runAppAndClose(final KafkaStreamsApplication<?> app) {
this.runApp(app);
app.stop();
}

private void runApp(final KafkaStreamsApplication<?> app) {
// run in Thread because the application blocks indefinitely
new Thread(app).start();
this.createTestRunner().run(app);
// Wait until stream application has consumed all data
this.awaitProcessing(app.createExecutableApp());
}

private TestApplicationRunner createTestRunner() {
return new TestApplicationRunner(this.getBootstrapServers(), withoutSchemaRegistry());
}

private CloseFlagApp createCloseFlagApplication() {
final CloseFlagApp app = new CloseFlagApp();
app.setInputTopics(List.of("input"));
Expand Down Expand Up @@ -196,7 +210,6 @@ private KafkaStreamsApplication<?> createWordCountApplication() {
}

private <T extends KafkaStreamsApplication<?>> T configure(final T application) {
application.setBootstrapServers(this.getBootstrapServers());
application.setKafkaConfig(TestTopologyFactory.createStreamsTestConfig(this.stateDir));
return application;
}
Expand Down
Loading