use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class ValueConverterTest method testJsonConverterError.
@Test
public void testJsonConverterError() throws Exception {
NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());
FieldDesc fieldDesc = typeDesc.getField("department");
AvroConverter<RecordRef, String> converter1 = (AvroConverter<RecordRef, String>) transducer.getValueConverter(fieldDesc);
try {
converter1.convertToDatum("{name:'R&D',internalId:'12345',externalId:null,type:null}");
fail("NetSuiteException expected");
} catch (Exception e) {
assertThat(e, instanceOf(NetSuiteException.class));
NetSuiteException nsException = (NetSuiteException) e;
assertNotNull(nsException.getCode());
assertNotNull(nsException.getContext());
assertNotNull(nsException.getContext().get(ExceptionContext.KEY_MESSAGE));
}
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class ValueConverterTest method testJsonConverterComplexObject.
@Test
public void testJsonConverterComplexObject() throws Exception {
NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());
Account account1 = new SimpleObjectComposer<>(Account.class).composeObject();
RecordRef recordRef1 = new RecordRef();
recordRef1.setInternalId("120001");
recordRef1.setName("Talend France");
RecordRef recordRef2 = new RecordRef();
recordRef2.setInternalId("120002");
recordRef2.setName("Talend China");
RecordRefList recordRefList1 = new RecordRefList();
recordRefList1.getRecordRef().add(recordRef1);
recordRefList1.getRecordRef().add(recordRef2);
account1.setSubsidiaryList(recordRefList1);
BooleanCustomFieldRef customFieldRef1 = new BooleanCustomFieldRef();
customFieldRef1.setInternalId("100001");
customFieldRef1.setScriptId("custentity_field1");
customFieldRef1.setValue(true);
StringCustomFieldRef customFieldRef2 = new StringCustomFieldRef();
customFieldRef2.setInternalId("100002");
customFieldRef2.setScriptId("custentity_field2");
customFieldRef2.setValue("test123");
CustomFieldList customFieldList = new CustomFieldList();
customFieldList.getCustomField().add(customFieldRef1);
customFieldList.getCustomField().add(customFieldRef2);
account1.setCustomFieldList(customFieldList);
AvroConverter<Account, String> converter1 = (AvroConverter<Account, String>) transducer.getValueConverter(account1.getClass());
assertEquals(AvroUtils._string(), converter1.getSchema());
assertEquals(account1.getClass(), converter1.getDatumClass());
String testJson1 = converter1.convertToAvro(account1);
assertNotNull(testJson1);
Account testAccount1 = converter1.convertToDatum(testJson1);
assertNotNull(testAccount1);
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class NsObjectTransducer method readField.
/**
* Read a value from a field.
*
* @param valueMap map containing raw values by names
* @param fieldDesc field descriptor
* @return value of a field or <code>null</code>
*/
protected Object readField(Map<String, Object> valueMap, FieldDesc fieldDesc) {
String fieldName = fieldDesc.getName();
AvroConverter valueConverter = getValueConverter(fieldDesc);
if (fieldDesc instanceof CustomFieldDesc) {
Object customField = valueMap.get(fieldName);
if (customField != null) {
Object value = getSimpleProperty(customField, "value");
return valueConverter.convertToAvro(value);
}
return null;
} else {
Object value = valueMap.get(fieldName);
return valueConverter.convertToAvro(value);
}
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class NsObjectTransducer method writeSimpleField.
/**
* Write a value to a simple field which is defined by NetSuite standard data model.
*
* @param nsObject target NetSuite data model object which to write field value to
* @param fieldDesc field descriptor
* @param replace specifies whether to forcibly replace a field's value
* @param nullFieldNames collection to register null'ed fields
* @param value value to be written, can be <code>null</code>
*/
protected void writeSimpleField(Object nsObject, SimpleFieldDesc fieldDesc, boolean replace, Collection<String> nullFieldNames, Object value) {
AvroConverter valueConverter = getValueConverter(fieldDesc);
Object targetValue = valueConverter.convertToDatum(value);
if (targetValue == null) {
if (replace) {
setSimpleProperty(nsObject, fieldDesc.getPropertyName(), null);
nullFieldNames.add(fieldDesc.getName());
}
} else {
setSimpleProperty(nsObject, fieldDesc.getPropertyName(), targetValue);
}
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class BigQueryAvroRegistryTest method testGetConverter_double.
@Test
public void testGetConverter_double() {
AvroConverter converter = BigQueryAvroRegistry.get().getConverter(Schema.create(Schema.Type.DOUBLE));
assertThat(converter.convertToAvro(123L), Matchers.<Object>is(123d));
assertThat(converter.convertToAvro(123), Matchers.<Object>is(123d));
assertThat(converter.convertToAvro((short) 123), Matchers.<Object>is(123d));
assertThat(converter.convertToAvro((byte) 123), Matchers.<Object>is(123d));
assertThat(converter.convertToAvro(123d), Matchers.<Object>is(123d));
assertThat(converter.convertToAvro(123f), Matchers.<Object>is(123d));
assertThat(converter.convertToAvro(null), nullValue());
}
Aggregations