Search in sources :

Example 6 with Result

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

the class ApiSqlValueCoercerTest method shouldCoerceNestedJsonStructWithCaseInsensitiveFields.

@Test
public void shouldCoerceNestedJsonStructWithCaseInsensitiveFields() {
    // Given:
    final SqlType structType = SqlTypes.struct().field("F1", SqlTypes.struct().field("FOO", SqlTypes.BIGINT).field("foo", SqlTypes.STRING).field("bar", SqlTypes.STRING).build()).build();
    final JsonObject obj = new JsonObject().put("F1", new JsonObject().put("foo", 12).put("`foo`", "v1").put("\"bar\"", "v2"));
    // When:
    final Result result = coercer.coerce(obj, structType);
    // Then:
    assertThat("", !result.failed());
    final Struct coerced = ((Optional<Struct>) result.value()).get();
    final Struct innerStruct = ((Struct) coerced.get("F1"));
    assertThat(innerStruct.get("FOO"), is(12L));
    assertThat(innerStruct.get("foo"), is("v1"));
    assertThat(innerStruct.get("bar"), is("v2"));
}
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 7 with Result

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

the class ApiSqlValueCoercerTest method shouldCoerceNestedJsonMapWithCaseSensitiveKeys.

@Test
public void shouldCoerceNestedJsonMapWithCaseSensitiveKeys() {
    // Given:
    final SqlType structType = SqlTypes.struct().field("F1", SqlTypes.map(SqlTypes.STRING, SqlTypes.BIGINT)).build();
    final JsonObject obj = new JsonObject().put("F1", new JsonObject().put("foo", 12));
    // When:
    final Result result = coercer.coerce(obj, structType);
    // Then:
    assertThat("", !result.failed());
    final Struct coerced = ((Optional<Struct>) result.value()).get();
    final Map<?, ?> innerMap = ((Map<?, ?>) coerced.get("F1"));
    assertThat(innerMap.get("foo"), is(12L));
    assertThat(innerMap.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 8 with Result

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

the class ApiSqlValueCoercerTest method shouldCoerceJsonStructWithCaseInsensitiveFields.

@Test
public void shouldCoerceJsonStructWithCaseInsensitiveFields() {
    // Given:
    final SqlType structType = SqlTypes.struct().field("FOO", SqlTypes.BIGINT).field("foo", SqlTypes.STRING).field("bar", SqlTypes.STRING).build();
    final JsonObject obj = new JsonObject().put("foo", 12).put("`foo`", "v1").put("\"bar\"", "v2");
    // When:
    final Result result = coercer.coerce(obj, structType);
    // Then:
    assertThat("", !result.failed());
    final Struct coerced = ((Optional<Struct>) result.value()).get();
    assertThat(coerced.get("FOO"), is(12L));
    assertThat(coerced.get("foo"), is("v1"));
    assertThat(coerced.get("bar"), is("v2"));
}
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 9 with Result

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

the class ApiSqlValueCoercerTest method shouldCoerceJsonArrayToNestedArray.

@Test
public void shouldCoerceJsonArrayToNestedArray() {
    // Given:
    final SqlType arrayType = SqlTypes.array(SqlTypes.struct().field("F1", SqlTypes.BIGINT).field("F2", SqlTypes.STRING).build());
    final JsonArray array = new JsonArray().add(new JsonObject().put("F1", 1).put("F2", "foo")).add(new JsonObject().put("F1", 2)).add(new JsonObject()).addNull();
    // When:
    final Result result = coercer.coerce(array, arrayType);
    // Then:
    assertThat("", !result.failed());
    final List<?> coerced = ((Optional<List<?>>) result.value()).get();
    assertThat(((Struct) coerced.get(0)).get("F1"), is(1L));
    assertThat(((Struct) coerced.get(0)).get("F2"), is("foo"));
    assertThat(((Struct) coerced.get(1)).get("F1"), is(2L));
    assertThat(((Struct) coerced.get(1)).get("F2"), is(nullValue()));
    assertThat(((Struct) coerced.get(2)).get("F1"), is(nullValue()));
    assertThat(((Struct) coerced.get(2)).get("F2"), is(nullValue()));
    assertThat(coerced.get(3), is(nullValue()));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) 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 10 with Result

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

the class ApiSqlValueCoercerTest method shouldSubsetCoerceToStruct.

@Test
public void shouldSubsetCoerceToStruct() {
    // Given:
    final JsonObject struct = new JsonObject().put("foo", "val1");
    final SqlType structType = SqlTypes.struct().field("FOO", SqlTypes.STRING).field("BAR", SqlTypes.STRING).build();
    // When:
    final Result result = coercer.coerce(struct, structType);
    // Then:
    assertThat("", !result.failed());
    final Optional<Struct> coerced = (Optional<Struct>) result.value();
    assertThat(coerced.get().get("FOO"), is("val1"));
    assertThat(coerced.get().get("BAR"), 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)

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