001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component.test;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.camel.Component;
023import org.apache.camel.Endpoint;
024import org.apache.camel.Exchange;
025import org.apache.camel.Processor;
026import org.apache.camel.WrappedFile;
027import org.apache.camel.component.mock.MockEndpoint;
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.UriEndpoint;
030import org.apache.camel.spi.UriParam;
031import org.apache.camel.spi.UriPath;
032import org.apache.camel.util.EndpointHelper;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * A <a href="http://camel.apache.org/test.html">Test Endpoint</a> is a
038 * <a href="http://camel.apache.org/mock.html">Mock Endpoint</a> for testing but it will
039 * pull all messages from the nested endpoint and use those as expected message body assertions.
040 *
041 * @version 
042 */
043@UriEndpoint(scheme = "test", title = "Test", syntax = "test:name", producerOnly = true, label = "core,testing", lenientProperties = true)
044public class TestEndpoint extends MockEndpoint {
045    private static final Logger LOG = LoggerFactory.getLogger(TestEndpoint.class);
046    private Endpoint expectedMessageEndpoint;
047    @UriPath(description = "Name of endpoint to lookup in the registry to use for polling messages used for testing") @Metadata(required = "true")
048    private String name;
049    @UriParam(label = "producer", defaultValue = "2000")
050    private long timeout = 2000L;
051
052    public TestEndpoint(String endpointUri, Component component) {
053        super(endpointUri, component);
054    }
055
056    public void setExpectedMessageEndpoint(Endpoint expectedMessageEndpoint) {
057        this.expectedMessageEndpoint = expectedMessageEndpoint;
058    }
059
060    @Override
061    protected void doStart() throws Exception {
062        LOG.debug("Consuming expected messages from: {}", expectedMessageEndpoint);
063
064        final List<Object> expectedBodies = new ArrayList<Object>();
065        EndpointHelper.pollEndpoint(expectedMessageEndpoint, new Processor() {
066            public void process(Exchange exchange) throws Exception {
067                // if file based we need to load the file into memory as the file may be deleted/moved afterwards
068                Object body = getInBody(exchange);
069                if (body instanceof WrappedFile) {
070                    body = exchange.getIn().getBody(byte[].class);
071                }
072                LOG.trace("Received message body {}", body);
073                expectedBodies.add(body);
074            }
075        }, timeout);
076
077        LOG.debug("Received: {} expected message(s) from: {}", expectedBodies.size(), expectedMessageEndpoint);
078        expectedBodiesReceived(expectedBodies);
079    }
080
081    /**
082     * This method allows us to convert or coerce the expected message body into some other type
083     */
084    protected Object getInBody(Exchange exchange) {
085        return exchange.getIn().getBody();
086    }
087
088    public long getTimeout() {
089        return timeout;
090    }
091
092    /**
093     * The timeout to use when polling for message bodies from the URI
094     */
095    public void setTimeout(long timeout) {
096        this.timeout = timeout;
097    }
098}