james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [4/5] james-project git commit: JAMES-1790 check that blobids exist when attaching them
Date Fri, 08 Jul 2016 14:46:39 GMT
http://git-wip-us.apache.org/repos/asf/james-project/blob/662fa4a9/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
index e64d6a9..6f9b3d3 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/MIMEMessageConverterTest.java
@@ -30,7 +30,10 @@ import org.apache.james.jmap.methods.ValueWithId.MessageWithId;
 import org.apache.james.jmap.model.CreationMessage;
 import org.apache.james.jmap.model.CreationMessage.DraftEmailer;
 import org.apache.james.jmap.model.CreationMessageId;
+import org.apache.james.mailbox.store.mail.model.AttachmentId;
+import org.apache.james.mailbox.store.mail.model.MessageAttachment;
 import org.apache.james.mime4j.Charsets;
+import org.apache.james.mime4j.dom.Entity;
 import org.apache.james.mime4j.dom.Message;
 import org.apache.james.mime4j.dom.Multipart;
 import org.apache.james.mime4j.dom.TextBody;
@@ -57,7 +60,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), messageHavingInReplyTo));
+                CreationMessageId.of("user|mailbox|1"), messageHavingInReplyTo), ImmutableList.of());
 
         // Then
         assertThat(result.getHeader().getFields("In-Reply-To")).extracting(Field::getBody)
@@ -68,7 +71,7 @@ public class MIMEMessageConverterTest {
     public void convertToMimeShouldThrowWhenMessageIsNull() {
         MIMEMessageConverter sut = new MIMEMessageConverter();
 
-        sut.convertToMime(new ValueWithId.CreationMessageEntry(CreationMessageId.of("any"),
null));
+        sut.convertToMime(new ValueWithId.CreationMessageEntry(CreationMessageId.of("any"),
null), ImmutableList.of());
     }
 
     @Test
@@ -85,7 +88,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getFrom()).extracting(Mailbox::getAddress).allMatch(f -> f.equals(joesEmail));
@@ -109,7 +112,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getDate()).isEqualToIgnoringMillis(Date.from(now));
@@ -130,7 +133,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content",
"charset");
@@ -150,7 +153,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content",
"charset");
@@ -171,7 +174,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content",
"charset");
@@ -192,7 +195,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getBody()).isInstanceOf(Multipart.class);
@@ -216,7 +219,7 @@ public class MIMEMessageConverterTest {
 
         String expectedHeaders = "MIME-Version: 1.0\r\n" +
                 "Content-Type: multipart/mixed;\r\n" +
-                " boundary=\"-=Part.0.";
+                " boundary=\"-=Part.1.";
         String expectedPart1 = "Content-Type: text/plain; charset=UTF-8\r\n" +
                 "\r\n" +
                 "Hello all!\r\n";
@@ -226,7 +229,7 @@ public class MIMEMessageConverterTest {
 
         // When
         byte[] convert = sut.convert(new MessageWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         String actual = new String(convert, Charsets.UTF_8);
@@ -249,7 +252,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getMimeType()).isEqualTo("text/plain");
@@ -269,7 +272,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getMimeType()).isEqualTo("text/html");
@@ -290,7 +293,7 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content",
"charset");
@@ -312,10 +315,52 @@ public class MIMEMessageConverterTest {
 
         // When
         Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
-                CreationMessageId.of("user|mailbox|1"), testMessage));
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of());
 
         // Then
         assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content",
"charset");
         assertThat(result.getMimeType()).isEqualTo("text/plain");
     }
+
+    @Test
+    public void convertToMimeShouldAddAttachmentWhenOne() {
+        // Given
+        MIMEMessageConverter sut = new MIMEMessageConverter();
+
+        CreationMessage testMessage = CreationMessage.builder()
+                .mailboxIds(ImmutableList.of("dead-bada55"))
+                .subject("subject")
+                .from(DraftEmailer.builder().name("sender").build())
+                .htmlBody("Hello <b>all<b>!")
+                .build();
+
+        String expectedCID = "<cid>";
+        String expectedMimeType = "image/png";
+        String text = "123456";
+        TextBody expectedBody = new BasicBodyFactory().textBody(text, Charsets.UTF_8);
+        MessageAttachment attachment = MessageAttachment.builder()
+                .attachment(org.apache.james.mailbox.store.mail.model.Attachment.builder()
+                    .attachmentId(AttachmentId.from("blodId"))
+                    .bytes(text.getBytes())
+                    .type(expectedMimeType)
+                    .build())
+                .cid(expectedCID)
+                .isInline(true)
+                .build();
+
+        // When
+        Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
+                CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of(attachment));
+
+        // Then
+        assertThat(result.getBody()).isInstanceOf(Multipart.class);
+        assertThat(result.isMultipart()).isTrue();
+        Multipart typedResult = (Multipart)result.getBody();
+        assertThat(typedResult.getBodyParts()).hasSize(2);
+        Entity attachmentPart = typedResult.getBodyParts().get(1);
+        assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expectedBody);
+        assertThat(attachmentPart.getDispositionType()).isEqualTo("inline");
+        assertThat(attachmentPart.getMimeType()).isEqualTo(expectedMimeType);
+        assertThat(attachmentPart.getHeader().getField("Content-ID").getBody()).isEqualTo(expectedCID);
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/662fa4a9/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 12eba05..178c554 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
@@ -20,6 +20,7 @@
 package org.apache.james.jmap.methods;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
@@ -33,7 +34,10 @@ import java.util.function.Function;
 import java.util.function.Supplier;
 import java.util.stream.Stream;
 
+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.CreationMessage.DraftEmailer;
@@ -54,13 +58,17 @@ import org.apache.james.jmap.utils.HtmlTextExtractor;
 import org.apache.james.jmap.utils.MailboxBasedHtmlTextExtractor;
 import org.apache.james.jmap.utils.SystemMailboxesProvider;
 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.MailboxPath;
 import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
 import org.apache.james.mailbox.store.TestId;
 import org.apache.james.mailbox.store.extractor.DefaultTextExtractor;
+import org.apache.james.mailbox.store.mail.AttachmentMapper;
+import org.apache.james.mailbox.store.mail.AttachmentMapperFactory;
 import org.apache.james.mailbox.store.mail.MessageMapper;
+import org.apache.james.mailbox.store.mail.model.AttachmentId;
 import org.apache.james.mailbox.store.mail.model.Mailbox;
 import org.apache.james.mailbox.store.mail.model.MailboxMessage;
 import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
@@ -72,6 +80,7 @@ import org.junit.Test;
 import com.google.common.base.Joiner;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 
 public class SetMessagesCreationProcessorTest {
     
@@ -133,6 +142,9 @@ public class SetMessagesCreationProcessorTest {
     private SystemMailboxesProvider fakeSystemMailboxesProvider;
     private MockMailboxSession session;
     private MIMEMessageConverter mimeMessageConverter;
+    private AttachmentMapper mockedAttachmentMapper;
+    private AttachmentMapperFactory mockedAttachmentMapperFactory;
+    private SetMessagesCreationProcessor sut;
 
     @Before
     public void setUp() throws MailboxException {
@@ -142,17 +154,19 @@ public class SetMessagesCreationProcessorTest {
                 .thenReturn(mockMapper);
         mockedMailSpool = mock(MailSpool.class);
         mockedMailFactory = mock(MailFactory.class);
-
+        mockedAttachmentMapperFactory = mock(AttachmentMapperFactory.class);
+        mockedAttachmentMapper = mock(AttachmentMapper.class);
+        when(mockedAttachmentMapperFactory.getAttachmentMapper(any(MailboxSession.class))).thenReturn(mockedAttachmentMapper);
+        
         fakeSystemMailboxesProvider = new TestSystemMailboxesProvider(() -> optionalOutbox,
() -> optionalDrafts);
         session = new MockMailboxSession(USER);
         mimeMessageConverter = new MIMEMessageConverter();
+        sut = new SetMessagesCreationProcessor(
+                stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, fakeSystemMailboxesProvider, mockedAttachmentMapperFactory);
     }
 
     @Test
     public void processShouldReturnEmptyCreatedWhenRequestHasEmptyCreate() {
-        SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(
-                stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, fakeSystemMailboxesProvider);
-
         SetMessagesRequest requestWithEmptyCreate = SetMessagesRequest.builder().build();
 
         SetMessagesResponse result = sut.process(requestWithEmptyCreate, session);
@@ -169,8 +183,8 @@ public class SetMessagesCreationProcessorTest {
         when(mockSessionMapperFactory.createMessageMapper(any(MailboxSession.class)))
                 .thenReturn(stubMapper);
 
-        SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(
-                mockSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, fakeSystemMailboxesProvider) {
+        sut = new SetMessagesCreationProcessor(
+                mockSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, fakeSystemMailboxesProvider, mockedAttachmentMapperFactory) {
             @Override
             protected MessageWithId createMessageInOutboxAndSend(ValueWithId.CreationMessageEntry
createdEntry, MailboxSession session, Mailbox outbox, Function<Long, MessageId> buildMessageIdFromUid)
{
                 return new MessageWithId(createdEntry.getCreationId(), FAKE_OUTBOX_MESSAGE);
@@ -190,7 +204,7 @@ public class SetMessagesCreationProcessorTest {
         // Given
         TestSystemMailboxesProvider doNotProvideOutbox = new TestSystemMailboxesProvider(Optional::empty,
() -> optionalDrafts);
         SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(
-                stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, doNotProvideOutbox);
+                stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, doNotProvideOutbox, mockedAttachmentMapperFactory);
         // When
         SetMessagesResponse actual = sut.process(createMessageInOutbox, session);
         
@@ -201,9 +215,6 @@ public class SetMessagesCreationProcessorTest {
 
     @Test
     public void processShouldCallMessageMapperWhenRequestHasNonEmptyCreate() throws MailboxException
{
-        // Given
-        SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(
-                stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, fakeSystemMailboxesProvider);
         // When
         sut.process(createMessageInOutbox, session);
 
@@ -213,9 +224,6 @@ public class SetMessagesCreationProcessorTest {
 
     @Test
     public void processShouldSendMailWhenRequestHasNonEmptyCreate() throws Exception {
-        // Given
-        SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(
-                stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, fakeSystemMailboxesProvider);
         // When
         sut.process(createMessageInOutbox, session);
 
@@ -225,9 +233,6 @@ public class SetMessagesCreationProcessorTest {
 
     @Test
     public void processShouldNotSpoolMailWhenNotSavingToOutbox() throws Exception {
-        // Given
-        SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(
-                stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, fakeSystemMailboxesProvider);
         // When
         SetMessagesRequest notInOutboxCreationRequest =
                 SetMessagesRequest.builder()
@@ -244,10 +249,6 @@ public class SetMessagesCreationProcessorTest {
 
     @Test
     public void processShouldReturnNotImplementedErrorWhenSavingToDrafts() {
-        // Given
-        SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(
-                stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, fakeSystemMailboxesProvider);
-
         CreationMessageId creationMessageId = CreationMessageId.of("anything-really");
         SetMessagesRequest createMessageInDrafts = SetMessagesRequest.builder()
                 .create(
@@ -267,9 +268,6 @@ public class SetMessagesCreationProcessorTest {
 
     @Test
     public void processShouldNotSendWhenSavingToDrafts() throws Exception {
-        // Given
-        SetMessagesCreationProcessor sut = new SetMessagesCreationProcessor(
-                stubSessionMapperFactory, mimeMessageConverter, mockedMailSpool, mockedMailFactory,
messageFactory, fakeSystemMailboxesProvider);
         // When
         CreationMessageId creationMessageId = CreationMessageId.of("anything-really");
         SetMessagesRequest createMessageInDrafts = SetMessagesRequest.builder()
@@ -283,6 +281,46 @@ public class SetMessagesCreationProcessorTest {
     }
 
 
+    @Test
+    public void assertAttachmentsExistShouldThrowWhenUnknownBlobId() throws AttachmentNotFoundException
{
+        BlobId unknownBlobId = BlobId.of("unknownBlobId");
+        AttachmentId unknownAttachmentId = AttachmentId.from(unknownBlobId.getRawValue());
+        when(mockedAttachmentMapper.getAttachment(unknownAttachmentId)).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 AttachmentNotFoundException
{
+        BlobId unknownBlobId1 = BlobId.of("unknownBlobId1");
+        BlobId unknownBlobId2 = BlobId.of("unknownBlobId2");
+        AttachmentId unknownAttachmentId1 = AttachmentId.from(unknownBlobId1.getRawValue());
+        AttachmentId unknownAttachmentId2 = AttachmentId.from(unknownBlobId2.getRawValue());
+
+        when(mockedAttachmentMapper.getAttachment(unknownAttachmentId1)).thenThrow(new AttachmentNotFoundException(unknownBlobId1.getRawValue()));
+        when(mockedAttachmentMapper.getAttachment(unknownAttachmentId2)).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)));
+    }
+    
     public static class TestSystemMailboxesProvider implements SystemMailboxesProvider {
 
         private final Supplier<Optional<Mailbox>> outboxSupplier;


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message