Search in sources :

Example 1 with ConnectKsqlSchemaTranslator

use of io.confluent.ksql.serde.connect.ConnectKsqlSchemaTranslator in project ksql by confluentinc.

the class DefaultSchemaInjectorFunctionalTest method shouldInferSchema.

private void shouldInferSchema(final org.apache.avro.Schema avroSchema, final Schema expectedKqlSchema) {
    // Given:
    final Schema expectedSchema = new ConnectKsqlSchemaTranslator(SchemaTranslationPolicies.of(this.translationPolicy)).toKsqlSchema(expectedKqlSchema);
    try {
        if (testType == TestType.WITHOUT_SCHEMA_ID) {
            when(srClient.getLatestSchemaMetadata(any())).thenReturn(new SchemaMetadata(1, 1, avroSchema.toString()));
        }
        when(srClient.getSchemaBySubjectAndId(any(), anyInt())).thenReturn(this.avroSchema);
        when(this.avroSchema.schemaType()).thenReturn("AVRO");
        when(this.avroSchema.rawSchema()).thenReturn(avroSchema);
        constructCSASSource(expectedSchema);
    } catch (final Exception e) {
        throw new AssertionError(e);
    }
    final PreparedStatement<Statement> prepared = KsqlParserTestUtil.buildSingleAst(this.statement, metaStore, true);
    // When:
    final KsqlConfig ksqlConfig = new KsqlConfig(ImmutableMap.of());
    final ConfiguredStatement<?> inferred = schemaInjector.inject(ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, ImmutableMap.of())));
    // Then:
    if (testType == TestType.CSAS_WITH_SCHEMA_ID) {
        validateCsasInference((ConfiguredStatement<CreateStreamAsSelect>) inferred, this.avroSchema);
    } else {
        final Statement withSchema = KsqlParserTestUtil.buildSingleAst(inferred.getStatementText(), metaStore, true).getStatement();
        final Schema actual = getSchemaForDdlStatement((CreateSource) withSchema);
        assertThat(FORMATTER.format(actual), equalTo(FORMATTER.format(expectedSchema)));
        assertThat(actual, equalTo(expectedSchema));
    }
}
Also used : SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) Schema(org.apache.kafka.connect.data.Schema) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) KsqlConfig(io.confluent.ksql.util.KsqlConfig) ConnectKsqlSchemaTranslator(io.confluent.ksql.serde.connect.ConnectKsqlSchemaTranslator) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect)

Aggregations

AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)1 SchemaMetadata (io.confluent.kafka.schemaregistry.client.SchemaMetadata)1 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)1 CreateStreamAsSelect (io.confluent.ksql.parser.tree.CreateStreamAsSelect)1 Statement (io.confluent.ksql.parser.tree.Statement)1 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)1 ConnectKsqlSchemaTranslator (io.confluent.ksql.serde.connect.ConnectKsqlSchemaTranslator)1 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)1 KsqlConfig (io.confluent.ksql.util.KsqlConfig)1 Schema (org.apache.kafka.connect.data.Schema)1