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