Search in sources :

Example 51 with Message

use of org.apache.camel.Message in project camel by apache.

the class CharsetTest method testCamel.

@Test
public void testCamel() throws Exception {
    results.expectedMessageCount(4);
    results.assertIsSatisfied();
    int counter = 0;
    List<Exchange> list = results.getReceivedExchanges();
    for (Exchange exchange : list) {
        Message in = exchange.getIn();
        Map<?, ?> body = in.getBody(Map.class);
        assertNotNull("Should have found body as a Map but was: " + ObjectHelper.className(in.getBody()), body);
        assertEquals("ITEM_DESC", expectedItemDesc[counter], body.get("ITEM_DESC"));
        LOG.info("Result: " + counter + " = " + body);
        counter++;
    }
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 52 with Message

use of org.apache.camel.Message in project camel by apache.

the class DelimitedWithNoDescriptorTest method testCamel.

@Test
public void testCamel() throws Exception {
    results.expectedMessageCount(4);
    results.assertIsSatisfied();
    int counter = 0;
    List<Exchange> list = results.getReceivedExchanges();
    for (Exchange exchange : list) {
        Message in = exchange.getIn();
        Map<?, ?> body = in.getBody(Map.class);
        assertNotNull("Should have found body as a Map but was: " + ObjectHelper.className(in.getBody()), body);
        assertEquals("NAME", expectedItemDesc[counter], body.get("NAME"));
        LOG.info("Result: " + counter + " = " + body);
        counter++;
    }
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 53 with Message

use of org.apache.camel.Message in project camel by apache.

the class AggregationStrategyClause method body.

/**
     * TODO: document
     *
     * Note: this is experimental and subject to changes in future releases.
     */
public <O, N> T body(final Class<O> oldType, final Class<N> newType, final BiFunction<O, N, Object> function) {
    return exchange((Exchange oldExchange, Exchange newExchange) -> {
        Message oldMessage = oldExchange != null ? oldExchange.getIn() : null;
        Message newMessage = ObjectHelper.notNull(newExchange, "NewExchange").getIn();
        Object result = function.apply(oldMessage != null ? oldMessage.getBody(oldType) : null, newMessage != null ? newMessage.getBody(newType) : null);
        if (oldExchange != null) {
            oldExchange.getIn().setBody(result);
            return oldExchange;
        } else {
            newExchange.getIn().setBody(result);
            return newExchange;
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message)

Example 54 with Message

use of org.apache.camel.Message in project camel by apache.

the class AggregationStrategyClause method message.

// *******************************
// Message
// *******************************
/**
     * TODO: document
     *
     * Note: this is experimental and subject to changes in future releases.
     */
public T message(final BiFunction<Message, Message, Message> function) {
    return exchange((Exchange oldExchange, Exchange newExchange) -> {
        Message oldMessage = oldExchange != null ? oldExchange.getIn() : null;
        Message newMessage = ObjectHelper.notNull(newExchange, "NewExchange").getIn();
        Message result = function.apply(oldMessage, newMessage);
        if (oldExchange != null) {
            oldExchange.setIn(result);
            return oldExchange;
        } else {
            newExchange.setIn(result);
            return newExchange;
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message)

Example 55 with Message

use of org.apache.camel.Message in project camel by apache.

the class MockEndpoint method performAssertions.

/**
     * Performs the assertions on the incoming exchange.
     *
     * @param exchange   the actual exchange
     * @param copy       a copy of the exchange (only store this)
     * @throws Exception can be thrown if something went wrong
     */
protected void performAssertions(Exchange exchange, Exchange copy) throws Exception {
    Message in = copy.getIn();
    Object actualBody = in.getBody();
    if (expectedHeaderValues != null) {
        if (actualHeaderValues == null) {
            actualHeaderValues = new CaseInsensitiveMap();
        }
        if (in.hasHeaders()) {
            actualHeaderValues.putAll(in.getHeaders());
        }
    }
    if (expectedPropertyValues != null) {
        if (actualPropertyValues == null) {
            actualPropertyValues = new ConcurrentHashMap<String, Object>();
        }
        actualPropertyValues.putAll(copy.getProperties());
    }
    if (expectedBodyValues != null) {
        int index = actualBodyValues.size();
        if (expectedBodyValues.size() > index) {
            Object expectedBody = expectedBodyValues.get(index);
            if (expectedBody != null) {
                // prefer to convert body early, for example when using files
                // we need to read the content at this time
                Object body = in.getBody(expectedBody.getClass());
                if (body != null) {
                    actualBody = body;
                }
            }
            actualBodyValues.add(actualBody);
        }
    }
    // let counter be 0 index-based in the logs
    if (LOG.isDebugEnabled()) {
        String msg = getEndpointUri() + " >>>> " + counter + " : " + copy + " with body: " + actualBody;
        if (copy.getIn().hasHeaders()) {
            msg += " and headers:" + copy.getIn().getHeaders();
        }
        LOG.debug(msg);
    }
    // record timestamp when exchange was received
    copy.setProperty(Exchange.RECEIVED_TIMESTAMP, new Date());
    // add a copy of the received exchange
    addReceivedExchange(copy);
    // and then increment counter after adding received exchange
    ++counter;
    Processor processor = processors.get(getReceivedCounter()) != null ? processors.get(getReceivedCounter()) : defaultProcessor;
    if (processor != null) {
        try {
            // must process the incoming exchange and NOT the copy as the idea
            // is the end user can manipulate the exchange
            processor.process(exchange);
        } catch (Exception e) {
            // set exceptions on exchange so we can throw exceptions to simulate errors
            exchange.setException(e);
        }
    }
}
Also used : CaseInsensitiveMap(org.apache.camel.util.CaseInsensitiveMap) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) Endpoint(org.apache.camel.Endpoint) UriEndpoint(org.apache.camel.spi.UriEndpoint) DefaultEndpoint(org.apache.camel.impl.DefaultEndpoint) InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) BrowsableEndpoint(org.apache.camel.spi.BrowsableEndpoint) Date(java.util.Date)

Aggregations

Message (org.apache.camel.Message)721 Exchange (org.apache.camel.Exchange)341 Test (org.junit.Test)215 Processor (org.apache.camel.Processor)118 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)104 DefaultMessage (org.apache.camel.impl.DefaultMessage)51 DefaultExchange (org.apache.camel.impl.DefaultExchange)44 Endpoint (org.apache.camel.Endpoint)40 Response (javax.ws.rs.core.Response)38 InputStream (java.io.InputStream)36 HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)26 RouteBuilder (org.apache.camel.builder.RouteBuilder)25 Customer (org.apache.camel.component.cxf.jaxrs.testbean.Customer)25 ActionResponse (org.openstack4j.model.common.ActionResponse)25 IOException (java.io.IOException)24 Map (java.util.Map)24 DataHandler (javax.activation.DataHandler)21 Producer (org.apache.camel.Producer)21 CxfOperationException (org.apache.camel.component.cxf.CxfOperationException)19