Search in sources :

Example 1 with TestFrameworkException

use of io.confluent.ksql.test.TestFrameworkException in project ksql by confluentinc.

the class TestFileContext method getLineNumber.

private int getLineNumber(final String testName) {
    final Pattern pattern = Pattern.compile(".*\"name\"\\s*:\\s*\"" + Pattern.quote(testName) + "\".*");
    int lineNumber = 0;
    for (final String line : lines) {
        lineNumber++;
        if (pattern.matcher(line).matches()) {
            return lineNumber;
        }
    }
    throw new TestFrameworkException("Can't find test in test file" + System.lineSeparator() + "test: " + testName + System.lineSeparator() + "file: " + testPath);
}
Also used : Pattern(java.util.regex.Pattern) TestFrameworkException(io.confluent.ksql.test.TestFrameworkException)

Example 2 with TestFrameworkException

use of io.confluent.ksql.test.TestFrameworkException in project ksql by confluentinc.

the class JsonTestLoader method loadTestPathsFromDirectory.

private List<Path> loadTestPathsFromDirectory() {
    final InputStream s = JsonTestLoader.class.getClassLoader().getResourceAsStream(testDir.toString());
    if (s == null) {
        throw new TestFrameworkException("Test directory not found: " + testDir);
    }
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(s, UTF_8))) {
        final List<Path> tests = new ArrayList<>();
        String test;
        while ((test = reader.readLine()) != null) {
            if (test.endsWith(".json")) {
                tests.add(testDir.resolve(test));
            }
        }
        return tests;
    } catch (final IOException e) {
        throw new TestFrameworkException("Failed to read test dir: " + testDir, e);
    }
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) TestFrameworkException(io.confluent.ksql.test.TestFrameworkException) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 3 with TestFrameworkException

use of io.confluent.ksql.test.TestFrameworkException in project ksql by confluentinc.

the class TopicInfoCache method load.

private TopicInfo load(final String topicName) {
    try {
        final Optional<InternalTopic> internalTopic = INTERNAL_TOPIC_PATTERNS.stream().map(e -> e.match(topicName)).filter(Optional::isPresent).map(Optional::get).findFirst();
        if (internalTopic.isPresent()) {
            // Internal topic:
            final QueryId queryId = internalTopic.get().queryId();
            final PersistentQueryMetadata query = ksqlEngine.getPersistentQuery(queryId).orElseThrow(() -> new TestFrameworkException("Unknown queryId for internal topic: " + queryId));
            final QuerySchemas.MultiSchemaInfo schemasInfo = query.getQuerySchemas().getTopicInfo(topicName);
            final Set<KeyFormat> keyFormats = schemasInfo.getKeyFormats();
            final Set<ValueFormat> valueFormats = schemasInfo.getValueFormats();
            // foreign key joins once the above github issue is implemented.
            if (keyFormats.size() != 1) {
                final String result = keyFormats.size() == 0 ? "Zero" : "Multiple";
                throw new Exception(result + " key formats registered for topic." + System.lineSeparator() + "topic: " + topicName + "formats: " + keyFormats.stream().map(KeyFormat::getFormat).sorted().collect(Collectors.toList()));
            }
            // for stream-stream left/outer joins once the above github issue is implemented.
            if (valueFormats.size() > 1) {
                throw new Exception("Multiple value formats registered for topic." + System.lineSeparator() + "topic: " + topicName + "formats: " + valueFormats.stream().map(ValueFormat::getFormat).sorted().collect(Collectors.toList()));
            }
            return new TopicInfo(topicName, query.getLogicalSchema(), internalTopic.get().keyFormat(Iterables.getOnlyElement(keyFormats), query), Iterables.getOnlyElement(valueFormats, NONE_VALUE_FORMAT), internalTopic.get().changeLogWindowSize(query));
        }
        // Source / sink topic:
        final Set<TopicInfo> keyTypes = ksqlEngine.getMetaStore().getAllDataSources().values().stream().filter(source -> source.getKafkaTopicName().equals(topicName)).map(source -> new TopicInfo(topicName, source.getSchema(), source.getKsqlTopic().getKeyFormat(), source.getKsqlTopic().getValueFormat(), OptionalLong.empty())).collect(Collectors.toSet());
        if (keyTypes.isEmpty()) {
            throw new TestFrameworkException("No information found for topic '" + topicName + "'. Available topics: " + cache.asMap().keySet());
        }
        return Iterables.get(keyTypes, 0);
    } catch (final Exception e) {
        throw new TestFrameworkException("Failed to determine key type for" + System.lineSeparator() + "topic: " + topicName + System.lineSeparator() + "reason: " + e.getMessage(), e);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) LoadingCache(com.google.common.cache.LoadingCache) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) FormatFactory(io.confluent.ksql.serde.FormatFactory) KeyFormat(io.confluent.ksql.serde.KeyFormat) QuerySchemas(io.confluent.ksql.schema.query.QuerySchemas) Function(java.util.function.Function) WindowInfo(io.confluent.ksql.serde.WindowInfo) OptionalLong(java.util.OptionalLong) SchemaInfo(io.confluent.ksql.schema.query.QuerySchemas.SchemaInfo) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Duration(java.time.Duration) WindowType(io.confluent.ksql.model.WindowType) QueryId(io.confluent.ksql.query.QueryId) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) Deserializer(org.apache.kafka.common.serialization.Deserializer) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) TestFrameworkException(io.confluent.ksql.test.TestFrameworkException) TimeWindowedDeserializer(org.apache.kafka.streams.kstream.TimeWindowedDeserializer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) DurationParser(io.confluent.ksql.parser.DurationParser) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) ValueFormat(io.confluent.ksql.serde.ValueFormat) Collectors(java.util.stream.Collectors) AbstractKafkaSchemaSerDeConfig(io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) SerdeSupplier(io.confluent.ksql.test.serde.SerdeSupplier) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Serializer(org.apache.kafka.common.serialization.Serializer) Optional(java.util.Optional) SerdeUtil(io.confluent.ksql.test.utils.SerdeUtil) CacheBuilder(com.google.common.cache.CacheBuilder) Pattern(java.util.regex.Pattern) FormatInfo(io.confluent.ksql.serde.FormatInfo) ValueFormat(io.confluent.ksql.serde.ValueFormat) Optional(java.util.Optional) QueryId(io.confluent.ksql.query.QueryId) KeyFormat(io.confluent.ksql.serde.KeyFormat) TestFrameworkException(io.confluent.ksql.test.TestFrameworkException) QuerySchemas(io.confluent.ksql.schema.query.QuerySchemas) TestFrameworkException(io.confluent.ksql.test.TestFrameworkException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Aggregations

TestFrameworkException (io.confluent.ksql.test.TestFrameworkException)3 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)1 AbstractKafkaSchemaSerDeConfig (io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig)1 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)1 WindowType (io.confluent.ksql.model.WindowType)1 DurationParser (io.confluent.ksql.parser.DurationParser)1 QueryId (io.confluent.ksql.query.QueryId)1 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)1 QuerySchemas (io.confluent.ksql.schema.query.QuerySchemas)1 SchemaInfo (io.confluent.ksql.schema.query.QuerySchemas.SchemaInfo)1 FormatFactory (io.confluent.ksql.serde.FormatFactory)1 FormatInfo (io.confluent.ksql.serde.FormatInfo)1 KeyFormat (io.confluent.ksql.serde.KeyFormat)1