Search in sources :

Example 1 with Result

use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.

the class DefaultSqlValueCoercerTest method coerce.

private static Optional<?> coerce(final DefaultSqlValueCoercer coercer, final SqlType from, final SqlType to, final Object value) {
    // When:
    final Optional<SqlType> coercedType = coercer.canCoerce(from, to);
    final Result result = coercer.coerce(value, to);
    // Then:
    assertThat("canCoerce(" + from + "," + to + ")", coercedType, is(not(Optional.empty())));
    assertThat("coerce(" + value + "," + to + ")", result, is(not(Result.failure())));
    return result.value();
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result)

Example 2 with Result

use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.

the class DefaultSqlValueCoercerTest method assertUnsupported.

private static void assertUnsupported(final DefaultSqlValueCoercer coercer, final SqlType from, final SqlType to) {
    // Given:
    final Object value = InstanceInstances.instanceFor(from, to);
    // When:
    final Optional<SqlType> coercedType = coercer.canCoerce(from, to);
    final Result result = coercer.coerce(value, to);
    // Then:
    assertThat("canCoerce(" + from + "," + to + ")", coercedType, is(Optional.empty()));
    assertThat("coerce(" + value + "," + to + ")", result, is(Result.failure()));
}
Also used : SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result)

Example 3 with Result

use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.

the class ApiSqlValueCoercerTest method shouldCoerceJsonObjectToNestedMap.

@Test
public void shouldCoerceJsonObjectToNestedMap() {
    // Given:
    final SqlType mapType = SqlTypes.map(SqlTypes.STRING, SqlTypes.struct().field("F1", SqlTypes.BIGINT).field("F2", SqlTypes.STRING).build());
    final JsonObject obj = new JsonObject().put("k1", new JsonObject().put("F1", 1).put("F2", "foo")).put("k2", new JsonObject().put("F1", 2)).put("k3", new JsonObject()).putNull("k4");
    // When:
    final Result result = coercer.coerce(obj, mapType);
    // Then:
    assertThat("", !result.failed());
    final Map<?, ?> coerced = ((Optional<Map<?, ?>>) result.value()).get();
    assertThat(((Struct) coerced.get("k1")).get("F1"), is(1L));
    assertThat(((Struct) coerced.get("k1")).get("F2"), is("foo"));
    assertThat(((Struct) coerced.get("k2")).get("F1"), is(2L));
    assertThat(((Struct) coerced.get("k2")).get("F2"), is(nullValue()));
    assertThat(((Struct) coerced.get("k3")).get("F1"), is(nullValue()));
    assertThat(((Struct) coerced.get("k3")).get("F2"), is(nullValue()));
    assertThat(coerced.get("k4"), is(nullValue()));
}
Also used : Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result) Test(org.junit.Test)

Example 4 with Result

use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.

the class ApiSqlValueCoercerTest method shouldCoerceToJsonObjectWithNullValues.

@Test
public void shouldCoerceToJsonObjectWithNullValues() {
    // Given:
    final JsonObject jsonObject = new JsonObject().put("foo", (Long) null);
    final SqlType structType = SqlTypes.struct().field("foo", SqlTypes.decimal(2, 1)).build();
    // When:
    final Result result = coercer.coerce(jsonObject, structType);
    // Then:
    assertThat("", !result.failed());
    final Optional<Struct> coerced = (Optional<Struct>) result.value();
    assertThat(coerced.get().get("foo"), is(nullValue()));
}
Also used : Optional(java.util.Optional) JsonObject(io.vertx.core.json.JsonObject) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 5 with Result

use of io.confluent.ksql.schema.ksql.SqlValueCoercer.Result in project ksql by confluentinc.

the class ApiSqlValueCoercerTest method shouldNotCoerceToStructIfAnyFieldFailsToCoerce.

@Test
public void shouldNotCoerceToStructIfAnyFieldFailsToCoerce() {
    // Given:
    final Schema schema = SchemaBuilder.struct().field("foo", Schema.INT64_SCHEMA);
    final Struct struct = new Struct(schema).put("foo", 2L);
    final SqlType structType = SqlTypes.struct().field("foo", SqlTypes.array(SqlTypes.INTEGER)).build();
    // When:
    final Result result = coercer.coerce(struct, structType);
    // Then:
    assertThat(result, is(Result.failure()));
}
Also used : Schema(org.apache.kafka.connect.data.Schema) SqlType(io.confluent.ksql.schema.ksql.types.SqlType) Struct(org.apache.kafka.connect.data.Struct) Result(io.confluent.ksql.schema.ksql.SqlValueCoercer.Result) Test(org.junit.Test)

Aggregations

Result (io.confluent.ksql.schema.ksql.SqlValueCoercer.Result)14 SqlType (io.confluent.ksql.schema.ksql.types.SqlType)14 Test (org.junit.Test)12 JsonObject (io.vertx.core.json.JsonObject)11 Optional (java.util.Optional)11 Struct (org.apache.kafka.connect.data.Struct)10 JsonArray (io.vertx.core.json.JsonArray)2 BigDecimal (java.math.BigDecimal)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 List (java.util.List)1 Map (java.util.Map)1 Schema (org.apache.kafka.connect.data.Schema)1