0

我正在发送一条生成多条消息的消息,其中两条消息到达同一个 JMS 端点。

runner.send(sendMessageBuilder -> sendMessageBuilder.endpoint(inputMessage.getEndpoint())
    .messageType(MessageType.XML)
    .payload(inputMessage.getPayload())
    .header(JMSOUTPUTCORRELATIONID, correlationId));

for(OutputMessage outputMessage : inputMessage.getOutputMessages()) {
     runner.receive(receiveMessageBuilder -> receiveMessageBuilder.endpoint(outputMessage.getEndpoint())
        .schemaValidation(false)
        .payload(outputMessage.getPayload())
        .header(JMSOUTPUTCORRELATIONID, correlationId));
}

在同一端点上验证两条消息时,我无法找到将它们与各自的预期输出相匹配的方法。我想知道 Citrus 是否有内置的方法来做到这一点,或者如果第一个失败,我是否可以建立一个检查其他预期输出的条件。

4

2 回答 2

0

我添加了一个自定义验证器。

List<OutputMessage> outputMessages = inputMessage.getOutputMessages();
while(outputMessages.size() > 0) {
    OutputMessage outputMessage = outputMessages.get(0);
    runner.receive(receiveMessageBuilder -> receiveMessageBuilder.endpoint(outputMessage.getEndpoint())
        .schemaValidation(true)
        .validator(new MultipleOutputMessageValidator(outputMessages))
        .header(JMSOUTPUTCORRELATIONID, correlationId));
}

验证器提供了尚未验证的预期输出列表。然后它将尝试根据接收到的消息验证列表中的每个预期输出,如果验证成功,则从列表中删除该预期输出。

public class MultipleOutputMessageValidator extends DomXmlMessageValidator {
    private static Logger log = LoggerFactory.getLogger(MultipleOutputMessageValidator.class);
    private List<OutputMessage> controlMessages;

    public MultipleOutputMessageValidator(List<OutputMessage> controlMessages) {
        this.controlMessages = controlMessages;
    }

    @Override
    public void validateMessagePayload(Message receivedMessage, Message controlMessage, XmlMessageValidationContext validationContext, TestContext context) throws ValidationException {
        Boolean isValidated = false;
        for (OutputMessage message : this.controlMessages) {
            try {
                super.validateMessagePayload(receivedMessage, message, validationContext, context);
                isValidated = true;
                controlMessages.remove(message);
                break;
            } catch (ValidationException e) {
                // Do nothing for now
            }
        }
        if (!isValidated) {
            throw new ValidationException("None of the messages validated");
        }
    }
}
于 2017-06-08T08:03:26.783 回答
0

您应该使用 JMS 消息选择器,以便您可以根据技术标识符从该队列中“挑选”一条消息。例如,此选择器可以是 JMS 消息标头(在您的情况下为标头JMSOUTPUTCORRELATIONID)。通过这种方式,您可以确保首先收到要验证的消息。

示例用法:

receive(action -> action.endpoint(someEndpoint)
            .selector("correlationId='Cx1x123456789' AND operation='getOrders'"));

此处描述了 Citrus 消息选择器支持

于 2017-06-08T08:57:21.900 回答