Class MockProcessorContext

java.lang.Object
org.apache.kafka.streams.processor.MockProcessorContext
All Implemented Interfaces:
org.apache.kafka.streams.processor.internals.RecordCollector.Supplier, org.apache.kafka.streams.processor.ProcessorContext

@Deprecated public class MockProcessorContext extends Object implements org.apache.kafka.streams.processor.ProcessorContext, org.apache.kafka.streams.processor.internals.RecordCollector.Supplier
Deprecated.
Since 4.0. Use MockProcessorContext instead.
MockProcessorContext is a mock of ProcessorContext for users to test their 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.

  • Constructor Details

    • MockProcessorContext

      public MockProcessorContext()
      Deprecated.
      Create a 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.
    • MockProcessorContext

      public MockProcessorContext(Properties config)
      Deprecated.
      Create a 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.
      Parameters:
      config - a Properties object, used to configure the context and the processor.
    • MockProcessorContext

      public MockProcessorContext(Properties config, org.apache.kafka.streams.processor.TaskId taskId, File stateDir)
      Deprecated.
      Create a MockProcessorContext with a specified taskId and null stateDir.
      Parameters:
      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().
  • Method Details

    • applicationId

      public String applicationId()
      Deprecated.
      Specified by:
      applicationId in interface org.apache.kafka.streams.processor.ProcessorContext
    • taskId

      public org.apache.kafka.streams.processor.TaskId taskId()
      Deprecated.
      Specified by:
      taskId in interface org.apache.kafka.streams.processor.ProcessorContext
    • appConfigs

      public Map<String,Object> appConfigs()
      Deprecated.
      Specified by:
      appConfigs in interface org.apache.kafka.streams.processor.ProcessorContext
    • appConfigsWithPrefix

      public Map<String,Object> appConfigsWithPrefix(String prefix)
      Deprecated.
      Specified by:
      appConfigsWithPrefix in interface org.apache.kafka.streams.processor.ProcessorContext
    • currentSystemTimeMs

      public long currentSystemTimeMs()
      Deprecated.
      Specified by:
      currentSystemTimeMs in interface org.apache.kafka.streams.processor.ProcessorContext
    • currentStreamTimeMs

      public long currentStreamTimeMs()
      Deprecated.
      Specified by:
      currentStreamTimeMs in interface org.apache.kafka.streams.processor.ProcessorContext
    • keySerde

      public org.apache.kafka.common.serialization.Serde<?> keySerde()
      Deprecated.
      Specified by:
      keySerde in interface org.apache.kafka.streams.processor.ProcessorContext
    • valueSerde

      public org.apache.kafka.common.serialization.Serde<?> valueSerde()
      Deprecated.
      Specified by:
      valueSerde in interface org.apache.kafka.streams.processor.ProcessorContext
    • stateDir

      public File stateDir()
      Deprecated.
      Specified by:
      stateDir in interface org.apache.kafka.streams.processor.ProcessorContext
    • metrics

      public org.apache.kafka.streams.StreamsMetrics metrics()
      Deprecated.
      Specified by:
      metrics in interface org.apache.kafka.streams.processor.ProcessorContext
    • setRecordMetadata

      public void setRecordMetadata(String topic, int partition, long offset, org.apache.kafka.common.header.Headers headers, long timestamp)
      Deprecated.
      The context exposes these metadata for use in the processor. Normally, they are set by the Kafka Streams framework, but for the purpose of driving unit tests, you can set them directly.
      Parameters:
      topic - A topic name
      partition - A partition number
      offset - A record offset
      timestamp - A record timestamp
    • setTopic

      public void setTopic(String topic)
      Deprecated.
      The context exposes this metadata for use in the processor. Normally, they are set by the Kafka Streams framework, but for the purpose of driving unit tests, you can set it directly. Setting this attribute doesn't affect the others.
      Parameters:
      topic - A topic name
    • setPartition

      public void setPartition(int partition)
      Deprecated.
      The context exposes this metadata for use in the processor. Normally, they are set by the Kafka Streams framework, but for the purpose of driving unit tests, you can set it directly. Setting this attribute doesn't affect the others.
      Parameters:
      partition - A partition number
    • setOffset

      public void setOffset(long offset)
      Deprecated.
      The context exposes this metadata for use in the processor. Normally, they are set by the Kafka Streams framework, but for the purpose of driving unit tests, you can set it directly. Setting this attribute doesn't affect the others.
      Parameters:
      offset - A record offset
    • setHeaders

      public void setHeaders(org.apache.kafka.common.header.Headers headers)
      Deprecated.
      The context exposes this metadata for use in the processor. Normally, they are set by the Kafka Streams framework, but for the purpose of driving unit tests, you can set it directly. Setting this attribute doesn't affect the others.
      Parameters:
      headers - Record headers
    • setRecordTimestamp

      public void setRecordTimestamp(long recordTimestamp)
      Deprecated.
      The context exposes this metadata for use in the processor. Normally, they are set by the Kafka Streams framework, but for the purpose of driving unit tests, you can set it directly. Setting this attribute doesn't affect the others.
      Parameters:
      recordTimestamp - A record timestamp
    • setCurrentSystemTimeMs

      public void setCurrentSystemTimeMs(long currentSystemTimeMs)
      Deprecated.
    • setCurrentStreamTimeMs

      public void setCurrentStreamTimeMs(long currentStreamTimeMs)
      Deprecated.
    • topic

      public String topic()
      Deprecated.
      Specified by:
      topic in interface org.apache.kafka.streams.processor.ProcessorContext
    • partition

      public int partition()
      Deprecated.
      Specified by:
      partition in interface org.apache.kafka.streams.processor.ProcessorContext
    • offset

      public long offset()
      Deprecated.
      Specified by:
      offset in interface org.apache.kafka.streams.processor.ProcessorContext
    • headers

      public org.apache.kafka.common.header.Headers headers()
      Deprecated.
      Returns the headers of the current input record; could be 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 ValueTransformerimplementation.

      Specified by:
      headers in interface org.apache.kafka.streams.processor.ProcessorContext
      Returns:
      the headers
    • timestamp

      public long timestamp()
      Deprecated.
      Specified by:
      timestamp in interface org.apache.kafka.streams.processor.ProcessorContext
    • register

      public void register(org.apache.kafka.streams.processor.StateStore store, org.apache.kafka.streams.processor.StateRestoreCallback stateRestoreCallbackIsIgnoredInMock)
      Deprecated.
      Specified by:
      register in interface org.apache.kafka.streams.processor.ProcessorContext
    • getStateStore

      public <S extends org.apache.kafka.streams.processor.StateStore> S getStateStore(String name)
      Deprecated.
      Specified by:
      getStateStore in interface org.apache.kafka.streams.processor.ProcessorContext
    • schedule

      public 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
      Deprecated.
      Specified by:
      schedule in interface org.apache.kafka.streams.processor.ProcessorContext
      Throws:
      IllegalArgumentException
    • scheduledPunctuators

      public List<MockProcessorContext.CapturedPunctuator> scheduledPunctuators()
      Deprecated.
      Get the punctuators scheduled so far. The returned list is not affected by subsequent calls to schedule(...).
      Returns:
      A list of captured punctuators.
    • forward

      public <K, V> void forward(K key, V value)
      Deprecated.
      Specified by:
      forward in interface org.apache.kafka.streams.processor.ProcessorContext
    • forward

      public <K, V> void forward(K key, V value, org.apache.kafka.streams.processor.To to)
      Deprecated.
      Specified by:
      forward in interface org.apache.kafka.streams.processor.ProcessorContext
    • forwarded

      Deprecated.
      Get all the forwarded data this context has observed. The returned list will not be affected by subsequent interactions with the context. The data in the list is in the same order as the calls to forward(...).
      Returns:
      A list of key/value pairs that were previously passed to the context.
    • forwarded

      public List<MockProcessorContext.CapturedForward> forwarded(String childName)
      Deprecated.
      Get all the forwarded data this context has observed for a specific child by name. The returned list will not be affected by subsequent interactions with the context. The data in the list is in the same order as the calls to forward(...).
      Parameters:
      childName - The child name to retrieve forwards for
      Returns:
      A list of key/value pairs that were previously passed to the context.
    • resetForwards

      public void resetForwards()
      Deprecated.
      Clear the captured forwarded data.
    • commit

      public void commit()
      Deprecated.
      Specified by:
      commit in interface org.apache.kafka.streams.processor.ProcessorContext
    • committed

      public boolean committed()
      Deprecated.
      Whether ProcessorContext.commit() has been called in this context.
      Returns:
      true iff ProcessorContext.commit() has been called in this context since construction or reset.
    • resetCommit

      public void resetCommit()
      Deprecated.
      Reset the commit capture to false (whether or not it was previously true).
    • recordCollector

      public org.apache.kafka.streams.processor.internals.RecordCollector recordCollector()
      Deprecated.
      Specified by:
      recordCollector in interface org.apache.kafka.streams.processor.internals.RecordCollector.Supplier