Search in sources :

Example 96 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class KTableAggregateTest method testCountWithInternalStore.

@Test
public void testCountWithInternalStore() {
    final StreamsBuilder builder = new StreamsBuilder();
    final String input = "count-test-input";
    final MockProcessorSupplier<String, Long> proc = new MockProcessorSupplier<>();
    builder.table(input, consumed).groupBy(MockMapper.<String, String>selectValueKeyValueMapper(), stringSerialzied).count().toStream().process(proc);
    testCountHelper(builder, input, proc);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 97 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class KTableAggregateTest method testAggRepartition.

@Test
public void testAggRepartition() {
    final StreamsBuilder builder = new StreamsBuilder();
    final String topic1 = "topic1";
    final MockProcessorSupplier<String, String> proc = new MockProcessorSupplier<>();
    KTable<String, String> table1 = builder.table(topic1, consumed);
    KTable<String, String> table2 = table1.groupBy(new KeyValueMapper<String, String, KeyValue<String, String>>() {

        @Override
        public KeyValue<String, String> apply(String key, String value) {
            switch(key) {
                case "null":
                    return KeyValue.pair(null, value);
                case "NULL":
                    return null;
                default:
                    return KeyValue.pair(value, value);
            }
        }
    }, stringSerialzied).aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, MockAggregator.TOSTRING_REMOVER, stringSerde, "topic1-Canonized");
    table2.toStream().process(proc);
    driver.setUp(builder, stateDir);
    driver.process(topic1, "A", "1");
    driver.flushState();
    driver.process(topic1, "A", null);
    driver.flushState();
    driver.process(topic1, "A", "1");
    driver.flushState();
    driver.process(topic1, "B", "2");
    driver.flushState();
    driver.process(topic1, "null", "3");
    driver.flushState();
    driver.process(topic1, "B", "4");
    driver.flushState();
    driver.process(topic1, "NULL", "5");
    driver.flushState();
    driver.process(topic1, "B", "7");
    driver.flushState();
    assertEquals(Utils.mkList("1:0+1", "1:0+1-1", "1:0+1-1+1", "2:0+2", // noop
    "2:0+2-2", "4:0+4", // noop
    "4:0+4-4", "7:0+7"), proc.processed);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) Test(org.junit.Test)

Example 98 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class KTableAggregateTest method testCountCoalesced.

@Test
public void testCountCoalesced() {
    final StreamsBuilder builder = new StreamsBuilder();
    final String input = "count-test-input";
    final MockProcessorSupplier<String, Long> proc = new MockProcessorSupplier<>();
    builder.table(input, consumed).groupBy(MockMapper.<String, String>selectValueKeyValueMapper(), stringSerialzied).count("count").toStream().process(proc);
    driver.setUp(builder, stateDir);
    driver.process(input, "A", "green");
    driver.process(input, "B", "green");
    driver.process(input, "A", "blue");
    driver.process(input, "C", "yellow");
    driver.process(input, "D", "green");
    driver.flushState();
    assertEquals(Utils.mkList("blue:1", "yellow:1", "green:2"), proc.processed);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 99 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class KTableMapKeysTest method testMapKeysConvertingToStream.

@Test
public void testMapKeysConvertingToStream() {
    final StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic_map_keys";
    KTable<Integer, String> table1 = builder.table(topic1, Consumed.with(integerSerde, stringSerde));
    final Map<Integer, String> keyMap = new HashMap<>();
    keyMap.put(1, "ONE");
    keyMap.put(2, "TWO");
    keyMap.put(3, "THREE");
    KeyValueMapper<Integer, String, String> keyMapper = new KeyValueMapper<Integer, String, String>() {

        @Override
        public String apply(Integer key, String value) {
            return keyMap.get(key);
        }
    };
    KStream<String, String> convertedStream = table1.toStream(keyMapper);
    final String[] expected = new String[] { "ONE:V_ONE", "TWO:V_TWO", "THREE:V_THREE" };
    final int[] originalKeys = new int[] { 1, 2, 3 };
    final String[] values = new String[] { "V_ONE", "V_TWO", "V_THREE" };
    MockProcessorSupplier<String, String> processor = new MockProcessorSupplier<>();
    convertedStream.process(processor);
    driver.setUp(builder, stateDir);
    for (int i = 0; i < originalKeys.length; i++) {
        driver.process(topic1, originalKeys[i], values[i]);
    }
    driver.flushState();
    assertEquals(3, processor.processed.size());
    for (int i = 0; i < expected.length; i++) {
        assertEquals(expected[i], processor.processed.get(i));
    }
}
Also used : HashMap(java.util.HashMap) KeyValueMapper(org.apache.kafka.streams.kstream.KeyValueMapper) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 100 with MockProcessorSupplier

use of org.apache.kafka.test.MockProcessorSupplier in project apache-kafka-on-k8s by banzaicloud.

the class KTableMapValuesTest method testQueryableKTable.

@Test
public void testQueryableKTable() {
    final StreamsBuilder builder = new StreamsBuilder();
    String topic1 = "topic1";
    KTable<String, String> table1 = builder.table(topic1, consumed);
    KTable<String, Integer> table2 = table1.mapValues(new ValueMapper<CharSequence, Integer>() {

        @Override
        public Integer apply(CharSequence value) {
            return value.charAt(0) - 48;
        }
    }, Materialized.<String, Integer, KeyValueStore<Bytes, byte[]>>as("anyName").withValueSerde(Serdes.Integer()));
    MockProcessorSupplier<String, Integer> proc2 = new MockProcessorSupplier<>();
    table2.toStream().process(proc2);
    doTestKTable(builder, topic1, proc2);
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) Test(org.junit.Test)

Aggregations

MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)112 Test (org.junit.Test)109 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)34 HashMap (java.util.HashMap)29 MockStateStoreSupplier (org.apache.kafka.test.MockStateStoreSupplier)27 UUID (java.util.UUID)24 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)24 TaskId (org.apache.kafka.streams.processor.TaskId)24 SubscriptionInfo (org.apache.kafka.streams.processor.internals.assignment.SubscriptionInfo)23 HashSet (java.util.HashSet)21 MockInternalTopicManager (org.apache.kafka.test.MockInternalTopicManager)17 AssignmentInfo (org.apache.kafka.streams.processor.internals.assignment.AssignmentInfo)16 List (java.util.List)14 StreamsBuilderTest (org.apache.kafka.streams.StreamsBuilderTest)14 Metrics (org.apache.kafka.common.metrics.Metrics)13 Utils.mkList (org.apache.kafka.common.utils.Utils.mkList)11 Properties (java.util.Properties)9 TopicsInfo (org.apache.kafka.streams.processor.TopologyBuilder.TopicsInfo)8 InternalTopicConfig (org.apache.kafka.streams.processor.internals.InternalTopicConfig)8 KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)8