DefaultMessageHeaderValidator extends AbstractMessageValidator<HeaderValidationContext> { @Override public void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, HeaderValidationContext validationContext) { Map<String, Object> controlHeaders = controlMessage.getHeaders(); Map<String, Object> receivedHeaders = receivedMessage.getHeaders(); if (CollectionUtils.isEmpty(controlHeaders)) { return; } log.debug("Start message header validation ..."); for (Map.Entry<String, Object> entry : controlHeaders.entrySet()) { if (MessageHeaderUtils.isSpringInternalHeader(entry.getKey()) || entry.getKey().startsWith(MessageHeaders.MESSAGE_PREFIX) || entry.getKey().equals(EndpointUriResolver.ENDPOINT_URI_HEADER_NAME) || entry.getKey().equals(EndpointUriResolver.REQUEST_PATH_HEADER_NAME) || entry.getKey().equals(EndpointUriResolver.QUERY_PARAM_HEADER_NAME)) { continue; } final String headerName = getHeaderName(entry.getKey(), receivedHeaders, context, validationContext); if (!receivedHeaders.containsKey(headerName)) { throw new ValidationException("Validation failed: Header element '" + headerName + "' is missing"); } Object controlValue = entry.getValue(); validationContext.getValidators() .stream() .filter(validator -> validator.supports(headerName, Optional.ofNullable(controlValue).map(Object::getClass).orElse(null))) .findFirst() .orElse( validationContext.getValidatorNames() .stream() .map(beanName -> { try { return context.getReferenceResolver().resolve(beanName, HeaderValidator.class); } catch (NoSuchBeanDefinitionException e) { log.warn("Failed to resolve header validator for name: " + beanName); return null; } }) .filter(Objects::nonNull) .filter(validator -> validator.supports(headerName, Optional.ofNullable(controlValue).map(Object::getClass).orElse(null))) .findFirst() .orElse( getHeaderValidators(context).stream() .filter(validator -> validator.supports(headerName, Optional.ofNullable(controlValue).map(Object::getClass).orElse(null))) .findFirst() .orElse(new DefaultHeaderValidator()) ) ).validateHeader(headerName, receivedHeaders.get(headerName), controlValue, context, validationContext); } log.info("Message header validation successful: All values OK"); }  @Override void validateMessage(Message receivedMessage, Message controlMessage, TestContext context, HeaderValidationContext validationContext); @Override boolean supportsMessageType(String messageType, Message message); void addHeaderValidator(HeaderValidator validator); List<HeaderValidator> getValidators(); void setValidators(List<HeaderValidator> validators);  }
@Test public void testValidateNoMessageHeaders() throws Exception { Message receivedMessage = new DefaultMessage("Hello World!"); Message controlMessage = new DefaultMessage("Hello World!"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test public void testValidateMessageHeaders() throws Exception { Message receivedMessage = new DefaultMessage("Hello World!") .setHeader("foo", "foo_test") .setHeader("additional", "additional") .setHeader("bar", "bar_test"); Message controlMessage = new DefaultMessage("Hello World!") .setHeader("foo", "foo_test") .setHeader("bar", "bar_test"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test public void testValidateMessageHeadersIgnoreCase() throws Exception { try { Message receivedMessage = new DefaultMessage("Hello World!") .setHeader("X-Foo", "foo_test") .setHeader("X-Additional", "additional") .setHeader("X-Bar", "bar_test"); Message controlMessage = new DefaultMessage("Hello World!") .setHeader("x-foo", "foo_test") .setHeader("x-bar", "bar_test"); validationContext.setHeaderNameIgnoreCase(true); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); } finally { validationContext.setHeaderNameIgnoreCase(false); } }
@Test(expectedExceptions = ValidationException.class) public void testValidateMessageHeadersIgnoreCaseError() throws Exception { Message receivedMessage = new DefaultMessage("Hello World!") .setHeader("X-Foo", "foo_test") .setHeader("X-Additional", "additional") .setHeader("X-Bar", "bar_test"); Message controlMessage = new DefaultMessage("Hello World!") .setHeader("x-foo", "foo_test") .setHeader("x-bar", "bar_test"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test public void testValidateMessageHeadersVariableSupport() throws Exception { Message receivedMessage = new DefaultMessage("Hello World!") .setHeader("foo", "foo_test") .setHeader("additional", "additional") .setHeader("bar", "bar_test"); Message controlMessage = new DefaultMessage("Hello World!") .setHeader("foo", "citrus:concat('foo', '_test')") .setHeader("bar", "${bar}"); context.setVariable("bar", "bar_test"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test public void testValidateMessageHeadersMatcherSupport() throws Exception { Message receivedMessage = new DefaultMessage("Hello World!") .setHeader("foo", "foo_test") .setHeader("additional", "additional") .setHeader("bar", "bar_test"); Message controlMessage = new DefaultMessage("Hello World!") .setHeader("foo", "@startsWith('foo')@") .setHeader("bar", "@endsWith('_test')@"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test(expectedExceptions = ValidationException.class) public void testValidateError() throws Exception { Message receivedMessage = new DefaultMessage("Hello World!") .setHeader("foo", "other_value") .setHeader("bar", "bar_test"); Message controlMessage = new DefaultMessage("Hello World!") .setHeader("foo", "foo_test") .setHeader("bar", "bar_test"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }
@Test(expectedExceptions = ValidationException.class) public void testValidateErrorMissingHeader() throws Exception { Message receivedMessage = new DefaultMessage("Hello World!") .setHeader("bar", "bar_test"); Message controlMessage = new DefaultMessage("Hello World!") .setHeader("foo", "foo_test") .setHeader("bar", "bar_test"); validator.validateMessage(receivedMessage, controlMessage, context, validationContext); }