XslBasedResponseFormatter implements ResponseFormatter { @Override public byte[] formatResponse(final WilmaHttpRequest wilmaRequest, final HttpServletResponse resp, final byte[] templateResource, final ParameterList params) throws Exception { String xslResourcePath = checkAndGetXslResourcePath(params); byte[] xslResource = readXslResourceFromFileSystem(xslResourcePath); byte[] requestXmlResource = wilmaRequest.getBody().getBytes(StandardCharsets.UTF_8); return xslResponseGenerator.generateResponse(requestXmlResource, xslResource, templateResource); }  @Override byte[] formatResponse(final WilmaHttpRequest wilmaRequest, final HttpServletResponse resp,
                                 final byte[] templateResource, final ParameterList params);  }
@Test(expectedExceptions = ResponseFormattingFailedException.class) public void testFormatTemplateShouldThrowExceptionWhenXslFileParamDoesNotExist() throws Exception { underTest.formatResponse(wilmaRequest, response, templateResource, params); }
@Test(expectedExceptions = ResponseFormattingFailedException.class) public void testFormatTemplateShouldThrowExceptionWhenXslFileResourceDoesNotExist() throws Exception { params.addParameter(new Parameter(XSL_PARAM_KEY, XSL_PARAM_KEY)); given(stubResourcePathProvider.getTemplatesPathAsString()).willReturn(XSL_PARAM_KEY); given(fileFactory.createFile(Mockito.anyString())).willReturn(file); given(fileUtils.getFileAsByteArray(file)).willThrow(new IOException()); underTest.formatResponse(wilmaRequest, response, templateResource, params); }
@Test public void testFormatTemplateShouldGetRequestBodyFromWilmaRequest() throws Exception { params.addParameter(new Parameter(XSL_PARAM_KEY, XSL_PARAM_KEY)); given(stubResourcePathProvider.getTemplatesPathAsString()).willReturn(XSL_PARAM_KEY); given(fileFactory.createFile(Mockito.anyString())).willReturn(file); given(fileUtils.getFileAsByteArray(file)).willReturn(templateResource); given(wilmaRequest.getBody()).willReturn(templateResource.toString()); underTest.formatResponse(wilmaRequest, response, templateResource, params); verify(wilmaRequest).getBody(); }
@Test public void testFormatTemplateShouldCallGenerateResponseOnXslResponseGenerator() throws Exception { params.addParameter(new Parameter(XSL_PARAM_KEY, XSL_PARAM_KEY)); given(stubResourcePathProvider.getTemplatesPathAsString()).willReturn(XSL_PARAM_KEY); given(fileFactory.createFile(Mockito.anyString())).willReturn(file); given(fileUtils.getFileAsByteArray(file)).willReturn(templateResource); given(wilmaRequest.getBody()).willReturn(XSL_PARAM_KEY); underTest.formatResponse(wilmaRequest, response, templateResource, params); verify(xslResponseGenerator).generateResponse(XSL_PARAM_KEY.getBytes(), templateResource, templateResource); }