christophd commented on code in PR #27792:
URL: https://github.com/apache/camel/pull/27792#discussion_r4280285842


##########
components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusValidationProcessor.java:
##########
@@ -0,0 +1,223 @@
+/*
+ * 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.citrus;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.StreamCache;
+import org.citrusframework.CitrusContext;
+import org.citrusframework.context.TestContext;
+import org.citrusframework.message.DefaultMessage;
+import org.citrusframework.message.MessagePayloadBuilder;
+import org.citrusframework.message.MessagePayloadUtils;
+import org.citrusframework.message.MessageType;
+import org.citrusframework.validation.MessageValidator;
+import org.citrusframework.validation.MessageValidatorRegistry;
+import org.citrusframework.validation.context.DefaultMessageValidationContext;
+import org.citrusframework.validation.context.HeaderValidationContext;
+import org.citrusframework.validation.context.ValidationContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+class CitrusValidationProcessor implements Processor {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CitrusValidationProcessor.class);
+
+    private final CitrusContext citrusContext;
+    private final String messageType;
+    private final MessageValidationSpec spec;
+    private final MessageResponseSpec responseSpec;
+
+    CitrusValidationProcessor(CitrusContext citrusContext, String messageType,
+                              MessageValidationSpec validationSpec) {
+        this(citrusContext, messageType, validationSpec, null);
+    }
+
+    CitrusValidationProcessor(CitrusContext citrusContext, String messageType,
+                              MessageValidationSpec validationSpec,
+                              MessageResponseSpec responseSpec) {
+        this.citrusContext = citrusContext;
+        this.messageType = messageType;
+        this.spec = validationSpec;
+        this.responseSpec = responseSpec;
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        TestContext testContext = citrusContext.createTestContext();
+        propagateExchangeVariables(exchange, testContext);
+
+        try {
+            if (spec != null) {
+                DefaultMessage receivedMessage = 
buildReceivedMessage(exchange);
+                DefaultMessage controlMessage = buildControlMessage(spec, 
testContext);
+
+                String resolvedType = resolveMessageType(spec);
+                controlMessage.setType(resolvedType);
+
+                List<ValidationContext> validationContexts = 
buildValidationContexts(spec);
+                performCitrusValidation(receivedMessage, controlMessage, 
testContext, validationContexts, resolvedType);
+            }
+
+            if (responseSpec != null) {
+                applyResponse(exchange, responseSpec, testContext);
+            }
+        } finally {
+            writeBackExtractedVariables(exchange, testContext);
+        }
+    }
+
+    // --- Citrus validation helpers ---
+
+    private DefaultMessage buildReceivedMessage(Exchange exchange) {
+        Object body = exchange.getIn().getBody();
+        if (body instanceof StreamCache sc) {
+            body = exchange.getIn().getBody(String.class);
+            sc.reset();
+        }
+        Map<String, Object> headers = new 
LinkedHashMap<>(exchange.getIn().getHeaders());
+        return new DefaultMessage(body, headers);
+    }
+
+    private DefaultMessage buildControlMessage(MessageValidationSpec spec, 
TestContext testContext) {
+        Object resolvedBody = resolveBody(spec.getBody(), 
spec.getBodyBuilder(), testContext);
+        DefaultMessage controlMessage = resolvedBody != null
+                ? new DefaultMessage(resolvedBody)
+                : new DefaultMessage();
+
+        for (Map.Entry<String, Object> entry : spec.getHeaders().entrySet()) {
+            Object value = entry.getValue();
+            if (value instanceof String s) {
+                value = testContext.replaceDynamicContentInString(s);
+            }
+            controlMessage.setHeader(entry.getKey(), value);
+        }
+        return controlMessage;
+    }
+
+    private String resolveMessageType(MessageValidationSpec spec) {
+        if (messageType != null) {
+            if (MessageType.knows(messageType)) {
+                return 
MessageType.valueOf(messageType.toUpperCase(Locale.ROOT)).name();
+            } else {
+                return messageType.toUpperCase(Locale.ROOT);
+            }
+        }
+
+        String expectedBody = spec.getBody();
+        if (expectedBody != null) {
+            if (MessagePayloadUtils.isJson(expectedBody)) {
+                return MessageType.JSON.name();
+            } else if (MessagePayloadUtils.isXml(expectedBody)) {
+                return MessageType.XML.name();
+            } else if (MessagePayloadUtils.isYaml(expectedBody)) {
+                return MessageType.YAML.name();
+            }
+        }
+        return MessageType.PLAINTEXT.name();
+    }
+
+    private List<ValidationContext> 
buildValidationContexts(MessageValidationSpec spec) {
+        List<ValidationContext> contexts = new ArrayList<>();
+        contexts.add(new DefaultMessageValidationContext());
+        if (!spec.getHeaders().isEmpty()) {
+            contexts.add(new HeaderValidationContext());

Review Comment:
   Already fixed in 862ec7b (previous commit).



##########
components/camel-test/camel-test-citrus/src/main/docs/citrus-component.adoc:
##########
@@ -0,0 +1,412 @@
+= Citrus Component
+:doctitle: Citrus
+:shortname: citrus
+:artifactid: camel-test-citrus
+:description: Citrus message validation component for testing
+:since: 4.24
+:supportlevel: Preview
+:tabs-sync-option:
+:component-header: Only producer is supported
+
+*Since Camel {since}*
+
+*{component-header}*
+
+The Citrus component integrates with the https://citrusframework.org/[Citrus 
integration test framework]
+to perform message validation on exchanges arriving at a Camel endpoint. It 
allows test authors to
+verify exchange body content, headers, and path expressions (JsonPath, XPath) 
using Citrus validation
+modules.
+
+This component is intended for use in Camel route unit and integration tests.
+
+== Maven dependency
+
+[source,xml]
+----
+<dependency>
+    <groupId>org.apache.camel</groupId>
+    <artifactId>camel-test-citrus</artifactId>
+    <scope>test</scope>
+</dependency>
+----
+
+== URI format
+
+----
+citrus:operation/name[?options]
+----
+
+Where `operation` is `validate` or `receive`, and `name` is an optional 
identifier to distinguish
+multiple Citrus endpoints in the same route.
+
+* `validate` — validates exchanges without modifying them (pass-through 
validation).
+* `receive` — validates exchanges and returns a simulated response 
(request/response simulation).
+
+// component options: START
+include::partial$component-configure-options.adoc[]
+include::partial$component-endpoint-options.adoc[]
+include::partial$component-endpoint-headers.adoc[]
+// component options: END
+
+== Usage
+
+The Citrus endpoint acts as a producer-only endpoint. Routes send exchanges 
*to* `citrus:validate`,
+and the component validates each exchange using Citrus message validation.
+
+=== Validation modules
+
+The component does not bundle Citrus validation modules. You must add the 
required validation
+module dependency for your message type to your test classpath:
+
+[cols="1,2", options="header"]
+|===
+| Dependency | Description
+
+| `citrus-validation-json`
+| JSON message validation with JsonPath expressions and JSON schema support
+
+| `citrus-validation-xml`
+| XML message validation with XPath expressions, XML schema (XSD/WSDL) and 
XSLT support
+
+| `citrus-validation-text`
+| Plain text message validation including binary (stream and base64) and gzip 
support
+
+| `citrus-validation-yaml`
+| YAML message validation
+
+| `citrus-validation-binary`
+| Binary message validation with stream comparison and base64 encoding
+
+| `citrus-validation-hamcrest`
+| Hamcrest matcher assertions for use with JsonPath and XPath expressions
+|===
+
+All modules use the `org.citrusframework` group ID. Refer to the
+https://citrusframework.org/citrus/reference/html/[Citrus reference 
documentation] for detailed
+usage of each validation module.
+
+=== Citrus empowered message validation
+
+Citrus uses a powerful template-based approach to message validation: you 
provide an expected message template and
+Citrus compares it against the received message using format-aware comparison. 
This brings several
+capabilities beyond simple string equality.
+
+==== Ignore expressions
+
+Use the `@ignore@` placeholder anywhere in an expected message template to 
skip validation of dynamic
+values such as timestamps, generated IDs, or correlation tokens. The element 
must be present, but its
+value is not checked.
+
+[source,json]
+----
+{
+  "orderId": "@ignore@",
+  "timestamp": "@ignore@",
+  "status": "created"
+}
+----
+
+This works across all supported message types — in XML element and attribute 
values, JSON property values,
+and YAML property values. The ignore placeholder can also replace an entire 
sub-object or element
+including its children.
+
+For plain text messages, the `@ignore@` placeholder works on a positional 
basis — it matches a word-length
+character sequence at the exact position in the text. Use `@ignore(n)@` to 
ignore exactly _n_ characters
+when the value contains non-word characters:
+
+[source,plaintext]
+----
+Your current id is "@ignore@", it expires on 01/01/2030
+----
+
+This ignores the word following `id is "` based on the position of the ignore 
expression in the template.
+Because no character count is specified, Citrus ignores the entire word 
regardless of its length.
+The word boundary is determined by the first non-word character (whitespace, 
punctuation, etc.).
+
+[source,plaintext]
+----
+Your current id is "@ignore(12)@", it expires on @ignore(10)@
+----
+
+This ignores the next 12 characters starting at the position of the ignore 
expression in the template.
+This also includes whitespace and non-word characters (e.g. `Your current id 
is "1234/ 34567 D", it expires on 01/01/2030`).
+
+==== Validation matchers
+
+Citrus validation matchers are inline assertions embedded directly in the 
expected message template,
+surrounded by `@` markers. They work across all message formats (JSON, XML, 
YAML, plain text) and
+provide richer assertions than exact equality:
+
+[source,json]
+----
+{
+  "name": "@equalsIgnoreCase('JOHN')@",
+  "age": "@greaterThan(17)@",
+  "email": "@matches('[a-z]+@[a-z]+\\.[a-z]+')@",
+  "created": "@matchesDatePattern('yyyy-MM-dd')@",
+  "id": "@isNumber()@",
+  "token": "@startsWith('tk-')@",
+  "status": "@contains('active')@"
+}
+----
+
+You can also use `@variable('name')@` to extract the actual value into a 
Citrus test variable for
+later use, and `@assertThat(hamcrestMatcher)@` to use Hamcrest matchers inline 
(requires
+`citrus-validation-hamcrest`):
+
+[source,json]
+----
+{
+  "count": "@assertThat(allOf(greaterThan(0), lessThan(100)))@",
+  "type": "@assertThat(anyOf(equalTo(A), equalTo(B)))@"
+}
+----
+
+==== JSON validation
+
+Citrus JSON validation compares JSON object structures regardless of property 
order.
+It supports two modes configurable via `citrus.json.message.validation.strict`:
+
+* **Strict mode** (default): the received JSON must match the expected 
structure exactly — no
+  additional properties or array items are allowed.
+* **Non-strict mode**: the expected JSON can be a subset of the received 
message — additional
+  properties and array items in the received JSON are silently ignored. This 
is useful when you
+  only want to verify specific fields.
+
+JSON validation also supports:
+
+* **JsonPath expressions** for targeted field validation without specifying 
the full message body
+* **JSON schema validation** against JSON Schema V3/V4 definitions
+* **`@ignore@` and validation matchers** within JSON values
+
+==== XML validation
+
+Citrus XML validation performs a structural DOM tree comparison that correctly 
handles:
+
+* **XML namespaces** with different prefixes — the comparison is 
namespace-aware, so
+  `<ns1:element xmlns:ns1="...">` and `<ns2:element xmlns:ns2="...">` are 
treated as equivalent
+  when they share the same namespace URI.
+* **XML schema validation** against XSD and WSDL definitions to verify 
structural correctness.
+* **XPath expressions** for targeted element validation, with full namespace 
support via
+  namespace context configuration. This includes automatic namespace prefix 
resolution when the
+  prefixes differ between the control template and the received message.
+* **`@ignore@` and validation matchers** within XML element values and 
attributes.
+
+==== Plain text validation
+
+Plain text validation performs exact string comparison with support for:
+
+* **Positional `@ignore@`** to skip dynamic word-length sequences at specific 
positions.
+* **`@ignore(n)@`** to skip exactly _n_ characters (useful for values with 
non-word characters).
+* **`@variable('name')@`** to extract values at specific positions into test 
variables.
+* **Whitespace normalization** (configurable) to ignore line separator 
differences (CR, LF, CRLF)
+  or all whitespace.
+* **Gzip** and **binary/base64** content comparison via the respective message 
types.
+
+==== YAML validation
+
+YAML validation compares YAML documents structurally, similar to JSON 
validation:
+
+* **Strict/non-strict mode** — configurable via 
`citrus.yaml.message.validation.strict`.
+* **Path expressions** for ignoring specific elements in the YAML structure.
+* **`@ignore@` and validation matchers** within YAML values.
+
+=== Validation specification API
+
+The endpoint provides a fluent API for specifying validation expectations. You 
can define a single
+validation that applies to all incoming exchanges, or specify individual 
validations for each
+exchange in sequence.
+
+==== Single validation for all exchanges
+
+Use `validateAll()` to define one validation specification that is applied to 
every exchange.
+The returned `MessageValidationSpec` provides a fluent API with `body()`, 
`header()`, and
+`validate()` methods:
+
+[source,java]
+----
+CitrusEndpoint citrus = 
context.getEndpoint("citrus:validate?messageType=json", CitrusEndpoint.class);
+citrus.validateAll()
+    .body("{\"status\": \"ok\"}")
+    .header("operation", "test");
+citrus.expectedMessageCount(3);
+
+// All three exchanges are validated against the same spec
+template.sendBodyAndHeader("direct:start", "{\"status\": \"ok\"}", 
"operation", "test");
+template.sendBodyAndHeader("direct:start", "{\"status\": \"ok\"}", 
"operation", "test");
+template.sendBodyAndHeader("direct:start", "{\"status\": \"ok\"}", 
"operation", "test");
+
+citrus.assertIsSatisfied();
+----
+
+==== Per-exchange sequential validation
+
+Use `validate()` to specify different validation expectations for the 1st, 
2nd, 3rd, etc.
+exchange. Each call to `validate()` auto-increments the index, so specs are 
applied in order.
+Each incoming exchange triggers the next validation spec in sequence:
+
+[source,java]
+----
+CitrusEndpoint citrus = 
context.getEndpoint("citrus:validate?messageType=json", CitrusEndpoint.class);
+
+citrus.validate().body("{\"event\": \"created\"}").header("action", "create");
+citrus.validate().body("{\"event\": \"updated\"}").header("action", "update");
+citrus.validate().body("{\"event\": \"deleted\"}").header("action", "delete");
+citrus.expectedMessageCount(3);
+
+template.sendBodyAndHeader("direct:start", "{\"event\": \"created\"}", 
"action", "create");
+template.sendBodyAndHeader("direct:start", "{\"event\": \"updated\"}", 
"action", "update");
+template.sendBodyAndHeader("direct:start", "{\"event\": \"deleted\"}", 
"action", "delete");
+
+citrus.assertIsSatisfied();
+----
+
+You can also use `validate(index)` to target a specific exchange by its 
1-based position.
+
+When both `validate(index)` and `validateAll()` are configured, the indexed 
spec takes priority —
+it replaces `validateAll()` for that exchange entirely (the two are never 
combined). The
+`validateAll()` spec acts as a fallback for exchanges that have no matching 
indexed spec.
+
+If an exchange arrives at an index with no matching indexed spec and no 
`validateAll()` fallback,
+the endpoint records a failure. For the `validate` operation, the failure is 
collected and reported
+when `assertIsSatisfied()` is called (similar to MockEndpoint). For the 
`receive` operation, the
+failure is thrown immediately as an exception.

Review Comment:
   Fixed in 91692c4. Updated the docs to correctly describe that unmatched 
exchanges pass through silently.



##########
components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusEndpoint.java:
##########
@@ -0,0 +1,233 @@
+/*
+ * 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.citrus;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.Category;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.spi.UriPath;
+import org.citrusframework.CitrusContext;
+import org.citrusframework.message.MessagePayloadBuilder;
+import org.citrusframework.validation.context.ValidationContext;
+
+@UriEndpoint(firstVersion = "4.24.0", scheme = "citrus", title = "Citrus", 
syntax = "citrus:operation/name",
+             producerOnly = true, remote = false, category = { 
Category.TESTING })
+public class CitrusEndpoint extends MockEndpoint {
+
+    @UriPath(description = "The operation to perform. 'validate' validates 
exchanges without modifying them."
+                           + " 'receive' validates exchanges and returns a 
simulated response.",
+             enums = "validate,receive")
+    @Metadata(required = true)
+    private String operation;
+
+    @UriParam(description = "The message type for validation. Determines which 
Citrus validation module is used."
+                            + " Supported values: json, xml, yaml, plaintext.")
+    private String messageType;
+
+    private volatile CitrusContext citrusContext;
+    private volatile AtomicInteger nextSpecIndex = new AtomicInteger(0);
+
+    public CitrusEndpoint(String endpointUri, CitrusComponent component) {
+        super(endpointUri, component);
+    }
+
+    // --- Lifecycle ---
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        citrusContext = CitrusContext.create();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (citrusContext != null) {
+            citrusContext.close();
+            citrusContext = null;
+        }
+        super.doStop();
+    }
+
+    // --- Fluent validation specification API ---
+
+    public MessageValidationSpec validateAll() {
+        MessageValidationSpec messageValidationSpec = new 
MessageValidationSpec();
+        whenAnyExchangeReceived(createProcessor(messageValidationSpec));
+        return messageValidationSpec;
+    }
+
+    public MessageValidationSpec validate() {
+        return validate(nextSpecIndex.incrementAndGet());
+    }
+
+    public MessageValidationSpec validate(int index) {
+        MessageValidationSpec messageValidationSpec = new 
MessageValidationSpec();
+        whenExchangeReceived(index, createProcessor(messageValidationSpec));
+        return messageValidationSpec;
+    }
+
+    private CitrusValidationProcessor createProcessor(MessageValidationSpec 
validationSpec) {
+        return new CitrusValidationProcessor(citrusContext, messageType, 
validationSpec);
+    }
+
+    private CitrusValidationProcessor createProcessor(
+            MessageValidationSpec validationSpec,
+            MessageResponseSpec responseSpec) {
+        return new CitrusValidationProcessor(citrusContext, messageType, 
validationSpec, responseSpec);
+    }
+
+    // --- Gherkin-style API (for 'receive' operation) ---
+
+    public WhenSpec when() {
+        return new WhenSpec();
+    }
+
+    @Override
+    public void reset() {
+        super.reset();
+        nextSpecIndex = new AtomicInteger(0);
+    }
+
+    @Override
+    protected void performAssertions(Exchange exchange, Exchange copy) throws 
Exception {
+        super.performAssertions(exchange, copy);
+
+        // tryProcessing() in the superclass swallows processor exceptions and 
sets them on the
+        // exchange instead of adding them to the failures list. Capture them 
here so
+        // assertIsSatisfied() can report Citrus validation errors properly.
+        Exception e = exchange.getException();
+        if (e != null && !getFailures().contains(e)) {
+            getFailures().add(e);
+            if ("validate".equals(operation)) {
+                // For validate-only mode, clear the exception so it does not 
propagate
+                // through the route — validation errors are collected, not 
thrown.
+                exchange.setException(null);
+            }
+        }
+    }
+
+    // --- Getters and setters ---
+
+    public String getOperation() {
+        return operation;
+    }
+
+    public void setOperation(String operation) {
+        this.operation = operation;
+    }
+
+    public String getMessageType() {
+        return messageType;
+    }
+
+    public void setMessageType(String messageType) {
+        this.messageType = messageType;
+    }
+
+    // --- Gherkin-style inner classes ---
+
+    public class WhenSpec {
+
+        public WhenMessageValidationSpec receive() {
+            return receive(nextSpecIndex.incrementAndGet());
+        }
+
+        public WhenMessageValidationSpec receive(int index) {
+            WhenMessageValidationSpec messageValidationSpec = new 
WhenMessageValidationSpec(index);
+            whenExchangeReceived(index, 
createProcessor(messageValidationSpec));
+            return messageValidationSpec;
+        }
+
+        public WhenMessageValidationSpec receiveAll() {
+            WhenMessageValidationSpec messageValidationSpec = new 
WhenMessageValidationSpec(-1);
+            whenAnyExchangeReceived(createProcessor(messageValidationSpec));
+            return messageValidationSpec;
+        }
+    }
+
+    public class WhenMessageValidationSpec extends MessageValidationSpec {
+
+        private final int index;
+
+        WhenMessageValidationSpec(int index) {
+            this.index = index;
+        }
+
+        @Override
+        public WhenMessageValidationSpec body(MessagePayloadBuilder 
bodyBuilder) {
+            super.body(bodyBuilder);
+            return this;
+        }
+
+        @Override
+        public WhenMessageValidationSpec body(String body) {
+            super.body(body);
+            return this;
+        }
+
+        @Override
+        public WhenMessageValidationSpec header(String headerName, Object 
value) {
+            super.header(headerName, value);
+            return this;
+        }
+
+        @Override
+        public MessageValidationSpec validate(ValidationContext.Builder<?, 
?>... validationContexts) {
+            super.validate(validationContexts);
+            return this;
+        }

Review Comment:
   Fixed in 91692c4. Return type now correctly returns 
WhenMessageValidationSpec for fluent chaining.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to