Citrus Framework - 尝试在 JSON 中使用验证器会导致错误 header ACCEPT

Citrus Framework - Attempt to use validator in JSON causes error with header ACCEPT

我已经使用 Citrus Framework 为通过 HTTP REST 通信的 Spring 启动服务编写了一个集成测试。

我能够在 JSON 中嵌入一些 Citrus 验证 "methods" 来处理像 @isNumber()@ 这样总是在变化的时间戳的情况。然而,当我尝试其他一些时,我 运行 遇到了问题。

JSON 中还有另一个字段 (commandID),其中包含一个 UUID,并且可能会从一次调用更改为另一次调用。我首先决定使用 @match("<regex>")@ 来匹配 UUID 模式,当它产生问题时,切换到尝试使用 @ignore@,但也产生了同样的问题。

这是来自控制台的错误:

13:29:21.962 [main] ERROR com.consol.citrus.report.LoggingReporter - TEST FAILED MissionPlannerIT.testPlanMission <edu.mit.ll.mission_services.service.mission_planner> Nested exception is:
com.consol.citrus.exceptions.TestCaseFailedException: Validation failed: Values not equal for header element 'Accept', expected 'application/json' but was 'application/json,application/*+json'

我不知道这是什么问题,也不知道如何解决。

gen-route-command.json:

{
    "header": {
        "timestamp": "@isNumber()@",
        "typeID": "edu.mit.ll.mission_services.messages.GenerateRouteCommand",
        "transaction": {
            "id": 1,
            "startTime": "@isNumber()@"
        },
        "signature": {
            "algorithm": null,
            "keySize": 0,
            "keyValue": null,
            "sender": null
        }
    },
    "commandID": "@ignore@",
    "commandType": "GENERATE_ROUTE"
}

@isNumber()@ 函数起作用并允许这些元素的匹配通过验证。 @ignore@,然而并没有。我已经尝试了两种变体:@ignore@@ignore()@,都不起作用。前面说了,@match(...)@也不行。

更新:

我修改了一些东西,以便尝试在测试用例代码中进行验证。我将 JSON 中的值改回了 UUID。

runner.http(builder -> builder
    .server(routeGeneratorServer)
    .receive()
    .post("/v1/missionServices/missionPlanning/generateRoute")
    .accept(ContentType.APPLICATION_JSON.getMimeType())
    .payload(new ClassPathResource("templates/gen-route-command.json"))
    .validate("$.commandID", "@ignore@"));

gen-route-command.json:

{
    "header": {
        "timestamp": "@isNumber()@",
        "typeID": "edu.mit.ll.mission_services.messages.GenerateRouteCommand",
        "transaction": {
            "id": 1,
            "startTime": "@isNumber()@"
        },
        "signature": {
            "algorithm": null,
            "keySize": 0,
            "keyValue": null,
            "sender": null
        }
    },
    "commandID": "0710d523-43da-4f68-90c7-a2b4544a955d",
    "commandType": "GENERATE_ROUTE"
}

不幸的是,这不起作用。我得到了我最初试图用 @ignore@@match(...)@.

来缓解的错误
14:12:57.299 [main] ERROR com.consol.citrus.report.LoggingReporter - TEST FAILED MissionPlannerIT.testPlanMission <edu.mit.ll.mission_services.service.mission_planner> Nested exception is:
com.consol.citrus.exceptions.TestCaseFailedException: Failed to validate JSON text:
{"header":{"timestamp":1581016372132,"typeID":"edu.mit.ll.mission_services.messages.GenerateRouteCommand","transaction":{"id":1,"startTime":1581016372130},"signature":{"algorithm":null,"keySize":0,"keyValue":null,"sender":null}},"commandID":"9302ebde-894b-43a3-b7a3-92a158c7170e","commandType":"GENERATE_ROUTE"} Values not equal for entry: 'commandID', expected '0710d523-43da-4f68-90c7-a2b4544a955d' but was '9302ebde-894b-43a3-b7a3-92a158c7170e'

commandID 值不同,因为它每个 运行 生成一个新的 UUID。

测试中的验证似乎没有 "ignore" 值,而是 JSON 验证标记了 UUID 差异。

JSON 中嵌入的 Citrus "functions" 似乎不是问题所在。问题是我没有在消息中设置 AcceptContent-Type headers 服务正在为 Citrus 接收调用发布消息。由于接收呼叫设置为查找消息类型 application/json,因此它失败了,因为我的消息根本没有 headers(这是我的印象)。

柑橘电话:

// Set route generator to receive and validate generate route command.
runner.http(builder -> builder
    .server(routeGeneratorServer)
    .receive()
    .post("/v1/missionServices/missionPlanning/generateRoute")
    .accept(ContentType.APPLICATION_JSON.getMimeType())
    .payload(new ClassPathResource("templates/gen-route-command.json")));

它收到的 "generate route command" 消息没有 headers 定义。一旦我将 AcceptContent-Type headers 设置为 application/json,测试就可以继续了。