use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.
the class KGroupedTableImplTest method shouldReduceWithInternalStoreName.
@Test
public void shouldReduceWithInternalStoreName() {
final KeyValueMapper<String, Number, KeyValue<String, Integer>> intProjection = (key, value) -> KeyValue.pair(key, value.intValue());
final KTable<String, Integer> reduced = builder.table(topic, Consumed.with(Serdes.String(), Serdes.Double()), Materialized.<String, Double, KeyValueStore<Bytes, byte[]>>as("store").withKeySerde(Serdes.String()).withValueSerde(Serdes.Double())).groupBy(intProjection).reduce(MockReducer.INTEGER_ADDER, MockReducer.INTEGER_SUBTRACTOR);
final MockApiProcessorSupplier<String, Integer, Void, Void> supplier = getReducedResults(reduced);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
assertReduced(supplier.theCapturedProcessor().lastValueAndTimestampPerKey(), topic, driver);
assertNull(reduced.queryableStoreName());
}
}
use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.
the class KStreamFilterTest method testFilter.
@Test
public void testFilter() {
final StreamsBuilder builder = new StreamsBuilder();
final int[] expectedKeys = new int[] { 1, 2, 3, 4, 5, 6, 7 };
final KStream<Integer, String> stream;
final MockApiProcessorSupplier<Integer, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String()));
stream.filter(isMultipleOfThree).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);
}
}
assertEquals(2, supplier.theCapturedProcessor().processed().size());
}
use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.
the class KStreamFlatMapValuesTest method testFlatMapValues.
@Test
public void testFlatMapValues() {
final StreamsBuilder builder = new StreamsBuilder();
final ValueMapper<Number, Iterable<String>> mapper = value -> {
final ArrayList<String> result = new ArrayList<>();
result.add("v" + value);
result.add("V" + value);
return result;
};
final int[] expectedKeys = { 0, 1, 2, 3 };
final KStream<Integer, Integer> stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.Integer()));
final MockApiProcessorSupplier<Integer, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
stream.flatMapValues(mapper).process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<Integer, Integer> inputTopic = driver.createInputTopic(topicName, new IntegerSerializer(), new IntegerSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
for (final int expectedKey : expectedKeys) {
// passing the timestamp to inputTopic.create to disambiguate the call
inputTopic.pipeInput(expectedKey, expectedKey, 0L);
}
}
final KeyValueTimestamp[] expected = { new KeyValueTimestamp<>(0, "v0", 0), new KeyValueTimestamp<>(0, "V0", 0), new KeyValueTimestamp<>(1, "v1", 0), new KeyValueTimestamp<>(1, "V1", 0), new KeyValueTimestamp<>(2, "v2", 0), new KeyValueTimestamp<>(2, "V2", 0), new KeyValueTimestamp<>(3, "v3", 0), new KeyValueTimestamp<>(3, "V3", 0) };
assertArrayEquals(expected, supplier.theCapturedProcessor().processed().toArray());
}
use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.
the class KStreamFlatMapValuesTest method testFlatMapValuesWithKeys.
@Test
public void testFlatMapValuesWithKeys() {
final StreamsBuilder builder = new StreamsBuilder();
final ValueMapperWithKey<Integer, Number, Iterable<String>> mapper = (readOnlyKey, value) -> {
final ArrayList<String> result = new ArrayList<>();
result.add("v" + value);
result.add("k" + readOnlyKey);
return result;
};
final int[] expectedKeys = { 0, 1, 2, 3 };
final KStream<Integer, Integer> stream = builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.Integer()));
final MockApiProcessorSupplier<Integer, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
stream.flatMapValues(mapper).process(supplier);
try (final TopologyTestDriver driver = new TopologyTestDriver(builder.build(), props)) {
final TestInputTopic<Integer, Integer> inputTopic = driver.createInputTopic(topicName, new IntegerSerializer(), new IntegerSerializer(), Instant.ofEpochMilli(0L), Duration.ZERO);
for (final int expectedKey : expectedKeys) {
// passing the timestamp to inputTopic.create to disambiguate the call
inputTopic.pipeInput(expectedKey, expectedKey, 0L);
}
}
final KeyValueTimestamp[] expected = { new KeyValueTimestamp<>(0, "v0", 0), new KeyValueTimestamp<>(0, "k0", 0), new KeyValueTimestamp<>(1, "v1", 0), new KeyValueTimestamp<>(1, "k1", 0), new KeyValueTimestamp<>(2, "v2", 0), new KeyValueTimestamp<>(2, "k2", 0), new KeyValueTimestamp<>(3, "v3", 0), new KeyValueTimestamp<>(3, "k3", 0) };
assertArrayEquals(expected, supplier.theCapturedProcessor().processed().toArray());
}
use of org.apache.kafka.test.MockApiProcessorSupplier in project kafka by apache.
the class KStreamGlobalKTableJoinTest method setUp.
@Before
public void setUp() {
builder = new StreamsBuilder();
final KStream<Integer, String> stream;
// value of stream optionally contains key of table
final GlobalKTable<String, String> table;
final KeyValueMapper<Integer, String, String> keyMapper;
final MockApiProcessorSupplier<Integer, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
final Consumed<Integer, String> streamConsumed = Consumed.with(Serdes.Integer(), Serdes.String());
final Consumed<String, String> tableConsumed = Consumed.with(Serdes.String(), Serdes.String());
stream = builder.stream(streamTopic, streamConsumed);
table = builder.globalTable(globalTableTopic, tableConsumed);
keyMapper = (key, value) -> {
final String[] tokens = value.split(",");
// If not present, use null to indicate no match
return tokens.length > 1 ? tokens[1] : null;
};
stream.join(table, keyMapper, MockValueJoiner.TOSTRING_JOINER).process(supplier);
final Properties props = StreamsTestUtils.getStreamsConfig(Serdes.Integer(), Serdes.String());
driver = new TopologyTestDriver(builder.build(), props);
processor = supplier.theCapturedProcessor();
}
Aggregations