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 d9d098d9a1b326a0266f836e6e96e8051a76384e Author: Rene Cordier <[email protected]> AuthorDate: Tue Feb 11 11:49:32 2020 +0700 JAMES-3057 Add a create default method in MailboxMapper --- .../james/mailbox/store/StoreMailboxManager.java | 2 +- .../james/mailbox/store/mail/MailboxMapper.java | 13 +++++++- .../store/mail/model/MailboxMapperTest.java | 37 +++++++++++++++++++++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java index bcad33d..f023f5f 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/StoreMailboxManager.java @@ -385,7 +385,7 @@ public class StoreMailboxManager implements MailboxManager { Mailbox mailbox = doCreateMailbox(mailboxPath); MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(mailboxSession); try { - mapper.execute(Mapper.toTransaction(() -> mailboxIds.add(mapper.save(mailbox)))); + mapper.execute(Mapper.toTransaction(() -> mailboxIds.add(mapper.create(mailbox)))); // notify listeners eventBus.dispatch(EventFactory.mailboxAdded() .randomEventId() diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/MailboxMapper.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/MailboxMapper.java index 3ca9c5f..65a7489 100644 --- a/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/MailboxMapper.java +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/mail/MailboxMapper.java @@ -32,13 +32,24 @@ import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.search.MailboxQuery; import org.apache.james.mailbox.store.transaction.Mapper; +import com.google.common.base.Preconditions; + /** * Mapper for {@link Mailbox} actions. A {@link MailboxMapper} has a lifecycle from the start of a request * to the end of the request. * */ public interface MailboxMapper extends Mapper { - + + /** + * Create the given {@link Mailbox} to the underlying storage + */ + default MailboxId create(Mailbox mailbox) throws MailboxException { + Preconditions.checkArgument(mailbox.getMailboxId() == null, "A mailbox we want to create should not have a mailboxId set already"); + + return save(mailbox); + } + /** * Save the give {@link Mailbox} to the underlying storage */ 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 e90e01c..125292b 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 @@ -82,12 +82,47 @@ public abstract class MailboxMapperTest { } @Test - void findMailboxByPathWhenAbsentShouldFail() throws MailboxException { + void findMailboxByPathWhenAbsentShouldFail() { assertThatThrownBy(() -> mailboxMapper.findMailboxByPath(MailboxPath.forUser(BENWA, "INBOX"))) .isInstanceOf(MailboxNotFoundException.class); } @Test + void createShouldPersistTheMailbox() throws MailboxException { + benwaInboxMailbox.setMailboxId(null); + mailboxMapper.create(benwaInboxMailbox); + + MailboxAssert.assertThat(mailboxMapper.findMailboxByPath(benwaInboxPath)).isEqualTo(benwaInboxMailbox); + MailboxAssert.assertThat(mailboxMapper.findMailboxById(benwaInboxMailbox.getMailboxId())).isEqualTo(benwaInboxMailbox); + } + + @Test + void createShouldThrowWhenMailboxAlreadyExists() throws MailboxException { + benwaInboxMailbox.setMailboxId(null); + mailboxMapper.create(benwaInboxMailbox); + + Mailbox mailbox = new Mailbox(benwaInboxMailbox); + mailbox.setMailboxId(null); + + assertThatThrownBy(() -> mailboxMapper.create(mailbox)) + .isInstanceOf(MailboxExistsException.class); + } + + @Test + void createShouldSetAMailboxIdForMailbox() throws MailboxException { + benwaInboxMailbox.setMailboxId(null); + MailboxId mailboxId = mailboxMapper.create(benwaInboxMailbox); + + assertThat(mailboxId).isNotNull(); + } + + @Test + void createShouldThrowWhenMailboxIdNotNull() { + assertThatThrownBy(() -> mailboxMapper.create(benwaInboxMailbox)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test void saveShouldPersistTheMailbox() throws MailboxException { mailboxMapper.save(benwaInboxMailbox); assertThat(mailboxMapper.findMailboxByPath(benwaInboxPath)).isEqualTo(benwaInboxMailbox); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
