Search in sources :

Example 26 with TargetTestClass

use of io.atlasmap.java.test.TargetTestClass in project atlasmap by atlasmap.

the class JavaWriterUtilMock method getObjectFromParent.

/**
 * Retrieve a child object (which may be a complex class or collection class)
 * from the given parentObject.
 *
 * @param field
 *            - provided for convenience, probably not needed here
 * @param ParentObject
 *            - the object to find the child on
 * @param segmentContext
 *            - the segment of the field's path that references the child object
 */
@Override
public Object getObjectFromParent(Field field, Object parentObject, SegmentContext segmentContext) throws AtlasException {
    String segment = segmentContext.getSegment();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Retrieving child '" + segmentContext.getSegmentPath() + "'.\n\tparentObject: " + parentObject);
    }
    if (parentObject == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Cannot find child '" + segmentContext.getSegmentPath() + "', parent is null.");
        }
        return null;
    }
    // clean up our segment from something like "@addressLine1" to "addressLine1".
    // collection segments like "orders[4]" will be cleaned to "orders"
    String cleanedSegment = AtlasPath.cleanPathSegment(segmentContext.getSegment());
    Object childObject = null;
    if (parentObject instanceof TargetAddress && "addressLine1".equals(cleanedSegment)) {
        childObject = ((TargetAddress) parentObject).getAddressLine1();
    } else if ("orders".equals(cleanedSegment) && parentObject instanceof TestListOrders) {
        childObject = ((TestListOrders) parentObject).getOrders();
    } else if ("orders".equals(cleanedSegment) && parentObject instanceof TargetOrderArray) {
        childObject = ((TargetOrderArray) parentObject).getOrders();
    } else if ("address".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
        childObject = ((TargetOrder) parentObject).getAddress();
    } else if ("address".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        childObject = ((TargetTestClass) parentObject).getAddress();
    } else if ("listOrders".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        childObject = ((TargetTestClass) parentObject).getListOrders();
    } else if ("orderArray".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        childObject = ((TargetTestClass) parentObject).getOrderArray();
    } else if ("contact".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
        childObject = ((TargetOrder) parentObject).getContact();
    } else if ("numberOrders".equals(cleanedSegment) && parentObject instanceof TargetOrderArray) {
        childObject = ((TargetOrderArray) parentObject).getNumberOrders();
    } else if ("primitives".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        childObject = ((TargetTestClass) parentObject).getPrimitives();
    } else if ("intArrayField".equals(cleanedSegment) && parentObject instanceof TargetFlatPrimitiveClass) {
        childObject = ((TargetFlatPrimitiveClass) parentObject).getIntArrayField();
    } else if ("boxedStringArrayField".equals(cleanedSegment) && parentObject instanceof TargetFlatPrimitiveClass) {
        childObject = ((TargetFlatPrimitiveClass) parentObject).getBoxedStringArrayField();
    } else if ("contact".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        childObject = ((TargetTestClass) parentObject).getContact();
    } else if ("nothing".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        childObject = null;
    } else {
        String clz = parentObject.getClass().getSimpleName();
        String getter = "get" + cleanedSegment.substring(0, 1).toUpperCase() + cleanedSegment.substring(1);
        String fix = "} else if (\"" + cleanedSegment + "\".equals(cleanedSegment) && parentObject instanceof " + clz + ") {\n " + "childObject = ((" + clz + ")parentObject)." + getter + "();";
        LOG.error(fix);
        throw new AtlasException("Don't know how to handle get object from parent: " + parentObject + ", segment: " + cleanedSegment);
    }
    // FIXME: Matt, right? an @ here indicates use the getter, or does @ mean access
    // member?
    boolean useGetter = AtlasPath.isAttributeSegment(segment);
    if (useGetter) {
    // FIXME: matt, something like this, but with reflection and what not
    // childObject = parentObject.getAddressLine1();
    } else {
    // FIXME: Matt, something lik this, but with reflection
    // childObject = parentObject.addressLine1;
    }
    if (LOG.isDebugEnabled()) {
        if (childObject == null) {
            LOG.debug("Could not find child object for path: " + segmentContext.getSegmentPath());
        } else {
            LOG.debug("Found child object for path '" + segmentContext.getSegmentPath() + "': " + childObject);
        }
    }
    // TODO: matt, should we throw an exception here if null?
    return childObject;
}
Also used : TargetFlatPrimitiveClass(io.atlasmap.java.test.TargetFlatPrimitiveClass) TestListOrders(io.atlasmap.java.test.TestListOrders) TargetAddress(io.atlasmap.java.test.TargetAddress) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetTestClass(io.atlasmap.java.test.TargetTestClass) AtlasException(io.atlasmap.api.AtlasException) TargetOrderArray(io.atlasmap.java.test.TargetOrderArray)

Example 27 with TargetTestClass

use of io.atlasmap.java.test.TargetTestClass in project atlasmap by atlasmap.

the class JavaWriterUtilMock method setObjectOnParent.

/**
 * Set the given object within the parentObject.
 *
 * @param field
 *            - provided if we need it, I don't think we will since we already
 *            have the value in hand?
 * @param segmentContext
 *            - current segment for the field's path, this will be the last
 *            segment in the path.
 * @param parentObject
 *            - the object we're setting the value in
 * @param childObject
 *            - the childObject to set
 */
@SuppressWarnings({ "unchecked" })
@Override
public void setObjectOnParent(Field field, SegmentContext segmentContext, Object parentObject, Object childObject) throws AtlasException {
    String segment = segmentContext.getSegment();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Setting object '" + segmentContext.getSegmentPath() + "'.\n\tchildObject: " + childObject + "\n\tparentObject: " + parentObject);
    }
    // now the cleanedSegment is a cleaned name such as "addressLine1"
    String cleanedSegment = AtlasPath.cleanPathSegment(segment);
    if ("addressLine1".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
        ((TargetAddress) parentObject).setAddressLine1((String) childObject);
    } else if ("addressLine1".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
        ((TargetAddress) parentObject).setAddressLine1((String) childObject);
    } else if ("addressLine2".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
        ((TargetAddress) parentObject).setAddressLine2((String) childObject);
    } else if ("city".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
        ((TargetAddress) parentObject).setCity((String) childObject);
    } else if ("state".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
        ((TargetAddress) parentObject).setState((String) childObject);
    } else if ("zipCode".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
        ((TargetAddress) parentObject).setZipCode((String) childObject);
    } else if ("orders".equals(cleanedSegment) && parentObject instanceof TestListOrders) {
        ((TestListOrders) parentObject).setOrders((List<BaseOrder>) childObject);
    } else if ("orders".equals(cleanedSegment) && parentObject instanceof TargetOrderArray) {
        ((TargetOrderArray) parentObject).setOrders((TargetOrder[]) childObject);
    } else if ("address".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
        ((TargetOrder) parentObject).setAddress((TargetAddress) childObject);
    } else if ("name".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        ((TargetTestClass) parentObject).setName((String) childObject);
    } else if ("address".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        ((TargetTestClass) parentObject).setAddress((TargetAddress) childObject);
    } else if ("listOrders".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        ((TargetTestClass) parentObject).setListOrders((TestListOrders) childObject);
    } else if ("orderId".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
        ((TargetOrder) parentObject).setOrderId((Integer) childObject);
    } else if ("orderArray".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        ((TargetTestClass) parentObject).setOrderArray((TargetOrderArray) childObject);
    } else if ("numberOrders".equals(cleanedSegment) && parentObject instanceof TargetOrderArray) {
        ((TargetOrderArray) parentObject).setNumberOrders((Integer) childObject);
    } else if ("contact".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
        ((TargetOrder) parentObject).setContact((TargetContact) childObject);
    } else if ("firstName".equals(cleanedSegment) && parentObject instanceof TargetContact) {
        ((TargetContact) parentObject).setFirstName((String) childObject);
    } else if ("lastName".equals(cleanedSegment) && parentObject instanceof TargetContact) {
        ((TargetContact) parentObject).setLastName((String) childObject);
    } else if ("primitives".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        ((TargetTestClass) parentObject).setPrimitives((TargetFlatPrimitiveClass) childObject);
    } else if ("intArrayField".equals(cleanedSegment) && parentObject instanceof TargetFlatPrimitiveClass) {
        ((TargetFlatPrimitiveClass) parentObject).setIntArrayField((int[]) childObject);
    } else if ("boxedStringArrayField".equals(cleanedSegment) && parentObject instanceof TargetFlatPrimitiveClass) {
        ((TargetFlatPrimitiveClass) parentObject).setBoxedStringArrayField((String[]) childObject);
    } else if ("statesLong".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
        ((TargetTestClass) parentObject).setStatesLong((StateEnumClassLong) childObject);
    } else {
        String clz = parentObject.getClass().getSimpleName();
        String clz2 = childObject.getClass().getSimpleName();
        String setter = "set" + cleanedSegment.substring(0, 1).toUpperCase() + cleanedSegment.substring(1);
        String fix = "} else if (\"" + cleanedSegment + "\".equals(cleanedSegment) && parentObject instanceof " + clz + ") {\n " + "((" + clz + ") parentObject)." + setter + "((" + clz2 + ")childObject);";
        LOG.error(fix);
        throw new AtlasException("FIX: " + fix);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Object after value written: " + parentObject);
    }
}
Also used : StateEnumClassLong(io.atlasmap.java.test.StateEnumClassLong) TargetFlatPrimitiveClass(io.atlasmap.java.test.TargetFlatPrimitiveClass) TestListOrders(io.atlasmap.java.test.TestListOrders) BaseOrder(io.atlasmap.java.test.BaseOrder) TargetContact(io.atlasmap.java.test.TargetContact) TargetAddress(io.atlasmap.java.test.TargetAddress) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetTestClass(io.atlasmap.java.test.TargetTestClass) AtlasException(io.atlasmap.api.AtlasException) TargetOrderArray(io.atlasmap.java.test.TargetOrderArray)

Example 28 with TargetTestClass

use of io.atlasmap.java.test.TargetTestClass in project atlasmap by atlasmap.

the class BaseDocumentWriterTest method reset.

@Before
public void reset() {
    classLoader = Thread.currentThread().getContextClassLoader();
    writer = new DocumentJavaFieldWriter(conversionService);
    writer.setTargetValueConverter(new TargetValueConverter(classLoader, conversionService) {

        public Object convert(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
            return targetField.getValue();
        }
    });
    field = null;
    segmentContexts = new LinkedList<>();
    targetTestClassInstance = new TargetTestClass();
    targetTestClassInstance.setContact(new TargetContact());
    targetTestClassInstance.setAddress(new TargetAddress());
    targetOrderListInstance = new TestListOrders();
    targetOrderListInstance.setOrders(new LinkedList<>());
    targetOrderListInstance.getOrders().add(new TargetOrder());
    targetOrderListInstance.getOrders().add(new TargetOrder());
    targetTestClassInstance.setListOrders(targetOrderListInstance);
    targetOrderArrayInstance = new TargetOrderArray();
    targetOrderArrayInstance.setOrders(new BaseOrder[2]);
    targetOrderArrayInstance.getOrders()[0] = new TargetOrder();
    targetOrderArrayInstance.getOrders()[1] = new TargetOrder();
    targetTestClassInstance.setOrderArray(targetOrderArrayInstance);
}
Also used : AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) TargetContact(io.atlasmap.java.test.TargetContact) TargetAddress(io.atlasmap.java.test.TargetAddress) AtlasException(io.atlasmap.api.AtlasException) TargetOrderArray(io.atlasmap.java.test.TargetOrderArray) Field(io.atlasmap.v2.Field) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) TestListOrders(io.atlasmap.java.test.TestListOrders) LookupTable(io.atlasmap.v2.LookupTable) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetTestClass(io.atlasmap.java.test.TargetTestClass) Before(org.junit.Before)

Example 29 with TargetTestClass

use of io.atlasmap.java.test.TargetTestClass in project atlasmap by atlasmap.

the class DocumentJavaFieldWriterTest method testFullWrite.

@Test
public void testFullWrite() throws Exception {
    addClassForFieldPath("/", TargetTestClass.class);
    addClassForFieldPath("/address", TargetAddress.class);
    addClassForFieldPath("/listOrders", TestListOrders.class);
    addClassForFieldPath("/listOrders/orders<5>", TargetOrder.class);
    addClassForFieldPath("/listOrders/orders<5>/address", TargetAddress.class);
    addClassForFieldPath("/orderArray", TargetOrderArray.class);
    addClassForFieldPath("/orderArray/orders[5]", TargetOrder.class);
    addClassForFieldPath("/orderArray/orders[5]/contact", TargetContact.class);
    addClassForFieldPath("/primitives", TargetFlatPrimitiveClass.class);
    addClassForFieldPath("/primitives/intArrayField[]", int.class);
    addClassForFieldPath("/primitives/boxedStringArrayField[19]", String.class);
    addClassForFieldPath("/statesLong", StateEnumClassLong.class);
    write("/name", "someName");
    TargetTestClass o = (TargetTestClass) writer.getRootObject();
    ensureNotNullAndClass(o, TargetTestClass.class);
    assertEquals("someName", o.getName());
    write("/address/addressLine1", "123 any street");
    ensureNotNullAndClass(o.getAddress(), TargetAddress.class);
    assertEquals("123 any street", o.getAddress().getAddressLine1());
    write("/listOrders/orders<5>/orderId", 1234);
    ensureNotNullAndClass(o.getListOrders(), TestListOrders.class);
    ensureNotNullAndClass(o.getListOrders().getOrders(), LinkedList.class);
    assertEquals(6, o.getListOrders().getOrders().size());
    for (int i = 0; i < 6; i++) {
        System.out.println("Checking #" + i);
        BaseOrder order = o.getListOrders().getOrders().get(i);
        if (i == 5) {
            ensureNotNullAndClass(order, TargetOrder.class);
        } else {
            assertNull(order);
        }
    }
    BaseOrder order = o.getListOrders().getOrders().get(5);
    assertEquals((Integer) 1234, order.getOrderId());
    write("/listOrders/orders<2>/address/city", "Austin");
    assertEquals(6, o.getListOrders().getOrders().size());
    // ensure earlier fields are ok
    order = o.getListOrders().getOrders().get(5);
    assertEquals((Integer) 1234, order.getOrderId());
    // ensure our new field is ok
    order = o.getListOrders().getOrders().get(2);
    ensureNotNullAndClass(order.getAddress(), TargetAddress.class);
    assertEquals("Austin", order.getAddress().getCity());
    write("/orderArray/numberOrders", 56);
    ensureNotNullAndClass(o.getOrderArray(), TargetOrderArray.class);
    assertEquals((Integer) 56, o.getOrderArray().getNumberOrders());
    write("/orderArray/orders[2]/contact/firstName", "fName");
    ensureNotNullAndClass(o.getOrderArray().getOrders(), TargetOrder[].class);
    assertEquals(3, o.getOrderArray().getOrders().length);
    for (int i = 0; i < 3; i++) {
        System.out.println("Checking #" + i);
        BaseOrder order2 = o.getOrderArray().getOrders()[i];
        if (i == 2) {
            ensureNotNullAndClass(order2, TargetOrder.class);
        } else {
            assertNull(order2);
        }
    }
    ensureNotNullAndClass(o.getOrderArray().getOrders()[2].getContact(), TargetContact.class);
    // ensure previous fields are ok
    assertEquals((Integer) 56, o.getOrderArray().getNumberOrders());
    // ensure new field is ok
    assertEquals("fName", o.getOrderArray().getOrders()[2].getContact().getFirstName());
    write("/orderArray/orders[2]/contact/lastName", "lName");
    // ensure previous fields are ok
    assertEquals((Integer) 56, o.getOrderArray().getNumberOrders());
    assertEquals("fName", o.getOrderArray().getOrders()[2].getContact().getFirstName());
    // ensure new field is ok
    assertEquals("lName", o.getOrderArray().getOrders()[2].getContact().getLastName());
    // test writing primitive array values
    write("/primitives/intArrayField[10]", 3425);
    ensureNotNullAndClass(o.getPrimitives(), TargetFlatPrimitiveClass.class);
    ensureNotNullAndClass(o.getPrimitives().getIntArrayField(), int[].class);
    assertEquals(11, o.getPrimitives().getIntArrayField().length);
    assertEquals(3425, o.getPrimitives().getIntArrayField()[10]);
    write("/primitives/boxedStringArrayField[10]", "boxedString");
    ensureNotNullAndClass(o.getPrimitives().getBoxedStringArrayField(), String[].class);
    assertEquals(11, o.getPrimitives().getBoxedStringArrayField().length);
    for (int i = 0; i < 10; i++) {
        System.out.println("Checking #" + i);
        assertNull(o.getPrimitives().getBoxedStringArrayField()[i]);
    }
    ensureNotNullAndClass(o.getPrimitives().getBoxedStringArrayField()[10], String.class);
    assertEquals("boxedString", o.getPrimitives().getBoxedStringArrayField()[10]);
    // test writing enum values
    write("/statesLong", StateEnumClassLong.Massachusetts);
    assertNotNull(o.getStatesLong());
    assertEquals(StateEnumClassLong.Massachusetts, o.getStatesLong());
    // test overwriting values
    assertEquals("123 any street", o.getAddress().getAddressLine1());
    write("/address/addressLine1", "123 any street (2)");
    assertEquals("123 any street (2)", o.getAddress().getAddressLine1());
    order = o.getListOrders().getOrders().get(5);
    assertEquals((Integer) 1234, order.getOrderId());
    write("/listOrders/orders<5>/orderId", 2221234);
    assertEquals((Integer) 2221234, order.getOrderId());
    order = o.getListOrders().getOrders().get(2);
    assertEquals("Austin", order.getAddress().getCity());
    write("/listOrders/orders<2>/address/city", "Austin (2)");
    assertEquals("Austin (2)", order.getAddress().getCity());
    assertEquals((Integer) 56, o.getOrderArray().getNumberOrders());
    write("/orderArray/numberOrders", 22256);
    assertEquals((Integer) 22256, o.getOrderArray().getNumberOrders());
    assertEquals("fName", o.getOrderArray().getOrders()[2].getContact().getFirstName());
    write("/orderArray/orders[2]/contact/firstName", "fName (2)");
    assertEquals("fName (2)", o.getOrderArray().getOrders()[2].getContact().getFirstName());
    assertEquals("lName", o.getOrderArray().getOrders()[2].getContact().getLastName());
    write("/orderArray/orders[2]/contact/lastName", "lName (2)");
    assertEquals("lName (2)", o.getOrderArray().getOrders()[2].getContact().getLastName());
    assertEquals(3425, o.getPrimitives().getIntArrayField()[10]);
    write("/primitives/intArrayField[10]", 2223425);
    assertEquals(2223425, o.getPrimitives().getIntArrayField()[10]);
    assertEquals("boxedString", o.getPrimitives().getBoxedStringArrayField()[10]);
    write("/primitives/boxedStringArrayField[10]", "boxedString (2)");
    assertEquals("boxedString (2)", o.getPrimitives().getBoxedStringArrayField()[10]);
    assertEquals(StateEnumClassLong.Massachusetts, o.getStatesLong());
    write("/statesLong", StateEnumClassLong.Alabama);
    assertEquals(StateEnumClassLong.Alabama, o.getStatesLong());
}
Also used : BaseOrder(io.atlasmap.java.test.BaseOrder) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetTestClass(io.atlasmap.java.test.TargetTestClass) Test(org.junit.Test)

Example 30 with TargetTestClass

use of io.atlasmap.java.test.TargetTestClass in project atlasmap by atlasmap.

the class JavaFieldReaderTest method testReadNotExistingPath.

@Test
public void testReadNotExistingPath() throws Exception {
    TargetTestClass source = new TargetTestClass();
    reader.setDocument(source);
    read("/address/addressLine1", FieldType.STRING);
    assertEquals(0, audits.size());
    assertNull(field.getValue());
    assertEquals(0, audits.size());
}
Also used : TargetTestClass(io.atlasmap.java.test.TargetTestClass) Test(org.junit.jupiter.api.Test)

Aggregations

TargetTestClass (io.atlasmap.java.test.TargetTestClass)46 Test (org.junit.jupiter.api.Test)28 AtlasContext (io.atlasmap.api.AtlasContext)26 AtlasSession (io.atlasmap.api.AtlasSession)26 TargetContact (io.atlasmap.java.test.TargetContact)24 File (java.io.File)24 BaseOrder (io.atlasmap.java.test.BaseOrder)15 Test (org.junit.Test)14 AtlasMappingBaseTest (io.atlasmap.itests.reference.AtlasMappingBaseTest)13 AtlasMappingBaseTest (io.atlasmap.reference.AtlasMappingBaseTest)11 TargetAddress (io.atlasmap.java.test.TargetAddress)10 TargetOrder (io.atlasmap.java.test.TargetOrder)7 TargetOrderArray (io.atlasmap.java.test.TargetOrderArray)5 JavaField (io.atlasmap.java.v2.JavaField)5 Field (io.atlasmap.v2.Field)5 TestListOrders (io.atlasmap.java.test.TestListOrders)4 AtlasJsonTestUnrootedMapper (io.atlasmap.json.test.AtlasJsonTestUnrootedMapper)4 AtlasException (io.atlasmap.api.AtlasException)3 TargetFlatPrimitiveClass (io.atlasmap.java.test.TargetFlatPrimitiveClass)3 FieldGroup (io.atlasmap.v2.FieldGroup)2