/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.mail;

import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.xwiki.component.util.DefaultParameterizedType;
import org.xwiki.test.ComponentManagerRule;

import com.icegreen.greenmail.util.GreenMail;

import static org.junit.Assert.*;

/**
 * Integration tests to prove that mail sending is working fully end to end.
 *
 * @version $Id$
 * @since 6.1M2
 */
public class IntegrationTest
{
    @Rule
    public ComponentManagerRule componentManager = new ComponentManagerRule();

    private GreenMail mail;

    @Before
    public void startMail()
    {
        this.mail = new GreenMail();
        this.mail.start();
    }

    @After
    public void stopMail()
    {
        if (this.mail != null) {
            this.mail.stop();
        }
    }

    @Test
    @Ignore("Goal is to make this test pass!")
    public void sendMail() throws Exception
    {
        // Step 1: Create a JavaMail Session
        MailSenderConfiguration configuration = this.componentManager.getInstance(MailSenderConfiguration.class);
        Session session = Session.getInstance(configuration.getAllProperties(), new XWikiAuthenticator(configuration));

        // Step 2: Create the Message to send
        MimeMessageFactory mimeMessageFactory = this.componentManager.getInstance(MimeMessageFactory.class);
        MimeMessage message = mimeMessageFactory.create("john@doe.com", "subject", session);

        // Step 3: Add the Message Body
        Multipart multipart = new MimeMultipart("mixed");
        // Add HTML in the body
        MimeBodyPartFactory<String> htmlPartFactory = this.componentManager.getInstance(
            new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), "html");
        multipart.addBodyPart(htmlPartFactory.create("some html here"));
        message.setContent(multipart);

        // Step 4: Send the mail
        MailSender sender = this.componentManager.getInstance(MailSender.class);
        sender.send(message, session);

        // Verify that the mail has been sent
        this.mail.waitForIncomingEmail(10000, 1);
        MimeMessage[] messages = this.mail.getReceivedMessages();
        assertEquals(1, messages.length);
    }
}

@@ -1627,16 +1627,210 @@
  mail;%0A%0A
+    private MailSenderConfiguration configuration;%0A%0A    private MimeMessageFactory mimeMessageFactory;%0A%0A    private MimeBodyPartFactory%3CString%3E htmlPartFactory;%0A%0A    private MailSender sender;%0A%0A
     @Bef
@@ -2071,59 +2071,14 @@
    @
-Test%0A    @Ignore(%22Goal is to make this test pass!%22)
+Before
 %0A   
@@ -2094,16 +2094,18 @@
 oid 
-sendMail
+initialize
 () t
@@ -2138,158 +2138,587 @@
     
-// Step 1: Create a Java
+this.configuration = this.componentManager.getInstance(
 Mail
- 
 Se
-ssion%0A        MailSenderConfiguration configuration = this.componentManager.getInstance(MailSenderConfiguration.class);
+nderConfiguration.class);%0A        this.mimeMessageFactory = this.componentManager.getInstance(MimeMessageFactory.class);%0A        this.htmlPartFactory = this.componentManager.getInstance(%0A            new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), %22html%22);%0A        this.sender = this.componentManager.getInstance(MailSender.class);%0A    %7D%0A%0A    @Test%0A    @Ignore(%22Goal is to make this test pass!%22)%0A    public void sendMail() throws Exception%0A    %7B%0A        // Step 1: Create a JavaMail Session
 %0A   
@@ -2739,16 +2739,28 @@
 ession =
+%0A           
  Session
@@ -2772,16 +2772,21 @@
 nstance(
+this.
 configur
@@ -2834,16 +2834,21 @@
 ticator(
+this.
 configur
@@ -2926,117 +2926,8 @@
 sage
-Factory mimeMessageFactory = this.componentManager.getInstance(MimeMessageFactory.class);%0A        MimeMessage
  mes
@@ -2933,16 +2933,21 @@
 ssage = 
+this.
 mimeMess
@@ -3144,195 +3144,8 @@
     
-MimeBodyPartFactory%3CString%3E htmlPartFactory = this.componentManager.getInstance(%0A            new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class), %22html%22);%0A        
 mult
@@ -3162,16 +3162,21 @@
 odyPart(
+this.
 htmlPart
@@ -3295,89 +3295,13 @@
     
-MailSender sender = this.componentManager.getInstance(MailSender.class);%0A        
+this.
 send

Implement a new Platform Mail Sender API * Make the integration test easier to read to better show what's involved when using the Mail Sending API