Coverage Report - org.apache.camel.component.mock.AssertionClause
 
Classes in this File Line Coverage Branch Coverage Complexity
AssertionClause
62% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.component.mock;
 19  
 
 20  
 import org.apache.camel.Exchange;
 21  
 import org.apache.camel.Expression;
 22  
 import org.apache.camel.Predicate;
 23  
 import static org.apache.camel.builder.ExpressionBuilder.bodyExpression;
 24  
 import static org.apache.camel.builder.ExpressionBuilder.headerExpression;
 25  
 import org.apache.camel.builder.Fluent;
 26  
 import org.apache.camel.builder.FluentArg;
 27  
 import org.apache.camel.builder.ValueBuilder;
 28  
 
 29  
 import java.util.ArrayList;
 30  
 import java.util.List;
 31  
 
 32  
 /**
 33  
  * A builder of assertions on message exchanges
 34  
  *
 35  
  * @version $Revision: 1.1 $
 36  
  */
 37  4
 public abstract class AssertionClause<E extends Exchange> implements Runnable {
 38  
 
 39  4
     private List<Predicate<E>> predicates = new ArrayList<Predicate<E>>();
 40  
 
 41  
     // Builder methods
 42  
     //-------------------------------------------------------------------------
 43  
 
 44  
     /**
 45  
      * Adds the given predicate to this assertion clause
 46  
      */
 47  
     public AssertionClause<E> predicate(Predicate<E> predicate) {
 48  0
         addPredicate(predicate);
 49  0
         return this;
 50  
     }
 51  
 
 52  
     /**
 53  
      * Returns a predicate and value builder for headers on an exchange
 54  
      */
 55  
     @Fluent
 56  
     public ValueBuilder<E> header(@FluentArg("name")String name) {
 57  4
         Expression<E> expression = headerExpression(name);
 58  4
         return new PredicateValueBuilder(expression);
 59  
     }
 60  
 
 61  
     /**
 62  
      * Returns a predicate and value builder for the inbound body on an exchange
 63  
      */
 64  
     @Fluent
 65  
     public PredicateValueBuilder body() {
 66  0
         Expression<E> expression = bodyExpression();
 67  0
         return new PredicateValueBuilder(expression);
 68  
     }
 69  
 
 70  
     /**
 71  
      * Returns a predicate and value builder for the inbound message body as a specific type
 72  
      */
 73  
     @Fluent
 74  
     public <T> PredicateValueBuilder bodyAs(@FluentArg("class")Class<T> type) {
 75  0
         Expression<E> expression = bodyExpression(type);
 76  0
         return new PredicateValueBuilder(expression);
 77  
     }
 78  
 
 79  
     /**
 80  
      * Returns a predicate and value builder for the outbound body on an exchange
 81  
      */
 82  
     @Fluent
 83  
     public PredicateValueBuilder outBody() {
 84  0
         Expression<E> expression = bodyExpression();
 85  0
         return new PredicateValueBuilder(expression);
 86  
     }
 87  
 
 88  
     /**
 89  
      * Returns a predicate and value builder for the outbound message body as a specific type
 90  
      */
 91  
     @Fluent
 92  
     public <T> PredicateValueBuilder outBody(@FluentArg("class")Class<T> type) {
 93  0
         Expression<E> expression = bodyExpression(type);
 94  0
         return new PredicateValueBuilder(expression);
 95  
     }
 96  
 
 97  
     /**
 98  
      * Performs any assertions on the given exchange
 99  
      */
 100  
     protected void applyAssertionOn(MockEndpoint endpoint, int index, E exchange) {
 101  4
         for (Predicate<E> predicate : predicates) {
 102  4
             predicate.assertMatches(endpoint.getEndpointUri() + " ", exchange);
 103  4
         }
 104  4
     }
 105  
 
 106  
     protected void addPredicate(Predicate<E> predicate) {
 107  4
         predicates.add(predicate);
 108  4
     }
 109  
 
 110  4
     public class PredicateValueBuilder extends ValueBuilder<E> {
 111  
 
 112  4
         public PredicateValueBuilder(Expression<E> expression) {
 113  4
             super(expression);
 114  4
         }
 115  
 
 116  
         protected Predicate<E> onNewPredicate(Predicate<E> predicate) {
 117  4
             addPredicate(predicate);
 118  4
             return predicate;
 119  
         }
 120  
     }
 121  
 }