Citrus Framework - 尝试在 JSON 中使用验证器会导致标头 ACCEPT 出错



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

我能够在 JSON 中嵌入一些 Citrus 验证"方法"来处理@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。

测试中的验证似乎不会"忽略"该值,而是 JSON 验证标记 UUID 差异。

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

柑橘的呼唤:

// 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")));

它收到的"生成路由命令"消息没有定义标头。一旦我将AcceptContent-Type标头设置为application/json,测试就可以继续了。

最新更新