mercredi 29 juillet 2020

How to verify stringified json in pact

I am trying to build a pact between two services using asynchronous communication.

This is the code I used for generate the pact:

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "provider", providerType = ProviderType.ASYNCH)
public class StringifiedPactTest {

    @Pact(consumer = "consumer", provider = "provider")
    public MessagePact generatePact(MessagePactBuilder builder) {
        return builder.hasPactWith("provider")
                .expectsToReceive("A valid aws sns event")
                .withContent(new PactDslJsonBody().stringType(new String[]{"MessageId", "TopicArn"}).stringValue("Message", new PactDslJsonBody().stringType("Value", "Foo").toString()))
                .toPact();
    }

    @Test
    @PactTestFor(pactMethod = "generatePact")
    public void buildPact(List<Message> messages) {

    }
}

And the generated pact is

{
  "consumer": {
    "name": "consumer"
  },
  "provider": {
    "name": "provider"
  },
  "messages": [
    {
      "description": "A valid aws sns event",
      "metaData": {
        "contentType": "application/json"
      },
      "contents": {
        "TopicArn": "string",
        "Message": "{\"Value\":\"Foo\"}",
        "MessageId": "string"
      },
      "matchingRules": {
        "body": {
          "$.MessageId": {
            "matchers": [
              {
                "match": "type"
              }
            ],
            "combine": "AND"
          },
          "$.TopicArn": {
            "matchers": [
              {
                "match": "type"
              }
            ],
            "combine": "AND"
          }
        }
      }
    }
  ],
  "metadata": {
    "pactSpecification": {
      "version": "3.0.0"
    },
    "pact-jvm": {
      "version": "4.0.10"
    }
  }
}

This means the producer should have a "Message" that matches {"Value" : "Foo"}, any other combination like {"Value" : "Bar" } won't be successful. Is there any way to add matching rules inside a stringified json? Thanks!

Aucun commentaire:

Enregistrer un commentaire