Search in sources :

Example 1 with WindowData

use of io.confluent.ksql.test.model.WindowData in project ksql by confluentinc.

the class TestExecutor method validateCreatedMessage.

private static void validateCreatedMessage(final String topicName, final Record expectedRecord, final ProducerRecord<?, ?> actualProducerRecord, final boolean ranWithInsertStatements, final int messageIndex) {
    final Object actualKey = coerceRecordFields(actualProducerRecord.key());
    final Object actualValue = coerceRecordFields(actualProducerRecord.value());
    final long actualTimestamp = actualProducerRecord.timestamp();
    final Headers actualHeaders = actualProducerRecord.headers();
    final JsonNode expectedKey = expectedRecord.getJsonKey().orElse(NullNode.getInstance());
    final JsonNode expectedValue = expectedRecord.getJsonValue().orElseThrow(() -> new KsqlServerException("could not get expected value from test record: " + expectedRecord));
    final long expectedTimestamp = expectedRecord.timestamp().orElse(actualTimestamp);
    final List<TestHeader> expectedHeaders = expectedRecord.headers().orElse(ImmutableList.of());
    final AssertionError error = new AssertionError("Topic '" + topicName + "', message " + messageIndex + ": Expected <" + expectedKey + ", " + expectedValue + "> " + "with timestamp=" + expectedTimestamp + " and headers=[" + printHeaders(expectedHeaders) + "] but was " + getProducerRecordInString(actualProducerRecord));
    if (expectedRecord.getWindow() != null) {
        final Windowed<?> windowed = (Windowed<?>) actualKey;
        if (!new WindowData(windowed).equals(expectedRecord.getWindow()) || !ExpectedRecordComparator.matches(((Windowed<?>) actualKey).key(), expectedKey)) {
            throw error;
        }
    } else {
        if (!ExpectedRecordComparator.matches(actualKey, expectedKey)) {
            throw error;
        }
    }
    if (!ExpectedRecordComparator.matches(actualValue, expectedValue)) {
        throw error;
    }
    if (!ExpectedRecordComparator.matches(actualHeaders.toArray(), expectedHeaders)) {
        throw error;
    }
    if ((actualTimestamp != expectedTimestamp) && (!ranWithInsertStatements || expectedTimestamp != 0L)) {
        throw error;
    }
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) TestHeader(io.confluent.ksql.test.model.TestHeader) WindowData(io.confluent.ksql.test.model.WindowData) Headers(org.apache.kafka.common.header.Headers) JsonNode(com.fasterxml.jackson.databind.JsonNode) KsqlServerException(io.confluent.ksql.util.KsqlServerException)

Example 2 with WindowData

use of io.confluent.ksql.test.model.WindowData in project ksql by confluentinc.

the class RecordTest method shouldGetSessionWindowKey.

@Test
public void shouldGetSessionWindowKey() {
    // Given:
    final Record record = new Record(TOPIC_NAME, "foo", null, "bar", null, Optional.of(1000L), new WindowData(100L, 1000L, "SESSION"), Optional.empty());
    // When:
    final Object key = record.key();
    // Then:
    assertThat(key, instanceOf(Windowed.class));
    final Windowed<?> windowed = (Windowed<?>) key;
    assertThat(windowed.window(), instanceOf(SessionWindow.class));
    assertThat(windowed.window().start(), equalTo(100L));
    assertThat(windowed.window().end(), equalTo(1000L));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) WindowData(io.confluent.ksql.test.model.WindowData) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Test(org.junit.Test)

Example 3 with WindowData

use of io.confluent.ksql.test.model.WindowData in project ksql by confluentinc.

the class RecordTest method shouldGetTimeWindowKey.

@Test
public void shouldGetTimeWindowKey() {
    // Given:
    final Record record = new Record(TOPIC_NAME, "foo", null, "bar", null, Optional.of(1000L), new WindowData(100L, 1000L, "TIME"), Optional.empty());
    // When:
    final Object key = record.key();
    // Then:
    assertThat(key, instanceOf(Windowed.class));
    final Windowed<?> windowed = (Windowed<?>) key;
    assertThat(windowed.window(), instanceOf(TimeWindow.class));
    assertThat(windowed.window().start(), equalTo(100L));
    assertThat(windowed.window().end(), equalTo(1000L));
}
Also used : Windowed(org.apache.kafka.streams.kstream.Windowed) WindowData(io.confluent.ksql.test.model.WindowData) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) Test(org.junit.Test)

Example 4 with WindowData

use of io.confluent.ksql.test.model.WindowData in project ksql by confluentinc.

the class RecordTest method shouldGetHeaders.

@Test
public void shouldGetHeaders() {
    // Given:
    final Record record = new Record(TOPIC_NAME, "foo", null, "bar", null, Optional.of(1000L), new WindowData(100L, 1000L, "SESSION"), Optional.of(ImmutableList.of(new TestHeader("a", new byte[] { 12 }))));
    // When:
    final List<TestHeader> headers = record.headers().get();
    // Then:
    assertThat(headers.size(), equalTo(1));
    assertThat(headers.get(0).key(), equalTo("a"));
    assertThat(headers.get(0).value(), equalTo(new byte[] { 12 }));
}
Also used : TestHeader(io.confluent.ksql.test.model.TestHeader) WindowData(io.confluent.ksql.test.model.WindowData) Test(org.junit.Test)

Aggregations

WindowData (io.confluent.ksql.test.model.WindowData)4 Windowed (org.apache.kafka.streams.kstream.Windowed)3 Test (org.junit.Test)3 TestHeader (io.confluent.ksql.test.model.TestHeader)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 KsqlServerException (io.confluent.ksql.util.KsqlServerException)1 Headers (org.apache.kafka.common.header.Headers)1 SessionWindow (org.apache.kafka.streams.kstream.internals.SessionWindow)1 TimeWindow (org.apache.kafka.streams.kstream.internals.TimeWindow)1