Search in sources :

Example 31 with JsonField

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

the class SchemaInspector 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();
        JsonNode rootNode = objectMapper.readTree(schema);
        Map<String, JsonNode> definitionMap = new HashMap<>();
        populateDefinitions(rootNode, definitionMap);
        JsonField rootNodeType = getJsonFieldBuilder(null, rootNode, null, definitionMap).build();
        if (rootNodeType.getCollectionType() == CollectionType.LIST) {
            LOG.warn("Topmost array is not supported");
            if (rootNodeType instanceof JsonComplexType) {
                ((JsonComplexType) rootNodeType).getJsonFields().getJsonField().clear();
            }
            rootNodeType.setStatus(FieldStatus.UNSUPPORTED);
            jsonDocument.getFields().getField().add(rootNodeType);
        } else if (rootNodeType instanceof JsonComplexType && ((JsonComplexType) rootNodeType).getJsonFields().getJsonField().size() != 0) {
            jsonDocument.getFields().getField().addAll(((JsonComplexType) rootNodeType).getJsonFields().getJsonField());
        } 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)

Example 32 with JsonField

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

the class SchemaInspector method loadProperties.

private List<JsonField> loadProperties(JsonNode node, String parentPath, Map<String, JsonNode> definitionMap) throws JsonInspectionException {
    List<JsonField> answer = new ArrayList<>();
    JsonNode properties = node.get("properties");
    if (properties == null || !properties.fields().hasNext()) {
        LOG.warn("An object node without 'properties', it will be ignored: {}", node);
        return answer;
    }
    Iterator<Entry<String, JsonNode>> topFields = properties.fields();
    while (topFields.hasNext()) {
        Entry<String, JsonNode> entry = topFields.next();
        if (!entry.getValue().isObject()) {
            LOG.warn("Ignoring non-object field '{}'", entry);
            continue;
        }
        JsonField type = getJsonFieldBuilder(entry.getKey(), entry.getValue(), parentPath, definitionMap).build();
        answer.add(type);
    }
    return answer;
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 33 with JsonField

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

the class JsonFieldReaderTest method testComplexJsonDocumentNestedObjectArray.

@Test
public void testComplexJsonDocumentNestedObjectArray() throws Exception {
    final String document = "{\"menu\": {\n" + "  \"id\": \"file\",\n" + "  \"value\": \"Filed\",\n" + "  \"popup\": {\n" + "    \"menuitem\": [\n" + "      {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\n" + "      {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n" + "      {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n" + "    ]\n" + "  }\n" + "}}";
    reader.setDocument(document);
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath("/menu/id");
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(field);
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("file"));
    field.setPath("/menu/value");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Filed"));
    field.setPath("/menu/popup/menuitem[0]/value");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("New"));
    field.setPath("/menu/popup/menuitem[0]/onclick");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("CreateNewDoc()"));
    field.setPath("/menu/popup/menuitem[1]/value");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Open"));
    field.setPath("/menu/popup/menuitem[1]/onclick");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("OpenDoc()"));
    field.setPath("/menu/popup/menuitem[2]/value");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Close"));
    field.setPath("/menu/popup/menuitem[2]/onclick");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("CloseDoc()"));
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) Test(org.junit.Test)

Example 34 with JsonField

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

the class JsonFieldReaderTest method testComplexJsonDocumentHighlyComplexNested.

@Test
public void testComplexJsonDocumentHighlyComplexNested() throws Exception {
    final String document = new String(Files.readAllBytes(Paths.get("src/test/resources/highly-complex-nested-object.json")));
    reader.setDocument(document);
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath("/items/item[0]/id");
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(field);
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("0001"));
    resetField(field);
    field.setPath("/items/item[0]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("donut"));
    resetField(field);
    field.setPath("/items/item[0]/name");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Cake"));
    resetField(field);
    field.setPath("/items/item[0]/ppu");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is(0.55));
    resetField(field);
    // array of objects
    field.setPath("/items/item[0]/batters/batter[0]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("1001"));
    resetField(field);
    field.setPath("/items/item[0]/batters/batter[0]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Regular"));
    resetField(field);
    field.setPath("/items/item[0]/batters/batter[1]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("1002"));
    resetField(field);
    field.setPath("/items/item[0]/batters/batter[1]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Chocolate"));
    resetField(field);
    field.setPath("/items/item[0]/batters/batter[2]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("1003"));
    resetField(field);
    field.setPath("/items/item[0]/batters/batter[2]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Blueberry"));
    resetField(field);
    field.setPath("/items/item[0]/batters/batter[3]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("1004"));
    resetField(field);
    field.setPath("/items/item[0]/batters/batter[3]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Devil's Food"));
    resetField(field);
    // simple array
    field.setPath("/items/item[0]/topping[0]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5001"));
    resetField(field);
    field.setPath("/items/item[0]/topping[0]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("None"));
    resetField(field);
    field.setPath("/items/item[0]/topping[1]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5002"));
    resetField(field);
    field.setPath("/items/item[0]/topping[1]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Glazed"));
    resetField(field);
    field.setPath("/items/item[0]/topping[2]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5005"));
    resetField(field);
    field.setPath("/items/item[0]/topping[2]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Sugar"));
    resetField(field);
    field.setPath("/items/item[0]/topping[3]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5007"));
    resetField(field);
    field.setPath("/items/item[0]/topping[3]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Powdered Sugar"));
    resetField(field);
    field.setPath("/items/item[0]/topping[4]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5006"));
    resetField(field);
    field.setPath("/items/item[0]/topping[4]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Chocolate with Sprinkles"));
    resetField(field);
    field.setPath("/items/item[0]/topping[5]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5003"));
    resetField(field);
    field.setPath("/items/item[0]/topping[5]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Chocolate"));
    resetField(field);
    field.setPath("/items/item[0]/topping[6]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5004"));
    resetField(field);
    field.setPath("/items/item[0]/topping[6]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Maple"));
    resetField(field);
    field.setPath("/items/item[1]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("0002"));
    resetField(field);
    field.setPath("/items/item[1]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("donut"));
    resetField(field);
    field.setPath("/items/item[1]/name");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Raised"));
    resetField(field);
    field.setPath("/items/item[1]/ppu");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is(0.55));
    resetField(field);
    // array of objects
    field.setPath("/items/item[1]/batters/batter[0]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("1001"));
    resetField(field);
    field.setPath("/items/item[1]/batters/batter[0]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Regular"));
    resetField(field);
    field.setPath("/items/item[1]/topping[0]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5001"));
    resetField(field);
    field.setPath("/items/item[1]/topping[0]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("None"));
    resetField(field);
    field.setPath("/items/item[1]/topping[1]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5002"));
    resetField(field);
    field.setPath("/items/item[1]/topping[1]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Glazed"));
    resetField(field);
    field.setPath("/items/item[1]/topping[2]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5005"));
    resetField(field);
    field.setPath("/items/item[1]/topping[2]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Sugar"));
    resetField(field);
    field.setPath("/items/item[1]/topping[3]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5003"));
    resetField(field);
    field.setPath("/items/item[1]/topping[3]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Chocolate"));
    resetField(field);
    field.setPath("/items/item[1]/topping[4]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5004"));
    resetField(field);
    field.setPath("/items/item[1]/topping[4]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Maple"));
    resetField(field);
    field.setPath("/items/item[1]/topping[5]/id");
    reader.read(session);
    assertNull(field.getValue());
    resetField(field);
    field.setPath("/items/item[2]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("0003"));
    resetField(field);
    field.setPath("/items/item[2]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("donut"));
    resetField(field);
    field.setPath("/items/item[2]/name");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Old Fashioned"));
    resetField(field);
    field.setPath("/items/item[2]/ppu");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is(0.55));
    resetField(field);
    field.setPath("/items/item[3]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("0004"));
    resetField(field);
    field.setPath("/items/item[3]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("bar"));
    resetField(field);
    field.setPath("/items/item[3]/name");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Bar"));
    resetField(field);
    field.setPath("/items/item[3]/ppu");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is(0.75));
    resetField(field);
    field.setPath("/items/item[4]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("0005"));
    resetField(field);
    field.setPath("/items/item[4]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("twist"));
    resetField(field);
    field.setPath("/items/item[4]/name");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Twist"));
    resetField(field);
    field.setPath("/items/item[4]/ppu");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is(0.65));
    resetField(field);
    field.setPath("/items/item[5]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("0006"));
    resetField(field);
    field.setPath("/items/item[5]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("filled"));
    resetField(field);
    field.setPath("/items/item[5]/name");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Filled"));
    resetField(field);
    field.setPath("/items/item[5]/ppu");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is(0.75));
    resetField(field);
    field.setPath("/items/item[5]/fillings/filling[2]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("7004"));
    resetField(field);
    field.setPath("/items/item[5]/fillings/filling[3]/addcost");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is(0));
    resetField(field);
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) Test(org.junit.Test)

Example 35 with JsonField

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

the class JsonFieldReaderTest method testSameFieldNameInDifferentPath.

@Test
public void testSameFieldNameInDifferentPath() throws Exception {
    final String document = new String(Files.readAllBytes(Paths.get("src/test/resources/same-field-name-in-different-path.json")));
    reader.setDocument(document);
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath("/name");
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(field);
    reader.read(session);
    assertEquals("name", field.getValue());
    field.setPath("/object1/name");
    reader.read(session);
    assertEquals("object1-name", field.getValue());
    field.setPath("/object2/name");
    reader.read(session);
    assertEquals("object2-name", field.getValue());
    field.setPath("/object1/object2/name");
    reader.read(session);
    assertEquals("object1-object2-name", field.getValue());
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) Test(org.junit.Test)

Aggregations

JsonField (io.atlasmap.json.v2.JsonField)63 Test (org.junit.Test)45 JsonDocument (io.atlasmap.json.v2.JsonDocument)30 JsonComplexType (io.atlasmap.json.v2.JsonComplexType)15 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)10 Head (io.atlasmap.spi.AtlasInternalSession.Head)8 AtlasMapping (io.atlasmap.v2.AtlasMapping)7 Mapping (io.atlasmap.v2.Mapping)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Field (io.atlasmap.v2.Field)4 Validation (io.atlasmap.v2.Validation)4 AtlasMappingUtil (io.atlasmap.core.AtlasMappingUtil)2 DefaultAtlasConversionService (io.atlasmap.core.DefaultAtlasConversionService)2 AtlasJsonModelFactory (io.atlasmap.json.v2.AtlasJsonModelFactory)2 AtlasModuleDetail (io.atlasmap.spi.AtlasModuleDetail)2 AtlasModuleMode (io.atlasmap.spi.AtlasModuleMode)2 AtlasModelFactory (io.atlasmap.v2.AtlasModelFactory)2 DataSource (io.atlasmap.v2.DataSource)2 DataSourceType (io.atlasmap.v2.DataSourceType)2 FieldType (io.atlasmap.v2.FieldType)2