Search in sources :

Example 21 with JsonField

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

the class JsonFieldReaderTest method testSimpleJsonDocumentWithRoot.

@Test
public void testSimpleJsonDocumentWithRoot() throws Exception {
    final String document = " {\"car\" :{ \"brand\" : \"Mercedes\", \"doors\" : 5 } }";
    reader.setDocument(document);
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath("/car/doors");
    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(5));
    resetField(field);
    field.setPath("/car/brand");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Mercedes"));
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) Test(org.junit.Test)

Example 22 with JsonField

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

the class JsonFieldReaderTest method testComplexJsonDocumentHighlyNested.

@Test
public void testComplexJsonDocumentHighlyNested() throws Exception {
    final String document = new String(Files.readAllBytes(Paths.get("src/test/resources/highly-nested-object.json")));
    reader.setDocument(document);
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath("/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("/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("donut"));
    resetField(field);
    field.setPath("/name");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Cake"));
    resetField(field);
    field.setPath("/ppu");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is(0.55));
    resetField(field);
    field.setPath("/batters/batter[0]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("1001"));
    resetField(field);
    field.setPath("/batters/batter[0]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Regular"));
    resetField(field);
    field.setPath("/batters/batter[1]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("1002"));
    resetField(field);
    field.setPath("/batters/batter[1]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Chocolate"));
    resetField(field);
    field.setPath("/batters/batter[2]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("1003"));
    resetField(field);
    field.setPath("/batters/batter[2]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Blueberry"));
    resetField(field);
    field.setPath("/batters/batter[3]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("1004"));
    resetField(field);
    field.setPath("/batters/batter[3]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Devil's Food"));
    resetField(field);
    field.setPath("/topping[0]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5001"));
    resetField(field);
    field.setPath("/topping[0]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("None"));
    resetField(field);
    field.setPath("/topping[1]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5002"));
    resetField(field);
    field.setPath("/topping[1]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Glazed"));
    resetField(field);
    field.setPath("/topping[2]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5005"));
    resetField(field);
    field.setPath("/topping[2]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Sugar"));
    resetField(field);
    field.setPath("/topping[3]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5007"));
    resetField(field);
    field.setPath("/topping[3]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Powdered Sugar"));
    resetField(field);
    field.setPath("/topping[4]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5006"));
    resetField(field);
    field.setPath("/topping[4]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Chocolate with Sprinkles"));
    resetField(field);
    field.setPath("/topping[5]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5003"));
    resetField(field);
    field.setPath("/topping[5]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Chocolate"));
    resetField(field);
    field.setPath("/topping[6]/id");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("5004"));
    resetField(field);
    field.setPath("/topping[6]/type");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is("Maple"));
    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 23 with JsonField

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

the class JsonFieldReaderTest method testSimpleJsonDocument.

@Test
public void testSimpleJsonDocument() throws Exception {
    final String document = "   { \"brand\" : \"Mercedes\", \"doors\" : 5 }";
    reader.setDocument(document);
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath("/brand");
    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("Mercedes"));
    field.setFieldType(null);
    field.setPath("/doors");
    reader.read(session);
    assertNotNull(field.getValue());
    assertThat(field.getValue(), Is.is(5));
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) Test(org.junit.Test)

Example 24 with JsonField

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

the class JsonFieldReaderTest method testBoundaryValue.

private AtlasInternalSession testBoundaryValue(String fileName, String fieldPath, FieldType fieldType, Object expectedObject) throws Exception {
    String filePath = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "jsonFields" + File.separator + fileName;
    String document = new String(Files.readAllBytes(Paths.get(filePath)));
    reader.setDocument(document);
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath(fieldPath);
    field.setFieldType(fieldType);
    AtlasInternalSession session = read(field);
    assertEquals(expectedObject, field.getValue());
    return session;
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession)

Example 25 with JsonField

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

the class JsonFieldWriterTest method testWriteSimpleObjectNoRoot.

@Test
public void testWriteSimpleObjectNoRoot() throws Exception {
    JsonField field = AtlasJsonModelFactory.createJsonField();
    field.setPath("/brand");
    field.setValue("Mercedes");
    field.setFieldType(FieldType.STRING);
    write(field);
    Assert.assertNotNull(writer.getRootNode());
    Assert.assertThat(writer.getRootNode().toString(), Is.is("{\"brand\":\"Mercedes\"}"));
    JsonField field2 = AtlasJsonModelFactory.createJsonField();
    field2.setPath("/doors");
    field2.setValue(5);
    field2.setFieldType(FieldType.INTEGER);
    write(field2);
    Assert.assertThat(writer.getRootNode().toString(), Is.is("{\"brand\":\"Mercedes\",\"doors\":5}"));
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) 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