WilmaHttpResponseWriter extends WilmaHttpEntityWriter<WilmaHttpResponse> { @Override public boolean write(final WilmaHttpResponse response, final boolean bodyDecompressed) { boolean successful = false; String outputFile = INITIAL_FILENAME; try { String messageLoggerId = response.getWilmaMessageLoggerId(); String messageId = response.getWilmaMessageId(); outputFile = getOutputFileName(messageLoggerId, bodyDecompressed); BufferedWriter writer = bufferedWriterFactory.createBufferedWriter(outputFile, OUTPUT_BUFFER_SIZE); if (writer != null) { writeWilmaLoggerId(writer, messageId); String headers = prepareHeadersInfo(response); writeHeaders(writer, headers); int statusCode = response.getStatusCode(); writeStatusCode(statusCode, writer); String body = response.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 WilmaHttpResponse response, final boolean bodyDecompressed);  }
@Test public void testWriteShouldContentToWriter() throws IOException { doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willReturn(bufferedWriter); given(response.getWilmaMessageLoggerId()).willReturn(MESSAGE_ID); given(response.getWilmaMessageId()).willReturn(MESSAGE_ID); given(response.getBody()).willReturn(BODY); underTest.write(response, true); 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_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willReturn(bufferedWriter); given(response.getBody()).willReturn(null); given(response.getWilmaMessageLoggerId()).willReturn(MESSAGE_ID); underTest.write(response, true); verify(bufferedWriter, never()).append(null); }
@Test public void testWriteWhenBufferedWriterThrowsIOExceptionShouldLogError() throws IOException { Whitebox.setInternalState(underTest, "logger", logger); IOException e = new IOException(); doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willThrow(e); given(response.getWilmaMessageLoggerId()).willReturn(MESSAGE_ID); underTest.write(response, true); verify(logger).error(COULD_NOT_WRITE_MESSAGE_ERROR, e); }
@Test public void testWriteWhenCannotCloseFileShouldLogError() throws IOException { Whitebox.setInternalState(underTest, "logger", logger); IOException e = new IOException(); doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willReturn(bufferedWriter); given(response.getWilmaMessageLoggerId()).willReturn(MESSAGE_ID); willThrow(e).given(bufferedWriter).close(); underTest.write(response, true); verify(logger).error(COULD_NOT_WRITE_MESSAGE_ERROR, e); }
@Test public void testWriteWhenWriterIsNullShouldDoNothing() throws IOException { doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willReturn(null); given(response.getWilmaMessageLoggerId()).willReturn(MESSAGE_ID); underTest.write(response, true); verify(bufferedWriter, never()).append(Mockito.anyString()); }
@Test public void testWriteShouldAppendStatusCodeToWriter() throws IOException { doReturn(OUTPUT_FILE).when(underTest).getOutputFileName(MESSAGE_ID, true); given(bufferedWriterFactory.createBufferedWriter(OUTPUT_FILE, OUTPUT_BUFFER_SIZE)).willReturn(bufferedWriter); given(response.getWilmaMessageLoggerId()).willReturn(MESSAGE_ID); given(response.getStatusCode()).willReturn(200); underTest.write(response, true); verify(bufferedWriter).append("Status code:200"); }