Search in sources :

Example 66 with JsonDocument

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

the class JsonSchemaInspectorTest method inspectComplexObjectNoRoot.

@Test
public void inspectComplexObjectNoRoot() throws Exception {
    final String schema = new String(Files.readAllBytes(Paths.get("src/test/resources/inspect/schema/complex-object-unrooted.json")));
    JsonDocument document = inspectionService.inspectJsonSchema(schema);
    assertNotNull(document);
    assertEquals(3, document.getFields().getField().size());
    JsonComplexType address = (JsonComplexType) document.getFields().getField().get(0);
    assertNotNull(address);
    assertEquals(5, address.getJsonFields().getJsonField().size());
    JsonField address1 = address.getJsonFields().getJsonField().get(0);
    assertNotNull(address1);
    assertEquals("addressLine1", address1.getName());
    assertEquals("/address/addressLine1", address1.getPath());
    assertEquals(FieldType.STRING, address1.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, address1.getStatus());
    JsonField address2 = address.getJsonFields().getJsonField().get(1);
    assertNotNull(address2);
    assertEquals("addressLine2", address2.getName());
    assertEquals("/address/addressLine2", address2.getPath());
    assertEquals(FieldType.STRING, address2.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, address2.getStatus());
    JsonField city = address.getJsonFields().getJsonField().get(2);
    assertNotNull(city);
    assertEquals("city", city.getName());
    assertEquals("/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("/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("/address/zipCode", postalCode.getPath());
    assertEquals(FieldType.STRING, postalCode.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, postalCode.getStatus());
    JsonComplexType contact = (JsonComplexType) document.getFields().getField().get(1);
    assertNotNull(contact);
    assertEquals(4, contact.getJsonFields().getJsonField().size());
    JsonField firstName = contact.getJsonFields().getJsonField().get(0);
    assertNotNull(firstName);
    assertEquals("firstName", firstName.getName());
    assertEquals("/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("/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("/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("/contact/zipCode", zipCode.getPath());
    assertEquals(FieldType.STRING, zipCode.getFieldType());
    assertEquals(FieldStatus.SUPPORTED, zipCode.getStatus());
    JsonField orderId = (JsonField) document.getFields().getField().get(2);
    assertNotNull(orderId);
    assertEquals("orderId", orderId.getName());
    assertEquals("/orderId", orderId.getPath());
    assertEquals(FieldType.BIG_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.jupiter.api.Test)

Example 67 with JsonDocument

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

the class JsonSchemaInspectorTest method inspectJsonSchemaTopmostArraySimple.

@Test
public void inspectJsonSchemaTopmostArraySimple() throws Exception {
    final String schema = "{\"$schema\": \"http://json-schema.org/\"," + " \"type\": \"array\"," + " \"items\": { \"type\": \"integer\"}}";
    JsonDocument document = inspectionService.inspectJsonSchema(schema);
    assertNotNull(document);
    assertEquals(1, document.getFields().getField().size());
    JsonField jsonField = (JsonField) document.getFields().getField().get(0);
    assertEquals(FieldStatus.SUPPORTED, jsonField.getStatus());
    assertEquals(CollectionType.LIST, jsonField.getCollectionType());
    assertEquals(FieldType.BIG_INTEGER, jsonField.getFieldType());
    assertEquals("", jsonField.getName());
    assertEquals("/<>", jsonField.getPath());
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) JsonDocument(io.atlasmap.json.v2.JsonDocument) Test(org.junit.jupiter.api.Test)

Example 68 with JsonDocument

use of io.atlasmap.json.v2.JsonDocument 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 69 with JsonDocument

use of io.atlasmap.json.v2.JsonDocument 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 70 with JsonDocument

use of io.atlasmap.json.v2.JsonDocument 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

JsonDocument (io.atlasmap.json.v2.JsonDocument)73 JsonField (io.atlasmap.json.v2.JsonField)65 JsonComplexType (io.atlasmap.json.v2.JsonComplexType)38 Test (org.junit.jupiter.api.Test)32 Test (org.junit.Test)29 Field (io.atlasmap.v2.Field)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 JsonEnumField (io.atlasmap.json.v2.JsonEnumField)6 IOException (java.io.IOException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 JsonInspectionService (io.atlasmap.json.inspect.JsonInspectionService)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)2 JsonFields (io.atlasmap.json.v2.JsonFields)2 JsonInspectionRequest (io.atlasmap.json.v2.JsonInspectionRequest)2 JsonInspectionResponse (io.atlasmap.json.v2.JsonInspectionResponse)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2