Search in sources :

Example 6 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldThrowIfForwardedWithDeprecatedChildName.

@Test
public void shouldThrowIfForwardedWithDeprecatedChildName() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        @Override
        public void process(final String key, final Long value) {
            // noinspection deprecation
            context().forward(key, value, "child1");
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    try {
        processor.process("foo", 5L);
        fail("Should have thrown an UnsupportedOperationException.");
    } catch (final UnsupportedOperationException expected) {
    // expected
    }
}
Also used : AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 7 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest 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 context = new MockProcessorContext(config, new TaskId(1, 1), dummyFile);
    assertEquals("testFullConstructor", context.applicationId());
    assertEquals(new TaskId(1, 1), context.taskId());
    assertEquals("testFullConstructor", context.appConfigs().get(StreamsConfig.APPLICATION_ID_CONFIG));
    assertEquals("testFullConstructor", context.appConfigsWithPrefix("application.").get("id"));
    assertEquals(Serdes.String().getClass(), context.keySerde().getClass());
    assertEquals(Serdes.Long().getClass(), context.valueSerde().getClass());
    assertEquals(dummyFile, context.stateDir());
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Properties(java.util.Properties) File(java.io.File) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 8 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldCaptureApplicationAndRecordMetadata.

@Test
public void shouldCaptureApplicationAndRecordMetadata() {
    final Properties config = new Properties();
    config.put(StreamsConfig.APPLICATION_ID_CONFIG, "testMetadata");
    config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "");
    final AbstractProcessor<String, Object> processor = new AbstractProcessor<String, Object>() {

        @Override
        public void process(final String key, final Object value) {
            context().forward("appId", context().applicationId());
            context().forward("taskId", context().taskId());
            context().forward("topic", context().topic());
            context().forward("partition", context().partition());
            context().forward("offset", context().offset());
            context().forward("timestamp", context().timestamp());
            context().forward("key", key);
            context().forward("value", value);
        }
    };
    final MockProcessorContext context = new MockProcessorContext(config);
    processor.init(context);
    try {
        processor.process("foo", 5L);
        fail("Should have thrown an exception.");
    } catch (final IllegalStateException expected) {
    // expected, since the record metadata isn't initialized
    }
    context.resetForwards();
    context.setRecordMetadata("t1", 0, 0L, 0L);
    {
        processor.process("foo", 5L);
        final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
        assertEquals(new KeyValue<>("appId", "testMetadata"), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("taskId", new TaskId(0, 0)), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("topic", "t1"), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("partition", 0), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("offset", 0L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("timestamp", 0L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("key", "foo"), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("value", 5L), forwarded.next().keyValue());
    }
    context.resetForwards();
    // record metadata should be "sticky"
    context.setOffset(1L);
    context.setTimestamp(10L);
    {
        processor.process("bar", 50L);
        final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
        assertEquals(new KeyValue<>("appId", "testMetadata"), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("taskId", new TaskId(0, 0)), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("topic", "t1"), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("partition", 0), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("offset", 1L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("timestamp", 10L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("key", "bar"), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("value", 50L), forwarded.next().keyValue());
    }
    context.resetForwards();
    // record metadata should be "sticky"
    context.setTopic("t2");
    context.setPartition(30);
    {
        processor.process("baz", 500L);
        final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
        assertEquals(new KeyValue<>("appId", "testMetadata"), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("taskId", new TaskId(0, 0)), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("topic", "t2"), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("partition", 30), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("offset", 1L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("timestamp", 10L), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("key", "baz"), forwarded.next().keyValue());
        assertEquals(new KeyValue<>("value", 500L), forwarded.next().keyValue());
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Iterator(java.util.Iterator) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) Properties(java.util.Properties) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 9 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldCaptureOutputRecords.

@Test
public void shouldCaptureOutputRecords() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        @Override
        public void process(final String key, final Long value) {
            context().forward(key + value, key.length() + value);
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("barbaz", 50L);
    final Iterator<CapturedForward> forwarded = context.forwarded().iterator();
    assertEquals(new KeyValue<>("foo5", 8L), forwarded.next().keyValue());
    assertEquals(new KeyValue<>("barbaz50", 56L), forwarded.next().keyValue());
    assertFalse(forwarded.hasNext());
    context.resetForwards();
    assertEquals(0, context.forwarded().size());
}
Also used : CapturedForward(org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward) AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) Test(org.junit.Test)

Example 10 with MockProcessorContext

use of org.apache.kafka.streams.processor.MockProcessorContext in project apache-kafka-on-k8s by banzaicloud.

the class MockProcessorContextTest method shouldStoreAndReturnStateStores.

@Test
public void shouldStoreAndReturnStateStores() {
    final AbstractProcessor<String, Long> processor = new AbstractProcessor<String, Long>() {

        @Override
        public void process(final String key, final Long value) {
            // noinspection unchecked
            final KeyValueStore<String, Long> stateStore = (KeyValueStore<String, Long>) context().getStateStore("my-state");
            stateStore.put(key, (stateStore.get(key) == null ? 0 : stateStore.get(key)) + value);
            stateStore.put("all", (stateStore.get("all") == null ? 0 : stateStore.get("all")) + value);
        }
    };
    final MockProcessorContext context = new MockProcessorContext();
    final KeyValueStore<String, Long> store = new InMemoryKeyValueStore<>("my-state", Serdes.String(), Serdes.Long());
    context.register(store, false, null);
    store.init(context, store);
    processor.init(context);
    processor.process("foo", 5L);
    processor.process("bar", 50L);
    assertEquals(5L, (long) store.get("foo"));
    assertEquals(50L, (long) store.get("bar"));
    assertEquals(55L, (long) store.get("all"));
}
Also used : AbstractProcessor(org.apache.kafka.streams.processor.AbstractProcessor) KeyValueStore(org.apache.kafka.streams.state.KeyValueStore) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) MockProcessorContext(org.apache.kafka.streams.processor.MockProcessorContext) InMemoryKeyValueStore(org.apache.kafka.streams.state.internals.InMemoryKeyValueStore) Test(org.junit.Test)

Aggregations

MockProcessorContext (org.apache.kafka.streams.processor.MockProcessorContext)11 Test (org.junit.Test)11 AbstractProcessor (org.apache.kafka.streams.processor.AbstractProcessor)9 CapturedForward (org.apache.kafka.streams.processor.MockProcessorContext.CapturedForward)3 Iterator (java.util.Iterator)2 Properties (java.util.Properties)2 TaskId (org.apache.kafka.streams.processor.TaskId)2 File (java.io.File)1 Processor (org.apache.kafka.streams.processor.Processor)1 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)1 Punctuator (org.apache.kafka.streams.processor.Punctuator)1 To (org.apache.kafka.streams.processor.To)1 KeyValueStore (org.apache.kafka.streams.state.KeyValueStore)1 InMemoryKeyValueStore (org.apache.kafka.streams.state.internals.InMemoryKeyValueStore)1