Search in sources :

Example 1 with MimeBodyPartFactory

use of org.xwiki.mail.MimeBodyPartFactory in project xwiki-platform by xwiki.

the class HTMLMimeBodyPartFactoryTest method createWhenHTMLAndEmbeddedImagesAndNormalAttachments.

@Test
public void createWhenHTMLAndEmbeddedImagesAndNormalAttachments() throws Exception {
    Attachment normalAttachment = mock(Attachment.class, "normalAttachment");
    when(normalAttachment.getFilename()).thenReturn("attachment1.png");
    Attachment embeddedAttachment = mock(Attachment.class, "embeddedAttachment");
    when(embeddedAttachment.getFilename()).thenReturn("embeddedAttachment.png");
    MimeBodyPart embeddedAttachmentBodyPart = mock(MimeBodyPart.class);
    MimeBodyPartFactory attachmentBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, Attachment.class), "xwiki/attachment");
    when(attachmentBodyPartFactory.create(same(embeddedAttachment), any(Map.class))).thenReturn(embeddedAttachmentBodyPart);
    MimeBodyPart normalAttachmentBodyPart = mock(MimeBodyPart.class);
    when(attachmentBodyPartFactory.create(same(normalAttachment), any(Map.class))).thenReturn(normalAttachmentBodyPart);
    MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create("<p>html... <img src='cid:embeddedAttachment.png'/></p>", Collections.<String, Object>singletonMap("attachments", Arrays.asList(normalAttachment, embeddedAttachment)));
    MimeMultipart multipart = (MimeMultipart) bodyPart.getContent();
    assertEquals(2, multipart.getCount());
    MimeMultipart htmlMultipart = (MimeMultipart) multipart.getBodyPart(0).getContent();
    assertEquals(2, htmlMultipart.getCount());
    assertEquals("<p>html... <img src='cid:embeddedAttachment.png'/></p>", htmlMultipart.getBodyPart(0).getContent());
    assertSame(embeddedAttachmentBodyPart, htmlMultipart.getBodyPart(1));
    assertSame(normalAttachmentBodyPart, multipart.getBodyPart(1));
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) Attachment(com.xpn.xwiki.api.Attachment) MimeBodyPart(javax.mail.internet.MimeBodyPart) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with MimeBodyPartFactory

use of org.xwiki.mail.MimeBodyPartFactory in project xwiki-platform by xwiki.

the class HTMLMimeBodyPartFactoryTest method createWhenHTMLAndEmbeddedImages.

@Test
public void createWhenHTMLAndEmbeddedImages() throws Exception {
    Attachment attachment = mock(Attachment.class);
    MimeBodyPart attachmentBodyPart = mock(MimeBodyPart.class);
    MimeBodyPartFactory attachmentBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, Attachment.class), "xwiki/attachment");
    when(attachmentBodyPartFactory.create(same(attachment), any(Map.class))).thenReturn(attachmentBodyPart);
    MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create("<p>some html</p>", Collections.<String, Object>singletonMap("attachments", Arrays.asList(attachment)));
    MimeMultipart multipart = ((MimeMultipart) bodyPart.getContent());
    assertEquals(2, multipart.getCount());
    assertEquals("<p>some html</p>", multipart.getBodyPart(0).getContent());
    assertSame(attachmentBodyPart, multipart.getBodyPart(1));
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) Attachment(com.xpn.xwiki.api.Attachment) MimeBodyPart(javax.mail.internet.MimeBodyPart) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with MimeBodyPartFactory

use of org.xwiki.mail.MimeBodyPartFactory in project xwiki-platform by xwiki.

the class HTMLMimeBodyPartFactoryTest method createWhenHTMLAndAlternateTextContent.

@Test
public void createWhenHTMLAndAlternateTextContent() throws Exception {
    MimeBodyPart textBodyPart = mock(MimeBodyPart.class);
    MimeBodyPartFactory defaultBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class));
    when(defaultBodyPartFactory.create(eq("some text"), any(Map.class))).thenReturn(textBodyPart);
    MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create("<p>some html</p>", Collections.<String, Object>singletonMap("alternate", "some text"));
    MimeMultipart multipart = ((MimeMultipart) bodyPart.getContent());
    assertEquals(2, multipart.getCount());
    assertSame(textBodyPart, multipart.getBodyPart(0));
    assertEquals("<p>some html</p>", multipart.getBodyPart(1).getContent());
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) MimeBodyPart(javax.mail.internet.MimeBodyPart) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with MimeBodyPartFactory

use of org.xwiki.mail.MimeBodyPartFactory in project xwiki-platform by xwiki.

the class TemplateMimeBodyPartFactoryTest method createWithoutAttachment.

@Test
public void createWithoutAttachment() throws Exception {
    MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html");
    this.mocker.getComponentUnderTest().create(this.documentReference, Collections.<String, Object>singletonMap("velocityVariables", new HashMap<String, String>()));
    verify(htmlMimeBodyPartFactory).create("Hello <b>John Doe</b> <br />john@doe.com", Collections.<String, Object>singletonMap("alternate", "Hello John Doe, john@doe.com"));
}
Also used : HashMap(java.util.HashMap) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Example 5 with MimeBodyPartFactory

use of org.xwiki.mail.MimeBodyPartFactory in project xwiki-platform by xwiki.

the class TemplateMimeBodyPartFactoryTest method createWithAttachmentAndTemplateAttachments.

@Test
public void createWithAttachmentAndTemplateAttachments() throws Exception {
    MimeBodyPartFactory<String> htmlMimeBodyPartFactory = this.mocker.getInstance(new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "text/html");
    Attachment attachment1 = mock(Attachment.class, "attachment1");
    Map<String, Object> bodyPartParameters = new HashMap<>();
    bodyPartParameters.put("velocityVariables", new HashMap<String, String>());
    bodyPartParameters.put("attachments", Collections.singletonList(attachment1));
    bodyPartParameters.put("includeTemplateAttachments", true);
    // Mock the retrieval and conversion of attachments from the Template document
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    XWikiDocument xwikiDocument = mock(XWikiDocument.class);
    when(dab.getDocumentInstance(this.documentReference)).thenReturn(xwikiDocument);
    XWikiAttachment xwikiAttachment = mock(XWikiAttachment.class);
    when(xwikiDocument.getAttachmentList()).thenReturn(Collections.singletonList(xwikiAttachment));
    AttachmentConverter attachmentConverter = this.mocker.getInstance(AttachmentConverter.class);
    Attachment attachment2 = mock(Attachment.class, "attachment2");
    when(attachmentConverter.convert(Collections.singletonList(xwikiAttachment))).thenReturn(Collections.singletonList(attachment2));
    this.mocker.getComponentUnderTest().create(this.documentReference, bodyPartParameters);
    Map<String, Object> htmlParameters = new HashMap<>();
    htmlParameters.put("alternate", "Hello John Doe, john@doe.com");
    htmlParameters.put("attachments", Arrays.asList(attachment1, attachment2));
    verify(htmlMimeBodyPartFactory).create("Hello <b>John Doe</b> <br />john@doe.com", htmlParameters);
}
Also used : HashMap(java.util.HashMap) MimeBodyPartFactory(org.xwiki.mail.MimeBodyPartFactory) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Aggregations

MimeBodyPartFactory (org.xwiki.mail.MimeBodyPartFactory)10 HashMap (java.util.HashMap)9 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)9 Test (org.junit.Test)8 Attachment (com.xpn.xwiki.api.Attachment)6 MimeBodyPart (javax.mail.internet.MimeBodyPart)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 Map (java.util.Map)4 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)3 MessagingException (javax.mail.MessagingException)2 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 IOException (java.io.IOException)1 BodyPart (javax.mail.BodyPart)1 Multipart (javax.mail.Multipart)1 Before (org.junit.Before)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1