ReceiveMessageAction extends AbstractTestAction { protected void validateMessage(Message message, TestContext context) { for (VariableExtractor variableExtractor : variableExtractors) { variableExtractor.extractVariables(message, context); } if (validationCallback != null) { if (StringUtils.hasText(message.getName())) { context.getMessageStore().storeMessage(message.getName(), message); } else { context.getMessageStore().storeMessage(context.getMessageStore().constructMessageName(this, getOrCreateEndpoint(context)), message); } validationCallback.validate(message, context); } else { Message controlMessage = createControlMessage(context, messageType); if (StringUtils.hasText(controlMessage.getName())) { context.getMessageStore().storeMessage(controlMessage.getName(), message); } else { context.getMessageStore().storeMessage(context.getMessageStore().constructMessageName(this, getOrCreateEndpoint(context)), message); } if (!CollectionUtils.isEmpty(validators)) { for (MessageValidator<? extends ValidationContext> messageValidator : validators) { messageValidator.validateMessage(message, controlMessage, context, validationContexts); } if (validators.parallelStream() .map(Object::getClass) .noneMatch(DefaultMessageHeaderValidator.class::isAssignableFrom)) { MessageValidator<?extends ValidationContext> defaultMessageHeaderValidator = context.getMessageValidatorRegistry().getDefaultMessageHeaderValidator(); if (defaultMessageHeaderValidator != null) { defaultMessageHeaderValidator.validateMessage(message, controlMessage, context, validationContexts); } } } else { List<MessageValidator<? extends ValidationContext>> validators = context.getMessageValidatorRegistry().findMessageValidators(messageType, message); if (validators.isEmpty()) { if (controlMessage.getPayload() instanceof String && StringUtils.hasText(controlMessage.getPayload(String.class))) { throw new CitrusRuntimeException(String.format("Unable to find proper message validator for message type '%s' and validation contexts '%s'", messageType, validationContexts)); } else if (validationContexts.stream().anyMatch(item -> JsonPathMessageValidationContext.class.isAssignableFrom(item.getClass()) || XpathMessageValidationContext.class.isAssignableFrom(item.getClass()) || ScriptValidationContext.class.isAssignableFrom(item.getClass()))) { throw new CitrusRuntimeException(String.format("Unable to find proper message validator for message type '%s' and validation contexts '%s'", messageType, validationContexts)); } else { log.warn(String.format("Unable to find proper message validator for message type '%s' and validation contexts '%s'", messageType, validationContexts)); } } for (MessageValidator<? extends ValidationContext> messageValidator : validators) { messageValidator.validateMessage(message, controlMessage, context, validationContexts); } } } } ReceiveMessageAction(ReceiveMessageActionBuilder<?, ?> builder); @Override void doExecute(TestContext context); @Override boolean isDisabled(TestContext context); Endpoint getOrCreateEndpoint(TestContext context); Endpoint getEndpoint(); String getEndpointUri(); List<VariableExtractor> getVariableExtractors(); List<MessageProcessor> getMessageProcessors(); String getMessageType(); Map<String, Object> getMessageSelectorMap(); String getMessageSelector(); long getReceiveTimeout(); List<MessageValidator<? extends ValidationContext>> getValidators(); List<ValidationContext> getValidationContexts(); ValidationCallback getValidationCallback(); DataDictionary<?> getDataDictionary(); MessageContentBuilder getMessageBuilder();  }
@Test public void testDisabledReceiveMessage() { TestCase testCase = new DefaultTestCase(); TestActor disabledActor = new TestActor(); disabledActor.setDisabled(true); PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder(); controlMessageBuilder.setPayloadData("<TestRequest><Message>Hello World!</Message></TestRequest>"); reset(endpoint, consumer, endpointConfiguration); when(endpoint.createConsumer()).thenReturn(consumer); when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration); when(endpointConfiguration.getTimeout()).thenReturn(5000L); when(endpoint.getActor()).thenReturn(null); doAnswer(invocationOnMock -> { Message received = invocationOnMock.getArgument(0); Message control = invocationOnMock.getArgument(1); List<ValidationContext> validationContextList = invocationOnMock.getArgument(3); Assert.assertEquals(received.getPayload(String.class).trim(), control.getPayload(String.class).trim()); new DefaultMessageHeaderValidator().validateMessage(received, control, context, validationContextList); return null; }).when(validator).validateMessage(any(Message.class), any(Message.class), eq(context), any(List.class)); ReceiveMessageAction receiveAction = new ReceiveMessageAction.Builder() .endpoint(endpoint) .actor(disabledActor) .message(controlMessageBuilder) .build(); testCase.addTestAction(receiveAction); testCase.execute(context); }
@Test public void testDisabledReceiveMessageByEndpointActor() { TestCase testCase = new DefaultTestCase(); TestActor disabledActor = new TestActor(); disabledActor.setDisabled(true); PayloadTemplateMessageBuilder controlMessageBuilder = new PayloadTemplateMessageBuilder(); controlMessageBuilder.setPayloadData("<TestRequest><Message>Hello World!</Message></TestRequest>"); reset(endpoint, consumer, endpointConfiguration); when(endpoint.createConsumer()).thenReturn(consumer); when(endpoint.getEndpointConfiguration()).thenReturn(endpointConfiguration); when(endpointConfiguration.getTimeout()).thenReturn(5000L); when(endpoint.getActor()).thenReturn(disabledActor); doAnswer(invocationOnMock -> { Message received = invocationOnMock.getArgument(0); Message control = invocationOnMock.getArgument(1); List<ValidationContext> validationContextList = invocationOnMock.getArgument(3); Assert.assertEquals(received.getPayload(String.class).trim(), control.getPayload(String.class).trim()); new DefaultMessageHeaderValidator().validateMessage(received, control, context, validationContextList); return null; }).when(validator).validateMessage(any(Message.class), any(Message.class), eq(context), any(List.class)); ReceiveMessageAction receiveAction = new ReceiveMessageAction.Builder() .endpoint(endpoint) .message(controlMessageBuilder) .build(); testCase.addTestAction(receiveAction); testCase.execute(context); }