Search in sources :

Example 11 with AvroConverter

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());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RecordRef(com.netsuite.webservices.test.platform.core.RecordRef) NsObjectInputTransducer(org.talend.components.netsuite.input.NsObjectInputTransducer) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AvroConverter(org.talend.daikon.avro.converter.AvroConverter) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc) Test(org.junit.Test)

Example 12 with AvroConverter

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());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BooleanCustomFieldRef(com.netsuite.webservices.test.platform.core.BooleanCustomFieldRef) NsObjectInputTransducer(org.talend.components.netsuite.input.NsObjectInputTransducer) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) CustomFieldList(com.netsuite.webservices.test.platform.core.CustomFieldList) StringCustomFieldRef(com.netsuite.webservices.test.platform.core.StringCustomFieldRef) AvroConverter(org.talend.daikon.avro.converter.AvroConverter) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc) Test(org.junit.Test)

Example 13 with AvroConverter

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);
    }
}
Also used : CustomFieldRefType(org.talend.components.netsuite.client.model.customfield.CustomFieldRefType) ArrayList(java.util.ArrayList) List(java.util.List) NsRef(org.talend.components.netsuite.client.NsRef) AvroConverter(org.talend.daikon.avro.converter.AvroConverter)

Example 14 with AvroConverter

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"));
}
Also used : Schema(org.apache.avro.Schema) BigDecimal(java.math.BigDecimal) AvroConverter(org.talend.daikon.avro.converter.AvroConverter) Test(org.junit.Test)

Aggregations

AvroConverter (org.talend.daikon.avro.converter.AvroConverter)14 Test (org.junit.Test)11 NsObjectInputTransducer (org.talend.components.netsuite.input.NsObjectInputTransducer)7 FieldDesc (org.talend.components.netsuite.client.model.FieldDesc)6 RecordRef (com.netsuite.webservices.test.platform.core.RecordRef)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 BooleanCustomFieldRef (com.netsuite.webservices.test.platform.core.BooleanCustomFieldRef)2 CustomFieldList (com.netsuite.webservices.test.platform.core.CustomFieldList)2 StringCustomFieldRef (com.netsuite.webservices.test.platform.core.StringCustomFieldRef)2 Date (java.util.Date)2 Schema (org.apache.avro.Schema)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 Account (com.netsuite.webservices.test.lists.accounting.Account)1 AccountType (com.netsuite.webservices.test.lists.accounting.types.AccountType)1 ConsolidatedRate (com.netsuite.webservices.test.lists.accounting.types.ConsolidatedRate)1 RecordRefList (com.netsuite.webservices.test.platform.core.RecordRefList)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1