MessageQueue的契约:如果有MessageQueues,则示例提供程序测试 [英] Pact for MessageQueue's : Sample Provider test in case of MessageQueues

查看:122
本文介绍了MessageQueue的契约:如果有MessageQueues,则示例提供程序测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已通过参考pact仓库中给出的示例示例为MessageProvider编写了示例Pact测试。以下是消费者测试,该测试正在为Provider期望的消息生成PACT json文件。

I have written sample Pact test for MessageProvider by referring to sample example given in pact repo. Following is the consumer test which is generating the PACT json file for the message expected from Provider.

对于API,要验证PACT,我可以使用 pact-jvm-provider-maven插件来执行此操作。在这种情况下,将根据提供商的 actual 托管API服务对PACT进行验证。

In case of API, to verify the PACT, I was able to do this using "pact-jvm-provider-maven" plugin. In this case the PACT is verified against the actual hosted API service of Provider.

我的问题是,在MessageQueue中,如何验证PACT?是否会创建模拟队列?或我需要将一条消息发布到实际的Queue,并需要对照发布到Queue的该消息验证PACT消息。

My Question is, how in case of MessageQueue, PACT will be verified? Will a mock Queue gets created? or I need to publish a message to actual Queue and need to verify the PACT message against this message published to Queue.

有人可以解释它的工作原理吗?

Can someone explain how exactly it works?

还请指出要在提供者端编写的示例代码(示例测试),以验证MessageQueue的消息。

Also please point me to sample code (example test) to be written at provider end to verify the message of MessageQueue.

示例消息(消费者)测试:

Sample message (Consumer) test :

公共类Inbound_Receiving_OpenMessageTest {
专用字节[]接收OpenLoadDetailsMessage ;

public class Inbound_Receiving_OpenMessageTest { private byte[] receivingOpenLoadDetailsMessage;

@Rule
public MessagePactProviderRule mockProvider = new MessagePactProviderRule(this);

@Pact(provider = Configuration.ReceivingProviderOpen, consumer = Configuration.InboundConsumer)
public MessagePact createPact(MessagePactBuilder builder) {
    PactDslJsonBody body = (PactDslJsonBody) new PactDslJsonBody()
            .stringType("_id")
            .object("delivery")
                .stringType("deliveryNumber")
            .closeObject()
            .array("state")
                    .object()
                        .stringType("changeTime")
                        .stringValue("status", "OPEN")
                        .stringType("changeUser")
                    .closeObject()
            .closeArray();

    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("contentType", "application/json");

    return builder
            .given("Receiving(Open) App State")
            .expectsToReceive("Receiving Open Load details Test")
            .withMetadata(metadata)
            .withContent(body)
            .toPact();
}

@Test
@PactVerification({Configuration.ReceivingProviderOpen, "Receiving(Open) App State"})
public void test() throws Exception {
    Assert.assertNotNull(new String(receivingOpenLoadDetailsMessage));

    LoadDetails openLoadDetails = null;
    Gson gson = new GsonBuilder().create();
    String entity = new String(receivingOpenLoadDetailsMessage);
    openLoadDetails = gson.fromJson(entity, LoadDetails.class);

    if(openLoadDetails.getDelivery().getDeliveryNumber() == null || 
            openLoadDetails.getState().get(0).getChangeUser() == null ||
            openLoadDetails.getState().get(0).getChangeTime() == null ||
            openLoadDetails.getState().get(0).getStatus() == null){
        Assert.fail("Either one of the field 'deliveryNumber' or 'changeTime' or 'status' or 'changeUser' is NULL");
    }
}

public void setMessage(byte[] messageContents) {
    receivingOpenLoadDetailsMessage = messageContents;
}

}

推荐答案

此博客文章对其进行了详细说明。

This blog post explains it in more detail.

从本质上讲,其想法是,如果您可以验证将消息放入队列的代码是否符合合同(提供者),并且处理来自队列的消息的代码也符合合同(消费者),您实际上不需要消息队列来验证合同。

Essentially, the idea is that if you can verify that the code that puts the message onto the queue conforms to the contract (the provider), and the code that handles the message from the queue also conforms to the contract (the consumer), you don't actually need a message queue to verify the contract.

这篇关于MessageQueue的契约:如果有MessageQueues,则示例提供程序测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆