This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push: new 5f49ccb CAMEL-12010: Mock endpoint - Should reset StreamCache when evaluating expecations 5f49ccb is described below commit 5f49ccb22d781d1be35d64c590522c8694d8877a Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Nov 15 10:38:05 2017 +0100 CAMEL-12010: Mock endpoint - Should reset StreamCache when evaluating expecations --- .../camel/component/mock/AssertionClause.java | 9 ++++ .../component/mock/MockStreamCachingTest.java | 61 ++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java b/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java index f3cb263..d79e92d 100644 --- a/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java +++ b/camel-core/src/main/java/org/apache/camel/component/mock/AssertionClause.java @@ -23,6 +23,7 @@ import java.util.Set; import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.Predicate; +import org.apache.camel.StreamCache; import org.apache.camel.builder.ExpressionClause; import org.apache.camel.builder.ExpressionClauseSupport; import org.apache.camel.builder.ValueBuilder; @@ -103,6 +104,14 @@ public abstract class AssertionClause extends ExpressionClauseSupport<ValueBuild protected void applyAssertionOn(MockEndpoint endpoint, int index, Exchange exchange) { for (Predicate predicate : predicates) { currentIndex = index; + + Object value = exchange.hasOut() ? exchange.getOut().getBody() : exchange.getIn().getBody(); + // if the value is StreamCache then ensure its readable before evaluating any predicates + // by resetting it (this is also what StreamCachingAdvice does) + if (value instanceof StreamCache) { + ((StreamCache) value).reset(); + } + PredicateAssertHelper.assertMatches(predicate, "Assertion error at index " + index + " on mock " + endpoint.getEndpointUri() + " with predicate: ", exchange); } } diff --git a/camel-core/src/test/java/org/apache/camel/component/mock/MockStreamCachingTest.java b/camel-core/src/test/java/org/apache/camel/component/mock/MockStreamCachingTest.java new file mode 100644 index 0000000..fc35555 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/mock/MockStreamCachingTest.java @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.mock; + +import java.io.ByteArrayInputStream; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; + +public class MockStreamCachingTest extends ContextTestSupport { + + public void testMockStreamCaching() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(1); + getMockEndpoint("mock:result").message(0).body(String.class).contains("Camel"); + getMockEndpoint("mock:result").message(0).body(String.class).contains("World"); + + Object body = new ByteArrayInputStream("Hello Camel and Bye World".getBytes()); + + template.sendBody("direct:start", body); + + assertMockEndpointsSatisfied(); + } + + public void testMockStreamCachingConvertTo() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(1); + getMockEndpoint("mock:result").message(0).body().convertToString().contains("Camel"); + getMockEndpoint("mock:result").message(0).body().convertToString().contains("World"); + + Object body = new ByteArrayInputStream("Hello Camel and Bye World".getBytes()); + + template.sendBody("direct:start", body); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .streamCaching() + .to("mock:result"); + } + }; + } +} -- To stop receiving notification emails like this one, please contact ['"commits@camel.apache.org" <commits@camel.apache.org>'].