use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class SalesforceAvroRegistryTest method testStringToDateConverter.
@Test
public void testStringToDateConverter() throws ParseException {
Schema schema = //
SchemaBuilder.builder().record("Schema").fields().name("Field_A").type(AvroUtils._date()).noDefault().endRecord();
schema.getField("Field_A").addProp(SchemaConstants.TALEND_COLUMN_PATTERN, "yyyy-MM-dd");
AvroConverter<String, ?> converter = sRegistry.getConverterFromString(schema.getField("Field_A"));
assertNotNull(converter);
assertEquals(String.class, converter.getDatumClass());
assertEquals(schema.getField("Field_A").schema(), converter.getSchema());
Object value = converter.convertToAvro("2017-06-15");
assertNotNull(value);
assertThat(value, instanceOf(Long.class));
assertThat((Long) value, equalTo(Long.valueOf(dateFormat.parse("2017-06-15").getTime())));
assertNull(converter.convertToAvro(""));
assertNull(converter.convertToAvro(null));
Date date = dateFormat.parse("2017-06-15");
String sValue = ((AvroConverter<String, Long>) converter).convertToDatum(Long.valueOf(date.getTime()));
assertNotNull(sValue);
assertEquals(dateFormat.format(date), sValue);
assertNull(converter.convertToDatum(null));
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class BigQueryAvroRegistryTest method testGetConverter_long.
@Test
public void testGetConverter_long() {
AvroConverter converter = BigQueryAvroRegistry.get().getConverter(Schema.create(Schema.Type.LONG));
assertThat(converter.convertToAvro(123L), Matchers.<Object>is(123L));
assertThat(converter.convertToAvro(123), Matchers.<Object>is(123L));
assertThat(converter.convertToAvro((short) 123), Matchers.<Object>is(123L));
assertThat(converter.convertToAvro((byte) 123), Matchers.<Object>is(123L));
assertThat(converter.convertToAvro(123d), Matchers.<Object>is(123L));
assertThat(converter.convertToAvro(123f), Matchers.<Object>is(123L));
assertThat(converter.convertToAvro(null), nullValue());
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class ValueConverterTest method testIdentityConverters.
@Test
public void testIdentityConverters() throws Exception {
NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());
FieldDesc fieldDesc = typeDesc.getField("isInactive");
AvroConverter<Boolean, Boolean> converter1 = (AvroConverter<Boolean, Boolean>) transducer.getValueConverter(fieldDesc);
assertEquals(Boolean.TRUE, converter1.convertToAvro(Boolean.TRUE));
assertEquals(Boolean.FALSE, converter1.convertToDatum(Boolean.FALSE));
fieldDesc = typeDesc.getField("openingBalance");
AvroConverter<Double, Double> converter2 = (AvroConverter<Double, Double>) transducer.getValueConverter(fieldDesc);
assertEquals(Double.valueOf(12345.6789), converter2.convertToAvro(Double.valueOf(12345.6789)));
assertEquals(Double.valueOf(98765.4321), converter2.convertToDatum(Double.valueOf(98765.4321)));
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class ValueConverterTest method testEnumConverter.
@Test
public void testEnumConverter() throws Exception {
NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());
FieldDesc fieldDesc = typeDesc.getField("acctType");
AvroConverter<Enum<AccountType>, String> converter1 = (AvroConverter<Enum<AccountType>, String>) transducer.getValueConverter(fieldDesc);
assertEquals(AvroUtils._string(), converter1.getSchema());
assertEquals(AccountType.class, converter1.getDatumClass());
assertEquals(AccountType.ACCOUNTS_PAYABLE.value(), converter1.convertToAvro(AccountType.ACCOUNTS_PAYABLE));
assertEquals(AccountType.ACCOUNTS_PAYABLE, converter1.convertToDatum(AccountType.ACCOUNTS_PAYABLE.value()));
assertEquals(AccountType.ACCOUNTS_PAYABLE, converter1.convertToDatum(AccountType.ACCOUNTS_PAYABLE.name()));
fieldDesc = typeDesc.getField("generalRate");
assertNotNull(fieldDesc);
AvroConverter<Enum<ConsolidatedRate>, String> converter2 = (AvroConverter<Enum<ConsolidatedRate>, String>) transducer.getValueConverter(fieldDesc);
assertEquals(ConsolidatedRate.HISTORICAL.value(), converter2.convertToAvro(ConsolidatedRate.HISTORICAL));
assertEquals(ConsolidatedRate.HISTORICAL, converter2.convertToDatum(ConsolidatedRate.HISTORICAL.value()));
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class ValueConverterTest method testXMLGregorianCalendarConverter.
@Test
public void testXMLGregorianCalendarConverter() throws Exception {
DateTimeZone tz1 = DateTimeZone.getDefault();
MutableDateTime dateTime1 = new MutableDateTime(tz1);
dateTime1.setDate(System.currentTimeMillis());
Long controlValue1 = dateTime1.getMillis();
XMLGregorianCalendar xmlCalendar1 = datatypeFactory.newXMLGregorianCalendar();
xmlCalendar1.setYear(dateTime1.getYear());
xmlCalendar1.setMonth(dateTime1.getMonthOfYear());
xmlCalendar1.setDay(dateTime1.getDayOfMonth());
xmlCalendar1.setHour(dateTime1.getHourOfDay());
xmlCalendar1.setMinute(dateTime1.getMinuteOfHour());
xmlCalendar1.setSecond(dateTime1.getSecondOfMinute());
xmlCalendar1.setMillisecond(dateTime1.getMillisOfSecond());
xmlCalendar1.setTimezone(tz1.toTimeZone().getOffset(dateTime1.getMillis()) / 60000);
FieldDesc fieldInfo = typeDesc.getField("tranDate");
NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());
AvroConverter<XMLGregorianCalendar, Long> converter1 = (AvroConverter<XMLGregorianCalendar, Long>) transducer.getValueConverter(fieldInfo);
assertEquals(AvroUtils._logicalTimestamp(), converter1.getSchema());
assertEquals(XMLGregorianCalendar.class, converter1.getDatumClass());
assertEquals(controlValue1, converter1.convertToAvro(xmlCalendar1));
assertEquals(xmlCalendar1, converter1.convertToDatum(controlValue1));
AvroConverter<XMLGregorianCalendar, Object> converter2 = (AvroConverter<XMLGregorianCalendar, Object>) transducer.getValueConverter(fieldInfo);
assertEquals(xmlCalendar1, converter2.convertToDatum(new Date(controlValue1.longValue())));
assertNull(converter1.convertToAvro(null));
}
Aggregations