public class MockProcessorContext extends Object implements org.apache.kafka.streams.processor.ProcessorContext, org.apache.kafka.streams.processor.internals.RecordCollector.Supplier
MockProcessorContext is a mock of ProcessorContext for users to test their Processor,
Transformer, and ValueTransformer implementations.
The tests for this class (org.apache.kafka.streams.MockProcessorContextTest) include several behavioral tests that serve as example usage.
Note that this class does not take any automated actions (such as firing scheduled punctuators).
It simply captures any data it witnesses.
If you require more automated tests, we recommend wrapping your Processor in a minimal source-processor-sink
Topology and using the TopologyTestDriver.
| Modifier and Type | Class and Description |
|---|---|
static class |
MockProcessorContext.CapturedForward |
static class |
MockProcessorContext.CapturedPunctuator
MockProcessorContext.CapturedPunctuator holds captured punctuators, along with their scheduling information. |
| Constructor and Description |
|---|
MockProcessorContext()
|
MockProcessorContext(Properties config)
|
MockProcessorContext(Properties config,
org.apache.kafka.streams.processor.TaskId taskId,
File stateDir)
Create a
MockProcessorContext with a specified taskId and null stateDir. |
| Modifier and Type | Method and Description |
|---|---|
Map<String,Object> |
appConfigs() |
Map<String,Object> |
appConfigsWithPrefix(String prefix) |
String |
applicationId() |
void |
commit() |
boolean |
committed()
Whether
ProcessorContext.commit() has been called in this context. |
long |
currentStreamTimeMs() |
long |
currentSystemTimeMs() |
<K,V> void |
forward(K key,
V value) |
<K,V> void |
forward(K key,
V value,
org.apache.kafka.streams.processor.To to) |
List<MockProcessorContext.CapturedForward> |
forwarded()
Get all the forwarded data this context has observed.
|
List<MockProcessorContext.CapturedForward> |
forwarded(String childName)
Get all the forwarded data this context has observed for a specific child by name.
|
<S extends org.apache.kafka.streams.processor.StateStore> |
getStateStore(String name) |
org.apache.kafka.common.header.Headers |
headers()
Returns the headers of the current input record; could be
null if it is not
available. |
org.apache.kafka.common.serialization.Serde<?> |
keySerde() |
org.apache.kafka.streams.StreamsMetrics |
metrics() |
long |
offset() |
int |
partition() |
org.apache.kafka.streams.processor.internals.RecordCollector |
recordCollector() |
void |
register(org.apache.kafka.streams.processor.StateStore store,
org.apache.kafka.streams.processor.StateRestoreCallback stateRestoreCallbackIsIgnoredInMock) |
void |
resetCommit()
Reset the commit capture to
false (whether or not it was previously true). |
void |
resetForwards()
Clear the captured forwarded data.
|
org.apache.kafka.streams.processor.Cancellable |
schedule(Duration interval,
org.apache.kafka.streams.processor.PunctuationType type,
org.apache.kafka.streams.processor.Punctuator callback) |
List<MockProcessorContext.CapturedPunctuator> |
scheduledPunctuators()
Get the punctuators scheduled so far.
|
void |
setCurrentStreamTimeMs(long currentStreamTimeMs) |
void |
setCurrentSystemTimeMs(long currentSystemTimeMs) |
void |
setHeaders(org.apache.kafka.common.header.Headers headers)
The context exposes this metadata for use in the processor.
|
void |
setOffset(long offset)
The context exposes this metadata for use in the processor.
|
void |
setPartition(int partition)
The context exposes this metadata for use in the processor.
|
void |
setRecordMetadata(String topic,
int partition,
long offset,
org.apache.kafka.common.header.Headers headers,
long timestamp)
The context exposes these metadata for use in the processor.
|
void |
setRecordTimestamp(long recordTimestamp)
The context exposes this metadata for use in the processor.
|
void |
setTimestamp(long timestamp)
Deprecated.
Since 3.0.0; use
setRecordTimestamp(long) instead. |
void |
setTopic(String topic)
The context exposes this metadata for use in the processor.
|
File |
stateDir() |
org.apache.kafka.streams.processor.TaskId |
taskId() |
long |
timestamp() |
String |
topic() |
org.apache.kafka.common.serialization.Serde<?> |
valueSerde() |
public MockProcessorContext()
MockProcessorContext with dummy config and taskId and null stateDir.
Most unit tests using this mock won't need to know the taskId,
and most unit tests should be able to get by with the
InMemoryKeyValueStore, so the stateDir won't matter.public MockProcessorContext(Properties config)
MockProcessorContext with dummy taskId and null stateDir.
Most unit tests using this mock won't need to know the taskId,
and most unit tests should be able to get by with the
InMemoryKeyValueStore, so the stateDir won't matter.config - a Properties object, used to configure the context and the processor.public MockProcessorContext(Properties config, org.apache.kafka.streams.processor.TaskId taskId, File stateDir)
MockProcessorContext with a specified taskId and null stateDir.config - a Properties object, used to configure the context and the processor.taskId - a TaskId, which the context makes available via taskId().stateDir - a File, which the context makes available viw stateDir().public String applicationId()
applicationId in interface org.apache.kafka.streams.processor.ProcessorContextpublic org.apache.kafka.streams.processor.TaskId taskId()
taskId in interface org.apache.kafka.streams.processor.ProcessorContextpublic Map<String,Object> appConfigs()
appConfigs in interface org.apache.kafka.streams.processor.ProcessorContextpublic Map<String,Object> appConfigsWithPrefix(String prefix)
appConfigsWithPrefix in interface org.apache.kafka.streams.processor.ProcessorContextpublic long currentSystemTimeMs()
currentSystemTimeMs in interface org.apache.kafka.streams.processor.ProcessorContextpublic long currentStreamTimeMs()
currentStreamTimeMs in interface org.apache.kafka.streams.processor.ProcessorContextpublic org.apache.kafka.common.serialization.Serde<?> keySerde()
keySerde in interface org.apache.kafka.streams.processor.ProcessorContextpublic org.apache.kafka.common.serialization.Serde<?> valueSerde()
valueSerde in interface org.apache.kafka.streams.processor.ProcessorContextpublic File stateDir()
stateDir in interface org.apache.kafka.streams.processor.ProcessorContextpublic org.apache.kafka.streams.StreamsMetrics metrics()
metrics in interface org.apache.kafka.streams.processor.ProcessorContextpublic void setRecordMetadata(String topic, int partition, long offset, org.apache.kafka.common.header.Headers headers, long timestamp)
topic - A topic namepartition - A partition numberoffset - A record offsettimestamp - A record timestamppublic void setTopic(String topic)
topic - A topic namepublic void setPartition(int partition)
partition - A partition numberpublic void setOffset(long offset)
offset - A record offsetpublic void setHeaders(org.apache.kafka.common.header.Headers headers)
headers - Record headers@Deprecated public void setTimestamp(long timestamp)
setRecordTimestamp(long) instead.timestamp - A record timestamppublic void setRecordTimestamp(long recordTimestamp)
recordTimestamp - A record timestamppublic void setCurrentSystemTimeMs(long currentSystemTimeMs)
public void setCurrentStreamTimeMs(long currentStreamTimeMs)
public String topic()
topic in interface org.apache.kafka.streams.processor.ProcessorContextpublic int partition()
partition in interface org.apache.kafka.streams.processor.ProcessorContextpublic long offset()
offset in interface org.apache.kafka.streams.processor.ProcessorContextpublic org.apache.kafka.common.header.Headers headers()
null if it is not
available.
Note, that headers should never be null in the actual Kafka Streams runtime,
even if they could be empty. However, this mock does not guarantee non-null headers.
Thus, you either need to add a null check to your production code to use this mock
for testing or you always need to set headers manually via setHeaders(Headers) to
avoid a NullPointerException from your Processor implementation.
headers in interface org.apache.kafka.streams.processor.ProcessorContextpublic long timestamp()
timestamp in interface org.apache.kafka.streams.processor.ProcessorContextpublic void register(org.apache.kafka.streams.processor.StateStore store,
org.apache.kafka.streams.processor.StateRestoreCallback stateRestoreCallbackIsIgnoredInMock)
register in interface org.apache.kafka.streams.processor.ProcessorContextpublic <S extends org.apache.kafka.streams.processor.StateStore> S getStateStore(String name)
getStateStore in interface org.apache.kafka.streams.processor.ProcessorContextpublic org.apache.kafka.streams.processor.Cancellable schedule(Duration interval, org.apache.kafka.streams.processor.PunctuationType type, org.apache.kafka.streams.processor.Punctuator callback) throws IllegalArgumentException
schedule in interface org.apache.kafka.streams.processor.ProcessorContextIllegalArgumentExceptionpublic List<MockProcessorContext.CapturedPunctuator> scheduledPunctuators()
schedule(...).public <K,V> void forward(K key,
V value)
forward in interface org.apache.kafka.streams.processor.ProcessorContextpublic <K,V> void forward(K key,
V value,
org.apache.kafka.streams.processor.To to)
forward in interface org.apache.kafka.streams.processor.ProcessorContextpublic List<MockProcessorContext.CapturedForward> forwarded()
forward(...).public List<MockProcessorContext.CapturedForward> forwarded(String childName)
forward(...).childName - The child name to retrieve forwards forpublic void resetForwards()
public void commit()
commit in interface org.apache.kafka.streams.processor.ProcessorContextpublic boolean committed()
ProcessorContext.commit() has been called in this context.true iff ProcessorContext.commit() has been called in this context since construction or reset.public void resetCommit()
false (whether or not it was previously true).public org.apache.kafka.streams.processor.internals.RecordCollector recordCollector()
recordCollector in interface org.apache.kafka.streams.processor.internals.RecordCollector.Supplier