Search in sources :

Example 96 with JsonField

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

the class JsonSchemaInspectorTest method doInspectJsonSchemaCalendar.

private void doInspectJsonSchemaCalendar(String instance) throws Exception {
    JsonDocument document = inspectionService.inspectJsonSchema(instance);
    List<Field> fields = document.getFields().getField();
    JsonField f = (JsonField) fields.get(0);
    assertEquals("dtstart", f.getName());
    assertEquals("/dtstart", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(1);
    assertEquals("dtend", f.getName());
    assertEquals("/dtend", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(2);
    assertEquals("summary", f.getName());
    assertEquals("/summary", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(3);
    assertEquals("location", f.getName());
    assertEquals("/location", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(4);
    assertEquals("url", f.getName());
    assertEquals("/url", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(5);
    assertEquals("duration", f.getName());
    assertEquals("/duration", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(6);
    assertEquals("rdate", f.getName());
    assertEquals("/rdate", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(7);
    assertEquals("rrule", f.getName());
    assertEquals("/rrule", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(8);
    assertEquals("category", f.getName());
    assertEquals("/category", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(9);
    assertEquals("description", f.getName());
    assertEquals("/description", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonComplexType) fields.get(10);
    assertEquals("geo", f.getName());
    assertEquals("/geo", f.getPath());
    assertEquals(FieldType.COMPLEX, f.getFieldType());
    List<JsonField> geofields = ((JsonComplexType) f).getJsonFields().getJsonField();
    f = geofields.get(0);
    assertEquals("latitude", f.getName());
    assertEquals("/geo/latitude", f.getPath());
    assertEquals(FieldType.NUMBER, f.getFieldType());
    f = geofields.get(1);
    assertEquals("longitude", f.getName());
    assertEquals("/geo/longitude", f.getPath());
    assertEquals(FieldType.NUMBER, f.getFieldType());
}
Also used : JsonEnumField(io.atlasmap.json.v2.JsonEnumField) Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonField(io.atlasmap.json.v2.JsonField) JsonDocument(io.atlasmap.json.v2.JsonDocument)

Example 97 with JsonField

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

the class JsonSchemaInspectorTest method inspectJsonSchemaAddress.

@Test
public void inspectJsonSchemaAddress() throws Exception {
    final String schema = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/address.json")));
    JsonDocument document = inspectionService.inspectJsonSchema(schema);
    List<Field> fields = document.getFields().getField();
    JsonField f = (JsonField) fields.get(0);
    assertEquals("post-office-box", f.getName());
    assertEquals("/post-office-box", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(1);
    assertEquals("extended-address", f.getName());
    assertEquals("/extended-address", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(2);
    assertEquals("street-address", f.getName());
    assertEquals("/street-address", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(3);
    assertEquals("locality", f.getName());
    assertEquals("/locality", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(4);
    assertEquals("region", f.getName());
    assertEquals("/region", f.getPath());
    assertEquals(FieldType.COMPLEX, f.getFieldType());
    JsonComplexType c = (JsonComplexType) f;
    assertEquals(true, c.isEnumeration());
    List<String> regions = new ArrayList<>(Arrays.asList("NA", "EMEA", "LATAM", "APAC"));
    for (JsonEnumField e : c.getJsonEnumFields().getJsonEnumField()) {
        if (!regions.remove(e.getName())) {
            fail("Unknown enum value: " + e.getName());
        }
        ;
    }
    if (!regions.isEmpty()) {
        fail("Not found: " + regions);
    }
    f = (JsonField) fields.get(5);
    assertEquals("postal-code", f.getName());
    assertEquals("/postal-code", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
    f = (JsonField) fields.get(6);
    assertEquals("country-name", f.getName());
    assertEquals("/country-name", f.getPath());
    assertEquals(FieldType.STRING, f.getFieldType());
}
Also used : JsonEnumField(io.atlasmap.json.v2.JsonEnumField) Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) JsonField(io.atlasmap.json.v2.JsonField) JsonComplexType(io.atlasmap.json.v2.JsonComplexType) ArrayList(java.util.ArrayList) JsonEnumField(io.atlasmap.json.v2.JsonEnumField) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.jupiter.api.Test)

Example 98 with JsonField

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

the class JsonFieldReader method populateChildFields.

private void populateChildFields(AtlasInternalSession session, JsonNode node, FieldGroup fieldGroup, AtlasPath path) throws AtlasException {
    List<Field> newChildren = new ArrayList<>();
    for (Field child : fieldGroup.getField()) {
        AtlasPath childPath = new AtlasPath(child.getPath());
        JsonNode childNode = node.get(childPath.getLastSegment().getName());
        if (childPath.getLastSegment().getCollectionType() != CollectionType.NONE) {
            FieldGroup childGroup = populateCollectionItems(session, (ArrayNode) childNode, child);
            newChildren.add(childGroup);
        } else {
            if (child instanceof FieldGroup) {
                populateChildFields(session, childNode, (FieldGroup) child, childPath);
            } else {
                Object value = handleValueNode(session, childNode, (JsonField) child);
                child.setValue(value);
            }
            newChildren.add(child);
        }
    }
    fieldGroup.getField().clear();
    fieldGroup.getField().addAll(newChildren);
}
Also used : JsonEnumField(io.atlasmap.json.v2.JsonEnumField) Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) FieldGroup(io.atlasmap.v2.FieldGroup) ArrayList(java.util.ArrayList) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 99 with JsonField

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

the class JsonFieldReader method populateCollectionItems.

private FieldGroup populateCollectionItems(AtlasInternalSession session, JsonNode node, Field field) throws AtlasException {
    if (!node.isArray()) {
        throw new AtlasException(String.format("Couldn't find JSON array for field %s:%s", field.getDocId(), field.getPath()));
    }
    FieldGroup group = field instanceof FieldGroup ? (FieldGroup) field : AtlasModelFactory.createFieldGroupFrom(field, true);
    ArrayNode arrayNode = (ArrayNode) node;
    for (int i = 0; i < arrayNode.size(); i++) {
        AtlasPath itemPath = new AtlasPath(group.getPath());
        List<SegmentContext> segments = itemPath.getSegments(true);
        itemPath.setCollectionIndex(segments.size() - 1, i);
        if (field instanceof FieldGroup) {
            FieldGroup itemGroup = AtlasJsonModelFactory.cloneFieldGroup((FieldGroup) field);
            AtlasPath.setCollectionIndexRecursively(itemGroup, segments.size(), i);
            populateChildFields(session, arrayNode.get(i), itemGroup, itemPath);
            group.getField().add(itemGroup);
        } else {
            JsonField itemField = AtlasJsonModelFactory.cloneField((JsonField) field, false);
            itemField.setPath(itemPath.toString());
            Object value = handleValueNode(session, arrayNode.get(i), itemField);
            itemField.setValue(value);
            group.getField().add(itemField);
        }
    }
    return group;
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) JsonField(io.atlasmap.json.v2.JsonField) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AtlasException(io.atlasmap.api.AtlasException)

Example 100 with JsonField

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

the class JsonSchemaInspector method inspect.

public JsonDocument inspect(String schema) throws JsonInspectionException {
    if (schema == null || schema.isEmpty()) {
        throw new IllegalArgumentException("JSON schema cannot be null");
    }
    try {
        JsonDocument jsonDocument = AtlasJsonModelFactory.createJsonDocument();
        ObjectMapper objectMapper = new ObjectMapper().enable(MapperFeature.BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES);
        JsonNode rootNode = objectMapper.readTree(schema);
        Map<String, JsonNode> definitionMap = new HashMap<>();
        populateDefinitions(rootNode, definitionMap);
        JsonField rootNodeType = getJsonFieldBuilder("", rootNode, null, definitionMap, new HashSet<>(), false).build();
        if (rootNodeType instanceof JsonComplexType && ((JsonComplexType) rootNodeType).getJsonFields().getJsonField().size() != 0) {
            if (rootNodeType.getCollectionType() == null || rootNodeType.getCollectionType() == CollectionType.NONE) {
                jsonDocument.getFields().getField().addAll(((JsonComplexType) rootNodeType).getJsonFields().getJsonField());
            } else {
                // taking care of topmost collection
                jsonDocument.getFields().getField().add(rootNodeType);
            }
        } else if (rootNodeType.getFieldType() == FieldType.COMPLEX) {
            LOG.warn("No simple type nor property is defined for the root node. It's going to be empty");
        } else {
            jsonDocument.getFields().getField().add(rootNodeType);
        }
        return jsonDocument;
    } catch (Exception e) {
        throw new JsonInspectionException(e);
    }
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) HashMap(java.util.HashMap) JsonComplexType(io.atlasmap.json.v2.JsonComplexType) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonDocument(io.atlasmap.json.v2.JsonDocument) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet)

Aggregations

JsonField (io.atlasmap.json.v2.JsonField)119 JsonDocument (io.atlasmap.json.v2.JsonDocument)62 Test (org.junit.jupiter.api.Test)57 JsonComplexType (io.atlasmap.json.v2.JsonComplexType)34 Test (org.junit.Test)27 Field (io.atlasmap.v2.Field)18 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)15 Head (io.atlasmap.spi.AtlasInternalSession.Head)13 Mapping (io.atlasmap.v2.Mapping)13 AtlasMapping (io.atlasmap.v2.AtlasMapping)11 FieldGroup (io.atlasmap.v2.FieldGroup)9 JsonEnumField (io.atlasmap.json.v2.JsonEnumField)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 ArrayList (java.util.ArrayList)5 AtlasPath (io.atlasmap.core.AtlasPath)4 Validation (io.atlasmap.v2.Validation)4 BaseMapping (io.atlasmap.v2.BaseMapping)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 AtlasException (io.atlasmap.api.AtlasException)2 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)2