Roshan Raut
Roshan Raut

Reputation: 33

Writing groovy contract for spring cloud contract testing

I am new to contract testing and want to write a groovy contract for a response which contains sub objects with strings list of dictionaries and so on. I have a response like this:

{
    "data": [
      {
        "categories": [
          {
            "categories": [],
            "oid": "abc",
            "type": "xyz"
          },
          {
            "categories": [],
            "oid": "abb",
            "type": "xyy"
          }
        ],
        "oid": "ab"
      }
    ],
    "meta": {
      "datatype": "text",
      "language": "x",
      "market": "qw",
      "provider": "AFP",
      "status": "ok",
      "statusInfo": {},
      "supportedLanguages": [
        "x"
      ]
    }
  }

and for that I have written the following contract:

Contract.make {
    request {
        method 'GET'
        url '/foo'
    }
    response {
        status 200
        body(
                 "data"  : [
                     (
                        "categories": [
                            (
                            "categories" : [],
                            "oid" : anyNonEmptyString(),
                            "type" : "xyz"
                            ),
                            (
                            "categories" : [],
                            "oid" : anyNonEmptyString(),
                            "type" : "xyy"
                            )
                        ]
                        "oid" : regex('\w')
                     )
                 ],
                 "meta" : (
                    "datatype": "text",
                    "language": "x",
                    "market": "qw",
                    "provider": "AFP",
                    "status": "ok",
                    "statusInfo": (),
                    "supportedLanguages": ["x"]
                 )
        )
        headers {
            contentType(applicationJson())
        }
    }
}

But is not working properly. Can you help me to know what I am doing wrong over here.

Upvotes: 1

Views: 1629

Answers (1)

Marcin Grzejszczak
Marcin Grzejszczak

Reputation: 11179

Your contract is invalid. You're using parentheses where you should be using brackets

Contract.make {
    request {
        method 'GET'
        url '/foo'
    }
    response {
        status 200
        body(
                 "data"  : [
                     (
                        "categories": [
                            [ // was (
                            "categories" : [],
                            "oid" : anyNonEmptyString(),
                            "type" : "xyz"
                            ], // was )
                            [ // was (
                            "categories" : [],
                            "oid" : anyNonEmptyString(),
                            "type" : "xyy"
                            ] // was )
                        ]
                        "oid" : regex('\w')
                     )
                 ],
                 "meta" : [ // was (
                    "datatype": "text",
                    "language": "x",
                    "market": "qw",
                    "provider": "AFP",
                    "status": "ok",
                    "statusInfo": [:], // was ()
                    "supportedLanguages": ["x"]
                 ] // was )
        )
        headers {
            contentType(applicationJson())
        }
    }
}

Upvotes: 2

Related Questions