How do I go about creating multiple transformers on wiremock? I've seen on the documentation that you can call two response transformers on one stub but I'm not sure how you go about creating the second response transformer as well as giving it a name?
So this is the Second transformer I've created
public class test2 extends ResponseDefinitionTransformer{
String message;
boolean value;
@Override
public ResponseDefinition transform(Request rqst, ResponseDefinition rd, FileSource fs, Parameters prmtrs) {
message = rqst.getBodyAsString();
value = validateXMLSchema("xxx", message);
System.out.println("SECOND TRANSFORMER EXECUTED");
System.out.println("THIS IS THE CONDITION OF THE VALUE: " + value );
if(SchemaMatches){
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(200)
.withBody("XSD SCHEMA MATCHES")
.build();
}else{
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(404)
.withBody("MISMATCH")
.build();
}
}
@Override
public boolean applyGlobally() {
return false;
}
@Override
public String getName() {
return "tests";
}
}
This is the first transformer I've created:
public class Stub extends ResponseDefinitionTransformer {
@Override
public ResponseDefinition transform(com.github.tomakehurst.wiremock.http.Request rqst, ResponseDefinition rd, FileSource fs, Parameters prmtrs) {
message = rqst.getBodyAsString();
if(MandatoryFieldsExist){
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(200)
.withBody("MANDATORY FIELDS PRESENT ")
.build();
}
else{
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(404)
.withBody("MISMATCH")
.build();
}
}
@Override
public boolean applyGlobally() {
return false;
}
@Override
public String getName() {
return "example";
}
Finally, this is the code in my main method to call the transformer:
WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options().port(8080).extensions(Stub.class, test2.class));
WireMock wireMockClient = new WireMock();
wireMockServer.start();
stubFor(post(urlEqualTo("/user/test"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/xml")
.withBody("XML RECIEVED")
.withTransformers("example","tests")
)
);