christophd commented on code in PR #27792: URL: https://github.com/apache/camel/pull/27792#discussion_r4285033909
########## 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: Fixed: in `receive` mode, exceptions are no longer added to `getFailures()` — they stay on the exchange for `onException` to handle, and `assertIsSatisfied()` passes. Updated `receiveOperationTriggersOnException` test to also call `citrus.assertIsSatisfied()` to pin this. Also added the redelivery counter note to the error handling docs section: each redelivery counts as a new exchange in the mock counter, so `receive(2)` applies to the retry of the first exchange. _Claude Code on behalf of @christophd_ ########## 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: Acknowledged — this is inherent to `MockEndpoint`'s `whenExchangeReceived` too. Added a NOTE to the docs: spec setup must complete before exchanges start arriving, with explicit mention of SEDA and timer-based routes. _Claude Code on behalf of @christophd_ -- 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]
