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