use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class ValueConverterTest method testJsonConverterNestedObjectConcreteType.
@Test
public void testJsonConverterNestedObjectConcreteType() throws Exception {
NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());
ObjectMapper objectMapper = new ObjectMapper();
RecordRef recordRef1 = new RecordRef();
recordRef1.setInternalId("12345");
recordRef1.setName("R&D");
ObjectNode recordRefNode1 = JsonNodeFactory.instance.objectNode();
recordRefNode1.set("name", JsonNodeFactory.instance.textNode("R&D"));
recordRefNode1.set("internalId", JsonNodeFactory.instance.textNode("12345"));
recordRefNode1.set("externalId", JsonNodeFactory.instance.nullNode());
recordRefNode1.set("type", JsonNodeFactory.instance.nullNode());
String recordRefJson1 = objectMapper.writer().writeValueAsString(recordRef1);
FieldDesc fieldDesc = typeDesc.getField("department");
AvroConverter<RecordRef, String> converter1 = (AvroConverter<RecordRef, String>) transducer.getValueConverter(fieldDesc);
String testRecordRefJson1 = converter1.convertToAvro(recordRef1);
assertNotNull(testRecordRefJson1);
JsonNode testRecordRefNode1 = objectMapper.reader().readTree(testRecordRefJson1);
assertTrue(testRecordRefNode1.has("name"));
assertTrue(testRecordRefNode1.has("internalId"));
assertTrue(testRecordRefNode1.has("externalId"));
assertTrue(testRecordRefNode1.has("type"));
assertEquals(recordRef1.getName(), testRecordRefNode1.get("name").asText());
assertEquals(recordRef1.getInternalId(), testRecordRefNode1.get("internalId").asText());
assertEquals(recordRef1.getExternalId(), testRecordRefNode1.get("externalId").asText(null));
assertNull(testRecordRefNode1.get("type").asText(null));
RecordRef testRecordRef1 = converter1.convertToDatum(recordRefJson1);
assertNotNull(testRecordRef1);
assertEquals(recordRef1.getName(), testRecordRef1.getName());
assertEquals(recordRef1.getInternalId(), testRecordRef1.getInternalId());
assertEquals(recordRef1.getExternalId(), testRecordRef1.getExternalId());
assertEquals(recordRef1.getType(), testRecordRef1.getType());
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class ValueConverterTest method testJsonConverterNestedObjectPolymorphicType.
@Test
public void testJsonConverterNestedObjectPolymorphicType() throws Exception {
NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());
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);
FieldDesc fieldDesc = typeDesc.getField("customFieldList");
AvroConverter<CustomFieldList, String> converter1 = (AvroConverter<CustomFieldList, String>) transducer.getValueConverter(fieldDesc);
String testJson1 = converter1.convertToAvro(customFieldList);
assertNotNull(testJson1);
ObjectNode node1 = JsonNodeFactory.instance.objectNode();
ArrayNode list1 = JsonNodeFactory.instance.arrayNode();
node1.set("customField", list1);
ObjectNode customFieldNode1 = JsonNodeFactory.instance.objectNode();
customFieldNode1.set(NsTypeResolverBuilder.TYPE_PROPERTY_NAME, JsonNodeFactory.instance.textNode("BooleanCustomFieldRef"));
customFieldNode1.set("internalId", JsonNodeFactory.instance.textNode("100001"));
customFieldNode1.set("scriptId", JsonNodeFactory.instance.textNode("custentity_field1"));
customFieldNode1.set("value", JsonNodeFactory.instance.booleanNode(true));
list1.add(customFieldNode1);
ObjectNode customFieldNode2 = JsonNodeFactory.instance.objectNode();
customFieldNode2.set(NsTypeResolverBuilder.TYPE_PROPERTY_NAME, JsonNodeFactory.instance.textNode("StringCustomFieldRef"));
customFieldNode2.set("internalId", JsonNodeFactory.instance.textNode("100002"));
customFieldNode2.set("scriptId", JsonNodeFactory.instance.textNode("custentity_field2"));
customFieldNode2.set("value", JsonNodeFactory.instance.textNode("test123"));
list1.add(customFieldNode2);
CustomFieldList testCustomFieldList = converter1.convertToDatum(node1.toString());
assertNotNull(testCustomFieldList);
assertEquals(2, testCustomFieldList.getCustomField().size());
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class NsObjectTransducer method writeCustomField.
/**
* Write a custom field which is not defined by NetSuite standard data model.
*
* @param nsObject target NetSuite data model object which to write field value to
* @param fieldDesc field descriptor
* @param customFieldMap map of native custom field objects by names
* @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 writeCustomField(Object nsObject, CustomFieldDesc fieldDesc, Map<String, Object> customFieldMap, boolean replace, Collection<String> nullFieldNames, Object value) {
NsRef ref = fieldDesc.getCustomizationRef();
CustomFieldRefType customFieldRefType = fieldDesc.getCustomFieldType();
// Create custom field list wrapper if required
Object customFieldListWrapper = getSimpleProperty(nsObject, "customFieldList");
if (customFieldListWrapper == null) {
customFieldListWrapper = clientService.getBasicMetaData().createInstance("CustomFieldList");
setSimpleProperty(nsObject, "customFieldList", customFieldListWrapper);
}
List<Object> customFieldList = (List<Object>) getSimpleProperty(customFieldListWrapper, "customField");
Object customField = customFieldMap.get(ref.getScriptId());
AvroConverter valueConverter = getValueConverter(fieldDesc);
Object targetValue = valueConverter.convertToDatum(value);
if (targetValue == null) {
if (replace && customField != null && customFieldList != null) {
customFieldList.remove(customField);
nullFieldNames.add(fieldDesc.getName());
}
} else {
if (customField == null) {
// Custom field instance doesn't exist,
// create new instance and set identifiers
customField = clientService.getBasicMetaData().createInstance(customFieldRefType.getTypeName());
setSimpleProperty(customField, "scriptId", ref.getScriptId());
setSimpleProperty(customField, "internalId", ref.getInternalId());
customFieldList.add(customField);
customFieldMap.put(ref.getScriptId(), customField);
}
setSimpleProperty(customField, "value", targetValue);
}
}
use of org.talend.daikon.avro.converter.AvroConverter in project components by Talend.
the class SalesforceAvroRegistryTest method testStringToDecimalConverter.
@Test
public void testStringToDecimalConverter() {
Schema schema = //
SchemaBuilder.builder().record("Schema").fields().name("Field_A").type(AvroUtils._decimal()).noDefault().endRecord();
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("20000000000000000000000000.123456789");
assertNotNull(value);
assertThat(value, instanceOf(BigDecimal.class));
assertThat((BigDecimal) value, equalTo(new BigDecimal("20000000000000000000000000.123456789")));
assertNull(converter.convertToAvro(""));
assertNull(converter.convertToAvro(null));
String sValue = ((AvroConverter<String, BigDecimal>) converter).convertToDatum(new BigDecimal("20000000000000000000000000.123456789"));
assertNotNull(sValue);
assertThat(sValue, equalTo("20000000000000000000000000.123456789"));
}
Aggregations