HeaderFormatter implements ResponseFormatter { @Override public byte[] formatResponse(WilmaHttpRequest wilmaRequest, HttpServletResponse resp, byte[] responseResource, ParameterList params) { for (Parameter p : params.getAllParameters()) { String name = p.getName(); String value = p.getValue(); if (name != null && name.length() > 0 && value != null && value.length() > 0) { if (resp.containsHeader(name)) { resp.setHeader(name, value); } else { resp.addHeader(name, value); } } else { logger.warn("Template Formatter issue: specified parameters cannot be used in header: " + p.toString()); } } return responseResource; }  @Override byte[] formatResponse(WilmaHttpRequest wilmaRequest, HttpServletResponse resp, byte[] responseResource, ParameterList params);  }
@Test public void testFormatterDoesNothingWhenParameterIsNull() { underTest.formatResponse(request, response, null, parameterMap); verifyZeroInteractions(request); verifyZeroInteractions(response); }
@Test public void testFormatterDoesNothingWhenParameterIsEmpty() { given(parameterMap.isEmpty()).willReturn(true); underTest.formatResponse(request, response, null, parameterMap); verifyZeroInteractions(request); verifyZeroInteractions(response); }
@Test public void testFormatterAddNewHeader() { given(parameterMap.isEmpty()).willReturn(false); given(response.containsHeader("A")).willReturn(false); List<Parameter> parameters = new ArrayList<>(); Parameter p = new Parameter("A", "B"); parameters.add(p); given(parameterMap.getAllParameters()).willReturn(parameters); underTest.formatResponse(request, response, null, parameterMap); verify(response).addHeader("A", "B"); }
@Test public void testFormatterUpdateHeader() { given(parameterMap.isEmpty()).willReturn(false); given(response.containsHeader("A")).willReturn(true); List<Parameter> parameters = new ArrayList<>(); Parameter p = new Parameter("A", "B"); parameters.add(p); given(parameterMap.getAllParameters()).willReturn(parameters); underTest.formatResponse(request, response, null, parameterMap); verify(response).setHeader("A", "B"); }
@Test public void testFormatterDoNothingWhenParameterIsWrongNB() { given(parameterMap.isEmpty()).willReturn(false); List<Parameter> parameters = new ArrayList<>(); Parameter p = new Parameter(null, "B"); parameters.add(p); given(parameterMap.getAllParameters()).willReturn(parameters); underTest.formatResponse(request, response, null, parameterMap); verifyZeroInteractions(request); verifyZeroInteractions(response); }
@Test public void testFormatterDoNothingWhenParameterIsWrongEB() { given(parameterMap.isEmpty()).willReturn(false); List<Parameter> parameters = new ArrayList<>(); Parameter p = new Parameter("", "B"); parameters.add(p); given(parameterMap.getAllParameters()).willReturn(parameters); underTest.formatResponse(request, response, null, parameterMap); verifyZeroInteractions(request); verifyZeroInteractions(response); }
@Test public void testFormatterDoNothingWhenParameterIsWrongAE() { given(parameterMap.isEmpty()).willReturn(false); List<Parameter> parameters = new ArrayList<>(); Parameter p = new Parameter("A", ""); parameters.add(p); given(parameterMap.getAllParameters()).willReturn(parameters); underTest.formatResponse(request, response, null, parameterMap); verifyZeroInteractions(request); verifyZeroInteractions(response); }
@Test public void testFormatterDoNothingWhenParameterIsWrongAN() { given(parameterMap.isEmpty()).willReturn(false); List<Parameter> parameters = new ArrayList<>(); Parameter p = new Parameter("A", null); parameters.add(p); given(parameterMap.getAllParameters()).willReturn(parameters); underTest.formatResponse(request, response, null, parameterMap); verifyZeroInteractions(request); verifyZeroInteractions(response); }