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