Pages

Saturday, November 16, 2013

Webservices stubbing - generating responses


In certain situations, you need to create huge volume of test data xmls, test data responses. For example if your application is using some web service, to do performance testing of your application, you may need to stub the services. When you are stubbing the services you need large volume of test data responses to be created. Creating this manually is a time consuming process and error prone. Also if you have to change something and going and changing in each location is also a difficult task.

When I confronted with this scenario, I remembered about the Free Marker template (or FTL in short) engine I used in one of my assignments. The FTL template engine is a open source software which can read the templates in any format (normally .ftl format) and generate output files out of it.

A template is a file which has static and dynamic parts. The dynamic parts are marked as variables. For example, this is the response you want from a webservice but the employee id has to be different for each employee. You want to create 1000 employee responses from employee id 1 to 1000. Similarly you want the last name of the employee to be the employee id and the post code should be different.


<Employee>
 <Name>Ravi ${lastname}</Name>
 <Age>25</Age>
 <EmployeeId>${employeeid}</EmployeeId>
 <Address>${postcode}</Address>
</Employee>


Your template should like like above. Now in free marker you can generate this in few minutes.

I used Maven for building this project. So you need to have FTL dependency in your pom file. You can add the dependency to your POM file like this.


<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.20</version>
</dependency>

Now the code which generates this test xmls.


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class TestDataGenerator {

public static void main(String[] args) {
 // Freemarker configuration object
 Configuration cfg = new Configuration();
 try {
 // Load template from source folder
 Template template = cfg.getTemplate("src/main/resources/employee.ftl");
 for (int i = 0; i < 1000; i++) {
 // Build the data-model
  Map data = new HashMap();
  data.put("employeeid", i + "");
  data.put("lastname", i + "");
  data.put("postcode", i + " WG");

  // File output
  Writer file = new FileWriter(new File("C:\\stubdata\\" + i+ ".xml"));
  template.process(data, file);
  file.flush();
  file.close();
 }
} catch (IOException e) {
 e.printStackTrace();
} catch (TemplateException e) {
 e.printStackTrace();
}
}
}
Once you run this code, you should be able to see, in the stubdata folder, you can see 1000 test xmls with values replaced.

No comments:

Post a Comment

 
Blogger Templates