Search in sources :

Example 1 with OptionalCodec

use of com.datastax.oss.driver.internal.core.type.codec.extras.OptionalCodec in project java-driver by datastax.

the class CodecRegistryIT method should_be_able_to_register_and_use_custom_codec_with_generic_type.

@Test
public void should_be_able_to_register_and_use_custom_codec_with_generic_type() {
    // create a cluster with registered codecs using OptionalCodec
    OptionalCodec<Map<Integer, String>> optionalMapCodec = new OptionalCodec<>(TypeCodecs.mapOf(TypeCodecs.INT, TypeCodecs.TEXT));
    TypeCodec<Map<Integer, Optional<String>>> mapWithOptionalValueCodec = TypeCodecs.mapOf(TypeCodecs.INT, new OptionalCodec<>(TypeCodecs.TEXT));
    try (CqlSession session = (CqlSession) SessionUtils.baseBuilder().addTypeCodecs(optionalMapCodec, mapWithOptionalValueCodec).addContactEndPoints(CCM_RULE.getContactPoints()).withKeyspace(SESSION_RULE.keyspace()).build()) {
        PreparedStatement prepared = session.prepare("INSERT INTO test2 (k0, k1, v) values (?, ?, ?)");
        // optional map should work.
        Map<Integer, String> v0 = Maps.newHashMap(0, "value");
        Optional<Map<Integer, String>> v0Opt = Optional.of(v0);
        BoundStatement insert = prepared.boundStatementBuilder().setString(0, name.getMethodName()).setInt(1, 0).set(2, v0Opt, optionalMapCodec.getJavaType()).build();
        session.execute(insert);
        // optional absent map should work.
        Optional<Map<Integer, String>> absentMap = Optional.empty();
        insert = prepared.boundStatementBuilder().setString(0, name.getMethodName()).setInt(1, 1).set(2, absentMap, optionalMapCodec.getJavaType()).build();
        session.execute(insert);
        // map with optional value should work - note that you can't have null values in collections,
        // so this is not technically practical but want to validate that custom codec resolution
        // works
        // when it's composed in a collection codec.
        Map<Integer, Optional<String>> v2Map = Maps.newHashMap(1, Optional.of("hello"));
        insert = prepared.boundStatementBuilder().setString(0, name.getMethodName()).setInt(1, 2).set(2, v2Map, mapWithOptionalValueCodec.getJavaType()).build();
        session.execute(insert);
        ResultSet result = session.execute(SimpleStatement.builder("SELECT v from test2 where k0 = ?").addPositionalValues(name.getMethodName()).build());
        List<Row> rows = result.all();
        assertThat(rows).hasSize(3);
        Iterator<Row> iterator = rows.iterator();
        // row (at key 0) should have v0
        Row row = iterator.next();
        // should be able to retrieve value back as an optional map.
        assertThat(row.get(0, optionalMapCodec.getJavaType())).isEqualTo(v0Opt);
        // should be able to retrieve value back as map.
        assertThat(row.getMap(0, Integer.class, String.class)).isEqualTo(v0);
        // next row (at key 1) should be absent (null value).
        row = iterator.next();
        // value should be null.
        assertThat(row.isNull(0)).isTrue();
        // getting with codec should return Optional.empty()
        assertThat(row.get(0, optionalMapCodec.getJavaType())).isEqualTo(absentMap);
        // getting with map should return an empty map.
        assertThat(row.getMap(0, Integer.class, String.class)).isEmpty();
        // next row (at key 2) should have v2
        row = iterator.next();
        // getting with codec should return with the correct type.
        assertThat(row.get(0, mapWithOptionalValueCodec.getJavaType())).isEqualTo(v2Map);
        // getting with map should return a map without optional value.
        assertThat(row.getMap(0, Integer.class, String.class)).isEqualTo(Maps.newHashMap(1, "hello"));
    }
}
Also used : Optional(java.util.Optional) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CqlSession(com.datastax.oss.driver.api.core.CqlSession) OptionalCodec(com.datastax.oss.driver.internal.core.type.codec.extras.OptionalCodec) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Test(org.junit.Test)

Aggregations

CqlSession (com.datastax.oss.driver.api.core.CqlSession)1 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)1 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)1 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 Row (com.datastax.oss.driver.api.core.cql.Row)1 OptionalCodec (com.datastax.oss.driver.internal.core.type.codec.extras.OptionalCodec)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Test (org.junit.Test)1