Search in sources :

Example 81 with AtlasSession

use of io.atlasmap.api.AtlasSession in project atlasmap by atlasmap.

the class AtlasEndpoint method onExchange.

@Override
protected void onExchange(Exchange exchange) throws Exception {
    Message incomingMessage = exchange.getIn();
    String newResourceUri = incomingMessage.getHeader(AtlasConstants.ATLAS_RESOURCE_URI, String.class);
    if (newResourceUri != null) {
        incomingMessage.removeHeader(AtlasConstants.ATLAS_RESOURCE_URI);
        log.debug("{} set to {} creating new endpoint to handle exchange", AtlasConstants.ATLAS_RESOURCE_URI, newResourceUri);
        AtlasEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
        newEndpoint.onExchange(exchange);
        return;
    }
    AtlasSession atlasSession = getOrCreateAtlasContext(incomingMessage).createSession();
    populateSourceDocuments(exchange, atlasSession);
    getAtlasContext().process(atlasSession);
    List<Audit> errors = new ArrayList<>();
    for (Audit audit : atlasSession.getAudits().getAudit()) {
        switch(audit.getStatus()) {
            case ERROR:
                errors.add(audit);
                break;
            case WARN:
                LOG.warn("{}: docId='{}', path='{}'", audit.getMessage(), audit.getDocId(), audit.getPath());
                break;
            default:
                LOG.info("{}: docId='{}', path='{}'", audit.getMessage(), audit.getDocId(), audit.getPath());
        }
    }
    if (!errors.isEmpty()) {
        StringBuilder buf = new StringBuilder("Errors: ");
        errors.stream().forEach(a -> buf.append(String.format("[%s: docId='%s', path='%s'], ", a.getMessage(), a.getDocId(), a.getPath())));
        throw new AtlasException(buf.toString());
    }
    populateTargetDocuments(atlasSession, exchange);
}
Also used : Audit(io.atlasmap.v2.Audit) Message(org.apache.camel.Message) ArrayList(java.util.ArrayList) AtlasException(io.atlasmap.api.AtlasException) AtlasSession(io.atlasmap.api.AtlasSession)

Example 82 with AtlasSession

use of io.atlasmap.api.AtlasSession in project atlasmap by atlasmap.

the class AtlasEndpoint method populateSourceDocuments.

private void populateSourceDocuments(Exchange exchange, AtlasSession session) {
    if (session.getMapping().getDataSource() == null) {
        return;
    }
    DataSource[] sourceDataSources = session.getMapping().getDataSource().stream().filter(ds -> ds.getDataSourceType() == DataSourceType.SOURCE).toArray(DataSource[]::new);
    if (sourceDataSources.length == 0) {
        session.setDefaultSourceDocument(exchange.getIn().getBody());
        return;
    }
    if (sourceDataSources.length == 1) {
        String docId = sourceDataSources[0].getId();
        Object payload = extractPayload(sourceDataSources[0], exchange.getIn());
        if (docId == null || docId.isEmpty()) {
            session.setDefaultSourceDocument(payload);
        } else {
            session.setSourceDocument(docId, payload);
        }
        return;
    }
    // TODO handle headers docId - https://github.com/atlasmap/atlasmap/issues/67
    @SuppressWarnings("unchecked") Map<String, Message> sourceMessages = exchange.getProperty(sourceMapName, Map.class);
    if (sourceMessages == null) {
        return;
    }
    for (DataSource ds : sourceDataSources) {
        String docId = ds.getId();
        Object payload = extractPayload(ds, sourceMessages.get(docId));
        if (docId == null || docId.isEmpty()) {
            session.setDefaultSourceDocument(payload);
        } else {
            session.setSourceDocument(docId, payload);
        }
    }
}
Also used : Message(org.apache.camel.Message) LoggerFactory(org.slf4j.LoggerFactory) DataSource(io.atlasmap.v2.DataSource) DefaultAtlasContextFactory(io.atlasmap.core.DefaultAtlasContextFactory) HashMap(java.util.HashMap) Exchange(org.apache.camel.Exchange) AtlasMappingFormat(io.atlasmap.core.AtlasMappingService.AtlasMappingFormat) ArrayList(java.util.ArrayList) UriEndpoint(org.apache.camel.spi.UriEndpoint) UriParam(org.apache.camel.spi.UriParam) Map(java.util.Map) ResourceEndpoint(org.apache.camel.component.ResourceEndpoint) ResourceHelper(org.apache.camel.util.ResourceHelper) AtlasContextFactory(io.atlasmap.api.AtlasContextFactory) ExchangePattern(org.apache.camel.ExchangePattern) Properties(java.util.Properties) Logger(org.slf4j.Logger) AtlasException(io.atlasmap.api.AtlasException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DataSourceType(io.atlasmap.v2.DataSourceType) List(java.util.List) StringReader(java.io.StringReader) AtlasMapping(io.atlasmap.v2.AtlasMapping) Audit(io.atlasmap.v2.Audit) IOHelper(org.apache.camel.util.IOHelper) AtlasSession(io.atlasmap.api.AtlasSession) ObjectHelper(org.apache.camel.util.ObjectHelper) AtlasContext(io.atlasmap.api.AtlasContext) InputStream(java.io.InputStream) Message(org.apache.camel.Message) DataSource(io.atlasmap.v2.DataSource)

Example 83 with AtlasSession

use of io.atlasmap.api.AtlasSession in project atlasmap by atlasmap.

the class AtlasEndpointTest method perform.

private void perform(List<DataSource> dataSources, String sourceDocId, String targetDocId, boolean fromStream) throws Exception {
    final AtlasMapping mapping = new AtlasMapping();
    mapping.getDataSource().addAll(dataSources);
    final AtlasContext context = spy(AtlasContext.class);
    final AtlasSession session = spy(AtlasSession.class);
    when(context.createSession()).thenReturn(session);
    when(session.getMapping()).thenReturn(mapping);
    when(session.getAudits()).thenReturn(new Audits());
    final AtlasEndpoint endpoint = new AtlasEndpoint("atlas:test.xml", new AtlasComponent(), "test.xml");
    endpoint.setAtlasContext(context);
    final Exchange exchange = spy(Exchange.class);
    final Message inMessage = spy(Message.class);
    when(inMessage.getBody()).thenReturn(fromStream ? new ByteArrayInputStream("{test}".getBytes()) : "{test}");
    when(inMessage.getBody(String.class)).thenReturn("{test}");
    when(exchange.getIn()).thenReturn(inMessage);
    if (sourceDocId == null) {
        doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) {
                LOG.debug("setDefaultSourceDocument({})", invocation.getArgument(0).toString());
                assertEquals("{test}", invocation.getArgument(0).toString());
                return null;
            }
        }).when(session).setDefaultSourceDocument(any());
    } else {
        doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock invocation) {
                LOG.debug("setSourceDocument({}, {})", invocation.getArgument(0), invocation.getArgument(1));
                assertEquals(sourceDocId, invocation.getArgument(0));
                assertEquals("{test}", invocation.getArgument(1));
                return null;
            }
        }).when(session).setSourceDocument(any(), any());
    }
    final Message outMessage = spy(Message.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            LOG.debug("setBody({})", invocation.getArgument(0).toString());
            assertEquals("<target/>", invocation.getArgument(0));
            return null;
        }
    }).when(outMessage).setBody(any());
    doNothing().when(outMessage).setHeaders(any());
    doNothing().when(outMessage).setAttachments(any());
    if (targetDocId == null) {
        when(session.getDefaultTargetDocument()).thenAnswer(new Answer<Object>() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                LOG.debug("getDefaultTargetDocument()");
                return "<target/>";
            }
        });
    } else {
        when(session.getTargetDocument(any())).thenAnswer(new Answer<Object>() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                LOG.debug("getTargetDocument({})", invocation.getArgument(0).toString());
                assertEquals(targetDocId, invocation.getArgument(0));
                return "<target/>";
            }
        });
    }
    when(exchange.getOut()).thenReturn(outMessage);
    endpoint.onExchange(exchange);
}
Also used : Message(org.apache.camel.Message) Exchange(org.apache.camel.Exchange) AtlasMapping(io.atlasmap.v2.AtlasMapping) Audits(io.atlasmap.v2.Audits) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AtlasContext(io.atlasmap.api.AtlasContext) AtlasSession(io.atlasmap.api.AtlasSession)

Example 84 with AtlasSession

use of io.atlasmap.api.AtlasSession in project atlasmap by atlasmap.

the class XmlXmlAutoConversionTest method executeMapper.

protected JAXBElement<XmlFlatPrimitiveElement> executeMapper(String fileName) throws Exception {
    AtlasContext context = atlasContextFactory.createContext(new File(fileName).toURI());
    AtlasSession session = context.createSession();
    String source = AtlasTestUtil.loadFileAsString("src/test/resources/xmlToXml/atlas-xml-flatprimitive-attribute-autoconversion.xml");
    session.setDefaultSourceDocument(source);
    context.process(session);
    assertFalse(printAudit(session), session.hasErrors());
    Object object = session.getDefaultTargetDocument();
    assertNotNull(object);
    assertTrue(object instanceof String);
    JAXBElement<XmlFlatPrimitiveElement> xmlFPE = AtlasXmlTestHelper.unmarshal((String) object, XmlFlatPrimitiveElement.class);
    return xmlFPE;
}
Also used : AtlasContext(io.atlasmap.api.AtlasContext) XmlFlatPrimitiveElement(io.atlasmap.xml.test.v2.XmlFlatPrimitiveElement) File(java.io.File) AtlasSession(io.atlasmap.api.AtlasSession)

Example 85 with AtlasSession

use of io.atlasmap.api.AtlasSession in project atlasmap by atlasmap.

the class XmlXmlCollectionConverstionTest method testProcessCollectionListSimple.

@Test
public void testProcessCollectionListSimple() throws Exception {
    AtlasContext context = atlasContextFactory.createContext(new File("src/test/resources/xmlToXml/atlasmapping-collection-list-simple.xml").toURI());
    // contact<>.firstName -> contact<>.name
    String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    input += "<XmlOA>";
    for (int i = 0; i < 3; i++) {
        input += "<contact><firstName>name" + i + "</firstName></contact>";
    }
    input += "</XmlOA>";
    AtlasSession session = context.createSession();
    session.setDefaultSourceDocument(input);
    context.process(session);
    Object object = session.getDefaultTargetDocument();
    assertNotNull(object);
    assertTrue(object instanceof String);
    String output = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
    output += "<XmlOA>";
    for (int i = 0; i < 3; i++) {
        output += "<contact><name>name" + i + "</name></contact>";
    }
    output += "</XmlOA>";
    assertEquals(output, object);
}
Also used : AtlasContext(io.atlasmap.api.AtlasContext) File(java.io.File) AtlasSession(io.atlasmap.api.AtlasSession) Test(org.junit.Test) AtlasMappingBaseTest(io.atlasmap.reference.AtlasMappingBaseTest)

Aggregations

AtlasSession (io.atlasmap.api.AtlasSession)157 AtlasContext (io.atlasmap.api.AtlasContext)140 Test (org.junit.Test)134 File (java.io.File)129 AtlasMappingBaseTest (io.atlasmap.reference.AtlasMappingBaseTest)123 TargetContact (io.atlasmap.java.test.TargetContact)31 BaseContact (io.atlasmap.java.test.BaseContact)26 AtlasJsonTestUnrootedMapper (io.atlasmap.json.test.AtlasJsonTestUnrootedMapper)25 TargetFlatPrimitiveClass (io.atlasmap.java.test.TargetFlatPrimitiveClass)24 TargetFlatPrimitive (io.atlasmap.json.test.TargetFlatPrimitive)13 BaseOrder (io.atlasmap.java.test.BaseOrder)12 TargetOrder (io.atlasmap.java.test.TargetOrder)11 TargetTestClass (io.atlasmap.java.test.TargetTestClass)11 AtlasMapping (io.atlasmap.v2.AtlasMapping)9 XmlContactAttribute (io.atlasmap.xml.test.v2.XmlContactAttribute)9 AtlasJsonTestRootedMapper (io.atlasmap.json.test.AtlasJsonTestRootedMapper)8 BaseFlatPrimitiveClass (io.atlasmap.java.test.BaseFlatPrimitiveClass)7 SourceFlatPrimitiveClass (io.atlasmap.java.test.SourceFlatPrimitiveClass)7 URL (java.net.URL)4 Message (org.apache.camel.Message)4