Not able to transform response body

70 views
Skip to first unread message

Radha Venkatesh

unread,
Nov 28, 2022, 8:03:37 PM11/28/22
to wiremock-user
I am new to wire mock and have issues trying to transform a response body. I want to a new UUID in a field in the response body every time a request is made. 

stubFor(any(urlMatching("/calliope-server/calliope/api/v1/venues/" + venue + "/confluences"))
.willReturn(aResponse().withStatus(200).withBody(confluenceCreationResponse().toString())
.withTransformers("confluence-creation-transformer")));


where confluenceCreationResponse is created like this

UUID confluenceUUID = UUID.randomUUID();
ConfluenceCreationResponse response = new ConfluenceCreationResponse();
response.setUrl(URI.create(configProperties.getCalliopePublicUrl() + "/venues/" + venue + "/confluences/" + confluenceUUID));
response.setSdpAnswer("SDP answer");
response.setSdpAnswers(Collections.singletonList("SDP answer"));
response.setStatusCode(200);
return response;


Obviously, the same conflueneceUUID is present in the response every time. I have tried to transform this by using ConfluenceCreateBodyTransformer which does this

@Override
public Response transform(Request request, Response response, FileSource fileSource, Parameters parameters) {
UUID confluenceUUID = UUID.randomUUID();
ObjectMapper mapper = new ObjectMapper();
try {
ConfluenceCreationResponse transformedResponse = mapper.readValue(response.getBodyAsString(), ConfluenceCreationResponse.class);
String confluenceUrl = transformedResponse.getUrl().toString();
String transformedUrl = confluenceUrl.substring(0, confluenceUrl.indexOf("/confluences"));
transformedUrl = transformedUrl + "/confluences/" + confluenceUUID;
transformedResponse.setUrl(URI.create(transformedUrl));
return Response.Builder.like(response)
.but().body(transformedResponse.toString())
.build();
} catch (Exception ex) {
log.error("Problem transforming response", ex);
}
return Response.Builder.like(response).build();
}

@Override
public boolean applyGlobally() {
return false;
}

@Override
public String getName() {
return "confluence-creation-transformer";
}

and have started the wiremock server with
"--extensions", "ConfluenceCreateBodyTransformer",
"--local-response-templating",


However, transformation does not work and I see exceptions ;like this

    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.base/java.net.Socket.connect(Socket.java:608)
    at wiremock.org.apache.hc.client5.http.socket.PlainConnectionSocketFactory$1.run(PlainConnectionSocketFactory.java:87)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at wiremock.org.apache.hc.client5.http.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:84)
    at wiremock.org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:148)
    at wiremock.org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:407)
    at wiremock.org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:168)
    at wiremock.org.apache.hc.client5.http.impl.classic.InternalExecRuntime.connectEndpoint(InternalExecRuntime.java:178)
    at wiremock.org.apache.hc.client5.http.impl.classic.ConnectExec.execute(ConnectExec.java:136)
    at wiremock.org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
    at wiremock.org.apache.hc.client5.http.impl.classic.ExecChainElement$1.proceed(ExecChainElement.java:57)
    at wiremock.org.apache.hc.client5.http.impl.classic.ProtocolExec.execute(ProtocolExec.java:165)
    at wiremock.org.apache.hc.client5.http.impl.classic.ExecChainElement.execute(ExecChainElement.java:51)
    at wiremock.org.apache.hc.client5.http.impl.classic.InternalHttpClient.doExecute(InternalHttpClient.java:179)
    at wiremock.org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:75)
    at wiremock.org.apache.hc.client5.http.impl.classic.CloseableHttpClient.execute(CloseableHttpClient.java:89)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:489)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:472)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.executeRequest(HttpAdminClient.java:449)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:146)
    at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:398)
    at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:393)
    at com.github.tomakehurst.wiremock.client.WireMock.givenThat(WireMock.java:115)
    at com.github.tomakehurst.wiremock.client.WireMock.stubFor(WireMock.java:119)


What is it that I am missing?

Thanks,
Radha.
Reply all
Reply to author
Forward
0 new messages