use of org.apache.kafka.streams.TopologyTestDriver in project kafka by apache.
the class KTableAggregateTest method testAggRepartition.
@Test
public void testAggRepartition() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final KTable<String, String> table1 = builder.table(topic1, consumed);
final KTable<String, String> table2 = table1.groupBy((key, value) -> {
switch(key) {
case "null":
return KeyValue.pair(null, value);
case "NULL":
return null;
default:
return KeyValue.pair(value, value);
}
}, stringSerialized).aggregate(MockInitializer.STRING_INIT, MockAggregator.TOSTRING_ADDER, MockAggregator.TOSTRING_REMOVER, Materialized.<String, String, KeyValueStore<Bytes, byte[]>>as("topic1-Canonized").withValueSerde(stringSerde));
table2.toStream().process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), CONFIG, Instant.ofEpochMilli(0L))) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(topic1, new StringSerializer(), new StringSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
inputTopic.pipeInput("A", "1", 10L);
inputTopic.pipeInput("A", (String) null, 15L);
inputTopic.pipeInput("A", "1", 12L);
inputTopic.pipeInput("B", "2", 20L);
inputTopic.pipeInput("null", "3", 25L);
inputTopic.pipeInput("B", "4", 23L);
inputTopic.pipeInput("NULL", "5", 24L);
inputTopic.pipeInput("B", "7", 22L);
assertEquals(asList(new KeyValueTimestamp<>("1", "0+1", 10), new KeyValueTimestamp<>("1", "0+1-1", 15), new KeyValueTimestamp<>("1", "0+1-1+1", 15), new KeyValueTimestamp<>("2", "0+2", 20), new KeyValueTimestamp<>("2", "0+2-2", 23), new KeyValueTimestamp<>("4", "0+4", 23), new KeyValueTimestamp<>("4", "0+4-4", 23), new KeyValueTimestamp<>("7", "0+7", 22)), supplier.theCapturedProcessor().processed());
}
}
use of org.apache.kafka.streams.TopologyTestDriver in project kafka by apache.
the class KTableAggregateTest method testCountHelper.
private static void testCountHelper(final StreamsBuilder builder, final String input, final MockApiProcessorSupplier<String, Object, Void, Void> supplier) {
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), CONFIG, Instant.ofEpochMilli(0L))) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(input, new StringSerializer(), new StringSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
inputTopic.pipeInput("A", "green", 10L);
inputTopic.pipeInput("B", "green", 9L);
inputTopic.pipeInput("A", "blue", 12L);
inputTopic.pipeInput("C", "yellow", 15L);
inputTopic.pipeInput("D", "green", 11L);
assertEquals(asList(new KeyValueTimestamp<>("green", 1L, 10), new KeyValueTimestamp<>("green", 2L, 10), new KeyValueTimestamp<>("green", 1L, 12), new KeyValueTimestamp<>("blue", 1L, 12), new KeyValueTimestamp<>("yellow", 1L, 15), new KeyValueTimestamp<>("green", 2L, 12)), supplier.theCapturedProcessor().processed());
}
}
use of org.apache.kafka.streams.TopologyTestDriver in project kafka by apache.
the class KTableImplTest method testStateStoreLazyEval.
@Test
public void testStateStoreLazyEval() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final String topic2 = "topic2";
final KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed);
builder.table(topic2, consumed);
final KTableImpl<String, String, Integer> table1Mapped = (KTableImpl<String, String, Integer>) table1.mapValues(s -> Integer.valueOf(s));
table1Mapped.filter((key, value) -> (value % 2) == 0);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
assertEquals(0, driver.getAllStateStores().size());
}
}
use of org.apache.kafka.streams.TopologyTestDriver in project kafka by apache.
the class KTableImplTest method testMaterializedKTable.
@Test
public void testMaterializedKTable() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final String topic2 = "topic2";
final KTable<String, String> table1 = builder.table(topic1, consumed, Materialized.as("fred"));
final MockApiProcessorSupplier<String, Object, Void, Void> supplier = new MockApiProcessorSupplier<>();
table1.toStream().process(supplier);
final KTable<String, Integer> table2 = table1.mapValues(s -> Integer.valueOf(s));
table2.toStream().process(supplier);
final KTable<String, Integer> table3 = table2.filter((key, value) -> (value % 2) == 0);
table3.toStream().process(supplier);
table1.toStream().to(topic2, produced);
final KTable<String, String> table4 = builder.table(topic2, consumed);
table4.toStream().process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<String, String> inputTopic = driver.createInputTopic(topic1, new StringSerializer(), new StringSerializer());
inputTopic.pipeInput("A", "01", 5L);
inputTopic.pipeInput("B", "02", 100L);
inputTopic.pipeInput("C", "03", 0L);
inputTopic.pipeInput("D", "04", 0L);
inputTopic.pipeInput("A", "05", 10L);
inputTopic.pipeInput("A", "06", 8L);
}
final List<MockApiProcessor<String, Object, Void, Void>> processors = supplier.capturedProcessors(4);
assertEquals(asList(new KeyValueTimestamp<>("A", "01", 5), new KeyValueTimestamp<>("B", "02", 100), new KeyValueTimestamp<>("C", "03", 0), new KeyValueTimestamp<>("D", "04", 0), new KeyValueTimestamp<>("A", "05", 10), new KeyValueTimestamp<>("A", "06", 8)), processors.get(0).processed());
assertEquals(asList(new KeyValueTimestamp<>("A", 1, 5), new KeyValueTimestamp<>("B", 2, 100), new KeyValueTimestamp<>("C", 3, 0), new KeyValueTimestamp<>("D", 4, 0), new KeyValueTimestamp<>("A", 5, 10), new KeyValueTimestamp<>("A", 6, 8)), processors.get(1).processed());
assertEquals(asList(new KeyValueTimestamp<>("B", 2, 100), new KeyValueTimestamp<>("D", 4, 0), new KeyValueTimestamp<>("A", 6, 8)), processors.get(2).processed());
assertEquals(asList(new KeyValueTimestamp<>("A", "01", 5), new KeyValueTimestamp<>("B", "02", 100), new KeyValueTimestamp<>("C", "03", 0), new KeyValueTimestamp<>("D", "04", 0), new KeyValueTimestamp<>("A", "05", 10), new KeyValueTimestamp<>("A", "06", 8)), processors.get(3).processed());
}
use of org.apache.kafka.streams.TopologyTestDriver in project kafka by apache.
the class KTableImplTest method testStateStore.
@Test
public void testStateStore() {
final StreamsBuilder builder = new StreamsBuilder();
final String topic1 = "topic1";
final String topic2 = "topic2";
final KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(topic1, consumed);
final KTableImpl<String, String, String> table2 = (KTableImpl<String, String, String>) builder.table(topic2, consumed);
final KTableImpl<String, String, Integer> table1Mapped = (KTableImpl<String, String, Integer>) table1.mapValues(s -> Integer.valueOf(s));
final KTableImpl<String, Integer, Integer> table1MappedFiltered = (KTableImpl<String, Integer, Integer>) table1Mapped.filter((key, value) -> (value % 2) == 0);
table2.join(table1MappedFiltered, (v1, v2) -> v1 + v2);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
assertEquals(2, driver.getAllStateStores().size());
}
}
Aggregations