One thing, I'm setting up the context of a state u...
# wiremock-java
d
One thing, I'm setting up the context of a state using a dynamic field of req1's body. Now how would I access the state's property (present in req1) in other requests/responses. Other requests do not know the context as it is getting set dynamically in original request. please let me know if it needs more clarity
d
can you provide an a short example of the two requests?
d
PFB the example { "mappings": [ { "priority": 2, "scenarioName": "sc nam", "newScenarioState": "aSt", "request": { "method": "POST", "urlPathPattern": "/wiremock/res/res1" }, "response": { "status": 202 }, "serveEventListeners": [ { "name": "recordState", "parameters": { "context": "{{xPath request.body '//creationId/text()'}}", "state": { "id": "{{xPath request.body '//InstrId/text()'}}" } } } ] }, { "scenarioName": "sc nam", "requiredScenarioState": "aSt", "request": { "method": "GET", "urlPathPattern": "/wiremock/res/res1/details" }, "response": { "fixedDelayMilliseconds": 15000, "status": 200, "bodyFileName": "folder/file.xml", "headers": { "Content-Type": "application/xml" } } } ] } below is the file.xml - <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Document xmlns="abc.com"> <Tag1> <Tag2> <Tag3>33393918</Tag3> <Tag4>{{state context='dynamicValue??' property='id'}}</Tag4> </Tag2> </Tag1> </Document>
Here, first we set context of a state using a dynamic fetched value from request1- "context": "{{xPath request.body '//creationId/text()'}}", Later, we try to access that context, which we actually don't know as it had been fetched from XML request's one of the field. {{state context='dynamicValue??*'*property='id'}} I believe we must send the dynamic value to the context so that each request(ultimately through context) has a uniqueness to be identified and later to be accessed correctly
d
sry for the late response. The context can also be a static value. You can e.g. use the scenario as part of the name - or parts of the URL (your res1) - or just a plain identifier, depending on your needs. Internally, the context is just a plain string. So your straight forward example might work with
"context": "sc nam"
in case you don't want the first run to interfere with the next, you can delete it in your
details
call:
Copy code
"serveEventListeners": [
    {
      "name": "deleteState",
      "parameters": {
        "context": "sc nam"
      }
    }
  ]
d
let me try this way, thanks for your suggestion anyway
@Dirk Bolte, but see my req1 has a unique value which I can provide as context, later matching same context with req2's URL or other things BUT my req2 only has a URL which is all static does't have any dynamic value. req2 returns a a body where I'll put state value as needed now here question is how will we make req2 in context w.r.t. each req1 object
d
I'm not completely sure whether I understand what you're looking for. Can you provide an example on how the API should be used?
POST /wiremock/res/res1
GET /wiremock/res/res1/details
would work with a static context
d
GET api would execute after POST api in a series. The series will execute thrice. Depending upon condition set in req1, corresponding response of GET api would be returned. GET api do not have any field, so that we can match context w.r.t POST api. It only has a URL.
Here's the drawback while working with a static context, I had already tried-
d
so it's:
POST /wiremock/res/res1
GET /wiremock/res/res1/details
POST /wiremock/res/res1
GET /wiremock/res/res1/details
POST /wiremock/res/res1
GET /wiremock/res/res1/details
d
sorry , will add one more thing POST might execute multiple times, before a GET will pick corresponding POST ones
Here's the drawback while working with a static context, I had already tried. you can correct me here if anything- as multiple POST requests are made, GET would only respond for latest POST request
d
what would be the expected behavior?
d
GET should respond to each POST ones with specific response body
d
how is the real API achieving this?
if there's no identification within the request, there needs to be something different
a stack, an order, ...
d
we are getting response after passing it to exchange of spring
that's what I know
d
if you say the
GET
returns a specific response body, does it match the order of your requests? Like a queue?
d
it is a similar to a whole array which might include all the corresponding POST reqIds at once or one at a time
similar to stack somehow
d
In that case, maybe a
list
might help. You have one context and on every
POST
, you do a
addLast
. In the response, you just access the first element of the list (
[0]
) and do a
deleteLast
in the
serveEventListener
. There's an example for such a behavior in the project: https://github.com/wiremock/wiremock-state-extension/blob/develop/src/test/java/or[…]k/extensions/state/examples/StateExtensionQueueExampleTest.java
d
It'll be helpful if corresponding JSON implementation link is sent here
we work either JSONs
I found it, let me check
d
that example has no JSON version . But the java syntax pretty much matches the json names.
👀 1
d
@Dirk Bolte, I check little more deeply- we are consuming GET Resp in a queue manner one after other not list & we are passing XML body as GET Resp in which I am accessing state list value as-
Copy code
{{state context='hardContext' list='[0].properyt1' default='[]'}}
I'm deleting the list firstValue after GET Resp is returned using-
Copy code
"serveEventListeners": [
  {
    "name": "deleteState",
    "parameters": {
      "context": "hardContext",
      "list": {
        "deleteFirst": true
      }
    }
  }
]
BUT, when I hit the GET multiple times, it keeps on returning the same properyt1. Seems deletion is not taking place or correct me for syntax
d
Can you check the context names? In the snippets, you use different ones.
d
it was , I mistakenly pinged wrong here
is there any persistence or something
d
Can you send the whole configuration?
d
will send you in sometime
@Dirk Bolte, PFB { "mappings": [ { "priority": 2, "request": { "method": "POST", "urlPathPattern": "/wiremock/resources/add", "bodyPatterns": [ { "matchesXPath": "//resources/Id/[contains(text(),'02322')]" } ] }, "response": { "status": 202, "headers": { "Message-Request-Id": "R2{{randomValue length=4 type='NUMERIC'}}" } }, "serveEventListeners": [ { "name": "recordState", "parameters": { "context": "hardContext", "list": { "addFirst": { "resId": "{{xPath request.body '//resources/hkId/text()'}}" } } } } ] }, { "priority": 2, "request": { "method": "GET", "urlPathPattern": "/wiremock/resources/get", "customMatcher": { "name": "state-matcher", "parameters": { "hasContext": "hardContext" } } }, "response": { "status": 200, "bodyFileName": "response/resourceDetails.xml", "headers": { "Content-Type": "application/xml", "Message-Request-Id": "REQSTBPAID432{{randomValue length=4 type='NUMERIC'}}" } }, "serveEventListeners": [ { "name": "deleteState", "parameters": { "context": "hardContext", "list": { "deleteFirst": true } } } ] } ] } resourceDetails.xml: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Document> <Tag1> <Tag3> <MsgId>33393918</MsgId> </Tag3> <Tag2> <OrgnlResourceId>{{state context='hardContext' list='[0].resId' default='[]'}}</OrgnlResourceId> </Tag2> </Tag1> </Document>
d
I don't see an obvious error but I also can't reproduce it locally. Can you provide an isolated + reduced project on github for reproduction purposes including a failing test case for this scenario and open an issue in https://github.com/wiremock/wiremock-state-extension linking to it? Please ensure that the code does not contain any confidential or licensed parts
d
I am using below command - java -cp "wiremock-state-extension-standalone-0.8.0.jar:wiremock-standalone-3.9.0.jar" wiremock.Run --verbose --global-response-templating I have my service which is internally pointed wiremock. I believe this should help
@Dirk Bolte, Do you think any persistence : true can work, here. As state is not getting deleted. Also as part of project, do you require whole configuration or like docker and req and Resp files or anything else.
d
Should not have an impact. Have to try to reproduce it somehow
I sent requests looking like this (varying hkid):
Copy code
<resources>
    <Id>02322</Id>
    <hkId>2</hkId>
</resources>
I adapted the xpath matcher to
Copy code
"bodyPatterns": [
          {
            "matchesXPath": "//resources/Id[contains(text(),'02322')]"
          }
        ]
and it exactly behaved as intended: the responses contained the hkId in reverse order. Once empty, I get the default value defined in the stub mapping (
[]
) . If you still encounter issues, please provide an isolated + reduced project including a failing test to help isolating the issue.
🙌 1
d
It's working actually now. Thanks for help.
👍 1