Search in sources :

Example 1 with MockProcessor

use of org.apache.kafka.test.MockProcessor in project kafka by apache.

the class KStreamBranchTest method testKStreamBranch.

// Old PAPI. Needs to be migrated.
@SuppressWarnings({ "unchecked", "deprecation" })
@Test
public void testKStreamBranch() {
    final StreamsBuilder builder = new StreamsBuilder();
    final Predicate<Integer, String> isEven = (key, value) -> (key % 2) == 0;
    final Predicate<Integer, String> isMultipleOfThree = (key, value) -> (key % 3) == 0;
    final Predicate<Integer, String> isOdd = (key, value) -> (key % 2) != 0;
    final int[] expectedKeys = new int[] { 1, 2, 3, 4, 5, 6 };
    final KStream<Integer, String> stream;
    final KStream<Integer, String>[] branches;
    stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
    branches = stream.branch(isEven, isMultipleOfThree, isOdd);
    assertEquals(3, branches.length);
    final MockProcessorSupplier<Integer, String> supplier = new MockProcessorSupplier<>();
    for (final KStream<Integer, String> branch : branches) {
        branch.process(supplier);
    }
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
        final TestInputTopic<Integer, String> inputTopic = driver.createInputTopic(topicName, new IntegerSerializer(), new StringSerializer());
        for (final int expectedKey : expectedKeys) {
            inputTopic.pipeInput(expectedKey, "V" + expectedKey);
        }
    }
    final List<MockProcessor<Integer, String>> processors = supplier.capturedProcessors(3);
    assertEquals(3, processors.get(0).processed().size());
    assertEquals(1, processors.get(1).processed().size());
    assertEquals(2, processors.get(2).processed().size());
}
Also used : TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Properties(java.util.Properties) Consumed(org.apache.kafka.streams.kstream.Consumed) Test(org.junit.Test) KStream(org.apache.kafka.streams.kstream.KStream) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) List(java.util.List) Predicate(org.apache.kafka.streams.kstream.Predicate) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestInputTopic(org.apache.kafka.streams.TestInputTopic) StreamsTestUtils(org.apache.kafka.test.StreamsTestUtils) MockProcessor(org.apache.kafka.test.MockProcessor) Assert.assertEquals(org.junit.Assert.assertEquals) KStream(org.apache.kafka.streams.kstream.KStream) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) MockProcessor(org.apache.kafka.test.MockProcessor) IntegerSerializer(org.apache.kafka.common.serialization.IntegerSerializer) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Test(org.junit.Test)

Aggregations

List (java.util.List)1 Properties (java.util.Properties)1 IntegerSerializer (org.apache.kafka.common.serialization.IntegerSerializer)1 Serdes (org.apache.kafka.common.serialization.Serdes)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)1 TestInputTopic (org.apache.kafka.streams.TestInputTopic)1 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)1 Consumed (org.apache.kafka.streams.kstream.Consumed)1 KStream (org.apache.kafka.streams.kstream.KStream)1 Predicate (org.apache.kafka.streams.kstream.Predicate)1 MockProcessor (org.apache.kafka.test.MockProcessor)1 MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)1 StreamsTestUtils (org.apache.kafka.test.StreamsTestUtils)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Test (org.junit.Test)1