gnodet-bot commented on code in PR #27792:
URL: https://github.com/apache/camel/pull/27792#discussion_r4281313252


##########
catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/citrus.json:
##########
@@ -0,0 +1,50 @@
+{
+  "component": {
+    "kind": "component",
+    "name": "citrus",
+    "title": "Citrus",
+    "description": "Citrus message validation component for testing",
+    "deprecated": false,
+    "firstVersion": "4.24.0",
+    "label": "testing",
+    "javaType": "org.apache.camel.component.citrus.CitrusComponent",
+    "supportLevel": "Preview",
+    "groupId": "org.apache.camel",
+    "artifactId": "camel-test-citrus",
+    "version": "4.23.0-SNAPSHOT",
+    "scheme": "citrus",
+    "extendsScheme": "",
+    "syntax": "citrus:operation\/name",
+    "async": false,
+    "api": false,
+    "consumerOnly": false,
+    "producerOnly": true,
+    "lenientProperties": false,
+    "browsable": true,
+    "remote": false
+  },
+  "componentProperties": {
+    "lazyStartProducer": { "index": 0, "kind": "property", "displayName": 
"Lazy Start Producer", "group": "producer", "label": "producer", "required": 
false, "type": "boolean", "javaType": "boolean", "deprecated": false, 
"autowired": false, "secret": false, "defaultValue": false, "description": 
"Whether the producer should be started lazy (on the first message). By 
starting lazy you can use this to allow CamelContext and routes to startup in 
situations where a producer may otherwise fail during starting and cause the 
route to fail being started. By deferring this startup to be lazy then the 
startup failure can be handled during routing messages via Camel's routing 
error handlers. Beware that when the first message is processed then creating 
and starting the producer may take a little time and prolong the total 
processing time of the processing." },
+    "log": { "index": 1, "kind": "property", "displayName": "Log", "group": 
"producer", "label": "producer", "required": false, "type": "boolean", 
"javaType": "boolean", "deprecated": false, "autowired": false, "secret": 
false, "defaultValue": false, "description": "To turn on logging when the mock 
receives an incoming message. This will log only one time at INFO level for the 
incoming message. For more detailed logging, then set the logger to DEBUG level 
for the org.apache.camel.component.mock.MockEndpoint class." },
+    "autowiredEnabled": { "index": 2, "kind": "property", "displayName": 
"Autowired Enabled", "group": "advanced", "label": "advanced", "required": 
false, "type": "boolean", "javaType": "boolean", "deprecated": false, 
"autowired": false, "secret": false, "defaultValue": true, "description": 
"Whether autowiring is enabled. This is used for automatic autowiring options 
(the option must be marked as autowired) by looking up in the registry to find 
if there is a single instance of matching type, which then gets configured on 
the component. This can be used for automatic configuring JDBC data sources, 
JMS connection factories, AWS Clients, etc." },
+    "exchangeFormatter": { "index": 3, "kind": "property", "displayName": 
"Exchange Formatter", "group": "advanced", "label": "advanced", "required": 
false, "type": "object", "javaType": "org.apache.camel.spi.ExchangeFormatter", 
"deprecated": false, "autowired": true, "secret": false, "description": "To use 
a custom ExchangeFormatter to format the Exchange into a String suitable for 
logging." }
+  },
+  "properties": {
+    "operation": { "index": 0, "kind": "path", "displayName": "Operation", 
"group": "producer", "label": "", "required": true, "type": "enum", "javaType": 
"java.lang.String", "enum": [ "validate", "receive" ], "deprecated": false, 
"deprecationNote": "", "autowired": false, "secret": false, "description": "The 
operation to perform. 'validate' validates exchanges without modifying them. 
'receive' validates exchanges and returns a simulated response." },
+    "name": { "index": 1, "kind": "path", "displayName": "Name", "group": 
"producer", "label": "", "required": true, "type": "string", "javaType": 
"java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": 
false, "secret": false, "description": "Name of mock endpoint" },

Review Comment:
   🐛 **Catalog regression: `name` is marked `required: true` but 
`CitrusEndpoint` has no `@UriPath` for it**
   
   This entry is inherited from `MockEndpoint`, which does have `@UriPath 
String name`. However `CitrusEndpoint` removed that field (latest commit 
"Remove dead name field"), and `CitrusComponent.createEndpoint()` explicitly 
allows `name` to be absent/null. The catalog incorrectly signals to tooling 
that this path parameter is mandatory.
   
   Fix: override the catalog metadata so `name` is either removed from the path 
properties or its `required` flag is set to `false`.



##########
components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusEndpoint.java:
##########
@@ -0,0 +1,253 @@
+/*
+ * 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.context.TestContext;
+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 final 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));
+        applyImpliedMinimumCount(index);
+        return messageValidationSpec;
+    }
+
+    private CitrusValidationProcessor createProcessor(MessageValidationSpec 
validationSpec) {
+        return new CitrusValidationProcessor(this::createTestContext, 
messageType, validationSpec);
+    }
+
+    private CitrusValidationProcessor createProcessor(
+            MessageValidationSpec validationSpec,
+            MessageResponseSpec responseSpec) {
+        return new CitrusValidationProcessor(this::createTestContext, 
messageType, validationSpec, responseSpec);
+    }
+
+    private TestContext createTestContext() {
+        CitrusContext ctx = citrusContext;
+        if (ctx == null) {
+            throw new IllegalStateException("CitrusEndpoint is not started");
+        }
+        return ctx.createTestContext();
+    }
+
+    // --- Gherkin-style API (for 'receive' operation) ---
+
+    public WhenSpec when() {
+        return new WhenSpec();
+    }
+
+    private void applyImpliedMinimumCount(int index) {
+        if (getExpectedCount() < 0 && getExpectedMinimumCount() < index) {
+            expectedMinimumMessageCount(index);
+        }
+    }
+
+    @Override
+    public void reset() {
+        super.reset();
+        // guard against super constructor calling reset() before field 
initializer runs
+        if (nextSpecIndex != null) {
+            nextSpecIndex.set(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));
+            applyImpliedMinimumCount(index);
+            return messageValidationSpec;
+        }

Review Comment:
   â„šī¸ **Minor: intermediate `whenExchangeReceived` registration that is 
immediately overwritten**
   
   `receive(index)` registers a validation-only processor P1 via 
`whenExchangeReceived`. If the caller chains `.then().respond()`, 
`ThenSpec.respond()` immediately overwrites it with a validation+response 
processor P2. P1 is never invoked.
   
   For the common `when().receive().then().respond()` chain this is wasteful. 
If you intend to support `when().receive()` standalone (without `then()`), a 
comment explaining this design would help future readers. If not, consider 
deferring the `whenExchangeReceived` call to `ThenSpec.respond()`/`send()` only.



-- 
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