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