StringRegexpReplaceResponseFormatter implements ResponseFormatter { @Override public byte[] formatResponse(final WilmaHttpRequest wilmaRequest, final HttpServletResponse resp, final byte[] templateResource, final ParameterList params) throws Exception { byte[] result = templateResource; String template = null; if (!params.getAllParameters().isEmpty()) { template = getTemplate(templateResource, params); result = template.getBytes(StandardCharsets.UTF_8); } return result; }  @Override byte[] formatResponse(final WilmaHttpRequest wilmaRequest, final HttpServletResponse resp,
                                 final byte[] templateResource, final ParameterList params);  }
@Test public void testFormatTemplateShouldReplaceRegexp() throws Exception { templateResource = "123 this is a test template 123".getBytes(); parameterList.addParameter(new Parameter("[0-2]+", "HELLO")); byte[] actual = underTest.formatResponse(request, response, templateResource, parameterList); assertEquals(actual, "HELLO3 this is a test template HELLO3".getBytes()); }
@Test public void testFormatTemplateWhenMultipleParametersShouldReplaceString() throws Exception { templateResource = "123 this is a test template 123".getBytes(); parameterList.addParameter(new Parameter("[0-2]+", "HELLO")); parameterList.addParameter(new Parameter("is", "is not")); byte[] actual = underTest.formatResponse(request, response, templateResource, parameterList); assertEquals(actual, "HELLO3 this not is not a test template HELLO3".getBytes()); }
@Test public void testFormatTemplateWhenNoParametersShouldDoNothing() throws Exception { templateResource = "this is a test template".getBytes(); byte[] actual = underTest.formatResponse(request, response, templateResource, parameterList); assertEquals(actual, templateResource); }
@Test(expectedExceptions = ResponseFormattingFailedException.class) public void testFormatTemplateWhenRegexpSyntaxIncorrectShouldThrowException() throws Exception { templateResource = "123 this is a test template 123".getBytes(); parameterList.addParameter(new Parameter("{0-2}+", "HELLO")); underTest.formatResponse(request, response, templateResource, parameterList); }