Pact for MessageQueue's : Sample Provider test in case of MessageQueues

只愿长相守 提交于 2020-07-18 08:00:30

问题


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.

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.

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?

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 :

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;
}

}


回答1:


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.



来源:https://stackoverflow.com/questions/47365835/pact-for-messagequeues-sample-provider-test-in-case-of-messagequeues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!