public class MockProcessorContext<KForward,VForward> extends Object implements org.apache.kafka.streams.processor.api.ProcessorContext<KForward,VForward>, 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<K,V> |
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 |
|---|---|
<S extends org.apache.kafka.streams.processor.StateStore> |
addStateStore(S stateStore) |
Map<String,Object> |
appConfigs() |
Map<String,Object> |
appConfigsWithPrefix(String prefix) |
String |
applicationId() |
void |
commit() |
boolean |
committed()
Whether
ProcessingContext.commit() has been called in this context. |
long |
currentStreamTimeMs() |
long |
currentSystemTimeMs() |
<K extends KForward,V extends VForward> |
forward(org.apache.kafka.streams.processor.api.Record<K,V> record) |
<K extends KForward,V extends VForward> |
forward(org.apache.kafka.streams.processor.api.Record<K,V> record,
String childName) |
List<MockProcessorContext.CapturedForward<? extends KForward,? extends VForward>> |
forwarded()
Get all the forwarded data this context has observed.
|
List<MockProcessorContext.CapturedForward<? extends KForward,? extends VForward>> |
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.streams.processor.StateStoreContext |
getStateStoreContext()
Used to get a
StateStoreContext for use with
StateStore.init(StateStoreContext, StateStore)
if you need to initialize a store for your tests. |
org.apache.kafka.common.serialization.Serde<?> |
keySerde() |
org.apache.kafka.streams.StreamsMetrics |
metrics() |
org.apache.kafka.streams.processor.internals.RecordCollector |
recordCollector() |
Optional<org.apache.kafka.streams.processor.api.RecordMetadata> |
recordMetadata() |
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 |
setRecordMetadata(String topic,
int partition,
long offset)
The context exposes these metadata for use in the processor.
|
File |
stateDir() |
org.apache.kafka.streams.processor.TaskId |
taskId() |
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.api.ProcessingContextpublic org.apache.kafka.streams.processor.TaskId taskId()
taskId in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic Map<String,Object> appConfigs()
appConfigs in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic Map<String,Object> appConfigsWithPrefix(String prefix)
appConfigsWithPrefix in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic long currentSystemTimeMs()
currentSystemTimeMs in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic long currentStreamTimeMs()
currentStreamTimeMs in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic org.apache.kafka.common.serialization.Serde<?> keySerde()
keySerde in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic org.apache.kafka.common.serialization.Serde<?> valueSerde()
valueSerde in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic File stateDir()
stateDir in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic org.apache.kafka.streams.StreamsMetrics metrics()
metrics in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic void setRecordMetadata(String topic, int partition, long offset)
topic - A topic namepartition - A partition numberoffset - A record offsetpublic void setCurrentSystemTimeMs(long currentSystemTimeMs)
public void setCurrentStreamTimeMs(long currentStreamTimeMs)
public Optional<org.apache.kafka.streams.processor.api.RecordMetadata> recordMetadata()
recordMetadata in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic <S extends org.apache.kafka.streams.processor.StateStore> S getStateStore(String name)
getStateStore in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic <S extends org.apache.kafka.streams.processor.StateStore> void addStateStore(S stateStore)
public org.apache.kafka.streams.processor.Cancellable schedule(Duration interval, org.apache.kafka.streams.processor.PunctuationType type, org.apache.kafka.streams.processor.Punctuator callback)
schedule in interface org.apache.kafka.streams.processor.api.ProcessingContextpublic List<MockProcessorContext.CapturedPunctuator> scheduledPunctuators()
schedule(...).public <K extends KForward,V extends VForward> void forward(org.apache.kafka.streams.processor.api.Record<K,V> record)
public <K extends KForward,V extends VForward> void forward(org.apache.kafka.streams.processor.api.Record<K,V> record, String childName)
public List<MockProcessorContext.CapturedForward<? extends KForward,? extends VForward>> forwarded()
forward(...).public List<MockProcessorContext.CapturedForward<? extends KForward,? extends VForward>> 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.api.ProcessingContextpublic boolean committed()
ProcessingContext.commit() has been called in this context.true iff ProcessingContext.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.Supplierpublic org.apache.kafka.streams.processor.StateStoreContext getStateStoreContext()
StateStoreContext for use with
StateStore.init(StateStoreContext, StateStore)
if you need to initialize a store for your tests.StateStoreContext that delegates to this ProcessorContext.