Geek
Geek

Reputation: 99

Validating if a Particular String is available in the Response Body

The Response data contains something like this. "INBOUND_MESSAGE:

HTTP/1.1 200 Content-Type:application/json Transfer-Encoding:chunked Date:Fri, 22 Sep 2017 12:18:50 GMT

{"errors":{"deviceId":{"message":"Path deviceId is required.","name":"ValidatorError","properties":{"type":"required","message":"Path {PATH} is required.","path":"deviceId","value":""},"kind":"required","path":"deviceId","value":"","$isValidatorError":true},"name":{"message":"Path name is required.","name":"ValidatorError","properties":{"type":"required","message":"Path {PATH} is required.","path":"name","value":""},"kind":"required","path":"name","value":"","$isValidatorError":true}},"_message":"devices validation failed","message":"devices validation failed: deviceId: Path deviceId is required., name: Path name is required.","name":"ValidationError"}

"

Where i need to see if i can validate against $.Message and condition has to be "deviceId` is required". I used the below piece of code however the testcase is failing since multiple Messages are Available on $.Message

            http()
                .client(IotDevice)
                .receive()
                .response(HttpStatus.OK)
                .validate("$.message", "Path `deviceId` is required.");

Is there a Way to Search the Intended String inside the Response Message.

Regards BJ

Upvotes: 0

Views: 467

Answers (1)

gucce
gucce

Reputation: 625

Clarification on JSONPath

By using the JSONPath expression $.message you are not selecting multiple message entries, but exactly the one under the root (you could select all using $..message, with two dots).

If look at your JSON data in a formatted way, you can instantly see the value of the top-most message entry is devices validation failed: deviceId: Path deviceId is required., name: Path name is required. which seems to be a concatenation of all the messages within the errors section.

Solution

So, if your goal is to validate that the string Path deviceId is required is part of the top-most message entry, you can use the following Citrus matcher in combination with your JSONPath expression (see also Citrus documentation on JSONPath and validation matchers):

<message type="json">
  <validate>
    <json-path expression="$.message" value="@contains('Path deviceId is required')@"/>
  </validate>
</message>

Formatted JSON

{
  "errors": {
    "deviceId": {
      "message": "Path deviceId is required.",
      "name": "ValidatorError",
      "properties": {
        "type": "required",
        "message": "Path {PATH} is required.",
        "path": "deviceId",
        "value": ""
      },
      "kind": "required",
      "path": "deviceId",
      "value": "",
      "$isValidatorError": true
    },
    "name": {
      "message": "Path name is required.",
      "name": "ValidatorError",
      "properties": {
        "type": "required",
        "message": "Path {PATH} is required.",
        "path": "name",
        "value": ""
      },
      "kind": "required",
      "path": "name",
      "value": "",
      "$isValidatorError": true
    }
  },
  "_message": "devices validation failed",
  "message": "devices validation failed: deviceId: Path deviceId is required., name: Path name is required.",
  "name": "ValidationError"
}

Upvotes: 1

Related Questions