Search in sources :

Example 6 with XmlField

use of io.atlasmap.xml.v2.XmlField in project atlasmap by atlasmap.

the class XmlInstanceInspectionTest method testInspectXmlStringAsSourceMultipleChildren.

@Test
public void testInspectXmlStringAsSourceMultipleChildren() throws Exception {
    final String source = "<data>\n" + "     <intFields><int>3200</int><int>2500</int><int>15</int></intFields>\n" + "     <longFields><long>12421</long></longFields>\n" + "     <stringFields><string>abc</string></stringFields>\n" + "     <booleanFields><boolean>true</boolean></booleanFields>\n" + "     <doubleFields><double>12.0</double></doubleFields>\n" + "     <shortFields><short>1000</short></shortFields>\n" + "     <floatFields><float>234.5f</float></floatFields>\n" + "     <charFields><char>A</char></charFields>\n" + "</data>";
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument xmlDocument = service.inspectXmlDocument(source);
    Assert.assertNotNull(xmlDocument);
    Assert.assertNotNull(xmlDocument.getFields());
    Assert.assertThat(xmlDocument.getFields().getField().size(), Is.is(1));
    XmlComplexType root = (XmlComplexType) xmlDocument.getFields().getField().get(0);
    Assert.assertNotNull(root);
    Assert.assertThat(root.getXmlFields().getXmlField().size(), Is.is(8));
    // children
    XmlComplexType childZero = (XmlComplexType) root.getXmlFields().getXmlField().get(0);
    Assert.assertNotNull(childZero);
    Assert.assertThat(childZero.getXmlFields().getXmlField().size(), Is.is(3));
    XmlField childZeroZero = childZero.getXmlFields().getXmlField().get(0);
    Assert.assertNotNull(childZeroZero);
    Assert.assertThat(childZeroZero.getName(), Is.is("int"));
    Assert.assertThat(childZeroZero.getValue(), Is.is("3200"));
    Assert.assertThat(childZeroZero.getPath(), Is.is("/data/intFields/int"));
    XmlField childZeroOne = childZero.getXmlFields().getXmlField().get(1);
    Assert.assertNotNull(childZeroOne);
    Assert.assertThat(childZeroOne.getName(), Is.is("int"));
    Assert.assertThat(childZeroOne.getValue(), Is.is("2500"));
    Assert.assertThat(childZeroOne.getPath(), Is.is("/data/intFields/int[1]"));
// debugFields(xmlDocument.getFields());
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlField(io.atlasmap.xml.v2.XmlField) XmlDocument(io.atlasmap.xml.v2.XmlDocument) Test(org.junit.Test)

Example 7 with XmlField

use of io.atlasmap.xml.v2.XmlField in project atlasmap by atlasmap.

the class XmlSchemaInspectionMultipleNamespacesTest method testMultipleNoNamespaceSchemas.

@Test
public void testMultipleNoNamespaceSchemas() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/multiple-no-namespace-schemas.xml").toFile();
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument answer = service.inspectSchema(schemaFile);
    Assert.assertEquals(1, answer.getXmlNamespaces().getXmlNamespace().size());
    XmlNamespace namespace = answer.getXmlNamespaces().getXmlNamespace().get(0);
    Assert.assertEquals("tns", namespace.getAlias());
    Assert.assertEquals("io.atlasmap.xml.test:Root", namespace.getUri());
    Assert.assertEquals(null, namespace.isTargetNamespace());
    Assert.assertEquals(1, answer.getFields().getField().size());
    XmlComplexType complex = XmlComplexType.class.cast(answer.getFields().getField().get(0));
    Assert.assertEquals("tns:RootDocument", complex.getName());
    Assert.assertEquals(2, complex.getXmlFields().getXmlField().size());
    for (XmlField field : complex.getXmlFields().getXmlField()) {
        switch(field.getName()) {
            case "FirstElement":
                Assert.assertEquals(FieldType.COMPLEX, field.getFieldType());
                Assert.assertEquals("/tns:RootDocument/FirstElement", field.getPath());
                List<XmlField> firstFields = XmlComplexType.class.cast(field).getXmlFields().getXmlField();
                Assert.assertEquals(1, firstFields.size());
                XmlField firstField = firstFields.get(0);
                Assert.assertEquals("FirstValue", firstField.getName());
                Assert.assertEquals(FieldType.STRING, firstField.getFieldType());
                Assert.assertEquals("/tns:RootDocument/FirstElement/FirstValue", firstField.getPath());
                break;
            case "SecondElement":
                Assert.assertEquals(FieldType.COMPLEX, field.getFieldType());
                Assert.assertEquals("/tns:RootDocument/SecondElement", field.getPath());
                List<XmlField> secondFields = XmlComplexType.class.cast(field).getXmlFields().getXmlField();
                Assert.assertEquals(1, secondFields.size());
                XmlField secondField = secondFields.get(0);
                Assert.assertEquals("SecondValue", secondField.getName());
                Assert.assertEquals(FieldType.STRING, secondField.getFieldType());
                Assert.assertEquals("/tns:RootDocument/SecondElement/SecondValue", secondField.getPath());
                break;
            default:
                Assert.fail(String.format("Unknown field '%s'", field.getPath()));
        }
    }
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlField(io.atlasmap.xml.v2.XmlField) XmlDocument(io.atlasmap.xml.v2.XmlDocument) File(java.io.File) Test(org.junit.Test)

Example 8 with XmlField

use of io.atlasmap.xml.v2.XmlField in project atlasmap by atlasmap.

the class XmlFieldReaderTest method testReadDocumentWithSingleNamespaceSetElementValueAsString.

@Test
public void testReadDocumentWithSingleNamespaceSetElementValueAsString() throws Exception {
    String doc = getDocumentString("src/test/resources/simple_example_single_ns.xml");
    reader.setDocument(doc, true);
    XmlField xmlField = AtlasXmlModelFactory.createXmlField();
    xmlField.setPath("/x:orders/order/id");
    assertNull(xmlField.getValue());
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(xmlField);
    reader.read(session);
    assertNotNull(xmlField.getValue());
    assertThat(xmlField.getValue(), is("12312"));
}
Also used : Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) XmlField(io.atlasmap.xml.v2.XmlField) Test(org.junit.Test)

Example 9 with XmlField

use of io.atlasmap.xml.v2.XmlField in project atlasmap by atlasmap.

the class XmlFieldReaderTest method testThrowExceptionOnNullAmlFeilds.

public void testThrowExceptionOnNullAmlFeilds() throws Exception {
    String doc = getDocumentString("src/test/resources/complex_example.xml");
    reader.setDocument(doc, false);
    XmlField fieldList = null;
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(fieldList);
    reader.read(session);
}
Also used : Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) XmlField(io.atlasmap.xml.v2.XmlField)

Example 10 with XmlField

use of io.atlasmap.xml.v2.XmlField in project atlasmap by atlasmap.

the class XmlFieldReaderTest method testReadDocumentElementWithMultipleNamespaceComplexConstructorArg.

@Test
public void testReadDocumentElementWithMultipleNamespaceComplexConstructorArg() throws Exception {
    String doc = getDocumentString("src/test/resources/complex_example_multiple_ns.xml");
    reader.setDocument(doc, true);
    XmlField xmlField = AtlasXmlModelFactory.createXmlField();
    xmlField.setPath("/orders/q:order/id/@y:custId");
    assertNull(xmlField.getValue());
    Map<String, String> namespaces = new LinkedHashMap<>();
    namespaces.put("http://www.example.com/q/", "q");
    namespaces.put("http://www.example.com/y/", "y");
    namespaces.put("http://www.example.com/x/", "");
    XmlFieldReader multipleNamespacesReader = new XmlFieldReader(DefaultAtlasConversionService.getInstance(), namespaces);
    multipleNamespacesReader.setDocument(doc, true);
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(xmlField);
    multipleNamespacesReader.read(session);
    assertNotNull(xmlField.getValue());
    assertThat(xmlField.getValue(), is("cx"));
    XmlField xmlField2 = AtlasXmlModelFactory.createXmlField();
    xmlField2.setPath("/orders/order/id/@y:custId");
    assertNull(xmlField2.getValue());
    when(session.head().getSourceField()).thenReturn(xmlField2);
    multipleNamespacesReader.read(session);
    assertNotNull(xmlField2.getValue());
    assertThat(xmlField2.getValue(), is("aa"));
}
Also used : Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) XmlField(io.atlasmap.xml.v2.XmlField) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

XmlField (io.atlasmap.xml.v2.XmlField)46 Test (org.junit.Test)31 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)21 Head (io.atlasmap.spi.AtlasInternalSession.Head)21 XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)11 XmlDocument (io.atlasmap.xml.v2.XmlDocument)8 AtlasMapping (io.atlasmap.v2.AtlasMapping)7 Mapping (io.atlasmap.v2.Mapping)7 Field (io.atlasmap.v2.Field)6 XmlNamespace (io.atlasmap.xml.v2.XmlNamespace)6 File (java.io.File)6 Validation (io.atlasmap.v2.Validation)4 FieldType (io.atlasmap.v2.FieldType)3 Restriction (io.atlasmap.xml.v2.Restriction)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 AtlasMappingUtil (io.atlasmap.core.AtlasMappingUtil)2 DefaultAtlasConversionService (io.atlasmap.core.DefaultAtlasConversionService)2 AtlasModuleDetail (io.atlasmap.spi.AtlasModuleDetail)2 AtlasModuleMode (io.atlasmap.spi.AtlasModuleMode)2