001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.component.mock;
019
020 import org.apache.camel.Exchange;
021 import org.apache.camel.Expression;
022 import org.apache.camel.Predicate;
023 import static org.apache.camel.builder.ExpressionBuilder.bodyExpression;
024 import static org.apache.camel.builder.ExpressionBuilder.headerExpression;
025 import org.apache.camel.builder.Fluent;
026 import org.apache.camel.builder.FluentArg;
027 import org.apache.camel.builder.ValueBuilder;
028
029 import java.util.ArrayList;
030 import java.util.List;
031
032 /**
033 * A builder of assertions on message exchanges
034 *
035 * @version $Revision: 1.1 $
036 */
037 public abstract class AssertionClause<E extends Exchange> implements Runnable {
038
039 private List<Predicate<E>> predicates = new ArrayList<Predicate<E>>();
040
041 // Builder methods
042 //-------------------------------------------------------------------------
043
044 /**
045 * Adds the given predicate to this assertion clause
046 */
047 public AssertionClause<E> predicate(Predicate<E> predicate) {
048 addPredicate(predicate);
049 return this;
050 }
051
052 /**
053 * Returns a predicate and value builder for headers on an exchange
054 */
055 @Fluent
056 public ValueBuilder<E> header(@FluentArg("name")String name) {
057 Expression<E> expression = headerExpression(name);
058 return new PredicateValueBuilder(expression);
059 }
060
061 /**
062 * Returns a predicate and value builder for the inbound body on an exchange
063 */
064 @Fluent
065 public PredicateValueBuilder body() {
066 Expression<E> expression = bodyExpression();
067 return new PredicateValueBuilder(expression);
068 }
069
070 /**
071 * Returns a predicate and value builder for the inbound message body as a specific type
072 */
073 @Fluent
074 public <T> PredicateValueBuilder bodyAs(@FluentArg("class")Class<T> type) {
075 Expression<E> expression = bodyExpression(type);
076 return new PredicateValueBuilder(expression);
077 }
078
079 /**
080 * Returns a predicate and value builder for the outbound body on an exchange
081 */
082 @Fluent
083 public PredicateValueBuilder outBody() {
084 Expression<E> expression = bodyExpression();
085 return new PredicateValueBuilder(expression);
086 }
087
088 /**
089 * Returns a predicate and value builder for the outbound message body as a specific type
090 */
091 @Fluent
092 public <T> PredicateValueBuilder outBody(@FluentArg("class")Class<T> type) {
093 Expression<E> expression = bodyExpression(type);
094 return new PredicateValueBuilder(expression);
095 }
096
097 /**
098 * Performs any assertions on the given exchange
099 */
100 protected void applyAssertionOn(MockEndpoint endpoint, int index, E exchange) {
101 for (Predicate<E> predicate : predicates) {
102 predicate.assertMatches(endpoint.getEndpointUri() + " ", exchange);
103 }
104 }
105
106 protected void addPredicate(Predicate<E> predicate) {
107 predicates.add(predicate);
108 }
109
110 public class PredicateValueBuilder extends ValueBuilder<E> {
111
112 public PredicateValueBuilder(Expression<E> expression) {
113 super(expression);
114 }
115
116 protected Predicate<E> onNewPredicate(Predicate<E> predicate) {
117 addPredicate(predicate);
118 return predicate;
119 }
120 }
121 }