kulas
kulas

Reputation: 57

Spring Cloud Contract - empty path parameter randomly generated from the regexp

I am trying to introduce path param in urlPath as regexp in the following way:

import org.springframework.cloud.contract.spec.Contract

Contract.make {

    request {
        description("""
Represents a success scenario for searching Location Groups.
""")
        method 'GET'
        urlPath(value(regex('/trackedItems/([a-zA-Z0-9]*)/locationOccupancies'))) {
            queryParameters {
                parameter 'groupId' : anyNonBlankString()
            }
        }

    }
    response {
        status OK()
        body(
            objects: [
                id : $(anyPositiveInt()),
                locationName : $(anyNonBlankString()),
                locationDescription : $(anyNonBlankString()),
                locationType : $(anyNonBlankString()),
                locationEntry : $(iso8601WithOffset()),
                locationExit : $(iso8601WithOffset()),
                trackedItemId : $(fromRequest().path(1)),
                trackedItemQuantity : $(anyPositiveInt()),
            ]
        )

    }
}

In general it seems to work, however sometimes java tests are getting generated with empty parameter in path:

// when:
            ResponseOptions response = given().spec(request)
                    .queryParam("groupId","WMGOAMYTVGOBBZXADCRU")
                    .get("/trackedItems//locationOccupancies");

In the same time Wiremock mapping looks just fine:

{
  "id" : "0919f4ee-d487-415e-aaa2-eefbd19833f9",
  "request" : {
    "urlPathPattern" : "/trackedItems/([a-zA-Z0-9]*)/locationOccupancies",
    "method" : "GET",
    "queryParameters" : {
      "groupId" : {
        "matches" : "^\\s*\\S[\\S\\s]*"
      }
    }
  },
  "response" : {
    "status" : 200,
    "body" : "{\"objects\":{\"id\":527805002,\"locationName\":\"UWHAHTXXYOZNEOKNKGTC\",\"locationDescription\":\"JQZGDLZTXCAUAITPYJLX\",\"locationType\":\"JVUVLFHVBXWHSMACUZUL\",\"locationEntry\":\"0737-06-03T21:45:28+06:24\",\"locationExit\":\"3625-06-03T23:14:29Z\",\"trackedItemId\":\"{{{request.path.[1]}}}\",\"trackedItemQuantity\":1392018943}}",
    "transformers" : [ "response-template", "spring-cloud-contract" ]
  },
  "uuid" : "0919f4ee-d487-415e-aaa2-eefbd19833f9"
}

Is there something wrong with my contract? Maybe there is a better way to do this?

id "org.springframework.cloud.contract" version "3.0.1"
id 'org.springframework.boot' version '2.3.1.RELEASE'

Thanks!!

Edit: Same for id "org.springframework.cloud.contract" version "3.0.2"

Upvotes: 0

Views: 372

Answers (1)

Marcin Grzejszczak
Marcin Grzejszczak

Reputation: 11189

The problem was with the regex. Instead of /trackedItems/([a-zA-Z0-9]*)/locationOccupancies there should be /trackedItems/([a-zA-Z0-9]+)/locationOccupancies

Upvotes: 1

Related Questions