@Test public void doneShouldCopyMailFromOutboxToSentOnlyOneTimeWhenSuccess() throws Exception { MessageIdManager messageIdManager = mock(MessageIdManager.class); testee = new PostDequeueDecorator(mockedMailQueueItem, mailboxManager, new InMemoryMessageId.Factory(), messageIdManager, new SystemMailboxesProviderImpl(mailboxManager)); MailboxSession mailboxSession = mailboxManager.createSystemSession(USERNAME); mailboxManager.createMailbox(OUTBOX_MAILBOX_PATH, mailboxSession); MailboxId sentMailboxId = mailboxManager.createMailbox(SENT_MAILBOX_PATH, mailboxSession).get(); MessageManager messageManager = mailboxManager.getMailbox(OUTBOX_MAILBOX_PATH, mailboxSession); ComposedMessageId messageId = messageManager.appendMessage(AppendCommand.from(message), mailboxSession).getId(); mail.setAttribute(messageIdAttribute(messageId.getMessageId().serialize())); mail.setAttribute(USERNAME_ATTRIBUTE); ImmutableList<MessageResult> allMessages = ImmutableList.copyOf(messageManager.getMessages(MessageRange.all(), FetchGroup.MINIMAL, mailboxSession)); when(messageIdManager.getMessage(eq(messageId.getMessageId()), eq(FetchGroup.MINIMAL), any(MailboxSession.class))).thenReturn(allMessages); testee.done(true); testee.done(true); verify(messageIdManager, times(1)).getMessage(eq(messageId.getMessageId()), eq(FetchGroup.MINIMAL), any(MailboxSession.class)); verify(messageIdManager, times(1)).setInMailboxes(eq(messageId.getMessageId()), eq(ImmutableList.of(sentMailboxId)), any(MailboxSession.class)); verify(messageIdManager, times(1)).setFlags(eq(new Flags(Flag.SEEN)), eq(MessageManager.FlagsUpdateMode.ADD), eq(messageId.getMessageId()), eq(ImmutableList.of(sentMailboxId)), any(MailboxSession.class)); verifyNoMoreInteractions(messageIdManager); }
@Override public void done(boolean success) throws MailQueueException { mailQueueItem.done(success); if (success && mandatoryJmapMetaDataIsPresent()) { Optional<?> optionalRawMessageId = retrieveMessageId(); MessageId messageId = messageIdFactory.fromString((String) optionalRawMessageId.get()); Optional<String> username = retrieveUsername(); if (!getMail().getAttribute(IS_DELIVERED.getName()).isPresent()) { try { MailboxSession mailboxSession = mailboxManager.createSystemSession(Username.of(username.get())); moveFromOutboxToSentWithSeenFlag(messageId, mailboxSession); getMail().setAttribute(IS_DELIVERED); } catch (MailShouldBeInOutboxException e) { LOG.info("Message does not exist on Outbox anymore, it could have already been sent", e); } catch (MailboxException e) { throw new MailQueueException(e.getMessage(), e); } } } }
PostDequeueDecorator extends MailQueueItemDecorator { @Override public void done(boolean success) throws MailQueueException { mailQueueItem.done(success); if (success && mandatoryJmapMetaDataIsPresent()) { Optional<?> optionalRawMessageId = retrieveMessageId(); MessageId messageId = messageIdFactory.fromString((String) optionalRawMessageId.get()); Optional<String> username = retrieveUsername(); if (!getMail().getAttribute(IS_DELIVERED.getName()).isPresent()) { try { MailboxSession mailboxSession = mailboxManager.createSystemSession(Username.of(username.get())); moveFromOutboxToSentWithSeenFlag(messageId, mailboxSession); getMail().setAttribute(IS_DELIVERED); } catch (MailShouldBeInOutboxException e) { LOG.info("Message does not exist on Outbox anymore, it could have already been sent", e); } catch (MailboxException e) { throw new MailQueueException(e.getMessage(), e); } } } } }
PostDequeueDecorator extends MailQueueItemDecorator { @Override public void done(boolean success) throws MailQueueException { mailQueueItem.done(success); if (success && mandatoryJmapMetaDataIsPresent()) { Optional<?> optionalRawMessageId = retrieveMessageId(); MessageId messageId = messageIdFactory.fromString((String) optionalRawMessageId.get()); Optional<String> username = retrieveUsername(); if (!getMail().getAttribute(IS_DELIVERED.getName()).isPresent()) { try { MailboxSession mailboxSession = mailboxManager.createSystemSession(Username.of(username.get())); moveFromOutboxToSentWithSeenFlag(messageId, mailboxSession); getMail().setAttribute(IS_DELIVERED); } catch (MailShouldBeInOutboxException e) { LOG.info("Message does not exist on Outbox anymore, it could have already been sent", e); } catch (MailboxException e) { throw new MailQueueException(e.getMessage(), e); } } } } PostDequeueDecorator(MailQueueItem mailQueueItem,
                                MailboxManager mailboxManager,
                                Factory messageIdFactory,
                                MessageIdManager messageIdManager,
                                SystemMailboxesProvider systemMailboxesProvider); }
PostDequeueDecorator extends MailQueueItemDecorator { @Override public void done(boolean success) throws MailQueueException { mailQueueItem.done(success); if (success && mandatoryJmapMetaDataIsPresent()) { Optional<?> optionalRawMessageId = retrieveMessageId(); MessageId messageId = messageIdFactory.fromString((String) optionalRawMessageId.get()); Optional<String> username = retrieveUsername(); if (!getMail().getAttribute(IS_DELIVERED.getName()).isPresent()) { try { MailboxSession mailboxSession = mailboxManager.createSystemSession(Username.of(username.get())); moveFromOutboxToSentWithSeenFlag(messageId, mailboxSession); getMail().setAttribute(IS_DELIVERED); } catch (MailShouldBeInOutboxException e) { LOG.info("Message does not exist on Outbox anymore, it could have already been sent", e); } catch (MailboxException e) { throw new MailQueueException(e.getMessage(), e); } } } } PostDequeueDecorator(MailQueueItem mailQueueItem,
                                MailboxManager mailboxManager,
                                Factory messageIdFactory,
                                MessageIdManager messageIdManager,
                                SystemMailboxesProvider systemMailboxesProvider); @Override Mail getMail(); @Override void done(boolean success); }
PostDequeueDecorator extends MailQueueItemDecorator { @Override public void done(boolean success) throws MailQueueException { mailQueueItem.done(success); if (success && mandatoryJmapMetaDataIsPresent()) { Optional<?> optionalRawMessageId = retrieveMessageId(); MessageId messageId = messageIdFactory.fromString((String) optionalRawMessageId.get()); Optional<String> username = retrieveUsername(); if (!getMail().getAttribute(IS_DELIVERED.getName()).isPresent()) { try { MailboxSession mailboxSession = mailboxManager.createSystemSession(Username.of(username.get())); moveFromOutboxToSentWithSeenFlag(messageId, mailboxSession); getMail().setAttribute(IS_DELIVERED); } catch (MailShouldBeInOutboxException e) { LOG.info("Message does not exist on Outbox anymore, it could have already been sent", e); } catch (MailboxException e) { throw new MailQueueException(e.getMessage(), e); } } } } PostDequeueDecorator(MailQueueItem mailQueueItem,
                                MailboxManager mailboxManager,
                                Factory messageIdFactory,
                                MessageIdManager messageIdManager,
                                SystemMailboxesProvider systemMailboxesProvider); @Override Mail getMail(); @Override void done(boolean success);  }