Search in sources :

Example 6 with AvroConverter

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

Example 7 with AvroConverter

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);
}
Also used : RecordRefList(com.netsuite.webservices.test.platform.core.RecordRefList) Account(com.netsuite.webservices.test.lists.accounting.Account) RecordRef(com.netsuite.webservices.test.platform.core.RecordRef) BooleanCustomFieldRef(com.netsuite.webservices.test.platform.core.BooleanCustomFieldRef) NsObjectInputTransducer(org.talend.components.netsuite.input.NsObjectInputTransducer) CustomFieldList(com.netsuite.webservices.test.platform.core.CustomFieldList) StringCustomFieldRef(com.netsuite.webservices.test.platform.core.StringCustomFieldRef) AvroConverter(org.talend.daikon.avro.converter.AvroConverter) Test(org.junit.Test)

Example 8 with AvroConverter

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);
    }
}
Also used : CustomFieldDesc(org.talend.components.netsuite.client.model.CustomFieldDesc) AvroConverter(org.talend.daikon.avro.converter.AvroConverter)

Example 9 with AvroConverter

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);
    }
}
Also used : AvroConverter(org.talend.daikon.avro.converter.AvroConverter)

Example 10 with AvroConverter

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());
}
Also used : 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