Fetching dynamic values in wiremock from request json to response json

155 Views Asked by At

Request Body Payload-

{    
    "request":{
        "method" : "POST",
        "urlPattern" :"/payments/v1/payments",
        "headers":{
            "x-session-id":{
                "matches":"[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}"
        },       
            "x-request-id":{
                "matches":"[0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}"
        },
            "x-api-key":{
                "matches":"(?s).*"
            },         
           "Content-Type":{
                "equalTo":"application/json"
            },        
           "Accept":{
                "equalTo":"application/json"
            },
            "x-application-id":{
                "matches":"(?s).*"
            },
            "X-Client-Channel":{
                "equalTo":"client"
            },
            "Authorization":{
                "matches":"Bearer [a-zA-Z0-9&._-]{1,}"
            },
            "context":{
                "equalTo":"Pankin ostopolku"
            }   
        },
            "bodyPatterns": [
    
    {"matchesJsonPath": "$[?(@.isRetry == false)]"},  
    {"matchesJsonPath": "$[?(@.debtorAccount.IBAN == null)]"},
    {"matchesJsonPath": "$[?(@.debtorAccount.NotApplicable == 'NOT_APPLICABLE')]"},

    {"matchesJsonPath": "$[?(@.creditorAccount.IBAN =~ /.*/)]"},
    {"matchesJsonPath": "$[?(@.creditorAccount.NotApplicable == null)]"},

    {"matchesJsonPath": "$[?(@.creditorTransactionCode == '500')]"}, 
    
    {"matchesJsonPath": "$[?(@.instructedAmount.amount =~ /.*/)]"},
    {"matchesJsonPath": "$[?(@.instructedAmount.currency == 'EUR')]"},

    {"matchesJsonPath": "$[?(@.intermediaryAccount == '3531800')]"},
    {"matchesJsonPath": "$[?(@.ownMessage == null)]"},
    {"matchesJsonPath": "$[?(@.archiveId =~ /.*/)]"},
    {"matchesJsonPath": "$[?(@.paymentType =~ /.*/)]"}
    
    ]

     }   
  }

which looks like

{
    "isRetry": false,
    "debtorAccount": {
        "IBAN": null,
        "NotApplicable": "NOT_APPLICABLE"
    },
    "creditorAccount": {
        "IBAN": "FI2052091920276802",
        "NotApplicable": null
    },
    "creditorTransactionCode": "500",
    "instructedAmount": {
        **"amount": "57000.00",**
        "currency": "EUR"
    },
    "intermediaryAccount": "3531800",
    "ownMessage": null,
    **"archiveId": "202311235SCL00000026",**
    "paymentType": "CREDIT_ONLY"
}

Response body payload -

{
        "debtorAccount": {
            "NotApplicable": null
        },
        "additionalId": "0",
        "creditorAgent": {},
        "orderReceipt": "false",
        "needsSplit": "false",
        "transactionCode": null,
        "transactionDate": "2023-11-06",
        "ownMessage": null,
        **"archiveId": "202311065OCL00000007",**
        "transactionId": "59986920448925_2023-11-06_202311065OCL00000007_0",
        "paymentType": "CREDIT_ONLY",
        "paymentId": "A_59986920448925_2023-11-06_202311065OCL00000007_0",
        "creditorAccount": {
            "IBAN": null
        },
        "instructedAmount": {
            **"amount": "11015.42",**
            "currency": "EUR"
        },
        "bookingDate": "2023-11-06",
        "status": "PROCESSED",
        "modifications": {}
}

Here I wanted to have the value of archiveId and amount from the request body payload as the value of archiveId and amount in response body payload.

In the JSON how it can be achieved?

1

There are 1 best solutions below

0
On

You can use the jsonPath helper to extract the values from the request model.

Given the request body you posted, they would be something like:

{
  ...,
  "archiveId": "{{ jsonPath request.body '$.archiveId' }}",
  "instructedAmount": {
    "amount": "{{ jsonPath request.body '$.instructedAmount.amount' }}",
    ...,
  },
  ...,
}

I'm unsure of how you're supplying the response, but you'll need to enable response templating in the response.

...,
  "response": {
    "status": 200,
    "bodyFileName": "foo.json", // just an example, but you can use `body` or `jsonBody` as well
    "transformers": ["response-template"]
  }
}