SetMessagesCreationProcessor implements SetMessagesProcessor { @Override public SetMessagesResponse process(SetMessagesRequest request, MailboxSession mailboxSession) { TimeMetric timeMetric = metricFactory.timer(JMAP_PREFIX + "SetMessageCreationProcessor"); Builder responseBuilder = SetMessagesResponse.builder(); request.getCreate() .forEach(create -> handleCreate(create, responseBuilder, mailboxSession)); timeMetric.stopAndPublish(); return responseBuilder.build(); } @VisibleForTesting @Inject  SetMessagesCreationProcessor(MessageFullViewFactory messageFullViewFactory,
                                 SystemMailboxesProvider systemMailboxesProvider,
                                 AttachmentChecker attachmentChecker,
                                 MetricFactory metricFactory,
                                 MailboxManager mailboxManager,
                                 MailboxId.Factory mailboxIdFactory,
                                 MessageAppender messageAppender,
                                 MessageSender messageSender,
                                 ReferenceUpdater referenceUpdater,
                                 CanSendFrom canSendFrom); @Override SetMessagesResponse process(SetMessagesRequest request, MailboxSession mailboxSession);  }
@Test public void processShouldReturnEmptyCreatedWhenRequestHasEmptyCreate() { SetMessagesRequest requestWithEmptyCreate = SetMessagesRequest.builder().build(); SetMessagesResponse result = sut.process(requestWithEmptyCreate, session); assertThat(result.getCreated()).isEmpty(); assertThat(result.getNotCreated()).isEmpty(); }
@Test public void processShouldThrowWhenBothIsFlagAndKeywords() { expectedException.expect(IllegalArgumentException.class); SetMessagesRequest createMessageWithError = SetMessagesRequest.builder() .create( creationMessageId, creationMessageBuilder .mailboxId(OUTBOX_ID.serialize()) .isAnswered(Optional.of(true)) .keywords(ImmutableMap.of("$Answered", true)) .build()) .build(); sut.process(createMessageWithError, session); }
@Test public void processShouldCreateWhenKeywords() { SetMessagesRequest createMessageWithKeywords = SetMessagesRequest.builder() .create( creationMessageId, creationMessageBuilder .mailboxId(OUTBOX_ID.serialize()) .keywords(ImmutableMap.of("$Answered", true)) .build()) .build(); SetMessagesResponse result = sut.process(createMessageWithKeywords, session); assertThat(result.getCreated()).isNotEmpty(); assertThat(result.getNotCreated()).isEmpty(); }
@Test public void processShouldCreateWhenIsFlag() { SetMessagesRequest createMessageWithKeywords = SetMessagesRequest.builder() .create( creationMessageId, creationMessageBuilder .mailboxId(OUTBOX_ID.serialize()) .isAnswered(Optional.of(true)) .build()) .build(); SetMessagesResponse result = sut.process(createMessageWithKeywords, session); assertThat(result.getCreated()).isNotEmpty(); assertThat(result.getNotCreated()).isEmpty(); }
@Test public void processShouldReturnNonEmptyCreatedWhenRequestHasNonEmptyCreate() throws MailboxException { sut = new SetMessagesCreationProcessor(messageFullViewFactory, fakeSystemMailboxesProvider, new AttachmentChecker(mockedAttachmentManager), new RecordingMetricFactory(), mockedMailboxManager, mockedMailboxIdFactory, messageAppender, messageSender, referenceUpdater, canSendFrom); SetMessagesResponse result = sut.process(createMessageInOutbox, session); assertThat(result.getCreated()).isNotEmpty(); assertThat(result.getNotCreated()).isEmpty(); }
@Ignore("JAMES-1716 : should report an error") @Test public void processShouldReturnErrorWhenOutboxNotFound() { TestSystemMailboxesProvider doNotProvideOutbox = new TestSystemMailboxesProvider(Optional::empty, () -> optionalDrafts); SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(messageFullViewFactory, doNotProvideOutbox, new AttachmentChecker(mockedAttachmentManager), new RecordingMetricFactory(), mockedMailboxManager, mockedMailboxIdFactory, messageAppender, messageSender, referenceUpdater, canSendFrom); SetMessagesResponse actual = sut.process(createMessageInOutbox, session); assertThat(actual.getNotCreated()).hasSize(1).containsKey(creationMessageId); assertThat(actual.getNotCreated().get(creationMessageId).getType()).isEqualTo("invalidProperties"); assertThat(actual.getNotCreated().get(creationMessageId).getDescription()).contains("target mailbox does not exists"); }
@Test public void processShouldCallAppendMessageWhenRequestHasNonEmptyCreate() throws MailboxException { sut.process(createMessageInOutbox, session); verify(outbox).appendMessage(any(MessageManager.AppendCommand.class), any(MailboxSession.class)); }
@Test public void processShouldSendMailWhenRequestHasNonEmptyCreate() throws Exception { sut.process(createMessageInOutbox, session); verify(mockedMailSpool).send(any(Mail.class), any(MailMetadata.class)); }
@Test public void processShouldNotSpoolMailWhenNotSavingToOutbox() throws Exception { SetMessagesRequest notInOutboxCreationRequest = SetMessagesRequest.builder() .create(CreationMessageId.of("anything-really"), creationMessageBuilder.mailboxId("any-id-but-outbox-id") .build()) .build(); when(mockedMailboxManager.getMailbox(any(MailboxId.class), any())) .thenReturn(outbox); when(mockedMailboxIdFactory.fromString(anyString())).thenReturn(OUTBOX_ID); sut.process(notInOutboxCreationRequest, session); verify(mockedMailSpool, never()).send(any(Mail.class), any(MailMetadata.class)); }
@Test public void processShouldNotSendWhenSavingToDrafts() throws Exception { CreationMessageId creationMessageId = CreationMessageId.of("anything-really"); SetMessagesRequest createMessageInDrafts = SetMessagesRequest.builder() .create( creationMessageId, creationMessageBuilder.mailboxId(DRAFTS_ID.serialize()).build()) .build(); when(mockedMailboxManager.getMailbox(any(MailboxId.class), any())) .thenReturn(drafts); when(mockedMailboxIdFactory.fromString(anyString())).thenReturn(DRAFTS_ID); sut.process(createMessageInDrafts, session); verify(mockedMailSpool, never()).send(any(Mail.class), any(MailMetadata.class)); }