gnodet-bot commented on code in PR #27792: URL: https://github.com/apache/camel/pull/27792#discussion_r4280071873
########## 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: ⚠️ **Case-insensitive Camel headers become case-sensitive in Citrus** `new HeaderValidationContext()` validates headers with exact case. But `buildReceivedMessage` (line 98) copies `exchange.getIn().getHeaders()` into a plain `LinkedHashMap`, and the header names in the received message reflect whatever case the component set them to. The validation spec's header names (set via `.header("content-type", ...)`) must match exactly, even though Camel's own `CaseInsensitiveMap` would treat `content-type` and `Content-Type` as equal. Citrus 5.0.2 ships `HeaderValidationContext.Builder#ignoreCase(true)` for exactly this scenario: ```suggestion contexts.add(new HeaderValidationContext.Builder().ignoreCase(true).build()); ``` -- 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]
