Search in sources :

Example 26 with KStreamTestDriver

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

the class KTableKTableOuterJoinTest method testNotSendingOldValue.

@Test
public void testNotSendingOldValue() throws Exception {
    final KStreamBuilder builder = new KStreamBuilder();
    final int[] expectedKeys = new int[] { 0, 1, 2, 3 };
    KTable<Integer, String> table1;
    KTable<Integer, String> table2;
    KTable<Integer, String> joined;
    MockProcessorSupplier<Integer, String> proc;
    table1 = builder.table(intSerde, stringSerde, topic1, storeName1);
    table2 = builder.table(intSerde, stringSerde, topic2, storeName2);
    joined = table1.outerJoin(table2, MockValueJoiner.TOSTRING_JOINER);
    proc = new MockProcessorSupplier<>();
    builder.addProcessor("proc", proc, ((KTableImpl<?, ?, ?>) joined).name);
    driver = new KStreamTestDriver(builder, stateDir);
    assertTrue(((KTableImpl<?, ?, ?>) table1).sendingOldValueEnabled());
    assertTrue(((KTableImpl<?, ?, ?>) table2).sendingOldValueEnabled());
    assertFalse(((KTableImpl<?, ?, ?>) joined).sendingOldValueEnabled());
    for (int i = 0; i < 2; i++) {
        driver.process(topic1, expectedKeys[i], "X" + expectedKeys[i]);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+null<-null)", "1:(X1+null<-null)");
    for (int i = 0; i < 2; i++) {
        driver.process(topic2, expectedKeys[i], "Y" + expectedKeys[i]);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+Y0<-null)", "1:(X1+Y1<-null)");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "X" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+Y0<-null)", "1:(X1+Y1<-null)", "2:(X2+null<-null)", "3:(X3+null<-null)");
    // push all items to the other stream. this should produce four items.
    for (int expectedKey : expectedKeys) {
        driver.process(topic2, expectedKey, "YY" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+YY0<-null)", "1:(X1+YY1<-null)", "2:(X2+YY2<-null)", "3:(X3+YY3<-null)");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "X" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+YY0<-null)", "1:(X1+YY1<-null)", "2:(X2+YY2<-null)", "3:(X3+YY3<-null)");
    for (int i = 0; i < 2; i++) {
        driver.process(topic2, expectedKeys[i], null);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(X0+null<-null)", "1:(X1+null<-null)");
    for (int expectedKey : expectedKeys) {
        driver.process(topic1, expectedKey, "XX" + expectedKey);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("0:(XX0+null<-null)", "1:(XX1+null<-null)", "2:(XX2+YY2<-null)", "3:(XX3+YY3<-null)");
    for (int i = 1; i < 3; i++) {
        driver.process(topic1, expectedKeys[i], null);
    }
    driver.flushState();
    proc.checkAndClearProcessResult("1:(null<-null)", "2:(null+YY2<-null)");
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) Test(org.junit.Test)

Example 27 with KStreamTestDriver

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

the class KTableMapValuesTest method testNotSendingOldValue.

@Test
public void testNotSendingOldValue() throws IOException {
    KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, "anyStoreName");
    KTableImpl<String, String, Integer> table2 = (KTableImpl<String, String, Integer>) table1.mapValues(new ValueMapper<String, Integer>() {

        @Override
        public Integer apply(String value) {
            return new Integer(value);
        }
    });
    MockProcessorSupplier<String, Integer> proc = new MockProcessorSupplier<>();
    builder.addProcessor("proc", proc, table2.name);
    driver = new KStreamTestDriver(builder, stateDir, null, null);
    assertFalse(table1.sendingOldValueEnabled());
    assertFalse(table2.sendingOldValueEnabled());
    driver.process(topic1, "A", "01");
    driver.process(topic1, "B", "01");
    driver.process(topic1, "C", "01");
    driver.flushState();
    proc.checkAndClearProcessResult("A:(1<-null)", "B:(1<-null)", "C:(1<-null)");
    driver.process(topic1, "A", "02");
    driver.process(topic1, "B", "02");
    driver.flushState();
    proc.checkAndClearProcessResult("A:(2<-null)", "B:(2<-null)");
    driver.process(topic1, "A", "03");
    driver.flushState();
    proc.checkAndClearProcessResult("A:(3<-null)");
    driver.process(topic1, "A", null);
    driver.flushState();
    proc.checkAndClearProcessResult("A:(null<-null)");
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) Test(org.junit.Test)

Example 28 with KStreamTestDriver

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

the class KTableMapValuesTest method testValueGetter.

@Test
public void testValueGetter() throws IOException {
    KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    String topic2 = "topic2";
    String storeName1 = "storeName1";
    String storeName2 = "storeName2";
    KTableImpl<String, String, String> table1 = (KTableImpl<String, String, String>) builder.table(stringSerde, stringSerde, topic1, storeName1);
    KTableImpl<String, String, Integer> table2 = (KTableImpl<String, String, Integer>) table1.mapValues(new ValueMapper<String, Integer>() {

        @Override
        public Integer apply(String value) {
            return new Integer(value);
        }
    });
    KTableImpl<String, Integer, Integer> table3 = (KTableImpl<String, Integer, Integer>) table2.filter(new Predicate<String, Integer>() {

        @Override
        public boolean test(String key, Integer value) {
            return (value % 2) == 0;
        }
    });
    KTableImpl<String, String, String> table4 = (KTableImpl<String, String, String>) table1.through(stringSerde, stringSerde, topic2, storeName2);
    KTableValueGetterSupplier<String, String> getterSupplier1 = table1.valueGetterSupplier();
    KTableValueGetterSupplier<String, Integer> getterSupplier2 = table2.valueGetterSupplier();
    KTableValueGetterSupplier<String, Integer> getterSupplier3 = table3.valueGetterSupplier();
    KTableValueGetterSupplier<String, String> getterSupplier4 = table4.valueGetterSupplier();
    driver = new KStreamTestDriver(builder, stateDir, null, null);
    KTableValueGetter<String, String> getter1 = getterSupplier1.get();
    getter1.init(driver.context());
    KTableValueGetter<String, Integer> getter2 = getterSupplier2.get();
    getter2.init(driver.context());
    KTableValueGetter<String, Integer> getter3 = getterSupplier3.get();
    getter3.init(driver.context());
    KTableValueGetter<String, String> getter4 = getterSupplier4.get();
    getter4.init(driver.context());
    driver.process(topic1, "A", "01");
    driver.process(topic1, "B", "01");
    driver.process(topic1, "C", "01");
    driver.flushState();
    assertEquals("01", getter1.get("A"));
    assertEquals("01", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertEquals(new Integer(1), getter2.get("A"));
    assertEquals(new Integer(1), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertNull(getter3.get("A"));
    assertNull(getter3.get("B"));
    assertNull(getter3.get("C"));
    assertEquals("01", getter4.get("A"));
    assertEquals("01", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
    driver.process(topic1, "A", "02");
    driver.process(topic1, "B", "02");
    driver.flushState();
    assertEquals("02", getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertEquals(new Integer(2), getter2.get("A"));
    assertEquals(new Integer(2), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertEquals(new Integer(2), getter3.get("A"));
    assertEquals(new Integer(2), getter3.get("B"));
    assertNull(getter3.get("C"));
    assertEquals("02", getter4.get("A"));
    assertEquals("02", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
    driver.process(topic1, "A", "03");
    driver.flushState();
    assertEquals("03", getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertEquals(new Integer(3), getter2.get("A"));
    assertEquals(new Integer(2), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertNull(getter3.get("A"));
    assertEquals(new Integer(2), getter3.get("B"));
    assertNull(getter3.get("C"));
    assertEquals("03", getter4.get("A"));
    assertEquals("02", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
    driver.process(topic1, "A", null);
    driver.flushState();
    assertNull(getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    assertNull(getter2.get("A"));
    assertEquals(new Integer(2), getter2.get("B"));
    assertEquals(new Integer(1), getter2.get("C"));
    assertNull(getter3.get("A"));
    assertEquals(new Integer(2), getter3.get("B"));
    assertNull(getter3.get("C"));
    assertNull(getter4.get("A"));
    assertEquals("02", getter4.get("B"));
    assertEquals("01", getter4.get("C"));
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) ValueMapper(org.apache.kafka.streams.kstream.ValueMapper) Predicate(org.apache.kafka.streams.kstream.Predicate) KStreamTestDriver(org.apache.kafka.test.KStreamTestDriver) Test(org.junit.Test)

Example 29 with KStreamTestDriver

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

the class KTableMapValuesTest method testKTable.

@Test
public void testKTable() {
    final KStreamBuilder builder = new KStreamBuilder();
    String topic1 = "topic1";
    KTable<String, String> table1 = builder.table(stringSerde, stringSerde, topic1, "anyStoreName");
    KTable<String, Integer> table2 = table1.mapValues(new ValueMapper<CharSequence, Integer>() {

        @Override
        public Integer apply(CharSequence value) {
            return value.charAt(0) - 48;
        }
    });
    MockProcessorSupplier<String, Integer> proc2 = new MockProcessorSupplier<>();
    table2.toStream().process(proc2);
    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();
    assertEquals(Utils.mkList("A:1", "B:2", "C:3", "D:4"), proc2.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 30 with KStreamTestDriver

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

the class KTableSourceTest method testValueGetter.

@Test
public void testValueGetter() 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");
    KTableValueGetterSupplier<String, String> getterSupplier1 = table1.valueGetterSupplier();
    driver = new KStreamTestDriver(builder, stateDir, null, null);
    KTableValueGetter<String, String> getter1 = getterSupplier1.get();
    getter1.init(driver.context());
    driver.process(topic1, "A", "01");
    driver.process(topic1, "B", "01");
    driver.process(topic1, "C", "01");
    assertEquals("01", getter1.get("A"));
    assertEquals("01", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    driver.process(topic1, "A", "02");
    driver.process(topic1, "B", "02");
    assertEquals("02", getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    driver.process(topic1, "A", "03");
    assertEquals("03", getter1.get("A"));
    assertEquals("02", getter1.get("B"));
    assertEquals("01", getter1.get("C"));
    driver.process(topic1, "A", null);
    driver.process(topic1, "B", null);
    assertNull(getter1.get("A"));
    assertNull(getter1.get("B"));
    assertEquals("01", getter1.get("C"));
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) 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