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     */
017    package org.apache.camel.component.mock;
018    
019    
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import org.apache.camel.Exchange;
024    import org.apache.camel.Expression;
025    import org.apache.camel.Predicate;
026    import org.apache.camel.builder.ExpressionClause;
027    import org.apache.camel.builder.ValueBuilder;
028    import static org.apache.camel.builder.ExpressionBuilder.bodyExpression;
029    import static org.apache.camel.builder.ExpressionBuilder.headerExpression;
030    
031    /**
032     * A builder of assertions on message exchanges
033     *
034     * @version $Revision: 36865 $
035     */
036    public abstract class AssertionClause implements Runnable {
037    
038        private List<Predicate<Exchange>> predicates = new ArrayList<Predicate<Exchange>>();
039    
040        // Builder methods
041        // -------------------------------------------------------------------------
042    
043        /**
044         * Adds the given predicate to this assertion clause
045         */
046        public AssertionClause predicate(Predicate<Exchange> predicate) {
047            addPredicate(predicate);
048            return this;
049        }
050    
051        public ExpressionClause<AssertionClause> predicate() {
052            ExpressionClause<AssertionClause> clause = new ExpressionClause<AssertionClause>(this);
053            addPredicate(clause);
054            return clause;
055        }
056    
057        /**
058         * Returns a predicate and value builder for headers on an exchange
059         */
060        public ValueBuilder<Exchange> header(String name) {
061            Expression<Exchange> expression = headerExpression(name);
062            return new PredicateValueBuilder(expression);
063        }
064    
065        /**
066         * Returns a predicate and value builder for the inbound body on an exchange
067         */
068        public PredicateValueBuilder body() {
069            Expression<Exchange> expression = bodyExpression();
070            return new PredicateValueBuilder(expression);
071        }
072    
073        /**
074         * Returns a predicate and value builder for the inbound message body as a
075         * specific type
076         */
077        public <T> PredicateValueBuilder bodyAs(Class<T> type) {
078            Expression<Exchange> expression = bodyExpression(type);
079            return new PredicateValueBuilder(expression);
080        }
081    
082        /**
083         * Returns a predicate and value builder for the outbound body on an
084         * exchange
085         */
086        public PredicateValueBuilder outBody() {
087            Expression<Exchange> expression = bodyExpression();
088            return new PredicateValueBuilder(expression);
089        }
090    
091        /**
092         * Returns a predicate and value builder for the outbound message body as a
093         * specific type
094         */
095        public <T> PredicateValueBuilder outBody(Class<T> type) {
096            Expression<Exchange> expression = bodyExpression(type);
097            return new PredicateValueBuilder(expression);
098        }
099    
100        /**
101         * Performs any assertions on the given exchange
102         */
103        protected void applyAssertionOn(MockEndpoint endpoint, int index, Exchange exchange) {
104            for (Predicate<Exchange> predicate : predicates) {
105                predicate.assertMatches(endpoint.getEndpointUri() + " ", exchange);
106            }
107        }
108    
109        protected void addPredicate(Predicate<Exchange> predicate) {
110            predicates.add(predicate);
111        }
112    
113    
114    
115        public class PredicateValueBuilder extends ValueBuilder<Exchange> {
116    
117            public PredicateValueBuilder(Expression<Exchange> expression) {
118                super(expression);
119            }
120    
121            protected Predicate<Exchange> onNewPredicate(Predicate<Exchange> predicate) {
122                addPredicate(predicate);
123                return predicate;
124            }
125        }
126    }