1

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

}

Sunil
  • 136
  • 2
  • 12

1 Answers1

3

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.

  • Thanks Ronald for the pointer. I will go through the post and come back if more questions. – Sunil Nov 20 '17 at 03:34
  • Thanks Ronald. I went through the post and tried to come up with sample test for Provider side Message validation. I'm getting class ConfirmationKafkaMessageBuilder is not resolved error. Used below dependency, au.com.dius pact-jvm-provider-junit_2.11 3.5.4 . AND au.com.dius pact-jvm-provider-maven_2.11 3.5.4 test – Sunil Nov 20 '17 at 08:19
  • I was able to figure this out. ConfirmationKafkaMessageBuilder was a user defined function. Thanks – Sunil Nov 21 '17 at 07:24