https://linen.dev logo
#general
Title
# general
m

Manojkumar Kasiviswanathan

03/30/2023, 8:47 AM
Hi All, I am trying to create the below case in wiremock but not sure this is the right way any advice will be usefull. { "request": { "method": "GET", "url": "/api/users/test" }, "response": { "status": 200, "jsonBody": {{request.path[2] type=uuid}} } } Basically to the string I want to generate a UUID
r

Rob Elliot

03/30/2023, 9:16 AM
If you want to generate a UUID you want:
Copy code
{
  "request": {
    "method": "GET",
    "url": "/api/users/test"
  },
  "response": {
    "status": 200,
    "jsonBody": "{{randomValue type='UUID'}}"
  }
}
see https://wiremock.org/docs/response-templating/ under Random value helper
You will need to enable response templating, either globally or for that stub
m

Manojkumar Kasiviswanathan

03/30/2023, 9:36 AM
Thanks Rob but this will generate different UUID every time but I need UUID for a string. Example if I pass test they need to use test to create the UUID.
r

Rob Elliot

03/30/2023, 9:59 AM
There's no existing helper that will generate a deterministic UUID from a string input I'm afraid. If you're running WireMock open source you could write one that uses
UUID.nameUUIDFromBytes(input.getBytes())
Then you can register it on start as so:
Copy code
options()
    .extensions(new ResponseTemplateTransformer(false, Map.of("uuid-from-string", new UuidFromStringHelper())))
m

Manojkumar Kasiviswanathan

03/30/2023, 10:06 AM
Hmm currently , we are pull a image and copying only mapping and file like below
FROM rodolpheche/wiremock:2.31.0-alpine@sha256:c848ad9eb6bb028c8a364e55480793da10a26bd2c7ea3f0e28a311a22b2a0c04 ARG exposedPort=8083 ENV exposedPortEnv=$exposedPort # Add our wiremock mapping and response files to wiremocks default run path. COPY ./wiremock/files/ /home/wiremock/__files/data-adapter/ COPY ./wiremock/mappings/ /home/wiremock/mappings/data-adapter/ EXPOSE $exposedPort CMD ["--port","8083","--verbose","--global-response-templating=true"]
not sure where this config sits. any example pls ?
r

Rob Elliot

03/30/2023, 10:07 AM
It doesn't. If you're pulling an image there's no easy way to add a helper.
m

Manojkumar Kasiviswanathan

03/30/2023, 10:09 AM
ok got it will write a Application.java for this
Copy code
options()
    .extensions(new ResponseTemplateTransformer(false, Map.of("uuid-from-string", new UuidFromStringHelper())))
After that not sure How to involve this in a request
Copy code
{
  "request": {
    "method": "GET",
    "url": "/api/users/test"
  },
  "response": {
    "status": 200,
    "jsonBody": "{{request.path[0] to UUID}}"
  }
}
r

Rob Elliot

03/30/2023, 10:21 AM
I think you'd do:
Copy code
"jsonBody": "{{uuid-from-string request.path[0]}}"
m

Manojkumar Kasiviswanathan

03/30/2023, 10:22 AM
will test now 🙂