jeudi 24 septembre 2015

JavaEE: Unit test custom request wrapper

I have a custom wrapper class around an httpServletRequest object. The purpose of the wrapper is to validate the parameters against whitelists before sending the request on through the filterChain.

The issue I'm having is that I have overridden the getParameter method of the standard request object in the wrapper; It checks if the parameter is in a hashmap, and if so it will perform validation on the parameter using an instance of another class. How can I unit test the getParameter method? I have created unit tests for the actual object(Class that utilises Esapi.Validator()) that checks if the parameter value is valid input and returns a boolean value back to the wrapper, so is it even worth my time testing the wrapper?

TestRequestWrapper.java

public class TestRequestWrapper extends HttpServletRequestWrapper{
  private static HashMap<String, EsapiParameterValidator> validationMap = new HashMap<String, EsapiParameterValidator>();
  static{
      validationMap.put("lob", new EsapiParameterValidator("lob", "REG.lob", 3, false));
      // number of other entries
      }

  public TestRequestWrapper(HttpServletWrapper request){
    super(request);
  }

  public String getParameter(String parameter){
    String value = super,getParameter(parameter);
    if(validationMap.containsKey(parameter)){
      EsapiParameterValidator epv = validationMap.get(parameter);  
      value = epv.getValidatedParam(value) ? value : "";
    }
    return value;
  }

Aucun commentaire:

Enregistrer un commentaire