Search in sources :

Example 1 with TestHeader

use of io.confluent.ksql.test.model.TestHeader 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 TestHeader

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

the class TestExecutorTest method shouldFailOnMismatchedHeaders.

@Test
public void shouldFailOnMismatchedHeaders() {
    // Given:
    final TestHeader h1 = new TestHeader("a", new byte[] { 12, 23 });
    final TestHeader h2 = new TestHeader("b", new byte[] { 123 });
    final ProducerRecord<byte[], byte[]> rec0 = producerRecord(sinkTopic, 123456719L, "k1", "v1", Serdes.String().serializer(), ImmutableList.of(h1));
    final ProducerRecord<byte[], byte[]> rec1 = producerRecord(sinkTopic, 123456789L, "k2", "v2", Serdes.String().serializer(), ImmutableList.of(h1));
    when(kafkaService.readRecords(SINK_TOPIC_NAME)).thenReturn(ImmutableList.of(rec0, rec1));
    final Record expected_0 = new Record(SINK_TOPIC_NAME, "k1", TextNode.valueOf("k1"), "v1", TextNode.valueOf("v1"), Optional.of(123456719L), null, Optional.of(ImmutableList.of(h1)));
    final Record expected_1 = new Record(SINK_TOPIC_NAME, "k2", TextNode.valueOf("k2"), "v2", TextNode.valueOf("v2"), Optional.of(123456789L), null, Optional.of(ImmutableList.of(h2)));
    when(testCase.getOutputRecords()).thenReturn(ImmutableList.of(expected_0, expected_1));
    // When:
    final AssertionError e = assertThrows(AssertionError.class, () -> executor.buildAndExecuteQuery(testCase, listener));
    // Then:
    assertThat(e.getMessage(), containsString("Expected <\"k2\", \"v2\"> with timestamp=123456789 and headers=[{KEY=b,VALUE=ew==}] but was <k2, \"v2\"> with timestamp=123456789 and headers=[{KEY=a,VALUE=DBc=}]"));
}
Also used : TestHeader(io.confluent.ksql.test.model.TestHeader) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test)

Example 3 with TestHeader

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

the class ExpectedRecordComparatorTest method shouldMatchHeaders.

@Test
public void shouldMatchHeaders() {
    final TestHeader h0 = new TestHeader("a", new byte[] { 123 });
    final TestHeader h1 = new TestHeader("a", new byte[] { 12, 23 });
    final TestHeader h2 = new TestHeader("b", new byte[] { 123 });
    final TestHeader h0_copy = new TestHeader("a", new byte[] { 123 });
    assertThat(ExpectedRecordComparator.matches(new Header[] {}, ImmutableList.of()), equalTo(true));
    assertThat(ExpectedRecordComparator.matches(new Header[] { h0 }, ImmutableList.of(h0_copy)), equalTo(true));
    assertThat(ExpectedRecordComparator.matches(new Header[] { h0 }, ImmutableList.of(h1)), equalTo(false));
    assertThat(ExpectedRecordComparator.matches(new Header[] { h0 }, ImmutableList.of(h2)), equalTo(false));
    assertThat(ExpectedRecordComparator.matches(new Header[] { h0 }, ImmutableList.of(h0_copy, h1)), equalTo(false));
}
Also used : TestHeader(io.confluent.ksql.test.model.TestHeader) TestHeader(io.confluent.ksql.test.model.TestHeader) Header(org.apache.kafka.common.header.Header) Test(org.junit.Test)

Example 4 with TestHeader

use of io.confluent.ksql.test.model.TestHeader 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

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