use of org.apache.kafka.streams.processor.api.MockProcessorContext in project kafka by apache.
the class WordCountTransformerTest method test.
@Test
public void test() {
final MockProcessorContext<String, String> context = new MockProcessorContext<>();
// Create and initialize the transformer under test; including its provided store
final WordCountTransformerDemo.MyTransformerSupplier supplier = new WordCountTransformerDemo.MyTransformerSupplier();
for (final StoreBuilder<?> storeBuilder : supplier.stores()) {
final StateStore store = storeBuilder.withLoggingDisabled().build();
store.init(context.getStateStoreContext(), store);
context.getStateStoreContext().register(store, null);
}
final Transformer<String, String, KeyValue<String, String>> transformer = supplier.get();
transformer.init(new org.apache.kafka.streams.processor.MockProcessorContext() {
@Override
public <S extends StateStore> S getStateStore(final String name) {
return context.getStateStore(name);
}
@Override
public <K, V> void forward(final K key, final V value) {
context.forward(new Record<>((String) key, (String) value, 0L));
}
@Override
public Cancellable schedule(final Duration interval, final PunctuationType type, final Punctuator callback) {
return context.schedule(interval, type, callback);
}
});
// send a record to the transformer
transformer.transform("key", "alpha beta\tgamma\n\talpha");
// note that the transformer does not forward during transform()
assertTrue(context.forwarded().isEmpty());
// now, we trigger the punctuator, which iterates over the state store and forwards the contents.
context.scheduledPunctuators().get(0).getPunctuator().punctuate(0L);
// finally, we can verify the output.
final List<MockProcessorContext.CapturedForward<? extends String, ? extends String>> capturedForwards = context.forwarded();
final List<MockProcessorContext.CapturedForward<? extends String, ? extends String>> expected = asList(new MockProcessorContext.CapturedForward<>(new Record<>("alpha", "2", 0L)), new MockProcessorContext.CapturedForward<>(new Record<>("beta", "1", 0L)), new MockProcessorContext.CapturedForward<>(new Record<>("gamma", "1", 0L)));
assertThat(capturedForwards, is(expected));
}
use of org.apache.kafka.streams.processor.api.MockProcessorContext in project kafka by apache.
the class MockProcessorContextAPITest method shouldCaptureApplicationAndRecordMetadata.
@Test
public void shouldCaptureApplicationAndRecordMetadata() {
final Properties config = mkProperties(mkMap(mkEntry(StreamsConfig.APPLICATION_ID_CONFIG, "testMetadata"), mkEntry(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "")));
final Processor<String, Object, String, Object> processor = new Processor<String, Object, String, Object>() {
private ProcessorContext<String, Object> context;
@Override
public void init(final ProcessorContext<String, Object> context) {
this.context = context;
}
@Override
public void process(final Record<String, Object> record) {
context.forward(new Record<String, Object>("appId", context.applicationId(), 0L));
context.forward(new Record<String, Object>("taskId", context.taskId(), 0L));
if (context.recordMetadata().isPresent()) {
final RecordMetadata recordMetadata = context.recordMetadata().get();
context.forward(new Record<String, Object>("topic", recordMetadata.topic(), 0L));
context.forward(new Record<String, Object>("partition", recordMetadata.partition(), 0L));
context.forward(new Record<String, Object>("offset", recordMetadata.offset(), 0L));
}
context.forward(new Record<String, Object>("record", record, 0L));
}
};
final MockProcessorContext<String, Object> context = new MockProcessorContext<>(config);
processor.init(context);
processor.process(new Record<>("foo", 5L, 0L));
{
final List<CapturedForward<? extends String, ?>> forwarded = context.forwarded();
final List<CapturedForward<? extends String, ?>> expected = asList(new CapturedForward<>(new Record<>("appId", "testMetadata", 0L)), new CapturedForward<>(new Record<>("taskId", new TaskId(0, 0), 0L)), new CapturedForward<>(new Record<>("record", new Record<>("foo", 5L, 0L), 0L)));
assertThat(forwarded, is(expected));
}
context.resetForwards();
context.setRecordMetadata("t1", 0, 0L);
processor.process(new Record<>("foo", 5L, 0L));
{
final List<CapturedForward<? extends String, ?>> forwarded = context.forwarded();
final List<CapturedForward<? extends String, ?>> expected = asList(new CapturedForward<>(new Record<>("appId", "testMetadata", 0L)), new CapturedForward<>(new Record<>("taskId", new TaskId(0, 0), 0L)), new CapturedForward<>(new Record<>("topic", "t1", 0L)), new CapturedForward<>(new Record<>("partition", 0, 0L)), new CapturedForward<>(new Record<>("offset", 0L, 0L)), new CapturedForward<>(new Record<>("record", new Record<>("foo", 5L, 0L), 0L)));
assertThat(forwarded, is(expected));
}
}
use of org.apache.kafka.streams.processor.api.MockProcessorContext in project kafka by apache.
the class MockProcessorContextAPITest method shouldCapturePunctuator.
@Test
public void shouldCapturePunctuator() {
final Processor<String, Long, Void, Void> processor = new Processor<String, Long, Void, Void>() {
@Override
public void init(final ProcessorContext<Void, Void> context) {
context.schedule(Duration.ofSeconds(1L), PunctuationType.WALL_CLOCK_TIME, timestamp -> context.commit());
}
@Override
public void process(final Record<String, Long> record) {
}
};
final MockProcessorContext<Void, Void> context = new MockProcessorContext<>();
processor.init(context);
final MockProcessorContext.CapturedPunctuator capturedPunctuator = context.scheduledPunctuators().get(0);
assertThat(capturedPunctuator.getInterval(), is(Duration.ofMillis(1000L)));
assertThat(capturedPunctuator.getType(), is(PunctuationType.WALL_CLOCK_TIME));
assertThat(capturedPunctuator.cancelled(), is(false));
final Punctuator punctuator = capturedPunctuator.getPunctuator();
assertThat(context.committed(), is(false));
punctuator.punctuate(1234L);
assertThat(context.committed(), is(true));
}
use of org.apache.kafka.streams.processor.api.MockProcessorContext in project kafka by apache.
the class MockProcessorContextAPITest method fullConstructorShouldSetAllExpectedAttributes.
@Test
public void fullConstructorShouldSetAllExpectedAttributes() {
final Properties config = new Properties();
config.put(StreamsConfig.APPLICATION_ID_CONFIG, "testFullConstructor");
config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "");
config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.Long().getClass());
final File dummyFile = new File("");
final MockProcessorContext<Void, Void> context = new MockProcessorContext<>(config, new TaskId(1, 1), dummyFile);
assertThat(context.applicationId(), is("testFullConstructor"));
assertThat(context.taskId(), is(new TaskId(1, 1)));
assertThat(context.appConfigs().get(StreamsConfig.APPLICATION_ID_CONFIG), is("testFullConstructor"));
assertThat(context.appConfigsWithPrefix("application.").get("id"), is("testFullConstructor"));
assertThat(context.keySerde().getClass(), is(Serdes.String().getClass()));
assertThat(context.valueSerde().getClass(), is(Serdes.Long().getClass()));
assertThat(context.stateDir(), is(dummyFile));
}
use of org.apache.kafka.streams.processor.api.MockProcessorContext in project kafka by apache.
the class MockProcessorContextStateStoreTest method shouldEitherInitOrThrow.
@ParameterizedTest(name = "builder = {0}, timestamped = {1}, caching = {2}, logging = {3}")
@MethodSource(value = "parameters")
public void shouldEitherInitOrThrow(final StoreBuilder<StateStore> builder, final boolean timestamped, final boolean caching, final boolean logging) {
final File stateDir = TestUtils.tempDirectory();
try {
final MockProcessorContext<Void, Void> context = new MockProcessorContext<>(mkProperties(mkMap(mkEntry(StreamsConfig.APPLICATION_ID_CONFIG, ""), mkEntry(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, ""))), new TaskId(0, 0), stateDir);
final StateStore store = builder.build();
if (caching || logging) {
assertThrows(IllegalArgumentException.class, () -> store.init(context.getStateStoreContext(), store));
} else {
store.init(context.getStateStoreContext(), store);
store.close();
}
} finally {
try {
Utils.delete(stateDir);
} catch (final IOException e) {
// Failed to clean up the state dir. The JVM hooks will try again later.
}
}
}
Aggregations