WilmaHttpRequestWriter extends WilmaHttpEntityWriter<WilmaHttpRequest> { @Override public boolean write(final WilmaHttpRequest request, final boolean bodyDecompressed) { boolean successful = false; String outputFile = INITIAL_FILENAME; try { String messageLoggerId = request.getWilmaMessageLoggerId(); String messageId = request.getWilmaMessageId(); outputFile = getOutputFileName(messageLoggerId, bodyDecompressed); BufferedWriter writer = bufferedWriterFactory.createBufferedWriter(outputFile, OUTPUT_BUFFER_SIZE); if (writer != null) { String requestLine = request.getRemoteAddr() + " " + request.getRequestLine(); writeRequestLine(requestLine, writer); writeWilmaLoggerId(writer, messageId); String headers = prepareHeadersInfo(request); writeHeaders(writer, headers); String body = request.getBody(); writeBody(writer, body); writer.close(); successful = true; } } catch (IOException e) { logger.error("Could not write message to file:" + outputFile + "!", e); } return successful; }  @Override boolean write(final WilmaHttpRequest request, final boolean bodyDecompressed);  }
@Test public void testWriteShouldWriteContentToWriter() throws IOException { doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_LOGGER_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willReturn(bufferedWriter); given(request.getWilmaMessageLoggerId()).willReturn(MESSAGE_LOGGER_ID); given(request.getWilmaMessageId()).willReturn(MESSAGE_ID); given(request.getRequestLine()).willReturn(REQUEST_LINE); given(request.getRemoteAddr()).willReturn(REMOTE_ADDR); given(request.getBody()).willReturn(BODY); underTest.write(request, true); verify(bufferedWriter).append(REMOTE_ADDR + " " + REQUEST_LINE); verify(bufferedWriter).append(WilmaHttpRequest.WILMA_LOGGER_ID + ":" + MESSAGE_ID); verify(bufferedWriter).append("{" + HEADERS + "=" + HEADERS + "}\n+{" + EXTRA_HEADERS + "=" + EXTRA_HEADERS + "}\n-[" + EXTRA_HEADERS_REMOVE + "]"); verify(bufferedWriter).append(BODY); }
@Test public void testWriteShouldNotAppendBodyWhenItIsNull() throws IOException { doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_LOGGER_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willReturn(bufferedWriter); given(request.getBody()).willReturn(null); given(request.getWilmaMessageLoggerId()).willReturn(MESSAGE_LOGGER_ID); given(request.getRequestLine()).willReturn(REQUEST_LINE); given(request.getRemoteAddr()).willReturn(REMOTE_ADDR); underTest.write(request, true); verify(bufferedWriter, never()).append(null); }
@Test public void testWriteWhenBufferedWriterThrowsIOExceptionShouldLogError() throws IOException { IOException e = new IOException(); doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_LOGGER_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willThrow(e); given(request.getWilmaMessageLoggerId()).willReturn(MESSAGE_LOGGER_ID); underTest.write(request, true); verify(logger).error(COULD_NOT_WRITE_MESSAGE_ERROR, e); }
@Test public void testWriteWhenCannotCloseFileShouldLogError() throws IOException { IOException e = new IOException(); doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_LOGGER_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willReturn(bufferedWriter); given(request.getWilmaMessageLoggerId()).willReturn(MESSAGE_LOGGER_ID); willThrow(e).given(bufferedWriter).close(); underTest.write(request, true); verify(logger).error(COULD_NOT_WRITE_MESSAGE_ERROR, e); }
@Test public void testWriteWhenWriterIsNullShouldDoNothing() throws IOException { doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_LOGGER_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willReturn(null); given(request.getWilmaMessageLoggerId()).willReturn(MESSAGE_LOGGER_ID); underTest.write(request, true); verify(bufferedWriter, times(0)).append(Mockito.anyString()); }