Search in sources :

Example 21 with XmlComplexType

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

the class XmlInstanceInspectionTest method testInspectXmlStringAsSourceAttrsWithNamespace.

@Test
public void testInspectXmlStringAsSourceAttrsWithNamespace() throws Exception {
    final String source = "<data xmlns:y=\"http://y.namespace.com/\" y:intField='32000' longField='12421' stringField='abc' " + "booleanField='true' doubleField='12.0' shortField='1000' floatField='234.5f' y:charField='A' />";
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument xmlDocument = service.inspectXmlDocument(source);
    Assert.assertNotNull(xmlDocument);
    Assert.assertNotNull(xmlDocument.getFields());
    // check for namespace
    Assert.assertNotNull(xmlDocument.getXmlNamespaces());
    Assert.assertThat(xmlDocument.getXmlNamespaces().getXmlNamespace().size(), Is.is(1));
    XmlNamespace namespace = xmlDocument.getXmlNamespaces().getXmlNamespace().get(0);
    Assert.assertNotNull(namespace);
    Assert.assertEquals("y", namespace.getAlias());
    Assert.assertEquals("http://y.namespace.com/", namespace.getUri());
    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));
// debugFields(xmlDocument.getFields());
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlDocument(io.atlasmap.xml.v2.XmlDocument) Test(org.junit.Test)

Example 22 with XmlComplexType

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

the class XmlInstanceInspectionTest method testInspectXmlStringWithDefaultNamespace.

@Test
public void testInspectXmlStringWithDefaultNamespace() throws Exception {
    final String source = "<data xmlns=\"http://x.namespace.com/\">\n" + "     <intField>32000</intField>\n" + "     <longField>12421</longField>\n" + "     <stringField>abc</stringField>\n" + "     <booleanField>true</booleanField>\n" + "     <doubleField>12.0</doubleField>\n" + "     <shortField>1000</shortField>\n" + "     <floatField>234.5f</floatField>\n" + "     <charField>A</charField>\n" + "</data>";
    XmlInspectionService service = new XmlInspectionService();
    XmlDocument xmlDocument = service.inspectXmlDocument(source);
    Assert.assertNotNull(xmlDocument);
    // check for namespace
    Assert.assertNotNull(xmlDocument.getXmlNamespaces());
    XmlNamespace namespace = xmlDocument.getXmlNamespaces().getXmlNamespace().get(0);
    Assert.assertThat(xmlDocument.getXmlNamespaces().getXmlNamespace().size(), Is.is(1));
    Assert.assertNotNull(namespace);
    Assert.assertNull(namespace.getAlias());
    Assert.assertEquals("http://x.namespace.com/", namespace.getUri());
    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));
// debugFields(xmlDocument.getFields());
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlNamespace(io.atlasmap.xml.v2.XmlNamespace) XmlDocument(io.atlasmap.xml.v2.XmlDocument) Test(org.junit.Test)

Example 23 with XmlComplexType

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

the class XmlSchemaInspectionMultipleNamespacesTest method testMultipleNoNamespaceRootSchema.

@Test
public void testMultipleNoNamespaceRootSchema() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/multiple-no-namespace-root-schema.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("second", namespace.getAlias());
    Assert.assertEquals("io.atlasmap.xml.test:Second", namespace.getUri());
    Assert.assertEquals(null, namespace.isTargetNamespace());
    // Note that the FirstElement also appears here as it's a top level element of namespace ""
    Assert.assertEquals(2, answer.getFields().getField().size());
    XmlComplexType complex = XmlComplexType.class.cast(answer.getFields().getField().get(1));
    Assert.assertEquals("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("/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("/RootDocument/FirstElement/FirstValue", firstField.getPath());
                break;
            case "second:SecondElement":
                Assert.assertEquals(FieldType.COMPLEX, field.getFieldType());
                Assert.assertEquals("/RootDocument/second:SecondElement", field.getPath());
                List<XmlField> secondFields = XmlComplexType.class.cast(field).getXmlFields().getXmlField();
                Assert.assertEquals(1, secondFields.size());
                XmlField secondField = secondFields.get(0);
                Assert.assertEquals("second:SecondValue", secondField.getName());
                Assert.assertEquals(FieldType.STRING, secondField.getFieldType());
                Assert.assertEquals("/RootDocument/second:SecondElement/second: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 24 with XmlComplexType

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

the class XmlSchemaInspectionMultipleNamespacesTest method testSyndesis.

@Test
public void testSyndesis() throws Exception {
    File schemaFile = Paths.get("src/test/resources/inspect/multiple-namespaces-syndesis.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("http://syndesis.io/v1/swagger-connector-template/request", namespace.getUri());
    Assert.assertEquals(null, namespace.isTargetNamespace());
    List<Field> fields = answer.getFields().getField();
    Assert.assertEquals(1, fields.size());
    XmlComplexType complex = XmlComplexType.class.cast(fields.get(0));
    Assert.assertEquals("tns:request", complex.getName());
    List<XmlField> rootFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(1, rootFields.size());
    complex = XmlComplexType.class.cast(rootFields.get(0));
    Assert.assertEquals("tns:body", complex.getName());
    List<XmlField> bodyFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(1, bodyFields.size());
    complex = XmlComplexType.class.cast(bodyFields.get(0));
    Assert.assertEquals("Pet", complex.getName());
    List<XmlField> petFields = complex.getXmlFields().getXmlField();
    Assert.assertEquals(6, petFields.size());
    for (XmlField xmlField : petFields) {
        switch(xmlField.getName()) {
            case "id":
                Assert.assertEquals(FieldType.DECIMAL, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/id", xmlField.getPath());
                break;
            case "Category":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/Category", xmlField.getPath());
                List<XmlField> categoryFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(2, categoryFields.size());
                for (XmlField categoryField : categoryFields) {
                    switch(categoryField.getName()) {
                        case "id":
                            Assert.assertEquals(FieldType.DECIMAL, categoryField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/Category/id", categoryField.getPath());
                            break;
                        case "name":
                            Assert.assertEquals(FieldType.STRING, categoryField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/Category/name", categoryField.getPath());
                            break;
                        default:
                            Assert.fail(String.format("Unknown field '%s'", categoryField.getPath()));
                    }
                }
                break;
            case "name":
                Assert.assertEquals(FieldType.STRING, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/name", xmlField.getPath());
                break;
            case "photoUrl":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/photoUrl", xmlField.getPath());
                List<XmlField> photoUrlFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(1, photoUrlFields.size());
                XmlField photoUrlField = photoUrlFields.get(0);
                Assert.assertEquals(FieldType.STRING, photoUrlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/photoUrl/photoUrl", photoUrlField.getPath());
                break;
            case "tag":
                Assert.assertEquals(FieldType.COMPLEX, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/tag", xmlField.getPath());
                List<XmlField> tagFields = XmlComplexType.class.cast(xmlField).getXmlFields().getXmlField();
                Assert.assertEquals(1, tagFields.size());
                XmlField tagField = tagFields.get(0);
                Assert.assertEquals(FieldType.COMPLEX, tagField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/tag/Tag", tagField.getPath());
                List<XmlField> tagTagFields = XmlComplexType.class.cast(tagField).getXmlFields().getXmlField();
                Assert.assertEquals(2, tagTagFields.size());
                for (XmlField tagTagField : tagTagFields) {
                    switch(tagTagField.getName()) {
                        case "id":
                            Assert.assertEquals(FieldType.DECIMAL, tagTagField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/tag/Tag/id", tagTagField.getPath());
                            break;
                        case "name":
                            Assert.assertEquals(FieldType.STRING, tagTagField.getFieldType());
                            Assert.assertEquals("/tns:request/tns:body/Pet/tag/Tag/name", tagTagField.getPath());
                            break;
                        default:
                            Assert.fail(String.format("Unknown field '%s'", tagTagField.getPath()));
                    }
                }
                break;
            case "status":
                Assert.assertEquals(FieldType.STRING, xmlField.getFieldType());
                Assert.assertEquals("/tns:request/tns:body/Pet/status", xmlField.getPath());
                break;
            default:
                Assert.fail(String.format("Unknown field '%s'", xmlField.getPath()));
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) 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 25 with XmlComplexType

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

the class SchemaInspector method printElement.

private void printElement(XSElementDecl element, String root, XmlComplexType xmlComplexType, CollectionType collectionType) throws Exception {
    String rootName = root;
    if (element.getType().isComplexType()) {
        XmlComplexType complexType = getXmlComplexType();
        rootName = rootName + "/" + getNameNS(element);
        complexType.setName(getNameNS(element));
        complexType.setPath(rootName);
        complexType.setCollectionType(collectionType);
        xmlComplexType.getXmlFields().getXmlField().add(complexType);
        printComplexType(element.getType().asComplexType(), rootName, complexType);
    } else {
        if (element.getType() != null && element.getType().asSimpleType() != null) {
            XmlField xmlField = AtlasXmlModelFactory.createXmlField();
            xmlField.setName(getNameNS(element));
            xmlField.setPath(rootName + "/" + getNameNS(element));
            xmlComplexType.getXmlFields().getXmlField().add(xmlField);
            if (element.getDefaultValue() != null) {
                xmlField.setValue(element.getDefaultValue());
            } else if (element.getFixedValue() != null) {
                xmlField.setValue(element.getFixedValue());
            }
            XSRestrictionSimpleType typeRestriction = element.getType().asSimpleType().asRestriction();
            if (typeRestriction != null) {
                xmlField.setFieldType(XS_TYPE_TO_FIELD_TYPE_MAP.get(typeRestriction.getBaseType().getName()));
                mapRestrictions(xmlField, typeRestriction);
            }
            printSimpleType(element.getType().asSimpleType(), xmlField);
        }
    }
}
Also used : XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XSRestrictionSimpleType(com.sun.xml.xsom.XSRestrictionSimpleType) XmlField(io.atlasmap.xml.v2.XmlField)

Aggregations

XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)28 XmlDocument (io.atlasmap.xml.v2.XmlDocument)20 Test (org.junit.Test)20 XmlField (io.atlasmap.xml.v2.XmlField)13 XmlNamespace (io.atlasmap.xml.v2.XmlNamespace)9 File (java.io.File)8 Field (io.atlasmap.v2.Field)3 XmlFields (io.atlasmap.xml.v2.XmlFields)2 XSAttributeDecl (com.sun.xml.xsom.XSAttributeDecl)1 XSAttributeUse (com.sun.xml.xsom.XSAttributeUse)1 XSElementDecl (com.sun.xml.xsom.XSElementDecl)1 XSRestrictionSimpleType (com.sun.xml.xsom.XSRestrictionSimpleType)1 XSSchema (com.sun.xml.xsom.XSSchema)1 XSSimpleType (com.sun.xml.xsom.XSSimpleType)1 FieldType (io.atlasmap.v2.FieldType)1 Restriction (io.atlasmap.xml.v2.Restriction)1 XmlInspectionRequest (io.atlasmap.xml.v2.XmlInspectionRequest)1 XmlInspectionResponse (io.atlasmap.xml.v2.XmlInspectionResponse)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1