Search in sources :

Example 1 with SourceNode

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

the class TestExecutor method buildAndExecuteQuery.

public void buildAndExecuteQuery(final TestCase testCase, final TestExecutionListener listener) {
    topicInfoCache.clear();
    final KsqlConfig ksqlConfig = testCase.applyPersistedProperties(new KsqlConfig(config));
    try {
        System.setProperty(RuntimeBuildContext.KSQL_TEST_TRACK_SERDE_TOPICS, "true");
        final List<TopologyTestDriverContainer> topologyTestDrivers = topologyBuilder.buildStreamsTopologyTestDrivers(testCase, serviceContext, ksqlEngine, ksqlConfig, kafka, listener);
        writeInputIntoTopics(testCase.getInputRecords(), kafka);
        final Set<String> inputTopics = testCase.getInputRecords().stream().map(Record::getTopicName).collect(Collectors.toSet());
        for (final TopologyTestDriverContainer topologyTestDriverContainer : topologyTestDrivers) {
            if (validateResults) {
                verifyTopology(testCase);
            }
            final Set<String> topicsFromInput = topologyTestDriverContainer.getSourceTopicNames().stream().filter(inputTopics::contains).collect(Collectors.toSet());
            final Set<String> topicsFromKafka = topologyTestDriverContainer.getSourceTopicNames().stream().filter(topicName -> !inputTopics.contains(topicName)).collect(Collectors.toSet());
            if (!topicsFromInput.isEmpty()) {
                pipeRecordsFromProvidedInput(testCase, topologyTestDriverContainer);
            }
            for (final String kafkaTopic : topicsFromKafka) {
                pipeRecordsFromKafka(kafkaTopic, topologyTestDriverContainer);
            }
            topologyTestDriverContainer.getTopologyTestDriver().producedTopicNames().forEach(topicInfoCache::get);
        }
        verifyOutput(testCase);
        testCase.expectedException().map(ee -> {
            throw new AssertionError("Expected test to throw" + StringDescription.toString(ee));
        });
        kafka.getAllTopics().stream().map(Topic::getName).forEach(topicInfoCache::get);
        final List<PostTopicNode> knownTopics = topicInfoCache.all().stream().map(ti -> {
            final Topic topic = kafka.getTopic(ti.getTopicName());
            final OptionalInt partitions = topic == null ? OptionalInt.empty() : OptionalInt.of(topic.getNumPartitions());
            final Optional<SchemaMetadata> keyMetadata = SchemaRegistryUtil.getLatestSchema(serviceContext.getSchemaRegistryClient(), ti.getTopicName(), true);
            final Optional<SchemaMetadata> valueMetadata = SchemaRegistryUtil.getLatestSchema(serviceContext.getSchemaRegistryClient(), ti.getTopicName(), false);
            return new PostTopicNode(ti.getTopicName(), ti.getKeyFormat(), ti.getValueFormat(), partitions, fromSchemaMetadata(keyMetadata), fromSchemaMetadata(valueMetadata));
        }).collect(Collectors.toList());
        final List<SourceNode> knownSources = ksqlEngine.getMetaStore().getAllDataSources().values().stream().map(SourceNode::fromDataSource).collect(Collectors.toList());
        if (validateResults) {
            testCase.getPostConditions().verify(ksqlEngine.getMetaStore(), knownTopics);
        }
        listener.runComplete(knownTopics, knownSources);
    } catch (final RuntimeException e) {
        final Optional<Matcher<Throwable>> expectedExceptionMatcher = testCase.expectedException();
        if (!expectedExceptionMatcher.isPresent()) {
            throw e;
        }
        assertThat(e, isThrowable(expectedExceptionMatcher.get()));
    } finally {
        System.clearProperty(RuntimeBuildContext.KSQL_TEST_TRACK_SERDE_TOPICS);
    }
}
Also used : InternalFunctionRegistry(io.confluent.ksql.function.InternalFunctionRegistry) SchemaNode(io.confluent.ksql.test.model.SchemaNode) SequentialQueryIdGenerator(io.confluent.ksql.query.id.SequentialQueryIdGenerator) SourceNode(io.confluent.ksql.test.model.SourceNode) StringDescription(org.hamcrest.StringDescription) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) ServiceContext(io.confluent.ksql.services.ServiceContext) ProcessingLogContext(io.confluent.ksql.logging.processing.ProcessingLogContext) MutableMetaStore(io.confluent.ksql.metastore.MutableMetaStore) ByteBuffer(java.nio.ByteBuffer) DefaultConnectClient(io.confluent.ksql.services.DefaultConnectClient) NullNode(com.fasterxml.jackson.databind.node.NullNode) TestUtils(io.confluent.ksql.test.utils.TestUtils) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) TestRecord(org.apache.kafka.streams.test.TestRecord) CONNECT_REQUEST_TIMEOUT_DEFAULT(io.confluent.ksql.util.KsqlConfig.CONNECT_REQUEST_TIMEOUT_DEFAULT) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) TestHeader(io.confluent.ksql.test.model.TestHeader) DefaultServiceContext(io.confluent.ksql.services.DefaultServiceContext) ImmutableSet(com.google.common.collect.ImmutableSet) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) ImmutableMap(com.google.common.collect.ImmutableMap) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) Collection(java.util.Collection) TopicInfo(io.confluent.ksql.test.tools.TopicInfoCache.TopicInfo) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) MetaStoreImpl(io.confluent.ksql.metastore.MetaStoreImpl) DisabledKsqlClient(io.confluent.ksql.services.DisabledKsqlClient) List(java.util.List) Header(org.apache.kafka.common.header.Header) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Entry(java.util.Map.Entry) KsqlEngineMetrics(io.confluent.ksql.internal.KsqlEngineMetrics) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) StreamsConfig(org.apache.kafka.streams.StreamsConfig) StubKafkaTopicClient(io.confluent.ksql.test.tools.stubs.StubKafkaTopicClient) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) WindowData(io.confluent.ksql.test.model.WindowData) PostTopicNode(io.confluent.ksql.test.model.PostConditionsNode.PostTopicNode) Headers(org.apache.kafka.common.header.Headers) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) StubKafkaClientSupplier(io.confluent.ksql.test.tools.stubs.StubKafkaClientSupplier) SchemaRegistryUtil(io.confluent.ksql.schema.registry.SchemaRegistryUtil) ImmutableList(com.google.common.collect.ImmutableList) Windowed(org.apache.kafka.streams.kstream.Windowed) Objects.requireNonNull(java.util.Objects.requireNonNull) StubKafkaService(io.confluent.ksql.test.tools.stubs.StubKafkaService) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) ProtobufFormat(io.confluent.ksql.serde.protobuf.ProtobufFormat) TestOutputTopic(org.apache.kafka.streams.TestOutputTopic) Iterator(java.util.Iterator) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) MutableFunctionRegistry(io.confluent.ksql.function.MutableFunctionRegistry) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) StubKafkaConsumerGroupClient(io.confluent.ksql.test.tools.stubs.StubKafkaConsumerGroupClient) BytesUtils(io.confluent.ksql.util.BytesUtils) JUnitMatchers.isThrowable(org.junit.matchers.JUnitMatchers.isThrowable) KsqlServerException(io.confluent.ksql.util.KsqlServerException) Closeable(java.io.Closeable) Matcher(org.hamcrest.Matcher) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) Collections(java.util.Collections) UdfLoaderUtil(io.confluent.ksql.function.UdfLoaderUtil) PostTopicNode(io.confluent.ksql.test.model.PostConditionsNode.PostTopicNode) Optional(java.util.Optional) KsqlConfig(io.confluent.ksql.util.KsqlConfig) OptionalInt(java.util.OptionalInt) SourceNode(io.confluent.ksql.test.model.SourceNode) JUnitMatchers.isThrowable(org.junit.matchers.JUnitMatchers.isThrowable) TestOutputTopic(org.apache.kafka.streams.TestOutputTopic)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 NullNode (com.fasterxml.jackson.databind.node.NullNode)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Streams (com.google.common.collect.Streams)1 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)1 SchemaMetadata (io.confluent.kafka.schemaregistry.client.SchemaMetadata)1 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)1 KsqlEngine (io.confluent.ksql.engine.KsqlEngine)1 RuntimeBuildContext (io.confluent.ksql.execution.runtime.RuntimeBuildContext)1 FunctionRegistry (io.confluent.ksql.function.FunctionRegistry)1 InternalFunctionRegistry (io.confluent.ksql.function.InternalFunctionRegistry)1 MutableFunctionRegistry (io.confluent.ksql.function.MutableFunctionRegistry)1 UdfLoaderUtil (io.confluent.ksql.function.UdfLoaderUtil)1 KsqlEngineMetrics (io.confluent.ksql.internal.KsqlEngineMetrics)1 ProcessingLogContext (io.confluent.ksql.logging.processing.ProcessingLogContext)1