PrettyPrintProcessor extends ProcessorBase { @Override public void process(final WilmaHttpEntity entity) throws ApplicationException { String body = entity.getBody(); if ((body == null) || (body.length() == 0)) { return; } String contentTypeHeader = entity.getHeader("Content-Type"); if (contentTypeHeader != null && contentTypeHeader.contains("xml") && !contentTypeHeader.contains("image/svg+xml")) { try { Transformer transformer = transformerFactory.createTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = streamResultFactory.createStreamResult(); StreamSource source = streamSourceFactory.createStreamSourceFromString(body); transformer.transform(source, result); String xmlString = result.getWriter().toString(); entity.setBody(xmlString); } catch (TransformerException e) { logError(entity, e); } } else if (contentTypeHeader != null && contentTypeHeader.contains("json")) { handleJsonContent(entity); } }  @Override void process(final WilmaHttpEntity entity);  }
@Test public void testProcessShouldNotOnEmptyBody() throws ApplicationException, TransformerException { given(request.getBody()).willReturn(null); underTest.process(request); verify(request, never()).setBody(Mockito.anyString()); }
@Test public void testProcessShouldCallTransform() throws ApplicationException, TransformerException { given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(CONTENT_TYPE_SOAPXML); given(transformerFactory.createTransformer()).willReturn(transformer); given(streamResultFactory.createStreamResult()).willReturn(streamResult); given(streamSourceFactory.createStreamSourceFromString(BODY)).willReturn(streamSource); given(streamResult.getWriter()).willReturn(writer); underTest.process(request); verify(transformer).transform(streamSource, streamResult); }
@Test public void testProcessShouldNotCallTransformWhenImageXml() throws ApplicationException, TransformerException { given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(CONTENT_TYPE_SVGXML); underTest.process(request); verify(request, never()).setBody(Mockito.anyString()); }
@Test public void testProcessShouldSetRequestBodyXML() throws ApplicationException, TransformerException { given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(CONTENT_TYPE_SOAPXML); given(transformerFactory.createTransformer()).willReturn(transformer); given(streamResultFactory.createStreamResult()).willReturn(streamResult); given(streamSourceFactory.createStreamSourceFromString(BODY)).willReturn(streamSource); given(streamResult.getWriter()).willReturn(writer); given(writer.toString()).willReturn(BODY); underTest.process(request); verify(request).setBody(BODY); }
@Test public void testProcessWhenRequestBodyIsNotXmlAndNotJsonShouldNotDoAnything() throws ApplicationException { given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn("application/fastinfoset"); underTest.process(request); verify(request, never()).setBody(Mockito.anyString()); }
@Test public void testProcessWhenContentTypeIsNullShouldNotDoAnything() throws ApplicationException { given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(null); underTest.process(request); verify(request, never()).setBody(Mockito.anyString()); }
@Test public void testProcessWhenTransformerFactoryThrowsExceptionShouldLogError() throws ApplicationException, TransformerConfigurationException { Whitebox.setInternalState(underTest, "logger", logger); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(CONTENT_TYPE_SOAPXML); given(transformerFactory.createTransformer()).willThrow(new TransformerConfigurationException("")); underTest.process(request); verify(logger).error(Mockito.anyString()); }
@Test public void testProcessShouldSetRequestBodyJSON() throws ApplicationException, TransformerException { given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(CONTENT_TYPE_JSON); given(request.getBody()).willReturn("{\"test\":test}"); underTest.process(request); verify(request).setBody("{\n \"test\": \"test\"\n}"); }
@Test public void testProcessShouldNotSetRequestBodyJSONWhenBodyIsNull() throws ApplicationException, TransformerException { given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(CONTENT_TYPE_JSON); given(request.getBody()).willReturn(null); underTest.process(request); verify(request, never()).setBody(Mockito.anyString()); }
@Test public void testProcessWhenJsonParserThrowsExceptionShouldLogError() throws ApplicationException { Whitebox.setInternalState(underTest, "logger", logger); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(CONTENT_TYPE_JSON); given(request.getBody()).willReturn("{foo: ['bar', 'baz',]*/*-}"); underTest.process(request); verify(logger).error(Mockito.anyString()); }
@Test public void testProcessWhenJsonParserThrowsExceptionShouldLeaveOriginalBodyAsItIs() throws ApplicationException { Whitebox.setInternalState(underTest, "logger", logger); given(request.getHeader(CONTENT_TYPE_HEADER)).willReturn(CONTENT_TYPE_JSON); given(request.getBody()).willReturn("invalidjson{:}"); underTest.process(request); assertEquals("invalidjson{:}", request.getBody()); }