Search in sources :

Example 6 with JsonComplexType

use of io.atlasmap.json.v2.JsonComplexType in project atlasmap by atlasmap.

the class SchemaInspectorTest method inspectObjectArrayWithRoot.

@Test
public void inspectObjectArrayWithRoot() throws Exception {
    final String schema = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/complex-array-rooted.json")));
    JsonDocument document = inspectionService.inspectJsonSchema(schema);
    assertNotNull(document);
    assertEquals(1, document.getFields().getField().size());
    JsonComplexType root = (JsonComplexType) document.getFields().getField().get(0);
    assertNotNull(root);
    assertEquals(3, root.getJsonFields().getJsonField().size());
    assertEquals("SourceOrderList", root.getName());
    JsonComplexType orders = (JsonComplexType) root.getJsonFields().getJsonField().get(0);
    assertNotNull(orders);
    assertEquals(3, orders.getJsonFields().getJsonField().size());
    assertEquals("orders", orders.getName());
    assertEquals(CollectionType.LIST, orders.getCollectionType());
    assertEquals(FieldType.COMPLEX, orders.getFieldType());
    JsonField orderBatchNumber = root.getJsonFields().getJsonField().get(1);
    assertNotNull(orderBatchNumber);
    assertEquals("orderBatchNumber", orderBatchNumber.getName());
    assertEquals("/SourceOrderList/orderBatchNumber", orderBatchNumber.getPath());
    assertEquals(FieldType.INTEGER, orderBatchNumber.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, orderBatchNumber.getStatus());
    JsonField numberOrders = root.getJsonFields().getJsonField().get(2);
    assertNotNull(numberOrders);
    assertEquals("numberOrders", numberOrders.getName());
    assertEquals("/SourceOrderList/numberOrders", numberOrders.getPath());
    assertEquals(FieldType.INTEGER, numberOrders.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, numberOrders.getStatus());
    JsonComplexType address = (JsonComplexType) orders.getJsonFields().getJsonField().get(0);
    assertNotNull(address);
    assertEquals(5, address.getJsonFields().getJsonField().size());
    assertEquals("address", address.getName());
    assertEquals("/SourceOrderList/orders/address", address.getPath());
    JsonField addressLine1 = address.getJsonFields().getJsonField().get(0);
    assertNotNull(addressLine1);
    assertEquals("addressLine1", addressLine1.getName());
    assertEquals("/SourceOrderList/orders/address/addressLine1", addressLine1.getPath());
    assertEquals(FieldType.STRING, addressLine1.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, addressLine1.getStatus());
    JsonField addressLine2 = address.getJsonFields().getJsonField().get(1);
    assertNotNull(addressLine2);
    assertEquals("addressLine2", addressLine2.getName());
    assertEquals("/SourceOrderList/orders/address/addressLine2", addressLine2.getPath());
    assertEquals(FieldType.STRING, addressLine2.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, addressLine2.getStatus());
    JsonField city = address.getJsonFields().getJsonField().get(2);
    assertNotNull(city);
    assertEquals("city", city.getName());
    assertEquals("/SourceOrderList/orders/address/city", city.getPath());
    assertEquals(FieldType.STRING, city.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, city.getStatus());
    JsonField state = address.getJsonFields().getJsonField().get(3);
    assertNotNull(state);
    assertEquals("state", state.getName());
    assertEquals("/SourceOrderList/orders/address/state", state.getPath());
    assertEquals(FieldType.STRING, state.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, state.getStatus());
    JsonField postalCode = address.getJsonFields().getJsonField().get(4);
    assertNotNull(postalCode);
    assertEquals("zipCode", postalCode.getName());
    assertEquals("/SourceOrderList/orders/address/zipCode", postalCode.getPath());
    assertEquals(FieldType.STRING, postalCode.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, postalCode.getStatus());
    JsonComplexType contact = (JsonComplexType) orders.getJsonFields().getJsonField().get(1);
    assertNotNull(contact);
    assertEquals(4, contact.getJsonFields().getJsonField().size());
    assertEquals("contact", contact.getName());
    assertEquals("/SourceOrderList/orders/contact", contact.getPath());
    JsonField firstName = contact.getJsonFields().getJsonField().get(0);
    assertNotNull(firstName);
    assertEquals("firstName", firstName.getName());
    assertEquals("/SourceOrderList/orders/contact/firstName", firstName.getPath());
    assertEquals(FieldType.STRING, firstName.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, firstName.getStatus());
    JsonField lastName = contact.getJsonFields().getJsonField().get(1);
    assertNotNull(lastName);
    assertEquals("lastName", lastName.getName());
    assertEquals("/SourceOrderList/orders/contact/lastName", lastName.getPath());
    assertEquals(FieldType.STRING, lastName.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, lastName.getStatus());
    JsonField phoneNumber = contact.getJsonFields().getJsonField().get(2);
    assertNotNull(phoneNumber);
    assertEquals("phoneNumber", phoneNumber.getName());
    assertEquals("/SourceOrderList/orders/contact/phoneNumber", phoneNumber.getPath());
    assertEquals(FieldType.STRING, phoneNumber.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, phoneNumber.getStatus());
    JsonField zipCode = contact.getJsonFields().getJsonField().get(3);
    assertNotNull(zipCode);
    assertEquals("zipCode", zipCode.getName());
    assertEquals("/SourceOrderList/orders/contact/zipCode", zipCode.getPath());
    assertEquals(FieldType.STRING, zipCode.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, zipCode.getStatus());
    JsonField orderId = orders.getJsonFields().getJsonField().get(2);
    assertNotNull(orderId);
    assertEquals("orderId", orderId.getName());
    assertEquals("/SourceOrderList/orders/orderId", orderId.getPath());
    assertEquals(FieldType.INTEGER, orderId.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, orderId.getStatus());
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) JsonComplexType(io.atlasmap.json.v2.JsonComplexType) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.Test)

Example 7 with JsonComplexType

use of io.atlasmap.json.v2.JsonComplexType in project atlasmap by atlasmap.

the class JsonComplexTypeFactory method createJsonComlexField.

public static JsonComplexType createJsonComlexField() {
    JsonComplexType jsonComplexField = new JsonComplexType();
    jsonComplexField.setFieldType(FieldType.COMPLEX);
    return jsonComplexField;
}
Also used : JsonComplexType(io.atlasmap.json.v2.JsonComplexType)

Example 8 with JsonComplexType

use of io.atlasmap.json.v2.JsonComplexType in project atlasmap by atlasmap.

the class InstanceInspector method handleArrayNode.

private void handleArrayNode(JsonDocument jsonDocument, ArrayNode aNode, JsonComplexType parent, String aKey, int index) throws IOException {
    if (aNode.get(0).isObject()) {
        LOG.trace("ARRAY OF OBJECTS WITH PARENT ---> " + parent.getName().concat(String.valueOf(index)) + " WITH KEY ----> " + aKey + " AND SIZE OF ---> " + aNode.size());
        int childIndex = 0;
        JsonComplexType childObject = null;
        if (!aKey.equals(parent.getName())) {
            childObject = getJsonComplexType(parent, aKey, index);
            childObject.setCollectionType(CollectionType.LIST);
        }
        for (JsonNode jsonNode : aNode) {
            if (childObject != null) {
                // rest for child fields...
                handleObjectNode(jsonDocument, jsonNode, childObject, childIndex);
            } else {
                handleObjectNode(jsonDocument, jsonNode, parent, index);
            }
            childIndex++;
            index++;
        }
    } else if (aNode.get(0).isArray()) {
        LOG.trace("**TODO** > HANDLE ARRAY OF AN ARRAY WITH PARENT ---> " + parent.getName() + " WITH KEY ----> " + aKey);
    } else if (aNode.get(0).isValueNode()) {
        LOG.trace("**TODO** > HANDLE ARRAY OF A VALUES WITH PARENT ---> " + parent.getName() + " WITH KEY ----> " + aKey);
    }
}
Also used : JsonComplexType(io.atlasmap.json.v2.JsonComplexType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 9 with JsonComplexType

use of io.atlasmap.json.v2.JsonComplexType in project atlasmap by atlasmap.

the class InstanceInspector method handleValueEntry.

private void handleValueEntry(JsonDocument jsonDocument, Map.Entry<String, JsonNode> jsonNodeEntry, JsonComplexType parent, int index) {
    JsonNode theNode = jsonNodeEntry.getValue();
    String nodeKey = jsonNodeEntry.getKey();
    JsonField field = AtlasJsonModelFactory.createJsonField();
    if (nodeKey != null) {
        field.setName(nodeKey);
        if (parent != null) {
            LOG.trace("HANDLING AN VALUE NODE WITH PARENT ---> " + parent.getName() + " WITH INDEX OF " + index);
            if (index > 0 && (parent.getCollectionType() != null && parent.getCollectionType().compareTo(CollectionType.ARRAY) == 0)) {
                field.setPath(parent.getPath().concat("/").concat(nodeKey).concat("[").concat(String.valueOf(index)).concat("]"));
            } else if (index > 0 && (parent.getCollectionType() != null && parent.getCollectionType().compareTo(CollectionType.LIST) == 0)) {
                field.setPath(parent.getPath().concat("[").concat(String.valueOf(index)).concat("]/").concat(nodeKey));
            } else {
                field.setPath(parent.getPath().concat("/").concat(nodeKey));
            }
        } else {
            LOG.trace("HANDLING AN VALUE NODE WITH NO PARENT WITH INDEX OF " + index);
            field.setPath("/".concat(nodeKey));
        }
    }
    setNodeValueOnField(theNode, field);
    if (parent == null) {
        jsonDocument.getFields().getField().add(field);
    } else {
        parent.getJsonFields().getJsonField().add(field);
    }
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 10 with JsonComplexType

use of io.atlasmap.json.v2.JsonComplexType in project atlasmap by atlasmap.

the class InstanceInspector method handleObjectNode.

private void handleObjectNode(JsonDocument jsonDocument, JsonNode jsonNode, JsonComplexType parent, int index) throws IOException {
    LOG.trace("HANDLING AN OBJECT NODE " + jsonNode.fields().next().getKey() + " WITH PARENT ---> " + parent.getName() + " WITH INDEX OF " + index);
    Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
    while (fields.hasNext()) {
        Map.Entry<String, JsonNode> next = fields.next();
        String key = next.getKey();
        JsonNode node = next.getValue();
        if (node.isValueNode()) {
            handleValueEntry(jsonDocument, next, parent, index);
        } else if (node.isObject()) {
            LOG.trace("FOUND AN OBJECT NODE THAT IS A CONTAINER WITH KEY --> " + key + " WITH A PARENT INDEX OF " + index);
            JsonComplexType container = getJsonComplexType(parent, key, index);
            // rest index to zero when dealing with containers (we don't need an index on
            // containers)
            handleObjectNode(jsonDocument, next.getValue(), container, 0);
        } else if (node.isArray() && node.get(0).isObject()) {
            ArrayNode arrayNode = (ArrayNode) node;
            // index for children
            int innerIndex = 0;
            JsonComplexType deeperChild = getJsonComplexType(parent, key, index);
            if (parent.getCollectionType() == null) {
                deeperChild.setCollectionType(CollectionType.LIST);
            } else {
                deeperChild.setCollectionType(CollectionType.ARRAY);
            }
            for (JsonNode deeperJsonNode : arrayNode) {
                handleObjectNode(jsonDocument, deeperJsonNode, deeperChild, innerIndex);
                innerIndex++;
            }
        }
    }
}
Also used : JsonComplexType(io.atlasmap.json.v2.JsonComplexType) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Map(java.util.Map)

Aggregations

JsonComplexType (io.atlasmap.json.v2.JsonComplexType)21 JsonField (io.atlasmap.json.v2.JsonField)18 JsonDocument (io.atlasmap.json.v2.JsonDocument)17 Test (org.junit.Test)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 JsonFields (io.atlasmap.json.v2.JsonFields)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Field (io.atlasmap.v2.Field)2 Map (java.util.Map)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Ignore (org.junit.Ignore)1