use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.
the class KStreamKStreamLeftJoinTest method testLeftJoin.
@Test
public void testLeftJoin() throws Exception {
final KStreamBuilder builder = new KStreamBuilder();
final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
final KStream<Integer, String> stream1;
final KStream<Integer, String> stream2;
final KStream<Integer, String> joined;
final MockProcessorSupplier<Integer, String> processor;
processor = new MockProcessorSupplier<>();
stream1 = builder.stream(intSerde, stringSerde, topic1);
stream2 = builder.stream(intSerde, stringSerde, topic2);
joined = stream1.leftJoin(stream2, MockValueJoiner.TOSTRING_JOINER, JoinWindows.of(100), intSerde, stringSerde, stringSerde);
joined.process(processor);
final Collection<Set<String>> copartitionGroups = builder.copartitionGroups();
assertEquals(1, copartitionGroups.size());
assertEquals(new HashSet<>(Arrays.asList(topic1, topic2)), copartitionGroups.iterator().next());
driver = new KStreamTestDriver(builder, stateDir);
driver.setTime(0L);
for (int i = 0; i < 2; i++) {
driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
}
driver.flushState();
processor.checkAndClearProcessResult("0:X0+null", "1:X1+null");
for (int i = 0; i < 2; i++) {
driver.process(topic2, expectedKeys[i], "Y" + expectedKeys[i]);
}
driver.flushState();
processor.checkAndClearProcessResult("0:X0+Y0", "1:X1+Y1");
for (int i = 0; i < 3; i++) {
driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
}
driver.flushState();
processor.checkAndClearProcessResult("0:X0+Y0", "1:X1+Y1", "2:X2+null");
for (int expectedKey : expectedKeys) {
driver.process(topic2, expectedKey, "YY" + expectedKey);
}
driver.flushState();
processor.checkAndClearProcessResult("0:X0+YY0", "0:X0+YY0", "1:X1+YY1", "1:X1+YY1", "2:X2+YY2");
for (int expectedKey : expectedKeys) {
driver.process(topic1, expectedKey, "XX" + expectedKey);
}
driver.flushState();
processor.checkAndClearProcessResult("0:XX0+Y0", "0:XX0+YY0", "1:XX1+Y1", "1:XX1+YY1", "2:XX2+YY2", "3:XX3+YY3");
}
use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.
the class KStreamSelectKeyTest method testSelectKey.
@Test
public void testSelectKey() {
KStreamBuilder builder = new KStreamBuilder();
final Map<Number, String> keyMap = new HashMap<>();
keyMap.put(1, "ONE");
keyMap.put(2, "TWO");
keyMap.put(3, "THREE");
KeyValueMapper<Object, Number, String> selector = new KeyValueMapper<Object, Number, String>() {
@Override
public String apply(Object key, Number value) {
return keyMap.get(value);
}
};
final String[] expected = new String[] { "ONE:1", "TWO:2", "THREE:3" };
final int[] expectedValues = new int[] { 1, 2, 3 };
KStream<String, Integer> stream = builder.stream(stringSerde, integerSerde, topicName);
MockProcessorSupplier<String, Integer> processor = new MockProcessorSupplier<>();
stream.selectKey(selector).process(processor);
driver = new KStreamTestDriver(builder);
for (int expectedValue : expectedValues) {
driver.process(topicName, null, expectedValue);
}
assertEquals(3, processor.processed.size());
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 KGroupedStreamImplTest method shouldCountSessionWindows.
@Test
public void shouldCountSessionWindows() throws Exception {
final Map<Windowed<String>, Long> results = new HashMap<>();
groupedStream.count(SessionWindows.with(30), "session-store").foreach(new ForeachAction<Windowed<String>, Long>() {
@Override
public void apply(final Windowed<String> key, final Long value) {
results.put(key, value);
}
});
driver = new KStreamTestDriver(builder, TestUtils.tempDirectory());
driver.setTime(10);
driver.process(TOPIC, "1", "1");
driver.setTime(15);
driver.process(TOPIC, "2", "2");
driver.setTime(30);
driver.process(TOPIC, "1", "1");
driver.setTime(70);
driver.process(TOPIC, "1", "1");
driver.setTime(90);
driver.process(TOPIC, "1", "1");
driver.setTime(100);
driver.process(TOPIC, "1", "1");
driver.flushState();
assertEquals(Long.valueOf(2), results.get(new Windowed<>("1", new SessionWindow(10, 30))));
assertEquals(Long.valueOf(1), results.get(new Windowed<>("2", new SessionWindow(15, 15))));
assertEquals(Long.valueOf(3), results.get(new Windowed<>("1", new SessionWindow(70, 100))));
}
use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.
the class KGroupedStreamImplTest method shouldCountWindowed.
@Test
public void shouldCountWindowed() throws Exception {
final List<KeyValue<Windowed<String>, Long>> results = new ArrayList<>();
groupedStream.count(TimeWindows.of(500L), "aggregate-by-key-windowed").foreach(new ForeachAction<Windowed<String>, Long>() {
@Override
public void apply(final Windowed<String> key, final Long value) {
results.add(KeyValue.pair(key, value));
}
});
driver = new KStreamTestDriver(builder, TestUtils.tempDirectory(), 0);
driver.setTime(0);
driver.process(TOPIC, "1", "A");
driver.process(TOPIC, "2", "B");
driver.process(TOPIC, "3", "C");
driver.setTime(500);
driver.process(TOPIC, "1", "A");
driver.process(TOPIC, "1", "A");
driver.process(TOPIC, "2", "B");
driver.process(TOPIC, "2", "B");
assertThat(results, equalTo(Arrays.asList(KeyValue.pair(new Windowed<>("1", new TimeWindow(0, 500)), 1L), KeyValue.pair(new Windowed<>("2", new TimeWindow(0, 500)), 1L), KeyValue.pair(new Windowed<>("3", new TimeWindow(0, 500)), 1L), KeyValue.pair(new Windowed<>("1", new TimeWindow(500, 1000)), 1L), KeyValue.pair(new Windowed<>("1", new TimeWindow(500, 1000)), 2L), KeyValue.pair(new Windowed<>("2", new TimeWindow(500, 1000)), 1L), KeyValue.pair(new Windowed<>("2", new TimeWindow(500, 1000)), 2L))));
}
use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.
the class KStreamTransformValuesTest method testTransform.
@Test
public void testTransform() {
KStreamBuilder builder = new KStreamBuilder();
ValueTransformerSupplier<Number, Integer> valueTransformerSupplier = new ValueTransformerSupplier<Number, Integer>() {
public ValueTransformer<Number, Integer> get() {
return new ValueTransformer<Number, Integer>() {
private int total = 0;
@Override
public void init(ProcessorContext context) {
}
@Override
public Integer transform(Number value) {
total += value.intValue();
return total;
}
@Override
public Integer punctuate(long timestamp) {
return null;
}
@Override
public void close() {
}
};
}
};
final int[] expectedKeys = { 1, 10, 100, 1000 };
KStream<Integer, Integer> stream;
MockProcessorSupplier<Integer, Integer> processor = new MockProcessorSupplier<>();
stream = builder.stream(intSerde, intSerde, topicName);
stream.transformValues(valueTransformerSupplier).process(processor);
driver = new KStreamTestDriver(builder);
for (int expectedKey : expectedKeys) {
driver.process(topicName, expectedKey, expectedKey * 10);
}
assertEquals(4, processor.processed.size());
String[] expected = { "1:10", "10:110", "100:1110", "1000:11110" };
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], processor.processed.get(i));
}
}
Aggregations