V3.0合同中用于AMQ消息的stringType匹配器进行文字匹配而不是类型匹配

时间:2019-06-13 10:37:34

标签: pact pact-jvm

我有一个使用stringType匹配文件名的合同。合同还指定了类型上的匹配器,但是当我在提供程序端运行测试时,它将进行字面匹配。我已经包含了合同,调试时发送的JSON和屏幕截图。我注意到TypeMatcher是在MatchingRuleGroup中初始化的,但是没有字段。我不确定这是否正确

我尝试了3种情况:

  • stringValue(“ bestandsid”,“ 20190219_foo_20190124.txt”)->可行

  • stringType(“ bestandsid”,“ 20190101_foo_20190124.txt”)->不起作用,因为它原本希望使用20190101_foo_20190124.txt但得到的是20190219_foo_20190124.txt

  • stringType(“ bestandsid”)->不起作用。它原本期望'string'但得到了20190219_foo_20190124.txt

在提供商处使用

pact-jvm-provider-spring_2.12-版本3.6.2

我在消费者处使用

pact-jvm-consumer-junit_2.12-版本3.6.2

pact-jvm-consumer-java8_2.12-版本3.6.2

合同:

{
    "consumer": {
        "name": "Consumer-event"
    },
    "provider": {
        "name": "Provider"
    },
    "messages": [
        {
            "description": "A message notifying a newly available foo file",
            "metaData": {
                "bestandstype": "foo",
                "Content-Type": "application/json"
            },
            "contents": {
                "headers": {
                    "bestandstype": "foo",
                    "Content-Type": "application/json"
                },
                "payload": {
                    "bestandstype": "foo",
                    "bestandsid": "string"
                }
            },
            "providerStates": [
                {
                    "name": "A new FOO file is extracted"
                }
            ],
            "matchingRules": {
                "body": {
                    "$.payload$.bestandsid": {
                        "matchers": [
                            {
                                "match": "type"
                            }
                        ],
                        "combine": "AND"
                    }
                }
            }
        }
    ],
    "metadata": {
        "pactSpecification": {
            "version": "3.0.0"
        },
        "pact-jvm": {
            "version": "3.6.2"
        }
    }
}

实际事件

{
    "headers": {
        "Content-Type": "application/json",
        "bestandstype": "foo",
        "id": "6838a619-efb7-6867-28bc-eb24609aa980",
        "timestamp": 1560410882485
    },
    "payload": {
        "afleidDatum": null,
        "bestandsid": "20190219_foo_20190124.txt",
        "bestandstype": "foo",
        "bron": null,
        "planDatum": null,
        "planType": "BAR"
    }
}

消费者合同

    @Pact(provider = "DonnaFileshareReader", consumer = "DpcAdapter-event")
    public MessagePact eventNewDpcResource(MessagePactBuilder builder) {

        Map<String, String> metadata = new HashMap<>();
        metadata.put("Content-Type", "application/json");
        metadata.put("bestandstype", "dpc");

        PactDslJsonBody body = new PactDslJsonBody();
        body.object("headers")
            .stringValue("bestandstype", "dpc")
            .stringValue("Content-Type", "application/json")
            .closeObject();
        payload = new PactDslJsonBody();
        payload.stringValue("bestandstype", "dpc")
               .stringValue("bestandsid", EXAMPLE_DPC_FILE_NAME)
               .closeObject();
        body.object("payload", payload);

        return builder.given("A new DPC file is extracted")
                      .expectsToReceive("A message notifying a newly available dpc file")
                      .withMetadata(metadata)
                      .withContent(body)
                      .toPact();
    }

错误:


Verifying a pact between DpcAdapter-event and DonnaFileshareReader
  Given A new FOO file is extracted
  A message notifying a newly available foo file
2019-06-13 09:56:54.144  INFO [bar,,,] 7427 --- [           main] org.reflections.Reflections              : Reflections took 9011 ms to scan 388 urls, producing 275 keys and 35566 values 
    generates a message which
      has a matching body (FAILED)
      has matching metadata (OK)

Failures:

0) A message notifying a newly available foo file generates a message which has a matching body
      $.payload.bestandsid -> Expected 'string' but received '20190219_foo_20190124.txt'

调试 Debugging

我希望stringType与String类型匹配。

1 个答案:

答案 0 :(得分:0)

已解决。

通过将合同更改为1 PactDslJsonBody()而不是使用body(object,payload)。

新合同:

body.object("headers")
                .stringValue("bestandstype", "foo")
                .stringValue("Content-Type", "application/json")
                .closeObject();
        body.object("payload")
                .stringValue("bestandstype", "foo")
                .stringType("bestandsid", EXAMPLE_FOO_FILE_NAME)
                .closeObject();

测试中新消息的构造(尽管您也可以像原始问题中那样构造PactDslJsonBody并使用它)

        JSONObject resourceDetail = (JSONObject)body.getBody();
        JSONObject payload = resourceDetail.getJSONObject("payload");

        Message<byte[]> pactMessage = MessageBuilder
                .withPayload(payload.toString().getBytes())
                .copyHeaders(messageProvider.getMetadata())
                .build();