use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.
the class KStreamMapTest method testMap.
@Test
public void testMap() {
KStreamBuilder builder = new KStreamBuilder();
KeyValueMapper<Integer, String, KeyValue<String, Integer>> mapper = new KeyValueMapper<Integer, String, KeyValue<String, Integer>>() {
@Override
public KeyValue<String, Integer> apply(Integer key, String value) {
return KeyValue.pair(value, key);
}
};
final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
KStream<Integer, String> stream = builder.stream(intSerde, stringSerde, topicName);
MockProcessorSupplier<String, Integer> processor;
processor = new MockProcessorSupplier<>();
stream.map(mapper).process(processor);
driver = new KStreamTestDriver(builder);
for (int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, "V" + expectedKey);
}
assertEquals(4, processor.processed.size());
String[] expected = new String[] { "V0:0", "V1:1", "V2:2", "V3:3" };
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], processor.processed.get(i));
}
}
use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.
the class KStreamMapValuesTest method testFlatMapValues.
@Test
public void testFlatMapValues() {
KStreamBuilder builder = new KStreamBuilder();
ValueMapper<CharSequence, Integer> mapper = new ValueMapper<CharSequence, Integer>() {
@Override
public Integer apply(CharSequence value) {
return value.length();
}
};
final int[] expectedKeys = { 1, 10, 100, 1000 };
KStream<Integer, String> stream;
MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
stream = builder.stream(intSerde, stringSerde, topicName);
stream.mapValues(mapper).process(processor);
driver = new KStreamTestDriver(builder);
for (int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, Integer.toString(expectedKey));
}
assertEquals(4, processor.processed.size());
String[] expected = { "1:1", "10:2", "100:3", "1000:4" };
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], processor.processed.get(i));
}
}
use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.
the class KStreamPeekTest method shouldObserveStreamElements.
@Test
public void shouldObserveStreamElements() {
final KStreamBuilder builder = new KStreamBuilder();
final KStream<Integer, String> stream = builder.stream(Serdes.Integer(), Serdes.String(), topicName);
final List<KeyValue<Integer, String>> peekObserved = new ArrayList<>(), streamObserved = new ArrayList<>();
stream.peek(collect(peekObserved)).foreach(collect(streamObserved));
driver = new KStreamTestDriver(builder);
final List<KeyValue<Integer, String>> expected = new ArrayList<>();
for (int key = 0; key < 32; key++) {
final String value = "V" + key;
driver.process(topicName, key, value);
expected.add(new KeyValue<>(key, value));
}
assertEquals(expected, peekObserved);
assertEquals(expected, streamObserved);
}
use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.
the class KStreamTransformTest method testTransform.
@Test
public void testTransform() {
KStreamBuilder builder = new KStreamBuilder();
TransformerSupplier<Number, Number, KeyValue<Integer, Integer>> transformerSupplier = new TransformerSupplier<Number, Number, KeyValue<Integer, Integer>>() {
public Transformer<Number, Number, KeyValue<Integer, Integer>> get() {
return new Transformer<Number, Number, KeyValue<Integer, Integer>>() {
private int total = 0;
@Override
public void init(ProcessorContext context) {
}
@Override
public KeyValue<Integer, Integer> transform(Number key, Number value) {
total += value.intValue();
return KeyValue.pair(key.intValue() * 2, total);
}
@Override
public KeyValue<Integer, Integer> punctuate(long timestamp) {
return KeyValue.pair(-1, (int) timestamp);
}
@Override
public void close() {
}
};
}
};
final int[] expectedKeys = { 1, 10, 100, 1000 };
MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
KStream<Integer, Integer> stream = builder.stream(intSerde, intSerde, topicName);
stream.transform(transformerSupplier).process(processor);
driver = new KStreamTestDriver(builder);
for (int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, expectedKey * 10);
}
driver.punctuate(2);
driver.punctuate(3);
assertEquals(6, processor.processed.size());
String[] expected = { "2:10", "20:110", "200:1110", "2000:11110", "-1:2", "-1:3" };
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], processor.processed.get(i));
}
}
use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.
the class KTableKTableOuterJoinTest method testSendingOldValue.
@Test
public void testSendingOldValue() throws Exception {
final KStreamBuilder builder = new KStreamBuilder();
final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
KTable<Integer, String> table1;
KTable<Integer, String> table2;
KTable<Integer, String> joined;
MockProcessorSupplier<Integer, String> proc;
table1 = builder.table(intSerde, stringSerde, topic1, storeName1);
table2 = builder.table(intSerde, stringSerde, topic2, storeName2);
joined = table1.outerJoin(table2, MockValueJoiner.TOSTRING_JOINER);
((KTableImpl<?, ?, ?>) joined).enableSendingOldValues();
proc = new MockProcessorSupplier<>();
builder.addProcessor("proc", proc, ((KTableImpl<?, ?, ?>) joined).name);
driver = new KStreamTestDriver(builder, stateDir);
assertTrue(((KTableImpl<?, ?, ?>) table1).sendingOldValueEnabled());
assertTrue(((KTableImpl<?, ?, ?>) table2).sendingOldValueEnabled());
assertTrue(((KTableImpl<?, ?, ?>) joined).sendingOldValueEnabled());
for (int i = 0; i < 2; i++) {
driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+null<-null)", "1:(X1+null<-null)");
for (int i = 0; i < 2; i++) {
driver.process(topic2, expectedKeys[i], "Y" + expectedKeys[i]);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+Y0<-X0+null)", "1:(X1+Y1<-X1+null)");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "X" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+Y0<-X0+Y0)", "1:(X1+Y1<-X1+Y1)", "2:(X2+null<-null)", "3:(X3+null<-null)");
// push all items to the other stream. this should produce four items.
for (int expectedKey : expectedKeys) {
driver.process(topic2, expectedKey, "YY" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+YY0<-X0+Y0)", "1:(X1+YY1<-X1+Y1)", "2:(X2+YY2<-X2+null)", "3:(X3+YY3<-X3+null)");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "X" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+YY0<-X0+YY0)", "1:(X1+YY1<-X1+YY1)", "2:(X2+YY2<-X2+YY2)", "3:(X3+YY3<-X3+YY3)");
for (int i = 0; i < 2; i++) {
driver.process(topic2, expectedKeys[i], null);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(X0+null<-X0+YY0)", "1:(X1+null<-X1+YY1)");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "XX" + expectedKey);
}
driver.flushState();
proc.checkAndClearProcessResult("0:(XX0+null<-X0+null)", "1:(XX1+null<-X1+null)", "2:(XX2+YY2<-X2+YY2)", "3:(XX3+YY3<-X3+YY3)");
for (int i = 1; i < 3; i++) {
driver.process(topic1, expectedKeys[i], null);
}
driver.flushState();
proc.checkAndClearProcessResult("1:(null<-XX1+null)", "2:(null+YY2<-XX2+YY2)");
}
Aggregations