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.component.mock.MockEndpoint; 027import org.apache.camel.spi.Metadata; 028import org.apache.camel.spi.UriEndpoint; 029import org.apache.camel.spi.UriParam; 030import org.apache.camel.spi.UriPath; 031import org.apache.camel.util.EndpointHelper; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035/** 036 * A <a href="http://camel.apache.org/test.html">Test Endpoint</a> is a 037 * <a href="http://camel.apache.org/mock.html">Mock Endpoint</a> for testing but it will 038 * pull all messages from the nested endpoint and use those as expected message body assertions. 039 * 040 * @version 041 */ 042@UriEndpoint(scheme = "test", title = "Test", syntax = "test:name", producerOnly = true, label = "core,testing") 043public class TestEndpoint extends MockEndpoint { 044 private static final Logger LOG = LoggerFactory.getLogger(TestEndpoint.class); 045 private final Endpoint expectedMessageEndpoint; 046 @UriPath(description = "Name of endpoint to lookup in the registry to use for polling messages used for testing") @Metadata(required = "true") 047 private String name; 048 @UriParam(defaultValue = "2000") 049 private long timeout = 2000L; 050 051 public TestEndpoint(String endpointUri, Component component, Endpoint expectedMessageEndpoint) { 052 super(endpointUri, component); 053 this.expectedMessageEndpoint = expectedMessageEndpoint; 054 } 055 056 @Override 057 protected void doStart() throws Exception { 058 LOG.debug("Consuming expected messages from: {}", expectedMessageEndpoint); 059 060 final List<Object> expectedBodies = new ArrayList<Object>(); 061 EndpointHelper.pollEndpoint(expectedMessageEndpoint, new Processor() { 062 public void process(Exchange exchange) throws Exception { 063 Object body = getInBody(exchange); 064 LOG.trace("Received message body {}", body); 065 expectedBodies.add(body); 066 } 067 }, timeout); 068 069 LOG.debug("Received: {} expected message(s) from: {}", expectedBodies.size(), expectedMessageEndpoint); 070 expectedBodiesReceived(expectedBodies); 071 } 072 073 /** 074 * This method allows us to convert or coerce the expected message body into some other type 075 */ 076 protected Object getInBody(Exchange exchange) { 077 return exchange.getIn().getBody(); 078 } 079 080 public long getTimeout() { 081 return timeout; 082 } 083 084 /** 085 * The timeout to use when polling for message bodies from the URI 086 */ 087 public void setTimeout(long timeout) { 088 this.timeout = timeout; 089 } 090}