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


##########
components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusComponent.java:
##########
@@ -0,0 +1,47 @@
+/*
+ * 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.Map;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.spi.annotations.Component;
+import org.apache.camel.support.DefaultComponent;
+
+@Component("citrus")
+public class CitrusComponent extends DefaultComponent {
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
+        CitrusEndpoint endpoint = new CitrusEndpoint(uri, this);
+
+        String operation;
+        String name = null;
+        int idx = remaining.indexOf('/');
+        if (idx > 0) {
+            operation = remaining.substring(0, idx);
+            name = remaining.substring(idx + 1);
+        } else {
+            operation = remaining;
+        }
+
+        endpoint.setOperation(operation);

Review Comment:
   ⚠️ **Missing validation for `operation`**
   
   `operation` is set directly from the URI path with no guard against invalid 
values. A URI like `citrus:badvalue/name` will silently produce a 
non-functional endpoint and only fail at runtime inside `process()` when 
neither `"validate".equals(...)` nor `"receive".equals(...)` match — with no 
helpful error message.
   
   Consider validating early:
   ```suggestion
           if (!"validate".equals(operation) && !"receive".equals(operation)) {
               throw new IllegalArgumentException(
                   "Invalid citrus operation: '" + operation + "'. Valid values 
are: validate, receive");
           }
           endpoint.setOperation(operation);
   ```



##########
components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusProducer.java:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.citrus.CitrusEndpoint.MessageResponseSpec;
+import org.apache.camel.component.citrus.CitrusEndpoint.MessageValidationSpec;
+import org.apache.camel.support.DefaultProducer;
+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;
+
+public class CitrusProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CitrusProducer.class);
+
+    private volatile CitrusContext citrusContext;
+
+    public CitrusProducer(CitrusEndpoint endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public CitrusEndpoint getEndpoint() {
+        return (CitrusEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        citrusContext = CitrusContext.create();
+    }
+
+    @Override
+    public void process(Exchange exchange) throws Exception {
+        CitrusEndpoint endpoint = getEndpoint();
+
+        try {
+            int index = endpoint.nextExchangeIndex();
+            MessageValidationSpec spec = endpoint.resolveValidationSpec(index);
+            TestContext testContext = citrusContext.createTestContext();
+            propagateExchangeProperties(exchange, testContext);
+
+            if (spec != null) {
+                DefaultMessage receivedMessage = 
buildReceivedMessage(exchange);
+                DefaultMessage controlMessage = buildControlMessage(spec, 
testContext);
+
+                String messageType = resolveMessageType(endpoint, spec);
+                controlMessage.setType(messageType);
+
+                List<ValidationContext> validationContexts = 
buildValidationContexts(spec);
+
+                performValidation(receivedMessage, controlMessage, 
testContext, validationContexts, messageType);
+            }
+

Review Comment:
   ⚠️ **Null `spec` causes silent validation skip**
   
   When `resolveValidationSpec(index)` returns `null` (no validation spec 
configured at all), the entire validation block is silently bypassed. The 
exchange passes through as if it were valid, with no log, no warning, and no 
`ValidationException`. For a test-assertion component this is a dangerous 
default — a test that forgot to configure expectations would pass vacuously.
   
   Consider logging a warning for the unconfigured case:
   ```suggestion
               if (spec != null) {
                   DefaultMessage receivedMessage = 
buildReceivedMessage(exchange);
                   DefaultMessage controlMessage = buildControlMessage(spec, 
testContext);
   
                   String messageType = resolveMessageType(endpoint, spec);
                   controlMessage.setType(messageType);
   
                   List<ValidationContext> validationContexts = 
buildValidationContexts(spec);
   
                   performValidation(receivedMessage, controlMessage, 
testContext, validationContexts, messageType);
               } else {
                   LOG.warn("No validation spec configured for endpoint '{}' at 
exchange index {} — skipping validation",
                           endpoint.getEndpointUri(), index);
               }
   ```



##########
components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusEndpoint.java:
##########
@@ -0,0 +1,405 @@
+/*
+ * 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.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.Category;
+import org.apache.camel.Consumer;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+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.apache.camel.support.DefaultEndpoint;
+import org.citrusframework.exceptions.ValidationException;
+import org.citrusframework.message.MessagePayloadBuilder;
+import org.citrusframework.validation.context.ValidationContext;
+
+@UriEndpoint(firstVersion = "4.23.0", scheme = "citrus", title = "Citrus", 
syntax = "citrus:operation/name",
+             producerOnly = true, remote = false, category = { 
Category.TESTING })
+public class CitrusEndpoint extends DefaultEndpoint {
+
+    private final AtomicInteger exchangeIndex = new AtomicInteger();
+    private final AtomicInteger receivedCount = new AtomicInteger();
+    private final List<Exchange> receivedExchanges = new 
CopyOnWriteArrayList<>();
+    private final List<Throwable> failures = new CopyOnWriteArrayList<>();
+
+    private volatile CountDownLatch latch;
+
+    @UriPath(description = "The operation to perform. 'validate' validates 
exchanges without modifying them."
+                           + " 'receive' validates exchanges and returns a 
simulated response.")
+    @Metadata(required = true)
+    private String operation;

Review Comment:
   💡 **`@UriPath` on `operation` should declare `enums`**
   
   The valid values (`validate`, `receive`) are documented in the description 
string, but the Camel metadata layer also supports an `enums` attribute on 
`@UriPath` to enumerate allowed values. Without it, the component catalog and 
IDE tooling cannot validate the parameter at configuration time.
   
   ```suggestion
       @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;
   ```



##########
components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusProducer.java:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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.Map;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.component.citrus.CitrusEndpoint.MessageResponseSpec;
+import org.apache.camel.component.citrus.CitrusEndpoint.MessageValidationSpec;
+import org.apache.camel.support.DefaultProducer;
+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;
+
+public class CitrusProducer extends DefaultProducer {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(CitrusProducer.class);
+
+    private volatile CitrusContext citrusContext;
+
+    public CitrusProducer(CitrusEndpoint endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    public CitrusEndpoint getEndpoint() {
+        return (CitrusEndpoint) super.getEndpoint();
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        citrusContext = CitrusContext.create();
+    }
+

Review Comment:
   ⚠️ **`CitrusContext` created in `doStart()` but never closed**
   
   `CitrusContext` likely holds Citrus-internal resources (thread pools, 
validator registries). Creating it in `doStart()` without a matching `doStop()` 
to call `citrusContext.close()` leaks those resources when the Camel context 
stops. This is especially visible in test suites that start and stop Camel 
contexts repeatedly.
   
   ```suggestion
       @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();
       }
   ```



##########
components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusEndpoint.java:
##########
@@ -0,0 +1,405 @@
+/*
+ * 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.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.Category;
+import org.apache.camel.Consumer;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+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.apache.camel.support.DefaultEndpoint;
+import org.citrusframework.exceptions.ValidationException;
+import org.citrusframework.message.MessagePayloadBuilder;
+import org.citrusframework.validation.context.ValidationContext;
+
+@UriEndpoint(firstVersion = "4.23.0", scheme = "citrus", title = "Citrus", 
syntax = "citrus:operation/name",
+             producerOnly = true, remote = false, category = { 
Category.TESTING })
+public class CitrusEndpoint extends DefaultEndpoint {
+
+    private final AtomicInteger exchangeIndex = new AtomicInteger();
+    private final AtomicInteger receivedCount = new AtomicInteger();
+    private final List<Exchange> receivedExchanges = new 
CopyOnWriteArrayList<>();
+    private final List<Throwable> failures = new CopyOnWriteArrayList<>();
+
+    private volatile CountDownLatch latch;
+
+    @UriPath(description = "The operation to perform. 'validate' validates 
exchanges without modifying them."
+                           + " 'receive' validates exchanges and returns a 
simulated response.")
+    @Metadata(required = true)
+    private String operation;
+
+    @UriPath(description = "A unique name to identify this endpoint. Allows 
multiple Citrus endpoints"
+                           + " with different validation expectations in the 
same route.")
+    private String name;
+
+    @UriParam(description = "The message type for validation. Determines which 
Citrus validation module is used."
+                            + " Supported values: json, xml, yaml, plaintext.")
+    private String messageType;
+
+    private MessageValidationSpec allMessagesSpec;
+    private final Map<Integer, MessageValidationSpec> indexedSpecs = new 
LinkedHashMap<>();
+    private int nextSpecIndex;
+
+    private MessageResponseSpec allMessagesResponseSpec;
+    private final Map<Integer, MessageResponseSpec> indexedResponseSpecs = new 
LinkedHashMap<>();
+    private int nextResponseSpecIndex;
+
+    private int expectedCount = -1;

Review Comment:
   ⚠️ **Shared mutable state fields are not `volatile`**
   
   `latch` (line 51) is correctly declared `volatile`, but `allMessagesSpec`, 
`allMessagesResponseSpec`, `nextSpecIndex`, `nextResponseSpecIndex`, and 
`expectedCount` are plain fields. When test setup code (thread A) writes to 
these and the Camel routing thread (thread B) reads them in `process()`, the 
JMM does not guarantee visibility without either `volatile` or explicit 
synchronization.
   
   In practice, test frameworks often configure the endpoint on the test thread 
and trigger Camel exchanges on a separate thread (timer, async dispatch, etc.) 
— exactly the scenario where this matters.
   
   ```suggestion
       private volatile MessageValidationSpec allMessagesSpec;
       private final Map<Integer, MessageValidationSpec> indexedSpecs = new 
LinkedHashMap<>();
       private volatile int nextSpecIndex;
   
       private volatile MessageResponseSpec allMessagesResponseSpec;
       private final Map<Integer, MessageResponseSpec> indexedResponseSpecs = 
new LinkedHashMap<>();
       private volatile int nextResponseSpecIndex;
   
       private volatile int expectedCount = -1;
   ```



##########
components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusEndpoint.java:
##########
@@ -0,0 +1,405 @@
+/*
+ * 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.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.Category;
+import org.apache.camel.Consumer;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+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.apache.camel.support.DefaultEndpoint;
+import org.citrusframework.exceptions.ValidationException;
+import org.citrusframework.message.MessagePayloadBuilder;
+import org.citrusframework.validation.context.ValidationContext;
+
+@UriEndpoint(firstVersion = "4.23.0", scheme = "citrus", title = "Citrus", 
syntax = "citrus:operation/name",
+             producerOnly = true, remote = false, category = { 
Category.TESTING })
+public class CitrusEndpoint extends DefaultEndpoint {
+
+    private final AtomicInteger exchangeIndex = new AtomicInteger();
+    private final AtomicInteger receivedCount = new AtomicInteger();
+    private final List<Exchange> receivedExchanges = new 
CopyOnWriteArrayList<>();
+    private final List<Throwable> failures = new CopyOnWriteArrayList<>();
+
+    private volatile CountDownLatch latch;
+
+    @UriPath(description = "The operation to perform. 'validate' validates 
exchanges without modifying them."
+                           + " 'receive' validates exchanges and returns a 
simulated response.")
+    @Metadata(required = true)
+    private String operation;
+
+    @UriPath(description = "A unique name to identify this endpoint. Allows 
multiple Citrus endpoints"
+                           + " with different validation expectations in the 
same route.")
+    private String name;
+
+    @UriParam(description = "The message type for validation. Determines which 
Citrus validation module is used."
+                            + " Supported values: json, xml, yaml, plaintext.")
+    private String messageType;
+
+    private MessageValidationSpec allMessagesSpec;
+    private final Map<Integer, MessageValidationSpec> indexedSpecs = new 
LinkedHashMap<>();
+    private int nextSpecIndex;
+
+    private MessageResponseSpec allMessagesResponseSpec;
+    private final Map<Integer, MessageResponseSpec> indexedResponseSpecs = new 
LinkedHashMap<>();
+    private int nextResponseSpecIndex;
+
+    private int expectedCount = -1;
+
+    public CitrusEndpoint(String endpointUri, CitrusComponent component) {
+        super(endpointUri, component);
+    }
+
+    @Override
+    public Consumer createConsumer(Processor processor) throws Exception {
+        throw new UnsupportedOperationException("You cannot consume from this 
endpoint");
+    }
+
+    @Override
+    public Producer createProducer() throws Exception {
+        return new CitrusProducer(this);
+    }
+
+    // --- Fluent validation specification API (for 'validate' operation) ---
+
+    public MessageValidationSpec allMessages() {
+        if (allMessagesSpec == null) {
+            allMessagesSpec = new MessageValidationSpec();
+        }
+        return allMessagesSpec;
+    }
+
+    public MessageValidationSpec message() {
+        return message(nextSpecIndex++);
+    }
+
+    public MessageValidationSpec message(int index) {
+        return indexedSpecs.computeIfAbsent(index, k -> new 
MessageValidationSpec());
+    }
+
+    // --- Gherkin-style API (for 'receive' operation) ---
+
+    public WhenSpec when() {
+        return new WhenSpec();
+    }
+
+    public ThenSpec then() {
+        return new ThenSpec();
+    }
+
+    public CitrusEndpoint expectedMessageCount(int count) {
+        this.expectedCount = count;
+        this.latch = new CountDownLatch(count);
+        return this;
+    }
+

Review Comment:
   ⚠️ **`expectedMessageCount()` does not reset counters**
   
   If this method is called after exchanges have already been received (e.g., 
without calling `reset()` between test phases), `receivedCount` still holds the 
accumulated value from prior exchanges. `assertIsSatisfied()` then compares the 
stale count against the new `expectedCount`, producing a misleading assertion 
failure.
   
   `reset()` already resets `exchangeIndex` and `receivedCount` properly — 
`expectedMessageCount()` should do the same:
   ```suggestion
       public CitrusEndpoint expectedMessageCount(int count) {
           this.expectedCount = count;
           this.latch = new CountDownLatch(count);
           this.exchangeIndex.set(0);
           this.receivedCount.set(0);
           return this;
       }
   ```



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