This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit d86297c827fc63ea2e2ae22ec06bdcea11b77c59 Author: Tran Tien Duc <[email protected]> AuthorDate: Thu Feb 13 17:03:05 2020 +0700 JAMES-3056 Consistency test on failing renaming --- .../CassandraMailboxManagerConsistencyTest.java | 160 +++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerConsistencyTest.java b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerConsistencyTest.java index bd6f93f..e2977fc 100644 --- a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerConsistencyTest.java +++ b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/CassandraMailboxManagerConsistencyTest.java @@ -58,6 +58,7 @@ class CassandraMailboxManagerConsistencyTest { private static final Username USER = Username.of("user"); private static final String INBOX = "INBOX"; + private static final String INBOX_RENAMED = "INBOX_RENAMED"; @RegisterExtension static CassandraClusterExtension cassandra = new CassandraClusterExtension(MailboxAggregateModule.MODULE_WITH_QUOTA); @@ -66,6 +67,7 @@ class CassandraMailboxManagerConsistencyTest { private MailboxSession mailboxSession; private MailboxPath inboxPath; + private MailboxPath inboxPathRenamed; private MailboxQuery.UserBound allMailboxesSearchQuery; private CassandraMailboxDAO mailboxDAO; @@ -92,6 +94,7 @@ class CassandraMailboxManagerConsistencyTest { mailboxSession = testee.createSystemSession(USER); inboxPath = MailboxPath.forUser(USER, INBOX); + inboxPathRenamed = MailboxPath.forUser(USER, INBOX_RENAMED); allMailboxesSearchQuery = MailboxQuery.builder() .userAndNamespaceFrom(inboxPath) .expression(Wildcard.INSTANCE) @@ -211,6 +214,163 @@ class CassandraMailboxManagerConsistencyTest { } } + @Nested + class FailsOnRename { + + @Test + void renameShouldBeConsistentWhenMailboxDaoFails() throws Exception { + MailboxId inboxId = testee.createMailbox(inboxPath, mailboxSession) + .get(); + + doReturn(Mono.error(new RuntimeException("mock exception"))) + .when(mailboxDAO) + .save(any(Mailbox.class)); + + doQuietly(() -> testee.renameMailbox(inboxPath, inboxPathRenamed, mailboxSession)); + + SoftAssertions.assertSoftly(Throwing.consumer(softly -> { + softly.assertThat(testee.search(allMailboxesSearchQuery, mailboxSession)) + .hasOnlyOneElementSatisfying(mailboxMetaData -> { + softly.assertThat(mailboxMetaData.getId()).isEqualTo(inboxId); + softly.assertThat(mailboxMetaData.getPath()).isEqualTo(inboxPath); + }); + softly.assertThat(testee.list(mailboxSession)) + .containsExactly(inboxPath); + })); + } + + @Test + void renameShouldBeConsistentWhenMailboxPathDaoFails() throws Exception { + MailboxId inboxId = testee.createMailbox(inboxPath, mailboxSession) + .get(); + + doReturn(Mono.error(new RuntimeException("mock exception"))) + .when(mailboxPathV2DAO) + .save(any(MailboxPath.class), any(CassandraId.class)); + + doQuietly(() -> testee.renameMailbox(inboxPath, inboxPathRenamed, mailboxSession)); + + SoftAssertions.assertSoftly(Throwing.consumer(softly -> { + softly.assertThat(testee.search(allMailboxesSearchQuery, mailboxSession)) + .hasOnlyOneElementSatisfying(mailboxMetaData -> { + softly.assertThat(mailboxMetaData.getId()).isEqualTo(inboxId); + softly.assertThat(mailboxMetaData.getPath()).isEqualTo(inboxPath); + }); + softly.assertThat(testee.list(mailboxSession)) + .containsExactly(inboxPath); + })); + } + + @Disabled("JAMES-3056 cannot create a new mailbox because 'INBOX_RENAMED' already exists") + @Test + void createNewMailboxAfterAFailedRenameShouldCreateThatMailboxWhenMailboxDaoFails() throws Exception { + MailboxId inboxId = testee.createMailbox(inboxPath, mailboxSession) + .get(); + + doReturn(Mono.error(new RuntimeException("mock exception"))) + .doCallRealMethod() + .when(mailboxDAO) + .save(any(Mailbox.class)); + + doQuietly(() -> testee.renameMailbox(inboxPath, inboxPathRenamed, mailboxSession)); + MailboxId newMailboxId = testee.createMailbox(inboxPathRenamed, mailboxSession) + .get(); + + SoftAssertions.assertSoftly(Throwing.consumer(softly -> { + softly.assertThat(testee.search(allMailboxesSearchQuery, mailboxSession)) + .hasSize(2) + .anySatisfy(mailboxMetaData -> { + softly.assertThat(mailboxMetaData.getId()).isEqualTo(inboxId); + softly.assertThat(mailboxMetaData.getPath()).isEqualTo(inboxPath); + }) + .anySatisfy(mailboxMetaData -> { + softly.assertThat(mailboxMetaData.getId()).isEqualTo(newMailboxId); + softly.assertThat(mailboxMetaData.getPath()).isEqualTo(inboxPathRenamed); + }); + softly.assertThat(testee.list(mailboxSession)) + .containsExactlyInAnyOrder(inboxPath, inboxPathRenamed); + })); + } + + @Test + void createNewMailboxAfterAFailedRenameShouldCreateThatMailboxWhenMailboxPathDaoFails() throws Exception { + MailboxId inboxId = testee.createMailbox(inboxPath, mailboxSession) + .get(); + + doReturn(Mono.error(new RuntimeException("mock exception"))) + .doCallRealMethod() + .when(mailboxPathV2DAO) + .save(any(MailboxPath.class), any(CassandraId.class)); + + doQuietly(() -> testee.renameMailbox(inboxPath, inboxPathRenamed, mailboxSession)); + MailboxId newMailboxId = testee.createMailbox(inboxPathRenamed, mailboxSession) + .get(); + + SoftAssertions.assertSoftly(Throwing.consumer(softly -> { + softly.assertThat(testee.search(allMailboxesSearchQuery, mailboxSession)) + .hasSize(2) + .anySatisfy(mailboxMetaData -> { + softly.assertThat(mailboxMetaData.getId()).isEqualTo(inboxId); + softly.assertThat(mailboxMetaData.getPath()).isEqualTo(inboxPath); + }) + .anySatisfy(mailboxMetaData -> { + softly.assertThat(mailboxMetaData.getId()).isEqualTo(newMailboxId); + softly.assertThat(mailboxMetaData.getPath()).isEqualTo(inboxPathRenamed); + }); + softly.assertThat(testee.list(mailboxSession)) + .containsExactlyInAnyOrder(inboxPath, inboxPathRenamed); + })); + } + + @Disabled("JAMES-3056 creating mailbox returns an empty Optional") + @Test + void deleteMailboxAfterAFailedRenameShouldCreateThatMailboxWhenMailboxDaoFails() throws Exception { + MailboxId inboxId = testee.createMailbox(inboxPath, mailboxSession) + .get(); + + doReturn(Mono.error(new RuntimeException("mock exception"))) + .doCallRealMethod() + .when(mailboxDAO) + .save(any(Mailbox.class)); + + doQuietly(() -> testee.renameMailbox(inboxPath, inboxPathRenamed, mailboxSession)); + testee.deleteMailbox(inboxId, mailboxSession); + assertThat(testee.createMailbox(inboxPathRenamed, mailboxSession)) + .isNotEmpty(); + } + + @Test + void deleteMailboxAfterAFailedRenameShouldCreateThatMailboxWhenMailboxPathDaoFails() throws Exception { + MailboxId inboxId = testee.createMailbox(inboxPath, mailboxSession) + .get(); + + doReturn(Mono.error(new RuntimeException("mock exception"))) + .doCallRealMethod() + .when(mailboxPathV2DAO) + .save(any(MailboxPath.class), any(CassandraId.class)); + + doQuietly(() -> testee.renameMailbox(inboxPath, inboxPathRenamed, mailboxSession)); + testee.deleteMailbox(inboxId, mailboxSession); + MailboxId newMailboxId = testee.createMailbox(inboxPathRenamed, mailboxSession) + .get(); + + SoftAssertions.assertSoftly(Throwing.consumer(softly -> { + softly.assertThat(testee.search(allMailboxesSearchQuery, mailboxSession)) + .hasOnlyOneElementSatisfying(mailboxMetaData -> { + softly.assertThat(mailboxMetaData.getId()).isEqualTo(newMailboxId); + softly.assertThat(mailboxMetaData.getPath()).isEqualTo(inboxPathRenamed); + }); + softly.assertThat(testee.list(mailboxSession)) + .containsExactlyInAnyOrder(inboxPathRenamed); + })); + } + } + + @Nested + class FailsOnDelete { + // TODO + } + private void doQuietly(ThrowingRunnable runnable) { try { runnable.run(); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
