Search code examples
javawiremock

How to call the ResponseDefinitionTransformer in the main class


So I've gone ahead and tried to implement the ResponseDefinitionTransformer, this is my current code:

public class Stub extends ResponseDefinitionTransformer {


    FileOutputStream fos;
    String path;
    File file;
    byte[] message;
    boolean value;


    @Override
    public ResponseDefinition transform(Request rqst, ResponseDefinition rd, FileSource fs, Parameters prmtrs) {


        message = rqst.getBody();
        path = "C:/Users/xxx/Documents/NetBeansProjects/WeatherApplication/xmlFile.xml";
        file = new File(path);


        try {
            FileOutputStream fos = new FileOutputStream(file);
        } catch (FileNotFoundException ex) {
        }


        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException ex) {
            }


            try {
                fos.write(message);
            } catch (IOException ex) {
            }


            try {
                fos.flush();
            } catch (IOException ex) {
            }
            System.out.println("File Written Successfully");
            System.out.println("File Created and Written to");


            value = validateXMLSchema("shiporder.xsd", "xmlFile.xml");


        }

        if(value){
        return new ResponseDefinitionBuilder()
                    .withHeader("Content-Type", "application/xml")
                    .withStatus(200)
                    .withBody("Message Matched")
                    .build();
        }else{
            return new ResponseDefinitionBuilder()
                    .withHeader("Content-Type", "application/xml")
                    .withStatus(404)
                    .withBody("Message not Matched")
                    .build();

        }
    }


    @Override
    public String getName() {
        return "example";
    }

And then in the main method, this is my code, I'm getting an error in the ".withTransformer" bit. I'm not sure how to connect the ResponseDefintionTransformer to my actual stub. Also, shouldn't I be calling the override method? How does it know the url request? Don't I need to pass it the url?

public static void main(String[] args) throws IOException, InterruptedException, SAXException, JAXBException, org.xml.sax.SAXException, UnsupportedOperationException {
        Stub sub = new Stub();
        WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options().port(8080));
        wireMockServer.start();



    stubFor(any(urlEqualTo("/user/test")).atPriority(1)
            .willReturn(aResponse()
                    .withStatus(200)
                    .withHeader("Content-Type", "application/xml")
                    .withBody("XML RECIEVED")
                    .withTransformer("example")
            )
    );
}

Solution

    1. .withTransformer expects parameter keys and values in addition to the transformer name. The easiest way to get rid of this compilation error would be to call an alternative method .withTransformers("example")

    2. after the code compiles you still won't get the transformer working as your instance of the WireMockServer doesn't know about your transformer yet. You'll need to adjust the configuration:

      WireMockConfiguration.options()
              .extensions(stub)
              .port(8080)
      
    3. As you're defining the response in your transformer anyway, the stub definition could be simply:

      stubFor(any(urlEqualTo("/user/test"))
              .willReturn(aResponse()
                      .withTransformers(stub.getName())
              )
      );
      
    4. Update: BTW, as your transformer is global, you don't even have to define it at the stub definition. It will be applied to all the requests. see http://wiremock.org/docs/extending-wiremock/#non-global-transformations.