Search in sources :

Example 41 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class DataTypeClassNameParserTest method should_parse_user_type_when_definition_not_already_available.

@Test
@UseDataProvider(location = TestDataProviders.class, value = "locales")
public void should_parse_user_type_when_definition_not_already_available(Locale locale) {
    Locale def = Locale.getDefault();
    try {
        Locale.setDefault(locale);
        UserDefinedType addressType = (UserDefinedType) parse("org.apache.cassandra.db.marshal.UserType(" + "foo,61646472657373," + ("737472656574:org.apache.cassandra.db.marshal.UTF8Type," + "7a6970636f6465:org.apache.cassandra.db.marshal.Int32Type," + ("70686f6e6573:org.apache.cassandra.db.marshal.SetType(" + "org.apache.cassandra.db.marshal.UserType(foo,70686f6e65," + "6e616d65:org.apache.cassandra.db.marshal.UTF8Type," + "6e756d626572:org.apache.cassandra.db.marshal.UTF8Type)") + "))"));
        assertThat(addressType.getKeyspace().asInternal()).isEqualTo("foo");
        assertThat(addressType.getName().asInternal()).isEqualTo("address");
        assertThat(addressType.isFrozen()).isTrue();
        assertThat(addressType.getFieldNames().size()).isEqualTo(3);
        assertThat(addressType.getFieldNames().get(0).asInternal()).isEqualTo("street");
        assertThat(addressType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
        assertThat(addressType.getFieldNames().get(1).asInternal()).isEqualTo("zipcode");
        assertThat(addressType.getFieldTypes().get(1)).isEqualTo(DataTypes.INT);
        assertThat(addressType.getFieldNames().get(2).asInternal()).isEqualTo("phones");
        DataType phonesType = addressType.getFieldTypes().get(2);
        assertThat(phonesType).isInstanceOf(SetType.class);
        UserDefinedType phoneType = ((UserDefinedType) ((SetType) phonesType).getElementType());
        assertThat(phoneType.getKeyspace().asInternal()).isEqualTo("foo");
        assertThat(phoneType.getName().asInternal()).isEqualTo("phone");
        assertThat(phoneType.isFrozen()).isTrue();
        assertThat(phoneType.getFieldNames().size()).isEqualTo(2);
        assertThat(phoneType.getFieldNames().get(0).asInternal()).isEqualTo("name");
        assertThat(phoneType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
        assertThat(phoneType.getFieldNames().get(1).asInternal()).isEqualTo("number");
        assertThat(phoneType.getFieldTypes().get(1)).isEqualTo(DataTypes.TEXT);
    } finally {
        Locale.setDefault(def);
    }
}
Also used : Locale(java.util.Locale) SetType(com.datastax.oss.driver.api.core.type.SetType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) DataType(com.datastax.oss.driver.api.core.type.DataType) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 42 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class UserDefinedTypeListParserTest method should_resolve_nested_dependency.

@Test
public void should_resolve_nested_dependency() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, mockTypeRow("ks", "a", ImmutableList.of("bs"), ImmutableList.of("frozen<tuple<int, frozen<list<frozen<b>>>>>")), mockTypeRow("ks", "b", ImmutableList.of("i"), ImmutableList.of("int")));
    assertThat(types).hasSize(2);
    UserDefinedType aType = types.get(CqlIdentifier.fromInternal("a"));
    UserDefinedType bType = types.get(CqlIdentifier.fromInternal("b"));
    TupleType tupleType = (TupleType) aType.getFieldTypes().get(0);
    ListType listType = (ListType) tupleType.getComponentTypes().get(1);
    assertThat(listType.getElementType()).isEqualTo(bType);
}
Also used : ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 43 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class UserDefinedTypeListParserTest method should_parse_singleton_list.

@Test
public void should_parse_singleton_list() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, mockTypeRow("ks", "t", ImmutableList.of("i"), ImmutableList.of("int")));
    assertThat(types).hasSize(1);
    UserDefinedType type = types.get(CqlIdentifier.fromInternal("t"));
    assertThat(type.getKeyspace().asInternal()).isEqualTo("ks");
    assertThat(type.getName().asInternal()).isEqualTo("t");
    assertThat(type.getFieldNames()).containsExactly(CqlIdentifier.fromInternal("i"));
    assertThat(type.getFieldTypes()).containsExactly(DataTypes.INT);
}
Also used : UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 44 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class UserDefinedTypeListParserTest method should_resolve_set_dependency.

@Test
public void should_resolve_set_dependency() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, mockTypeRow("ks", "a", ImmutableList.of("bs"), ImmutableList.of("frozen<set<frozen<b>>>")), mockTypeRow("ks", "b", ImmutableList.of("i"), ImmutableList.of("int")));
    assertThat(types).hasSize(2);
    UserDefinedType aType = types.get(CqlIdentifier.fromInternal("a"));
    UserDefinedType bType = types.get(CqlIdentifier.fromInternal("b"));
    assertThat(((SetType) aType.getFieldTypes().get(0)).getElementType()).isEqualTo(bType);
}
Also used : SetType(com.datastax.oss.driver.api.core.type.SetType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 45 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class UserDefinedTypeListParserTest method should_parse_modern_table.

@Test
public void should_parse_modern_table() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, PERSON_ROW_3_0, ADDRESS_ROW_3_0);
    assertThat(types).hasSize(2);
    UserDefinedType personType = types.get(CqlIdentifier.fromInternal("person"));
    UserDefinedType addressType = types.get(CqlIdentifier.fromInternal("address"));
    assertThat(personType.getKeyspace().asInternal()).isEqualTo("ks");
    assertThat(personType.getName().asInternal()).isEqualTo("person");
    assertThat(personType.getFieldNames()).containsExactly(CqlIdentifier.fromInternal("first_name"), CqlIdentifier.fromInternal("last_name"), CqlIdentifier.fromInternal("address"));
    assertThat(personType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
    assertThat(personType.getFieldTypes().get(1)).isEqualTo(DataTypes.TEXT);
    assertThat(personType.getFieldTypes().get(2)).isSameAs(addressType);
}
Also used : UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Aggregations

UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)53 Test (org.junit.Test)33 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)21 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)20 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)15 TupleType (com.datastax.oss.driver.api.core.type.TupleType)13 CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 DataType (com.datastax.oss.driver.api.core.type.DataType)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ListType (com.datastax.oss.driver.api.core.type.ListType)6 SetType (com.datastax.oss.driver.api.core.type.SetType)6 SessionRule (com.datastax.oss.driver.api.testinfra.session.SessionRule)6 ParallelizableTests (com.datastax.oss.driver.categories.ParallelizableTests)6 Category (org.junit.experimental.categories.Category)6 Row (com.datastax.oss.driver.api.core.cql.Row)5 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)5 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)5 CcmRule (com.datastax.oss.driver.api.testinfra.ccm.CcmRule)5 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)4