DefaultMailboxBackup implements MailboxBackup { @Override public void backupAccount(Username username, OutputStream destination) throws IOException, MailboxException { MailboxSession session = mailboxManager.createSystemSession(username); List<MailAccountContent> accountContents = getAccountContentForUser(session); List<MailboxWithAnnotations> mailboxes = accountContents.stream() .map(MailAccountContent::getMailboxWithAnnotations) .collect(Guavate.toImmutableList()); Stream<MessageResult> messages = allMessagesForUser(accountContents); archive(mailboxes, messages, destination); } @Inject  DefaultMailboxBackup(MailboxManager mailboxManager, ArchiveService archiveService, MailArchiveRestorer archiveRestorer); @Override void backupAccount(Username username, OutputStream destination); @Override Publisher<BackupStatus> restore(Username username, InputStream source);  }
@Test void doBackupWithoutMailboxShouldStoreEmptyBackup() throws Exception { ByteArrayOutputStream destination = new ByteArrayOutputStream(BUFFER_SIZE); backup.backupAccount(USERNAME_1, destination); try (ZipAssert zipAssert = ZipAssert.assertThatZip(destination)) { zipAssert.containsOnlyEntriesMatching(); } }
@Test void doBackupWithoutMessageShouldStoreAnArchiveWithOnlyOneEntry() throws Exception { ByteArrayOutputStream destination = new ByteArrayOutputStream(BUFFER_SIZE); createMailbox(sessionUser, MAILBOX_PATH_USER1_MAILBOX1); backup.backupAccount(USERNAME_1, destination); try (ZipAssert zipAssert = ZipAssert.assertThatZip(destination)) { zipAssert.containsOnlyEntriesMatching(EntryChecks.hasName(MAILBOX_1_NAME + "/").isDirectory()); } }
@Test void doBackupMailboxWithAnnotationShouldStoreAnArchiveWithMailboxAndAnnotation() throws Exception { ByteArrayOutputStream destination = new ByteArrayOutputStream(BUFFER_SIZE); createMailbox(sessionUser, MAILBOX_PATH_USER1_MAILBOX1); mailboxManager.updateAnnotations(MAILBOX_PATH_USER1_MAILBOX1, sessionUser, WITH_ANNOTATION_1); backup.backupAccount(USERNAME_1, destination); try (ZipAssert zipAssert = ZipAssert.assertThatZip(destination)) { zipAssert.containsOnlyEntriesMatching(EntryChecks.hasName(MAILBOX_1_NAME + "/").isDirectory(), EntryChecks.hasName(MAILBOX_1_NAME + "/" + EXPECTED_ANNOTATIONS_DIR + "/").isDirectory(), EntryChecks.hasName(MAILBOX_1_NAME + "/" + EXPECTED_ANNOTATIONS_DIR + "/" + ANNOTATION_1_KEY.asString()).hasStringContent(ANNOTATION_1_CONTENT) ); } }
@Test void doBackupWithOneMessageShouldStoreAnArchiveWithTwoEntries() throws Exception { ByteArrayOutputStream destination = new ByteArrayOutputStream(BUFFER_SIZE); createMailboxWithMessages(sessionUser, MAILBOX_PATH_USER1_MAILBOX1, getMessage1AppendCommand()); backup.backupAccount(USERNAME_1, destination); try (ZipAssert zipAssert = ZipAssert.assertThatZip(destination)) { zipAssert.containsOnlyEntriesMatching( EntryChecks.hasName(MAILBOX_1_NAME + "/").isDirectory(), EntryChecks.hasName(MESSAGE_ID_1.serialize()).hasStringContent(MESSAGE_CONTENT_1) ); } }
@Test void doBackupWithTwoMailboxesAndOneMessageShouldStoreAnArchiveWithThreeEntries() throws Exception { ByteArrayOutputStream destination = new ByteArrayOutputStream(BUFFER_SIZE); createMailboxWithMessages(sessionUser, MAILBOX_PATH_USER1_MAILBOX1, getMessage1AppendCommand()); createMailbox(sessionUser, MAILBOX_PATH_USER1_MAILBOX2); backup.backupAccount(USERNAME_1, destination); try (ZipAssert zipAssert = ZipAssert.assertThatZip(destination)) { zipAssert.containsOnlyEntriesMatching( EntryChecks.hasName(MAILBOX_1_NAME + "/").isDirectory(), EntryChecks.hasName(MAILBOX_2_NAME + "/").isDirectory(), EntryChecks.hasName(MESSAGE_ID_1.serialize()).hasStringContent(MESSAGE_CONTENT_1) ); } }
@Test void doBackupShouldOnlyArchiveTheMailboxOfTheUser() throws Exception { ByteArrayOutputStream destination = new ByteArrayOutputStream(BUFFER_SIZE); createMailboxWithMessages(sessionUser, MAILBOX_PATH_USER1_MAILBOX1, getMessage1AppendCommand()); createMailboxWithMessages(sessionOtherUser, MAILBOX_PATH_OTHER_USER_MAILBOX1, getMessage1OtherUserAppendCommand()); backup.backupAccount(USERNAME_1, destination); try (ZipAssert zipAssert = ZipAssert.assertThatZip(destination)) { zipAssert.containsOnlyEntriesMatching( EntryChecks.hasName(MAILBOX_1_NAME + "/").isDirectory(), EntryChecks.hasName(MESSAGE_ID_1.serialize()).hasStringContent(MESSAGE_CONTENT_1) ); } }