Zahid Khan
06/13/2023, 3:34 PMtest
endpoint with the custom Request and Response JSON payload:
My REQUEST PAYLOAD:
{
"merchantId": "xxxx",
"data": [
{
"id": "unique-id-1",
"sensitiveData": "xxxxxx",
},
...
...
]
}
My RESPONSE PAYLOAD:
{
"status": "SUCCESS",
"tokens": [
{
"id": "unique-id-1",
"token": "xxxxxxxxxx",
},
...
...
]
}
What I'm trying to achieve?
Since my JSON Request payload is dynamic. I want to create a dynamic JSON Payload in response as well where JSON tokens values are static but the id's value in JSON is dynamic based on the request payload's id value.
CODE:
@SpringBatchTest
class FileTest{
private WireMockServer wireMockServer;
BeforeEach
void setUp() {
wireMockServer =
new WireMockServer(options().extensions(new
ResponseTemplateTransformer(true)).port(PORT));
WireMock.configureFor("localhost", PORT);
wireMockServer.start();
}
@AfterEach
void tearDown() {
wireMockServer.stop();
@Test
void testEndToEndFlowForSpringBatch() {
final String body = ResponsePayload.getResponseJsonPayload();
wireMockServer.addStubMapping(
stubFor(
post(urlPathMatching("/test"))
.willReturn(
aResponse()
.withBody(body)
.withStatus(200)
.withTransformers("response-template"))));
//morecode
}
ResponsePayload.getResponseJsonPayload() contains below value:
{
"response": "SUCCESS",
"tokens": [
{
"id": "{{jsonPath request.body '$.data[0].id'}}",
"token": "xxxxxx",
},
{
"id": "{{jsonPath request.body '$.data[1].id'}}",
"token": "xxxxxxx",
},
..
]
}
What is not working?
When I am debugging the program I can see the stub is throwing the following error:
<p>Problem accessing /test. Reason:
<pre> wiremock.com.github.jknack.handlebars.HandlebarsException: inline@1e0c0274:5:21: could not find helper: 'jsonPath'
"id": "{{jsonPath request.body '$.merchantId'}}",
^
</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>
Tom
06/13/2023, 6:38 PMZahid Khan
06/14/2023, 4:40 AMTom
06/14/2023, 8:24 AM