ExportService { public Mono<Task.Result> export(Progress progress, Username username) { return Mono.usingWhen( Mono.fromCallable(() -> zipMailboxesContent(progress, username)), inputStream -> export(progress, username, inputStream), this::closeResource, (inputStream, throwable) -> closeResource(inputStream), this::closeResource ); } @Inject  ExportService(MailboxBackup mailboxBackup, BlobStore blobStore, BlobExportMechanism blobExport, UsersRepository usersRepository); Mono<Task.Result> export(Progress progress, Username username);  }
@Test void exportUserMailboxesDataShouldReturnCompletedWhenUserDoesNotExist() { assertThat(testee.export(progress, UNKNOWN_USER).block()) .isEqualTo(Task.Result.COMPLETED); }
@Test void exportUserMailboxesDataShouldReturnCompletedWhenExistingUserWithoutMailboxes() { assertThat(testee.export(progress, BOB).block()) .isEqualTo(Task.Result.COMPLETED); }
@Test void exportUserMailboxesDataShouldReturnCompletedWhenExistingUser() throws Exception { createAMailboxWithAMail(MESSAGE_CONTENT); assertThat(testee.export(progress, BOB).block()) .isEqualTo(Task.Result.COMPLETED); }
@Test void exportUserMailboxesDataShouldProduceAnEmptyZipWhenUserDoesNotExist() throws Exception { testee.export(progress, UNKNOWN_USER).block(); ZipAssert.assertThatZip(new FileInputStream(getFileUrl())) .hasNoEntry(); }
@Test void exportUserMailboxesDataShouldProduceAnEmptyZipWhenExistingUserWithoutAnyMailboxes() throws Exception { testee.export(progress, BOB).block(); ZipAssert.assertThatZip(new FileInputStream(getFileUrl())) .hasNoEntry(); }
@Test void exportUserMailboxesDataShouldProduceAZipWithEntry() throws Exception { ComposedMessageId id = createAMailboxWithAMail(MESSAGE_CONTENT); testee.export(progress, BOB).block(); ZipAssert.assertThatZip(new FileInputStream(getFileUrl())) .containsOnlyEntriesMatching( ZipAssert.EntryChecks.hasName(INBOX + "/").isDirectory(), ZipAssert.EntryChecks.hasName(id.getMessageId().serialize()).hasStringContent(MESSAGE_CONTENT)); }
@Test void exportUserMailboxesDataShouldProduceAFileWithExpectedExtension() throws Exception { createAMailboxWithAMail(MESSAGE_CONTENT); testee.export(progress, BOB).block(); assertThat(Files.getFileExtension(getFileUrl())).isEqualTo(FileExtension.ZIP.getExtension()); }
@Test void exportUserMailboxesDataShouldProduceAFileWithExpectedName() throws Exception { createAMailboxWithAMail(MESSAGE_CONTENT); testee.export(progress, BOB).block(); File file = new File(getFileUrl()); assertThat(file.getName()).startsWith(FILE_PREFIX + BOB.asString()); }
@Test void exportUserMailboxesWithSizableDataShouldProduceAFile() throws Exception { ComposedMessageId id = createAMailboxWithAMail(TWELVE_MEGABYTES_STRING); testee.export(progress, BOB).block(); ZipAssert.assertThatZip(new FileInputStream(getFileUrl())) .containsOnlyEntriesMatching( ZipAssert.EntryChecks.hasName(INBOX + "/").isDirectory(), ZipAssert.EntryChecks.hasName(id.getMessageId().serialize()).hasStringContent(TWELVE_MEGABYTES_STRING)); }
@Test void exportUserMailboxesDataShouldDeleteBlobAfterCompletion() throws Exception { createAMailboxWithAMail(MESSAGE_CONTENT); testee.export(progress, BOB).block(); String fileName = Files.getNameWithoutExtension(getFileUrl()); String blobId = fileName.substring(fileName.lastIndexOf("-") + 1); SoftAssertions.assertSoftly(softly -> { assertThatThrownBy(() -> testSystem.blobStore.read(testSystem.blobStore.getDefaultBucketName(), FACTORY.from(blobId))) .isInstanceOf(ObjectNotFoundException.class); assertThatThrownBy(() -> testSystem.blobStore.read(testSystem.blobStore.getDefaultBucketName(), FACTORY.from(blobId))) .hasMessage(String.format("blob '%s' not found in bucket '%s'", blobId, testSystem.blobStore.getDefaultBucketName().asString())); }); }
@Test void exportUserMailboxesDataShouldReturnSuccessWhenBlobDeletingFails() throws Exception { createAMailboxWithAMail(MESSAGE_CONTENT); doReturn(Mono.error(new RuntimeException())) .when(testSystem.blobStore) .delete(any(), any()); Task.Result result = testee.export(progress, BOB).block(); assertThat(result).isEqualTo(Task.Result.COMPLETED); }
@Test void exportUserMailboxesDataShouldUpdateProgressWhenExporting() { doReturn(Mono.error(new RuntimeException())) .when(testSystem.blobStore) .save(any(), any(InputStream.class), any()); testee.export(progress, BOB, new ByteArrayInputStream(MESSAGE_CONTENT.getBytes())).block(); assertThat(progress.getStage()).isEqualTo(ExportService.Stage.EXPORTING); }
@Test void exportUserMailboxesDataShouldUpdateProgressWhenComplete() { testee.export(progress, BOB).block(); assertThat(progress.getStage()).isEqualTo(ExportService.Stage.COMPLETED); }