### Question:
UploadedDocumentServiceImpl implements UploadedDocumentService,
                                                    Serializable { @Override public DocumentUploadResponse uploadContent(final DocumentUploadChunk chunk) { try { DocumentUploadSession session = getSession(chunk); if (session.isComplete()) { return merge(session); } else { return new DocumentUploadResponse(DocumentUploadResponse.DocumentUploadState.UPLOADING, true); } } catch (Exception ex) { logger.warn("Cannot upload chunk {}: {}", chunk.getDocumentName(), ex); return new DocumentUploadResponse(DocumentUploadResponse.DocumentUploadState.FINISH, false); } } @Inject  UploadedDocumentServiceImpl(UploadedDocumentStorage storage); @Override DocumentUploadResponse uploadContent(final DocumentUploadChunk chunk); @Override void removeContent(String id); @PreDestroy void clear();  }### Answer:
@Test public void testSuccessfullyUpload() { DocumentUploadChunk chunk1 = new DocumentUploadChunk(DOC_ID, DOC_NAME, 0, 3, Base64.getEncoder().encodeToString(PART_1.getBytes())); DocumentUploadResponse response = uploadedDocumentService.uploadContent(chunk1); validateResponse(response, DocumentUploadResponse.DocumentUploadState.UPLOADING, true); DocumentUploadSession session = uploadedDocumentService.getUploadSessions().get(DOC_ID); Assertions.assertThat(session).isNotNull(); Assertions.assertThat(session.getChunks()) .isNotNull() .hasSize(1) .containsExactly(chunk1); checkParts(1); DocumentUploadChunk chunk2 = new DocumentUploadChunk(DOC_ID, DOC_NAME, 1, 3, Base64.getEncoder().encodeToString(PART_2.getBytes())); response = uploadedDocumentService.uploadContent(chunk2); validateResponse(response, DocumentUploadResponse.DocumentUploadState.UPLOADING, true); session = uploadedDocumentService.getUploadSessions().get(DOC_ID); Assertions.assertThat(session).isNotNull(); Assertions.assertThat(session.getChunks()) .isNotNull() .hasSize(2) .containsExactly(chunk1, chunk2); checkParts(2); DocumentUploadChunk chunk3 = new DocumentUploadChunk(DOC_ID, DOC_NAME, 3, 3, Base64.getEncoder().encodeToString(PART_3.getBytes())); response = uploadedDocumentService.uploadContent(chunk3); validateResponse(response, DocumentUploadResponse.DocumentUploadState.FINISH, true); session = uploadedDocumentService.getUploadSessions().get(DOC_ID); Assertions.assertThat(session).isNull(); File file = storage.getRootFolder().resolve(DOC_ID).resolve(DOC_NAME).toFile(); Assertions.assertThat(file) .isNotNull() .exists(); String content = new String(storage.getContent(DOC_ID)); Assertions.assertThat(content) .isNotNull() .isEqualTo(PART_1 + PART_2 + PART_3); }
@Test public void testUploadWithError() throws Exception { doThrow(new RuntimeException("Exception")).when(storage).uploadContentChunk(any()); DocumentUploadChunk chunk = new DocumentUploadChunk(DOC_ID, DOC_NAME, 0, 3, Base64.getEncoder().encodeToString(PART_1.getBytes())); DocumentUploadResponse response = uploadedDocumentService.uploadContent(chunk); validateResponse(response, DocumentUploadResponse.DocumentUploadState.FINISH, false); }