Search in sources :

Example 1 with CaseInsensitiveMap

use of org.apache.camel.util.CaseInsensitiveMap 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)

Example 2 with CaseInsensitiveMap

use of org.apache.camel.util.CaseInsensitiveMap in project camel by apache.

the class MockEndpoint method expectedHeaderReceived.

/**
     * Sets an expectation that the given header name & value are received by this endpoint
     * <p/>
     * You can set multiple expectations for different header names.
     * If you set a value of <tt>null</tt> that means we accept either the header is absent, or its value is <tt>null</tt>
     * <p/>
     * <b>Important:</b> The number of values must match the expected number of messages, so if you expect 3 messages, then
     * there must be 3 values.
     * <p/>
     * <b>Important:</b> This overrides any previous set value using {@link #expectedMessageCount(int)}
     */
public void expectedHeaderReceived(final String name, final Object value) {
    if (expectedHeaderValues == null) {
        expectedHeaderValues = new CaseInsensitiveMap();
        // we just wants to expects to be called once
        expects(new Runnable() {

            public void run() {
                for (int i = 0; i < getReceivedExchanges().size(); i++) {
                    Exchange exchange = getReceivedExchange(i);
                    for (Map.Entry<String, Object> entry : expectedHeaderValues.entrySet()) {
                        String key = entry.getKey();
                        Object expectedValue = entry.getValue();
                        // we accept that an expectedValue of null also means that the header may be absent
                        if (expectedValue != null) {
                            assertTrue("Exchange " + i + " has no headers", exchange.getIn().hasHeaders());
                            boolean hasKey = exchange.getIn().getHeaders().containsKey(key);
                            assertTrue("No header with name " + key + " found for message: " + i, hasKey);
                        }
                        Object actualValue = exchange.getIn().getHeader(key);
                        actualValue = extractActualValue(exchange, actualValue, expectedValue);
                        assertEquals("Header with name " + key + " for message: " + i, expectedValue, actualValue);
                    }
                }
            }
        });
    }
    expectedHeaderValues.put(name, value);
}
Also used : CaseInsensitiveMap(org.apache.camel.util.CaseInsensitiveMap) Exchange(org.apache.camel.Exchange)

Aggregations

CaseInsensitiveMap (org.apache.camel.util.CaseInsensitiveMap)2 Date (java.util.Date)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1 Message (org.apache.camel.Message)1 Processor (org.apache.camel.Processor)1 DefaultEndpoint (org.apache.camel.impl.DefaultEndpoint)1 InterceptSendToEndpoint (org.apache.camel.impl.InterceptSendToEndpoint)1 BrowsableEndpoint (org.apache.camel.spi.BrowsableEndpoint)1 UriEndpoint (org.apache.camel.spi.UriEndpoint)1