JAMES-2214 Attachment checking should be extracted from SetMessagesCreationProcessor
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b0f4d067 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b0f4d067 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b0f4d067 Branch: refs/heads/master Commit: b0f4d067ad6c1c0095a3898b71f59c976e215e11 Parents: d324caa Author: benwa <[email protected]> Authored: Mon Nov 13 10:23:04 2017 +0700 Committer: benwa <[email protected]> Committed: Wed Nov 15 17:59:49 2017 +0700 ---------------------------------------------------------------------- .../james/jmap/methods/AttachmentChecker.java | 73 +++++++++++++ .../methods/SetMessagesCreationProcessor.java | 47 ++------- .../jmap/methods/AttachmentCheckerTest.java | 105 +++++++++++++++++++ .../SetMessagesCreationProcessorTest.java | 62 ++--------- 4 files changed, 190 insertions(+), 97 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/b0f4d067/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/AttachmentChecker.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/AttachmentChecker.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/AttachmentChecker.java new file mode 100644 index 0000000..77bbd62 --- /dev/null +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/AttachmentChecker.java @@ -0,0 +1,73 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.methods; + +import java.util.List; + +import javax.inject.Inject; + +import org.apache.james.jmap.exceptions.AttachmentsNotFoundException; +import org.apache.james.jmap.model.Attachment; +import org.apache.james.jmap.model.BlobId; +import org.apache.james.mailbox.AttachmentManager; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.exception.AttachmentNotFoundException; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.model.AttachmentId; + +import com.github.fge.lambdas.Throwing; +import com.github.fge.lambdas.predicates.ThrowingPredicate; +import com.github.steveash.guavate.Guavate; + +public class AttachmentChecker { + private final AttachmentManager attachmentManager; + + @Inject + public AttachmentChecker(AttachmentManager attachmentManager) { + this.attachmentManager = attachmentManager; + } + + public void assertAttachmentsExist(ValueWithId.CreationMessageEntry entry, MailboxSession session) throws AttachmentsNotFoundException, MailboxException { + List<Attachment> attachments = entry.getValue().getAttachments(); + List<BlobId> notFounds = listAttachmentsNotFound(attachments, session); + if (!notFounds.isEmpty()) { + throw new AttachmentsNotFoundException(notFounds); + } + } + + private List<BlobId> listAttachmentsNotFound(List<Attachment> attachments, MailboxSession session) throws MailboxException { + ThrowingPredicate<Attachment> notExists = attachment -> { + try { + attachmentManager.getAttachment(getAttachmentId(attachment), session); + return false; + } catch (AttachmentNotFoundException e) { + return true; + } + }; + return attachments.stream() + .filter(Throwing.predicate(notExists).sneakyThrow()) + .map(Attachment::getBlobId) + .collect(Guavate.toImmutableList()); + } + + private AttachmentId getAttachmentId(Attachment attachment) { + return AttachmentId.from(attachment.getBlobId().getRawValue()); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/b0f4d067/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java index e2d13da..2f12ebd 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java @@ -32,8 +32,6 @@ import javax.mail.MessagingException; import org.apache.james.jmap.exceptions.AttachmentsNotFoundException; import org.apache.james.jmap.methods.ValueWithId.CreationMessageEntry; import org.apache.james.jmap.methods.ValueWithId.MessageWithId; -import org.apache.james.jmap.model.Attachment; -import org.apache.james.jmap.model.BlobId; import org.apache.james.jmap.model.CreationMessage; import org.apache.james.jmap.model.Message; import org.apache.james.jmap.model.MessageFactory; @@ -47,14 +45,11 @@ import org.apache.james.jmap.model.SetMessagesResponse; import org.apache.james.jmap.model.SetMessagesResponse.Builder; import org.apache.james.jmap.model.mailbox.Role; import org.apache.james.jmap.utils.SystemMailboxesProvider; -import org.apache.james.mailbox.AttachmentManager; import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageManager; -import org.apache.james.mailbox.exception.AttachmentNotFoundException; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.MailboxNotFoundException; -import org.apache.james.mailbox.model.AttachmentId; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.metrics.api.MetricFactory; import org.apache.james.metrics.api.TimeMetric; @@ -62,8 +57,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.github.fge.lambdas.Throwing; -import com.github.fge.lambdas.predicates.ThrowingPredicate; -import com.github.steveash.guavate.Guavate; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Joiner; import com.google.common.base.Splitter; @@ -74,7 +67,7 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor { private static final Logger LOG = LoggerFactory.getLogger(SetMailboxesCreationProcessor.class); private final MessageFactory messageFactory; private final SystemMailboxesProvider systemMailboxesProvider; - private final AttachmentManager attachmentManager; + private final AttachmentChecker attachmentChecker; private final MetricFactory metricFactory; private final MailboxManager mailboxManager; private final MailboxId.Factory mailboxIdFactory; @@ -82,14 +75,15 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor { private final MessageSender messageSender; @VisibleForTesting @Inject - SetMessagesCreationProcessor(MessageFactory messageFactory, SystemMailboxesProvider systemMailboxesProvider, - AttachmentManager attachmentManager, + SetMessagesCreationProcessor(MessageFactory messageFactory, + SystemMailboxesProvider systemMailboxesProvider, + AttachmentChecker attachmentChecker, MetricFactory metricFactory, MailboxManager mailboxManager, MailboxId.Factory mailboxIdFactory, MessageAppender messageAppender, MessageSender messageSender) { this.messageFactory = messageFactory; this.systemMailboxesProvider = systemMailboxesProvider; - this.attachmentManager = attachmentManager; + this.attachmentChecker = attachmentChecker; this.metricFactory = metricFactory; this.mailboxManager = mailboxManager; this.mailboxIdFactory = mailboxIdFactory; @@ -186,36 +180,7 @@ public class SetMessagesCreationProcessor implements SetMessagesProcessor { if (!message.isValid()) { throw new MailboxInvalidMessageCreationException(); } - assertAttachmentsExist(entry, session); - } - - @VisibleForTesting void assertAttachmentsExist(CreationMessageEntry entry, MailboxSession session) throws AttachmentsNotFoundException, MailboxException { - List<Attachment> attachments = entry.getValue().getAttachments(); - if (!attachments.isEmpty()) { - List<BlobId> notFounds = listAttachmentsNotFound(attachments, session); - if (!notFounds.isEmpty()) { - throw new AttachmentsNotFoundException(notFounds); - } - } - } - - private List<BlobId> listAttachmentsNotFound(List<Attachment> attachments, MailboxSession session) throws MailboxException { - ThrowingPredicate<Attachment> notExists = attachment -> { - try { - attachmentManager.getAttachment(getAttachmentId(attachment), session); - return false; - } catch (AttachmentNotFoundException e) { - return true; - } - }; - return attachments.stream() - .filter(Throwing.predicate(notExists).sneakyThrow()) - .map(Attachment::getBlobId) - .collect(Guavate.toImmutableList()); - } - - private AttachmentId getAttachmentId(Attachment attachment) { - return AttachmentId.from(attachment.getBlobId().getRawValue()); + attachmentChecker.assertAttachmentsExist(entry, session); } @VisibleForTesting void validateIsUserOwnerOfMailboxes(CreationMessageEntry entry, MailboxSession session) throws MailboxRightsException { http://git-wip-us.apache.org/repos/asf/james-project/blob/b0f4d067/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/AttachmentCheckerTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/AttachmentCheckerTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/AttachmentCheckerTest.java new file mode 100644 index 0000000..dca2a44 --- /dev/null +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/AttachmentCheckerTest.java @@ -0,0 +1,105 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.methods; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.james.jmap.exceptions.AttachmentsNotFoundException; +import org.apache.james.jmap.model.Attachment; +import org.apache.james.jmap.model.BlobId; +import org.apache.james.jmap.model.CreationMessage; +import org.apache.james.jmap.model.CreationMessageId; +import org.apache.james.mailbox.AttachmentManager; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.exception.AttachmentNotFoundException; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.mock.MockMailboxSession; +import org.apache.james.mailbox.model.AttachmentId; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; + +public class AttachmentCheckerTest { + + private final CreationMessageId creationMessageId = CreationMessageId.of("dlkja"); + private final CreationMessage.Builder creationMessageBuilder = CreationMessage.builder() + .from(CreationMessage.DraftEmailer.builder().name("alice").email("[email protected]").build()) + .to(ImmutableList.of(CreationMessage.DraftEmailer.builder().name("bob").email("[email protected]").build())) + .mailboxId("id") + .subject("Hey! "); + + private AttachmentManager attachmentManager; + private MailboxSession session; + + private AttachmentChecker sut; + + @Before + public void setUp() { + session = new MockMailboxSession("Jonhy"); + attachmentManager = mock(AttachmentManager.class); + + sut = new AttachmentChecker(attachmentManager); + } + + @Test + public void assertAttachmentsExistShouldThrowWhenUnknownBlobId() throws MailboxException { + BlobId unknownBlobId = BlobId.of("unknownBlobId"); + AttachmentId unknownAttachmentId = AttachmentId.from(unknownBlobId.getRawValue()); + when(attachmentManager.getAttachment(unknownAttachmentId, session)).thenThrow(new AttachmentNotFoundException(unknownBlobId.getRawValue())); + + assertThatThrownBy(() -> sut.assertAttachmentsExist( + new ValueWithId.CreationMessageEntry( + creationMessageId, + creationMessageBuilder.attachments( + Attachment.builder().size(12L).type("image/jpeg").blobId(unknownBlobId).build()) + .build() + ), + session)) + .isInstanceOf(AttachmentsNotFoundException.class); + } + + @Test + public void assertAttachmentsExistShouldThrowWhenUnknownBlobIds() throws MailboxException { + BlobId unknownBlobId1 = BlobId.of("unknownBlobId1"); + BlobId unknownBlobId2 = BlobId.of("unknownBlobId2"); + AttachmentId unknownAttachmentId1 = AttachmentId.from(unknownBlobId1.getRawValue()); + AttachmentId unknownAttachmentId2 = AttachmentId.from(unknownBlobId2.getRawValue()); + + when(attachmentManager.getAttachment(unknownAttachmentId1, session)).thenThrow(new AttachmentNotFoundException(unknownBlobId1.getRawValue())); + when(attachmentManager.getAttachment(unknownAttachmentId2, session)).thenThrow(new AttachmentNotFoundException(unknownBlobId2.getRawValue())); + + assertThatThrownBy(() -> sut.assertAttachmentsExist( + new ValueWithId.CreationMessageEntry( + creationMessageId, + creationMessageBuilder.attachments( + Attachment.builder().size(12L).type("image/jpeg").blobId(unknownBlobId1).build(), + Attachment.builder().size(23L).type("image/git").blobId(unknownBlobId2).build()) + .build() + ), + session)) + .isInstanceOf(AttachmentsNotFoundException.class) + .matches(e -> ((AttachmentsNotFoundException)e).getAttachmentIds().containsAll(ImmutableSet.of(unknownBlobId1, unknownBlobId2))); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/b0f4d067/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java index 25ece63..2857b1e 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java @@ -35,10 +35,7 @@ import java.util.stream.Stream; import javax.mail.Flags; -import org.apache.james.jmap.exceptions.AttachmentsNotFoundException; import org.apache.james.jmap.methods.ValueWithId.CreationMessageEntry; -import org.apache.james.jmap.model.Attachment; -import org.apache.james.jmap.model.BlobId; import org.apache.james.jmap.model.CreationMessage; import org.apache.james.jmap.model.CreationMessage.DraftEmailer; import org.apache.james.jmap.model.CreationMessageId; @@ -60,14 +57,11 @@ import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.MessageUid; -import org.apache.james.mailbox.exception.AttachmentNotFoundException; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.MailboxNotFoundException; import org.apache.james.mailbox.inmemory.InMemoryId; import org.apache.james.mailbox.mock.MockMailboxSession; -import org.apache.james.mailbox.model.AttachmentId; import org.apache.james.mailbox.model.ComposedMessageId; -import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MailboxId.Factory; import org.apache.james.mailbox.model.MailboxPath; import org.apache.james.mailbox.model.MessageId; @@ -84,7 +78,6 @@ import org.junit.rules.ExpectedException; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; public class SetMessagesCreationProcessorTest { @@ -112,10 +105,8 @@ public class SetMessagesCreationProcessorTest { private MessageFactory messageFactory; private MailSpool mockedMailSpool; - private MailFactory mockedMailFactory; private SystemMailboxesProvider fakeSystemMailboxesProvider; private MockMailboxSession session; - private MIMEMessageConverter mimeMessageConverter; private AttachmentManager mockedAttachmentManager; private MailboxManager mockedMailboxManager; private Factory mockedMailboxIdFactory; @@ -140,19 +131,19 @@ public class SetMessagesCreationProcessorTest { when(blobManager.toBlobId(any(MessageId.class))).thenReturn(org.apache.james.mailbox.model.BlobId.fromString("fake")); messageFactory = new MessageFactory(blobManager, messagePreview, messageContentExtractor, htmlTextExtractor); mockedMailSpool = mock(MailSpool.class); - mockedMailFactory = mock(MailFactory.class); + MailFactory mockedMailFactory = mock(MailFactory.class); mockedAttachmentManager = mock(AttachmentManager.class); mockedMailboxManager = mock(MailboxManager.class); - mockedMailboxIdFactory = mock(MailboxId.Factory.class); + mockedMailboxIdFactory = mock(Factory.class); fakeSystemMailboxesProvider = new TestSystemMailboxesProvider(() -> optionalOutbox, () -> optionalDrafts); session = new MockMailboxSession(USER); - mimeMessageConverter = new MIMEMessageConverter(); + MIMEMessageConverter mimeMessageConverter = new MIMEMessageConverter(); messageAppender = new MessageAppender(mockedAttachmentManager, mimeMessageConverter); messageSender = new MessageSender(mockedMailSpool, mockedMailFactory); sut = new SetMessagesCreationProcessor(messageFactory, fakeSystemMailboxesProvider, - mockedAttachmentManager, + new AttachmentChecker(mockedAttachmentManager), new NoopMetricFactory(), mockedMailboxManager, mockedMailboxIdFactory, @@ -241,7 +232,7 @@ public class SetMessagesCreationProcessorTest { @Test public void processShouldReturnNonEmptyCreatedWhenRequestHasNonEmptyCreate() throws MailboxException { // Given - sut = new SetMessagesCreationProcessor(messageFactory, fakeSystemMailboxesProvider, mockedAttachmentManager, new NoopMetricFactory(), mockedMailboxManager, mockedMailboxIdFactory, messageAppender, messageSender); + sut = new SetMessagesCreationProcessor(messageFactory, fakeSystemMailboxesProvider, new AttachmentChecker(mockedAttachmentManager), new NoopMetricFactory(), mockedMailboxManager, mockedMailboxIdFactory, messageAppender, messageSender); // When SetMessagesResponse result = sut.process(createMessageInOutbox, session); @@ -257,7 +248,7 @@ public class SetMessagesCreationProcessorTest { // Given TestSystemMailboxesProvider doNotProvideOutbox = new TestSystemMailboxesProvider(Optional::empty, () -> optionalDrafts); SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(messageFactory, doNotProvideOutbox, - mockedAttachmentManager, new NoopMetricFactory(), mockedMailboxManager, mockedMailboxIdFactory, + new AttachmentChecker(mockedAttachmentManager), new NoopMetricFactory(), mockedMailboxManager, mockedMailboxIdFactory, messageAppender, messageSender); // When @@ -335,47 +326,6 @@ public class SetMessagesCreationProcessorTest { verify(mockedMailSpool, never()).send(any(Mail.class), any(MailMetadata.class)); } - - @Test - public void assertAttachmentsExistShouldThrowWhenUnknownBlobId() throws MailboxException { - BlobId unknownBlobId = BlobId.of("unknownBlobId"); - AttachmentId unknownAttachmentId = AttachmentId.from(unknownBlobId.getRawValue()); - when(mockedAttachmentManager.getAttachment(unknownAttachmentId, session)).thenThrow(new AttachmentNotFoundException(unknownBlobId.getRawValue())); - - assertThatThrownBy(() -> sut.assertAttachmentsExist( - new CreationMessageEntry( - creationMessageId, - creationMessageBuilder.attachments( - Attachment.builder().size(12l).type("image/jpeg").blobId(unknownBlobId).build()) - .build() - ), - session)) - .isInstanceOf(AttachmentsNotFoundException.class); - } - - @Test - public void assertAttachmentsExistShouldThrowWhenUnknownBlobIds() throws MailboxException { - BlobId unknownBlobId1 = BlobId.of("unknownBlobId1"); - BlobId unknownBlobId2 = BlobId.of("unknownBlobId2"); - AttachmentId unknownAttachmentId1 = AttachmentId.from(unknownBlobId1.getRawValue()); - AttachmentId unknownAttachmentId2 = AttachmentId.from(unknownBlobId2.getRawValue()); - - when(mockedAttachmentManager.getAttachment(unknownAttachmentId1, session)).thenThrow(new AttachmentNotFoundException(unknownBlobId1.getRawValue())); - when(mockedAttachmentManager.getAttachment(unknownAttachmentId2, session)).thenThrow(new AttachmentNotFoundException(unknownBlobId2.getRawValue())); - - assertThatThrownBy(() -> sut.assertAttachmentsExist( - new CreationMessageEntry( - creationMessageId, - creationMessageBuilder.attachments( - Attachment.builder().size(12l).type("image/jpeg").blobId(unknownBlobId1).build(), - Attachment.builder().size(23l).type("image/git").blobId(unknownBlobId2).build()) - .build() - ), - session)) - .isInstanceOf(AttachmentsNotFoundException.class) - .matches(e -> ((AttachmentsNotFoundException)e).getAttachmentIds().containsAll(ImmutableSet.of(unknownBlobId1, unknownBlobId2))); - } - @Test public void validateIsUserOwnerOfMailboxesShouldThrowWhenMailboxIdDoesntExist() throws Exception { InMemoryId mailboxId = InMemoryId.of(6789); --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
