Hi all, While using state extension(wiremock-state...
# help
a
Hi all, While using state extension(wiremock-state-extension-standalone-0.2.1.jar) to load a simple json stub, I'm receiving the following error.
Copy code
Exception in thread "main" com.github.tomakehurst.wiremock.standalone.MappingFileException: Error loading file /Users/amarnadh.m/Documents/mappings/createUser.json:
'matchesJsonSchema' expected value cannot be null
        at com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource.loadMappingsInto(JsonFileMappingsSource.java:123)
        at com.github.tomakehurst.wiremock.core.WireMockApp.loadMappingsUsing(WireMockApp.java:248)
        at com.github.tomakehurst.wiremock.core.WireMockApp.loadDefaultMappings(WireMockApp.java:242)
        at com.github.tomakehurst.wiremock.core.WireMockApp.<init>(WireMockApp.java:123)
        at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:71)
Attached the stub below. Can you please let me know how to resolve this issue?
Copy code
{
  "mappings": [
    {
      "request": {
        "method": "POST",
        "url": "/api/v1/user",
        "bodyPatterns": [
          {
            "not": {
              "matchesJsonSchema": {
                "type": "object",
                "required": [
                  "firstName",
                  "lastName",
                  "username",
                  "email"
                ],
                "properties": {
                  "firstName": {
                    "type": "string"
                  },
                  "lastName": {
                    "type": "string"
                  },
                  "username": {
                    "type": "string"
                  },
                  "email": {
                    "type": "string"
                  },
                  "phoneNumbers": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        ]
      },
      "response": {
        "status": 400,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "{\"error\": \"Bad Request\"}"
      },
      "priority": 1
    },
    {
      "request": {
        "method": "POST",
        "url": "/api/v1/user"
      },
      "response": {
        "status": 201,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "{\"id\":\"{{randomInt lower=1 upper=20000}}\",\"username\":\"{{jsonPath request.body '$.username'}}\",\"firstName\":\"{{jsonPath request.body '$.firstName'}}\",\"lastName\":\"{{jsonPath request.body '$.lastName'}}\",\"email\":\"{{jsonPath request.body '$.email'}}\", \"phoneNumbers\": {{#if (jsonPath request.body '$.phoneNumbers')}}{{jsonPath request.body '$.phoneNumbers'}}{{else}}[]{{/if}}\n}",
        "transformers": [
          "response-template"
        ]
      },
      "postServeActions": [
        {
          "name": "recordState",
          "parameters": {
            "context": "{{jsonPath response.body '$.username'}}",
            "name": "userState",
            "state": {
              "fullBody": "{{{jsonPath response.body '$'}}}"
            }
          }
        }
      ],
      "priority": 2
    }
  ]
}
java -cp  "wiremock-state-extension-standalone-0.2.1.jar:~/Downloads/wiremock-standalone-3.9.1.jar" wiremock.Run --port 8181 --verbose
d
can you try this with a more recent version of the extension? The most recent version is 0.8.0 .
a
Hi, version seems to be the issue. The link to standalone jar seems to be broken in README which created some confusion. It is now starting but on making a request matching the above stub, I'm seeing this log message
No extension was found named "recordState"
starting wiremock with:
java -cp  wiremock-state-extension-standalone-0.8.0.jar:Downloads/wiremock-standalone-3.9.1.jar wiremock.Run  --global-response-templating --port 8181 --verbose
please ignore. mistake in my stub
👍 1
d
can you point me to the readme I should update
a
GitHub hyperlink in Standalone section
d
thx
a
Hi, if we add fullBody to state list
Copy code
"list": {
     "addLast": {
         "fullBody": "{{{jsonPath response.body '$'}}}"
          }
  }
How to delete state using deleteWhere? I'm trying this
Copy code
{
  "name": "deleteState",
  "parameters": {
    "list": {
      "deleteWhere": {
        "property": "username",
        "value": "{{request.path.username}}"
      }
    },
    "context": "user-list"
  }
}
I have also tried fullBody.username {{fullBody.username}} too. None of them seems to be deleting from the state
d
the extension can't sensibly interpret the body. It can only check on properties to explicitly add to the state. In order to use the username, do something like:
Copy code
"list": {
     "addLast": {
         "username": {{jsonPath request.body '$.username'}}
         "fullBody": "{{{jsonPath response.body '$'}}}"
          }
  }
a
ok, so the match is available on 1st level field as of now.
In readme its mentioned
The evaluation order of listeners within a stub as well as across stubs is not guaranteed.
Is there any reason for this especially within the stub.
d
the extension has no control over when it is called or in which order they are called and to avoid any misinterpretation over the meaning of order in your definition and it's impact on the state I wanted to state it explicitly.
a
In that case how can we handle a PUT API that will should update the user attributes in the list state. deleteWhere and list:addLast might not work without ordering
104 Views