Search in sources :

Example 31 with KStreamTestDriver

use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.

the class KTableSourceTest method testKTable.

@Test
public void testKTable() {
    final KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    KTable<String, Integer> table1 = builder.table(stringSerde, intSerde, topic1, "anyStoreName");
    MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
    table1.toStream().process(proc1);
    driver = new KStreamTestDriver(builder, stateDir);
    driver.process(topic1, "A", 1);
    driver.process(topic1, "B", 2);
    driver.process(topic1, "C", 3);
    driver.process(topic1, "D", 4);
    driver.flushState();
    driver.process(topic1, "A", null);
    driver.process(topic1, "B", null);
    driver.flushState();
    assertEquals(Utils.mkList("A:1", "B:2", "C:3", "D:4", "A:null", "B:null"), proc1.processed);
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 32 with KStreamTestDriver

use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.

the class KTableSourceTest method testNotSendingOldValue.

@Test
public void testNotSendingOldValue() throws IOException {
    final KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, "anyStoreName");
    MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
    builder.addProcessor("proc1", proc1, table1.name);
    driver = new KStreamTestDriver(builder, stateDir, null, null);
    driver.process(topic1, "A", "01");
    driver.process(topic1, "B", "01");
    driver.process(topic1, "C", "01");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(01<-null)", "B:(01<-null)", "C:(01<-null)");
    driver.process(topic1, "A", "02");
    driver.process(topic1, "B", "02");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(02<-null)", "B:(02<-null)");
    driver.process(topic1, "A", "03");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(03<-null)");
    driver.process(topic1, "A", null);
    driver.process(topic1, "B", null);
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(null<-null)", "B:(null<-null)");
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 33 with KStreamTestDriver

use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.

the class KTableSourceTest method testSendingOldValue.

@Test
public void testSendingOldValue() throws IOException {
    final KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, "anyStoreName");
    table1.enableSendingOldValues();
    assertTrue(table1.sendingOldValueEnabled());
    MockProcessorSupplier<String, Integer> proc1 = new MockProcessorSupplier<>();
    builder.addProcessor("proc1", proc1, table1.name);
    driver = new KStreamTestDriver(builder, stateDir, null, null);
    driver.process(topic1, "A", "01");
    driver.process(topic1, "B", "01");
    driver.process(topic1, "C", "01");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(01<-null)", "B:(01<-null)", "C:(01<-null)");
    driver.process(topic1, "A", "02");
    driver.process(topic1, "B", "02");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(02<-01)", "B:(02<-01)");
    driver.process(topic1, "A", "03");
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(03<-02)");
    driver.process(topic1, "A", null);
    driver.process(topic1, "B", null);
    driver.flushState();
    proc1.checkAndClearProcessResult("A:(null<-03)", "B:(null<-02)");
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 34 with KStreamTestDriver

use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.

the class KeyValuePrinterProcessorTest method testPrintKeyValueDefaultSerde.

@Test
public void testPrintKeyValueDefaultSerde() throws Exception {
    KeyValuePrinter<String, String> keyValuePrinter = new KeyValuePrinter<>(printStream, null);
    String[] suppliedKeys = { "foo", "bar", null };
    String[] suppliedValues = { "value1", "value2", "value3" };
    String[] expectedValues = { "[null]: foo , value1", "[null]: bar , value2", "[null]: null , value3" };
    KStream<String, String> stream = builder.stream(stringSerde, stringSerde, topicName);
    stream.process(keyValuePrinter);
    driver = new KStreamTestDriver(builder);
    for (int i = 0; i < suppliedKeys.length; i++) {
        driver.process(topicName, suppliedKeys[i], suppliedValues[i]);
    }
    String[] capturedValues = new String(baos.toByteArray(), Charset.forName("UTF-8")).split("\n");
    for (int i = 0; i < capturedValues.length; i++) {
        assertEquals(capturedValues[i], expectedValues[i]);
    }
}
Also used : KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) Test(org.junit.Test)

Example 35 with KStreamTestDriver

use of org.apache.kafka.test.KStreamTestDriver in project kafka by apache.

the class KeyValuePrinterProcessorTest method testPrintKeyValuesWithName.

@Test
public void testPrintKeyValuesWithName() throws Exception {
    KeyValuePrinter<String, String> keyValuePrinter = new KeyValuePrinter<>(printStream, "test-stream");
    String[] suppliedKeys = { "foo", "bar", null };
    String[] suppliedValues = { "value1", "value2", "value3" };
    String[] expectedValues = { "[test-stream]: foo , value1", "[test-stream]: bar , value2", "[test-stream]: null , value3" };
    KStream<String, String> stream = builder.stream(stringSerde, stringSerde, topicName);
    stream.process(keyValuePrinter);
    driver = new KStreamTestDriver(builder);
    for (int i = 0; i < suppliedKeys.length; i++) {
        driver.process(topicName, suppliedKeys[i], suppliedValues[i]);
    }
    String[] capturedValues = new String(baos.toByteArray(), Charset.forName("UTF-8")).split("\n");
    for (int i = 0; i < capturedValues.length; i++) {
        assertEquals(capturedValues[i], expectedValues[i]);
    }
}
Also used : KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) Test(org.junit.Test)

Aggregations

KStreamTestDriver (org.apache.kafka.test.KStreamTestDriver)70 Test (org.junit.Test)69 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)60 MockProcessorSupplier (org.apache.kafka.test.MockProcessorSupplier)26 HashSet (java.util.HashSet)12 Set (java.util.Set)12 KeyValue (org.apache.kafka.streams.KeyValue)10 KeyValueMapper (org.apache.kafka.streams.kstream.KeyValueMapper)9 Predicate (org.apache.kafka.streams.kstream.Predicate)9 ValueMapper (org.apache.kafka.streams.kstream.ValueMapper)9 HashMap (java.util.HashMap)7 MockKeyValueMapper (org.apache.kafka.test.MockKeyValueMapper)7 ArrayList (java.util.ArrayList)6 Windowed (org.apache.kafka.streams.kstream.Windowed)6 File (java.io.File)2 ForeachAction (org.apache.kafka.streams.kstream.ForeachAction)2 ProcessorContext (org.apache.kafka.streams.processor.ProcessorContext)2 Field (java.lang.reflect.Field)1 Random (java.util.Random)1 Aggregator (org.apache.kafka.streams.kstream.Aggregator)1