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