This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5308773e385397159c19175cfe591da56d3a434c Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Oct 31 16:58:05 2024 +0100 CAMEL-21402: Added unit test how to test with xpath in mock endpoints --- .../java/org/apache/camel/ContextTestSupport.java | 14 +++++ .../camel/issues/MockExpectedHeaderXPathTest.java | 61 ++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/core/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java b/core/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java index c4972b3a267..cff391bb199 100644 --- a/core/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java +++ b/core/camel-core/src/test/java/org/apache/camel/ContextTestSupport.java @@ -29,6 +29,7 @@ import javax.naming.Context; import org.apache.camel.builder.AdviceWith; import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.builder.LanguageBuilderFactory; import org.apache.camel.builder.NotifyBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.InterceptSendToMockEndpointStrategy; @@ -288,6 +289,19 @@ public abstract class ContextTestSupport extends TestSupport return consumer; } + /** + * A utility method allowing to build any language using a fluent syntax as shown in the next example: + * + * <pre> + * var exp = expression().tokenize().token("\n").end() + * </pre> + * + * @return an entry point to the builder of all supported languages. + */ + public LanguageBuilderFactory expression() { + return new LanguageBuilderFactory(); + } + /** * Allows a service to be registered a separate lifecycle service to start and stop the context; such as for Spring * when the ApplicationContext is started and stopped, rather than directly stopping the CamelContext diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/MockExpectedHeaderXPathTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/MockExpectedHeaderXPathTest.java new file mode 100644 index 00000000000..0d71d912634 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/issues/MockExpectedHeaderXPathTest.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.issues; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Test; + +import static org.apache.camel.support.builder.PredicateBuilder.not; + +public class MockExpectedHeaderXPathTest extends ContextTestSupport { + + protected String matchingBody = "<person name='James' city='London'/>"; + protected String notMatchingBody = "<person name='Hiram' city='Tampa'/>"; + protected String notMatchingBody2 = "<person name='Jack' city='Houston'/>"; + + @Test + public void testHeaderXPath() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(3); + + // xpath that takes input from a header + var xpath = expression().xpath().expression("/person[@name='James']").source("header:cheese").end(); + + // validate that some of the headers match and others do not + mock.message(0).predicate(not(xpath)); + mock.message(1).predicate(xpath); + mock.message(2).predicate(not(xpath)); + + template.sendBodyAndHeader("direct:test", "message 1", "cheese", notMatchingBody); + template.sendBodyAndHeader("direct:test", "message 2", "cheese", matchingBody); + template.sendBodyAndHeader("direct:test", "message 3", "cheese", notMatchingBody2); + + mock.assertIsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:test").to("mock:result"); + } + }; + } +}