Search in sources :

Example 6 with NsObjectInputTransducer

use of org.talend.components.netsuite.input.NsObjectInputTransducer 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));
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) MutableDateTime(org.joda.time.MutableDateTime) NsObjectInputTransducer(org.talend.components.netsuite.input.NsObjectInputTransducer) DateTimeZone(org.joda.time.DateTimeZone) AvroConverter(org.talend.daikon.avro.converter.AvroConverter) Date(java.util.Date) FieldDesc(org.talend.components.netsuite.client.model.FieldDesc) Test(org.junit.Test)

Example 7 with NsObjectInputTransducer

use of org.talend.components.netsuite.input.NsObjectInputTransducer 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 8 with NsObjectInputTransducer

use of org.talend.components.netsuite.input.NsObjectInputTransducer 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 9 with NsObjectInputTransducer

use of org.talend.components.netsuite.input.NsObjectInputTransducer 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 10 with NsObjectInputTransducer

use of org.talend.components.netsuite.input.NsObjectInputTransducer 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)

Aggregations

NsObjectInputTransducer (org.talend.components.netsuite.input.NsObjectInputTransducer)14 Test (org.junit.Test)11 FieldDesc (org.talend.components.netsuite.client.model.FieldDesc)8 Schema (org.apache.avro.Schema)7 IndexedRecord (org.apache.avro.generic.IndexedRecord)7 AvroConverter (org.talend.daikon.avro.converter.AvroConverter)7 TypeDesc (org.talend.components.netsuite.client.model.TypeDesc)4 RecordRef (com.netsuite.webservices.test.platform.core.RecordRef)3 Record (com.netsuite.webservices.v2014_2.platform.core.Record)3 Record (com.netsuite.webservices.v2016_2.platform.core.Record)3 ArrayList (java.util.ArrayList)3 GenericRecord (org.apache.avro.generic.GenericRecord)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 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