Search in sources :

Example 46 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class MetadataPortableResolverTest method when_duplicateExternalName_then_throws.

@Test
@Parameters({ "true, __key", "false, this" })
public void when_duplicateExternalName_then_throws(boolean key, String prefix) {
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).addIntField("field").build();
    ss.getPortableContext().registerClassDefinition(classDefinition);
    Map<String, String> options = ImmutableMap.of((key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID), String.valueOf(classDefinition.getFactoryId()), (key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID), String.valueOf(classDefinition.getClassId()), (key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION), String.valueOf(classDefinition.getVersion()));
    assertThatThrownBy(() -> MetadataPortableResolver.INSTANCE.resolveAndValidateFields(key, asList(field("field1", QueryDataType.INT, prefix + ".field"), field("field2", QueryDataType.VARCHAR, prefix + ".field")), options, ss)).isInstanceOf(QueryException.class).hasMessageMatching("Duplicate external name: (__key|this).field");
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) QueryException(com.hazelcast.sql.impl.QueryException) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 47 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class MetadataPortableResolverTest method when_userDeclaresFields_then_fieldsNotAddedFromClassDefinition.

@Test
@Parameters({ "true, __key", "false, this" })
public void when_userDeclaresFields_then_fieldsNotAddedFromClassDefinition(boolean key, String prefix) {
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).addIntField("field1").addStringField("field2").build();
    ss.getPortableContext().registerClassDefinition(classDefinition);
    Map<String, String> options = ImmutableMap.of((key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID), String.valueOf(classDefinition.getFactoryId()), (key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID), String.valueOf(classDefinition.getClassId()), (key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION), String.valueOf(classDefinition.getVersion()));
    Stream<MappingField> resolvedFields = INSTANCE.resolveAndValidateFields(key, singletonList(field("field2", QueryDataType.VARCHAR, prefix + ".field2")), options, ss);
    assertThat(resolvedFields).containsExactly(field("field2", QueryDataType.VARCHAR, prefix + ".field2"));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) MappingField(com.hazelcast.sql.impl.schema.MappingField) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 48 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class SampleMetadataResolverTest method test_portable.

@Test
public void test_portable() {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, PORTABLE_CLASS_ID, 0).build();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().addClassDefinition(classDefinition).build();
    Metadata metadata = SampleMetadataResolver.resolve(ss, new PortableClass(), key);
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, PORTABLE_FORMAT), entry(key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID, String.valueOf(PORTABLE_FACTORY_ID)), entry(key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID, String.valueOf(PORTABLE_CLASS_ID)), entry(key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION, "0"));
    metadata = SampleMetadataResolver.resolve(ss, ss.toData(new PortableClass()), key);
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, PORTABLE_FORMAT), entry(key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID, String.valueOf(PORTABLE_FACTORY_ID)), entry(key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID, String.valueOf(PORTABLE_CLASS_ID)), entry(key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION, "0"));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 49 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class SampleMetadataResolverTest method test_portableRecord.

@Test
public void test_portableRecord() {
    ClassDefinition classDefinition = new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, PORTABLE_CLASS_ID, PORTABLE_CLASS_VERSION).build();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().addClassDefinition(classDefinition).build();
    Metadata metadata = SampleMetadataResolver.resolve(ss, new PortableGenericRecordBuilder(classDefinition).build(), key);
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, PORTABLE_FORMAT), entry(key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID, String.valueOf(PORTABLE_FACTORY_ID)), entry(key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID, String.valueOf(PORTABLE_CLASS_ID)), entry(key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION, String.valueOf(PORTABLE_CLASS_VERSION)));
    metadata = SampleMetadataResolver.resolve(ss, ss.toData(new PortableGenericRecordBuilder(classDefinition).build()), key);
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, PORTABLE_FORMAT), entry(key ? OPTION_KEY_FACTORY_ID : OPTION_VALUE_FACTORY_ID, String.valueOf(PORTABLE_FACTORY_ID)), entry(key ? OPTION_KEY_CLASS_ID : OPTION_VALUE_CLASS_ID, String.valueOf(PORTABLE_CLASS_ID)), entry(key ? OPTION_KEY_CLASS_VERSION : OPTION_VALUE_CLASS_VERSION, String.valueOf(PORTABLE_CLASS_VERSION)));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) PortableGenericRecordBuilder(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 50 with ClassDefinition

use of com.hazelcast.nio.serialization.ClassDefinition in project hazelcast by hazelcast.

the class PortableUpsertTargetTest method test_set.

@Test
public void test_set() throws IOException {
    ClassDefinition innerClassDefinition = new ClassDefinitionBuilder(4, 5, 6).build();
    ClassDefinition classDefinition = new ClassDefinitionBuilder(1, 2, 3).addPortableField("null", innerClassDefinition).addPortableField("object", innerClassDefinition).addStringField("string").addCharField("character").addBooleanField("boolean").addByteField("byte").addShortField("short").addIntField("int").addLongField("long").addFloatField("float").addDoubleField("double").addDecimalField("decimal").addTimeField("time").addDateField("date").addTimestampField("timestamp").addTimestampWithTimezoneField("timestampTz").build();
    UpsertTarget target = new PortableUpsertTarget(classDefinition);
    UpsertInjector nullFieldInjector = target.createInjector("null", QueryDataType.OBJECT);
    UpsertInjector objectFieldInjector = target.createInjector("object", QueryDataType.OBJECT);
    UpsertInjector stringFieldInjector = target.createInjector("string", QueryDataType.VARCHAR);
    UpsertInjector characterFieldInjector = target.createInjector("character", QueryDataType.VARCHAR_CHARACTER);
    UpsertInjector booleanFieldInjector = target.createInjector("boolean", QueryDataType.BOOLEAN);
    UpsertInjector byteFieldInjector = target.createInjector("byte", QueryDataType.TINYINT);
    UpsertInjector shortFieldInjector = target.createInjector("short", QueryDataType.SMALLINT);
    UpsertInjector intFieldInjector = target.createInjector("int", QueryDataType.INT);
    UpsertInjector longFieldInjector = target.createInjector("long", QueryDataType.BIGINT);
    UpsertInjector floatFieldInjector = target.createInjector("float", QueryDataType.REAL);
    UpsertInjector doubleFieldInjector = target.createInjector("double", QueryDataType.DOUBLE);
    UpsertInjector decimalFieldInjector = target.createInjector("decimal", QueryDataType.DECIMAL);
    UpsertInjector timeFieldInjector = target.createInjector("time", QueryDataType.TIME);
    UpsertInjector dateFieldInjector = target.createInjector("date", QueryDataType.DATE);
    UpsertInjector timestampFieldInjector = target.createInjector("timestamp", QueryDataType.TIMESTAMP);
    UpsertInjector timestampTzFieldInjector = target.createInjector("timestampTz", QueryDataType.TIMESTAMP_WITH_TZ_OFFSET_DATE_TIME);
    target.init();
    nullFieldInjector.set(null);
    objectFieldInjector.set(new PortableGenericRecordBuilder(innerClassDefinition).build());
    stringFieldInjector.set("1");
    characterFieldInjector.set('2');
    booleanFieldInjector.set(true);
    byteFieldInjector.set((byte) 3);
    shortFieldInjector.set((short) 4);
    intFieldInjector.set(5);
    longFieldInjector.set(6L);
    floatFieldInjector.set(7.1F);
    doubleFieldInjector.set(7.2D);
    decimalFieldInjector.set(new BigDecimal("8.1"));
    timeFieldInjector.set(LocalTime.of(12, 23, 34));
    dateFieldInjector.set(LocalDate.of(2021, 2, 9));
    timestampFieldInjector.set(LocalDateTime.of(2021, 2, 9, 12, 23, 34, 1_000_000));
    timestampTzFieldInjector.set(OffsetDateTime.of(2021, 2, 9, 12, 23, 34, 200_000_000, UTC));
    Object portable = target.conclude();
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().build();
    InternalGenericRecord record = ss.readAsInternalGenericRecord(ss.toData(portable));
    assertThat(record.getGenericRecord("null")).isNull();
    assertThat(record.getGenericRecord("object")).isEqualTo(new PortableGenericRecordBuilder(innerClassDefinition).build());
    assertThat(record.getString("string")).isEqualTo("1");
    assertThat(record.getChar("character")).isEqualTo('2');
    assertThat(record.getBoolean("boolean")).isEqualTo(true);
    assertThat(record.getInt8("byte")).isEqualTo((byte) 3);
    assertThat(record.getInt16("short")).isEqualTo((short) 4);
    assertThat(record.getInt32("int")).isEqualTo(5);
    assertThat(record.getInt64("long")).isEqualTo(6L);
    assertThat(record.getFloat32("float")).isEqualTo(7.1F);
    assertThat(record.getFloat64("double")).isEqualTo(7.2D);
    assertThat(record.getDecimal("decimal")).isEqualTo(new BigDecimal("8.1"));
    assertThat(record.getTime("time")).isEqualTo(LocalTime.of(12, 23, 34));
    assertThat(record.getDate("date")).isEqualTo(LocalDate.of(2021, 2, 9));
    assertThat(record.getTimestamp("timestamp")).isEqualTo(LocalDateTime.of(2021, 2, 9, 12, 23, 34, 1_000_000));
    assertThat(record.getTimestampWithTimezone("timestampTz")).isEqualTo(OffsetDateTime.of(2021, 2, 9, 12, 23, 34, 200_000_000, UTC));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClassDefinition(com.hazelcast.nio.serialization.ClassDefinition) PortableGenericRecordBuilder(com.hazelcast.internal.serialization.impl.portable.PortableGenericRecordBuilder) BigDecimal(java.math.BigDecimal) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) ClassDefinitionBuilder(com.hazelcast.nio.serialization.ClassDefinitionBuilder) Test(org.junit.Test)

Aggregations

ClassDefinition (com.hazelcast.nio.serialization.ClassDefinition)70 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)37 Test (org.junit.Test)25 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)16 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)15 QuickTest (com.hazelcast.test.annotation.QuickTest)15 HazelcastSerializationException (com.hazelcast.nio.serialization.HazelcastSerializationException)10 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)10 FieldDefinition (com.hazelcast.nio.serialization.FieldDefinition)7 GenericRecordBuilder (com.hazelcast.nio.serialization.GenericRecordBuilder)7 Parameters (junitparams.Parameters)7 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)6 MappingField (com.hazelcast.sql.impl.schema.MappingField)6 Portable (com.hazelcast.nio.serialization.Portable)5 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 SerializationConfig (com.hazelcast.config.SerializationConfig)3 BufferObjectDataInput (com.hazelcast.internal.nio.BufferObjectDataInput)3 Data (com.hazelcast.internal.serialization.Data)3 BufferObjectDataInput (com.hazelcast.nio.BufferObjectDataInput)3