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"));
}
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()));
}
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"));
}
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()));
}
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());
}
Aggregations