gnodet-bot commented on code in PR #27792: URL: https://github.com/apache/camel/pull/27792#discussion_r4284731876
########## components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/MessageValidationSpec.java: ########## @@ -0,0 +1,72 @@ +/* + * 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.citrusframework.message.MessagePayloadBuilder; +import org.citrusframework.validation.context.ValidationContext; + +public class MessageValidationSpec { + + private String body; + private MessagePayloadBuilder bodyBuilder; + private final Map<String, Object> headers = new LinkedHashMap<>(); Review Comment: ⚠️ **Async risk:** these fields are read by `CitrusValidationProcessor` at exchange-processing time. If the endpoint is wired to an async consumer (SEDA, timer, or any thread-separated route), an exchange can arrive and the processor can read these fields before the test thread finishes `.body()`/`.header()` chaining — no synchronization or volatile on any of them. At minimum, document this: _"Spec setup must complete before exchanges start arriving. For async consumers (SEDA, timer-based routes), ensure all `validate()`/`when().receive()` chains are fully configured before starting the CamelContext."_ ########## components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusEndpoint.java: ########## @@ -0,0 +1,266 @@ +/* + * 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; + + @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 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()); Review Comment: ⚠️ **Index collision:** `validate()` and `WhenSpec.receive()` share the same `nextSpecIndex`. If a user calls `citrus.validate(2)` explicitly then `citrus.validate()` (auto-increment), the counter starts at 0 and increments to 1 — it has no awareness of the explicit `validate(2)` registration. Mixing explicit and auto-indexed calls can silently overwrite processor registrations at the same index. The docs say "each call to `validate()` auto-increments the index" but don't warn that mixing with explicit indices (`validate(N)`) causes collisions. Add a Javadoc note or throw if `index ≤ nextSpecIndex.get()` when called explicitly after auto-indexed calls. ########## components/camel-test/camel-test-citrus/src/main/java/org/apache/camel/component/citrus/CitrusEndpoint.java: ########## @@ -0,0 +1,266 @@ +/* + * 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; + + @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 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); + } + } + } + Review Comment: ⚠️ **Bug:** `getFailures().add(e)` fires unconditionally for both `validate` and `receive` modes. In `receive` mode, the exception should stay on the exchange so `onException` can handle it — but adding it to `getFailures()` means `assertIsSatisfied()` (or `MockEndpoint.assertIsSatisfied(context)`) will throw even when the route's error handler already managed the failure. `receiveOperationTriggersOnException()` masks this by not calling `citrus.assertIsSatisfied()` after the handled error. ```suggestion @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)) { if ("validate".equals(operation)) { // For validate-only mode, collect the error and clear the exception so it does not // propagate through the route — validation errors are collected, not thrown. getFailures().add(e); exchange.setException(null); } // For receive mode, leave the exception on the exchange so Camel's onException // handlers trigger. Do NOT add to failures — the error is handled by the route, // not by assertIsSatisfied(). } } ``` -- 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]
