This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit cf844dff46c8c6f9ef99198233676250eeb51af9 Author: Rene Cordier <[email protected]> AuthorDate: Tue Feb 11 11:49:59 2020 +0700 JAMES-3057 Separate the rename and create mailbox logic in CassandraMailboxMapper --- .../cassandra/mail/CassandraMailboxMapper.java | 24 +++++++++++ .../cassandra/mail/CassandraMailboxMapperTest.java | 46 ++++++++++++++++++++++ .../store/mail/model/MailboxMapperTest.java | 4 +- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java index 5b01741..5aebd17 100644 --- a/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java +++ b/mailbox/cassandra/src/main/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapper.java @@ -46,6 +46,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.github.steveash.guavate.Guavate; +import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import reactor.core.publisher.Flux; @@ -166,6 +167,29 @@ public class CassandraMailboxMapper implements MailboxMapper { } @Override + public MailboxId create(Mailbox mailbox) throws MailboxException { + Preconditions.checkArgument(mailbox.getMailboxId() == null, "A mailbox we want to create should not have a mailboxId set already"); + + CassandraId cassandraId = CassandraId.timeBased(); + mailbox.setMailboxId(cassandraId); + if (!tryCreate(mailbox, cassandraId).block()) { + throw new MailboxExistsException(mailbox.generateAssociatedPath().asString()); + } + return cassandraId; + } + + private Mono<Boolean> tryCreate(Mailbox cassandraMailbox, CassandraId cassandraId) { + return mailboxPathV2DAO.save(cassandraMailbox.generateAssociatedPath(), cassandraId) + .flatMap(isCreated -> { + if (isCreated) { + return mailboxDAO.save(cassandraMailbox) + .thenReturn(isCreated); + } + return Mono.just(isCreated); + }); + } + + @Override public MailboxId save(Mailbox mailbox) throws MailboxException { CassandraId cassandraId = retrieveId(mailbox); mailbox.setMailboxId(cassandraId); diff --git a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapperTest.java b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapperTest.java index e62e158..d5fbf3e 100644 --- a/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapperTest.java +++ b/mailbox/cassandra/src/test/java/org/apache/james/mailbox/cassandra/mail/CassandraMailboxMapperTest.java @@ -142,6 +142,25 @@ class CassandraMailboxMapperTest { } @Test + void createShouldBeConsistentWhenFailToPersistMailbox() { + doReturn(Mono.error(new RuntimeException("mock exception"))) + .when(mailboxDAO) + .save(inbox); + + inbox.setMailboxId(null); + doQuietly(() -> testee.create(inbox)); + + SoftAssertions.assertSoftly(softly -> { + softly.assertThatThrownBy(() -> testee.findMailboxByPath(inboxPath)) + .isInstanceOf(MailboxNotFoundException.class); + softly.assertThat(testee.findMailboxWithPathLike(inboxSearchQuery)) + .isEmpty(); + softly.assertThat(testee.findMailboxWithPathLike(allMailboxesSearchQuery)) + .isEmpty(); + }); + } + + @Test void saveOnCreateShouldBeConsistentWhenFailToPersistMailbox() { doReturn(Mono.error(new RuntimeException("mock exception"))) .when(mailboxDAO) @@ -330,6 +349,33 @@ class CassandraMailboxMapperTest { .isNotEqualTo(testee.findMailboxById(inboxId).getName()); } + @Disabled("JAMES-3057 org.apache.james.mailbox.exception.MailboxNotFoundException: INBOX can not be found") + @Test + void createAfterPreviousFailedCreateShouldCreateAMailbox() { + when(mailboxDAO.save(inbox)) + .thenReturn(Mono.error(new RuntimeException("mock exception"))) + .thenCallRealMethod(); + + inbox.setMailboxId(null); + doQuietly(() -> testee.create(inbox)); + inbox.setMailboxId(null); + doQuietly(() -> testee.create(inbox)); + + SoftAssertions.assertSoftly(Throwing.consumer(softly -> { + softly(softly) + .assertThat(testee.findMailboxByPath(inboxPath)) + .isEqualTo(inbox); + softly.assertThat(testee.findMailboxWithPathLike(inboxSearchQuery)) + .hasOnlyOneElementSatisfying(searchMailbox -> softly(softly) + .assertThat(searchMailbox) + .isEqualTo(inbox)); + softly.assertThat(testee.findMailboxWithPathLike(allMailboxesSearchQuery)) + .hasOnlyOneElementSatisfying(searchMailbox -> softly(softly) + .assertThat(searchMailbox) + .isEqualTo(inbox)); + })); + } + @Disabled("JAMES-3056 org.apache.james.mailbox.exception.MailboxNotFoundException: 'mailboxId' can not be found") @Test void saveAfterPreviousFailedSaveShouldCreateAMailbox() { diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxMapperTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxMapperTest.java index 125292b..9963ba7 100644 --- a/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxMapperTest.java +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/mail/model/MailboxMapperTest.java @@ -92,8 +92,8 @@ public abstract class MailboxMapperTest { benwaInboxMailbox.setMailboxId(null); mailboxMapper.create(benwaInboxMailbox); - MailboxAssert.assertThat(mailboxMapper.findMailboxByPath(benwaInboxPath)).isEqualTo(benwaInboxMailbox); - MailboxAssert.assertThat(mailboxMapper.findMailboxById(benwaInboxMailbox.getMailboxId())).isEqualTo(benwaInboxMailbox); + assertThat(mailboxMapper.findMailboxByPath(benwaInboxPath)).isEqualTo(benwaInboxMailbox); + assertThat(mailboxMapper.findMailboxById(benwaInboxMailbox.getMailboxId())).isEqualTo(benwaInboxMailbox); } @Test --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
