Skip to content

Commit

Permalink
Add tests and reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
raminqaf committed Jun 24, 2024
1 parent bfd6420 commit 866a737
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

package com.bakdata.kafka;

import static org.apache.kafka.streams.StreamsConfig.APPLICATION_SERVER_CONFIG;

import com.bakdata.kafka.StreamsExecutionOptions.StreamsExecutionOptionsBuilder;
import java.util.Optional;
import java.util.function.Consumer;
import lombok.Builder;
import lombok.NonNull;
Expand All @@ -50,5 +53,22 @@ public class RunningStreams {
Topology topology;
@NonNull
KafkaStreams streams;
HostInfo hostInfo;

/**
* Retrieves the host information based on the application server configuration.
*
* @return an {@code Optional} containing the {@link HostInfo} if the application server configuration is not empty;
* otherwise, an empty {@code Optional}.
*/
public Optional<HostInfo> getHostInfo() {
final String applicationServerConfig = this.config.getString(APPLICATION_SERVER_CONFIG);
return applicationServerConfig.isEmpty() ? Optional.empty()
: Optional.of(createHostInfo(applicationServerConfig));
}

private static HostInfo createHostInfo(final String applicationServerConfig) {
final String[] hostAndPort = applicationServerConfig.split(":");
return new HostInfo(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@

package com.bakdata.kafka;

import static org.apache.kafka.streams.StreamsConfig.APPLICATION_SERVER_CONFIG;

import java.util.Optional;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KafkaStreams.CloseOptions;
import org.apache.kafka.streams.KafkaStreams.State;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.state.HostInfo;

/**
* Runs a Kafka Streams application
Expand Down Expand Up @@ -121,7 +117,6 @@ private void runStreams() {
.streams(this.streams)
.config(this.config)
.topology(this.topology)
.hostInfo(this.hostInfo().orElse(null))
.build();
this.executionOptions.onStart(runningStreams);
}
Expand All @@ -134,16 +129,4 @@ private void awaitStreamsShutdown() {
throw new StreamsApplicationException("Error awaiting Streams shutdown", e);
}
}

private Optional<HostInfo> hostInfo() {
final String applicationServerConfig = this.config.getString(APPLICATION_SERVER_CONFIG);
return applicationServerConfig.isEmpty() ? Optional.empty()
: Optional.of(createHostInfo(applicationServerConfig));
}

private static HostInfo createHostInfo(final String applicationServerConfig) {
final String[] hostAndPort = applicationServerConfig.split(":");
return new HostInfo(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* MIT License
*
* Copyright (c) 2024 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 org.assertj.core.api.Assertions.assertThat;

import java.util.Map;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.state.HostInfo;
import org.junit.jupiter.api.Test;

class RunningStreamsTest {

@Test
void shouldHaveHostInfoIfApplicationServiceIsConfigure() {
final Topology topology = new Topology().addSource("test", "test-topic");
final StreamsConfig config = new StreamsConfig(
Map.of(StreamsConfig.APPLICATION_ID_CONFIG, "test-app",
StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092",
StreamsConfig.APPLICATION_SERVER_CONFIG, "localhost:9090"));
final RunningStreams runningStreams = RunningStreams.builder()
.config(config)
.topology(topology)
.streams(new KafkaStreams(topology, config))
.build();
assertThat(runningStreams.getHostInfo())
.hasValue(new HostInfo("localhost", 9090));
}

@Test
void shouldReturnEmptyHostInfoIfApplicationServiceIsNotConfigure() {
final Topology topology = new Topology().addSource("test", "test-topic");
final StreamsConfig config = new StreamsConfig(
Map.of(StreamsConfig.APPLICATION_ID_CONFIG, "test-app",
StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"));
final RunningStreams runningStreams = RunningStreams.builder()
.config(config)
.topology(topology)
.streams(new KafkaStreams(topology, config))
.build();
assertThat(runningStreams.getHostInfo())
.isEmpty();
}
}

0 comments on commit 866a737

Please sign in to comment.