forked from mitch-seymour/mastering-kafka-streams-and-ksqldb
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e22ded2
commit 0f098b9
Showing
4 changed files
with
123 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 50 additions & 55 deletions
105
chapter-02/hello-streams/src/test/java/com/example/DslExampleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,68 @@ | ||
package com.example; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import org.apache.kafka.common.serialization.Serdes; | ||
import org.apache.kafka.streams.StreamsBuilder; | ||
import org.apache.kafka.streams.TestInputTopic; | ||
import org.apache.kafka.streams.Topology; | ||
import org.apache.kafka.streams.TopologyTestDriver; | ||
import org.apache.kafka.streams.kstream.KStream; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class DslExampleTest { | ||
private TopologyTestDriver testDriver; | ||
private TestInputTopic<Void, String> inputTopic; | ||
|
||
|
||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); | ||
private final PrintStream originalOut = System.out; | ||
private final PrintStream originalErr = System.err; | ||
|
||
@BeforeEach | ||
protected void setUp() throws Exception { | ||
System.setOut(new PrintStream(outContent)); | ||
System.setErr(new PrintStream(errContent)); | ||
|
||
|
||
// the builder is used to construct the topology | ||
StreamsBuilder builder = new StreamsBuilder(); | ||
private TopologyTestDriver testDriver; | ||
private TestInputTopic<Void, String> inputTopic; | ||
|
||
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); | ||
private final PrintStream originalOut = System.out; | ||
private final PrintStream originalErr = System.err; | ||
|
||
@BeforeEach | ||
protected void setUp() throws Exception { | ||
System.setOut(new PrintStream(outContent)); | ||
System.setErr(new PrintStream(errContent)); | ||
|
||
// the builder is used to construct the topology | ||
StreamsBuilder builder = new StreamsBuilder(); | ||
|
||
// read from the source topic, "users" | ||
KStream<Void, String> stream = builder.stream("users"); | ||
|
||
// for each record that appears in the source topic, | ||
// print the value (as SayHelloProcessor) | ||
stream.foreach( | ||
(key, value) -> { | ||
System.out.println("(DSL) Hello, " + value); | ||
}); | ||
|
||
// the builder is used to construct the topology | ||
Topology topology = builder.build(); | ||
|
||
// read from the source topic, "users" | ||
KStream<Void, String> stream = builder.stream("users"); | ||
// create a test driver. we will use this to pipe data to our topology | ||
testDriver = Utils.createTestDriverAndTestTopic(topology); | ||
|
||
// for each record that appears in the source topic, | ||
// print the value (as SayHelloProcessor) | ||
stream.foreach( | ||
(key, value) -> { | ||
System.out.println("(DSL) Hello, " + value); | ||
}); | ||
|
||
|
||
// the builder is used to construct the topology | ||
Topology topology = builder.build(); | ||
// create the test input topic | ||
inputTopic = | ||
testDriver.createInputTopic( | ||
"users", Serdes.Void().serializer(), Serdes.String().serializer()); | ||
} | ||
|
||
|
||
|
||
// create a test driver. we will use this to pipe data to our topology | ||
testDriver = Utils.createTestDriverAndTestTopic(topology); | ||
|
||
// create the test input topic | ||
inputTopic = | ||
testDriver.createInputTopic( | ||
"users", Serdes.Void().serializer(), Serdes.String().serializer()); | ||
|
||
} | ||
@Test | ||
public void testMain() { | ||
// stimulate to use topic named "users" | ||
inputTopic.pipeInput("Robert"); | ||
assertThat(outContent.toString()).isEqualTo("(DSL) Hello, Robert" + System.lineSeparator()); | ||
} | ||
|
||
@Test | ||
public void testMain() { | ||
//stimulate to use topic named "users" | ||
inputTopic.pipeInput( "Robert"); | ||
assertThat(outContent.toString()).isEqualTo("(DSL) Hello, Robert"+System.lineSeparator() ); | ||
} | ||
@AfterEach | ||
public void restoreStreams() { | ||
System.setOut(originalOut); | ||
System.setErr(originalErr); | ||
} | ||
@AfterEach | ||
public void restoreStreams() { | ||
System.setOut(originalOut); | ||
System.setErr(originalErr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 26 additions & 27 deletions
53
chapter-02/hello-streams/src/test/java/com/example/Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,37 @@ | ||
package com.example; | ||
|
||
import java.util.Properties; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.common.serialization.Serdes; | ||
import org.apache.kafka.streams.StreamsConfig; | ||
import org.apache.kafka.streams.Topology; | ||
import org.apache.kafka.streams.TopologyTestDriver; | ||
|
||
public class Utils { | ||
/*** | ||
* set the required properties for running Kafka Streams | ||
* **/ | ||
static Properties createPros() { | ||
Properties config = new Properties(); | ||
config.put(StreamsConfig.APPLICATION_ID_CONFIG, "test"); | ||
config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummy:1234"); | ||
config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.Void().getClass()); | ||
config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); | ||
return config; | ||
} | ||
/**** | ||
* create a test driver. we will use this to pipe data to our topology | ||
* **/ | ||
static TopologyTestDriver createTestDriverAndTestTopic(final Topology topology) { | ||
// set the required properties for running Kafka Streams | ||
Properties config = createPros(); | ||
// create a test driver. we will use this to pipe data to our topology | ||
TopologyTestDriver testDriver = new TopologyTestDriver(topology, config); | ||
return testDriver; | ||
} | ||
|
||
/*** | ||
* set the required properties for running Kafka Streams | ||
* **/ | ||
static Properties createPros() { | ||
Properties config = new Properties(); | ||
config.put(StreamsConfig.APPLICATION_ID_CONFIG, "test"); | ||
config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummy:1234"); | ||
config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.Void().getClass()); | ||
config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass()); | ||
return config; | ||
} | ||
|
||
/**** | ||
* create a test driver. we will use this to pipe data to our topology | ||
* **/ | ||
static TopologyTestDriver createTestDriverAndTestTopic(final Topology topology) { | ||
// set the required properties for running Kafka Streams | ||
Properties config = createPros(); | ||
|
||
// create a test driver. we will use this to pipe data to our topology | ||
TopologyTestDriver testDriver = new TopologyTestDriver(topology, config); | ||
|
||
return testDriver; | ||
} | ||
} |