Search in sources :

Example 26 with TestInputTopic

use of org.apache.kafka.streams.TestInputTopic in project kafka by apache.

the class AbstractJoinIntegrationTest method runTestWithDriver.

void runTestWithDriver(final List<List<TestRecord<Long, String>>> expectedResult, final String storeName) {
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(STREAMS_CONFIG), STREAMS_CONFIG)) {
        final TestInputTopic<Long, String> right = driver.createInputTopic(INPUT_TOPIC_RIGHT, new LongSerializer(), new StringSerializer());
        final TestInputTopic<Long, String> left = driver.createInputTopic(INPUT_TOPIC_LEFT, new LongSerializer(), new StringSerializer());
        final TestOutputTopic<Long, String> outputTopic = driver.createOutputTopic(OUTPUT_TOPIC, new LongDeserializer(), new StringDeserializer());
        final Map<String, TestInputTopic<Long, String>> testInputTopicMap = new HashMap<>();
        testInputTopicMap.put(INPUT_TOPIC_RIGHT, right);
        testInputTopicMap.put(INPUT_TOPIC_LEFT, left);
        TestRecord<Long, String> expectedFinalResult = null;
        final long firstTimestamp = time.milliseconds();
        long eventTimestamp = firstTimestamp;
        final Iterator<List<TestRecord<Long, String>>> resultIterator = expectedResult.iterator();
        for (final Input<String> singleInputRecord : input) {
            testInputTopicMap.get(singleInputRecord.topic).pipeInput(singleInputRecord.record.key, singleInputRecord.record.value, ++eventTimestamp);
            final List<TestRecord<Long, String>> expected = resultIterator.next();
            if (expected != null) {
                final List<TestRecord<Long, String>> updatedExpected = new LinkedList<>();
                for (final TestRecord<Long, String> record : expected) {
                    updatedExpected.add(new TestRecord<>(record.key(), record.value(), null, firstTimestamp + record.timestamp()));
                }
                final List<TestRecord<Long, String>> output = outputTopic.readRecordsToList();
                assertThat(output, equalTo(updatedExpected));
                expectedFinalResult = updatedExpected.get(expected.size() - 1);
            }
        }
        if (storeName != null) {
            checkQueryableStore(storeName, expectedFinalResult, driver);
        }
    }
}
Also used : LongSerializer(org.apache.kafka.common.serialization.LongSerializer) HashMap(java.util.HashMap) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) LinkedList(java.util.LinkedList) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) TestInputTopic(org.apache.kafka.streams.TestInputTopic) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord)

Example 27 with TestInputTopic

use of org.apache.kafka.streams.TestInputTopic in project kafka by apache.

the class AbstractJoinIntegrationTest method runTestWithDriver.

void runTestWithDriver(final TestRecord<Long, String> expectedFinalResult, final String storeName) throws InterruptedException {
    try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(STREAMS_CONFIG), STREAMS_CONFIG)) {
        final TestInputTopic<Long, String> right = driver.createInputTopic(INPUT_TOPIC_RIGHT, new LongSerializer(), new StringSerializer());
        final TestInputTopic<Long, String> left = driver.createInputTopic(INPUT_TOPIC_LEFT, new LongSerializer(), new StringSerializer());
        final TestOutputTopic<Long, String> outputTopic = driver.createOutputTopic(OUTPUT_TOPIC, new LongDeserializer(), new StringDeserializer());
        final Map<String, TestInputTopic<Long, String>> testInputTopicMap = new HashMap<>();
        testInputTopicMap.put(INPUT_TOPIC_RIGHT, right);
        testInputTopicMap.put(INPUT_TOPIC_LEFT, left);
        final long firstTimestamp = time.milliseconds();
        long eventTimestamp = firstTimestamp;
        for (final Input<String> singleInputRecord : input) {
            testInputTopicMap.get(singleInputRecord.topic).pipeInput(singleInputRecord.record.key, singleInputRecord.record.value, ++eventTimestamp);
        }
        final TestRecord<Long, String> updatedExpectedFinalResult = new TestRecord<>(expectedFinalResult.key(), expectedFinalResult.value(), null, firstTimestamp + expectedFinalResult.timestamp());
        final List<TestRecord<Long, String>> output = outputTopic.readRecordsToList();
        assertThat(output.get(output.size() - 1), equalTo(updatedExpectedFinalResult));
        if (storeName != null) {
            checkQueryableStore(storeName, updatedExpectedFinalResult, driver);
        }
    }
}
Also used : LongSerializer(org.apache.kafka.common.serialization.LongSerializer) HashMap(java.util.HashMap) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) LongDeserializer(org.apache.kafka.common.serialization.LongDeserializer) TestInputTopic(org.apache.kafka.streams.TestInputTopic) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) TestRecord(org.apache.kafka.streams.test.TestRecord)

Aggregations

TestInputTopic (org.apache.kafka.streams.TestInputTopic)27 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)27 Properties (java.util.Properties)25 Serdes (org.apache.kafka.common.serialization.Serdes)25 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)25 Consumed (org.apache.kafka.streams.kstream.Consumed)25 Test (org.junit.Test)25 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)24 KeyValueTimestamp (org.apache.kafka.streams.KeyValueTimestamp)21 Duration (java.time.Duration)20 KStream (org.apache.kafka.streams.kstream.KStream)19 StreamsTestUtils (org.apache.kafka.test.StreamsTestUtils)19 Assert.assertEquals (org.junit.Assert.assertEquals)18 Materialized (org.apache.kafka.streams.kstream.Materialized)17 MockApiProcessorSupplier (org.apache.kafka.test.MockApiProcessorSupplier)17 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)17 KeyValue (org.apache.kafka.streams.KeyValue)16 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)15 KTable (org.apache.kafka.streams.kstream.KTable)15 Arrays.asList (java.util.Arrays.asList)14