/*
 * 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 java.io.OutputStream;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
 * Unit tests for {@link org.xwiki.mail.ExtendedMimeMessage}.
 *
 * @version $Id$
 * @since 7.4.1
 */
public class ExtendedMimeMessageTest
{
    private static final String XMAIL_TYPE_HEADER = "X-MailType";

    private static final String TEST_XMAIL_TYPE = "MyType";
    private static final String TEST_MESSAGE_ID = "messageId";
    private static final String TEST_CONTENT = "Content";

    @Test
    public void testOptimizedWrapping() throws Exception
    {
        ExtendedMimeMessage mimeMessage = new ExtendedMimeMessage();

        assertThat(ExtendedMimeMessage.wrap(mimeMessage), sameInstance(mimeMessage));
    }

    @Test
    public void testMimeMessageWrapping() throws Exception
    {
        MimeMessage mimeMessage = new MimeMessage((Session) null);
        mimeMessage.setText(TEST_CONTENT);

        assertThat((String) ExtendedMimeMessage.wrap(mimeMessage).getContent(), equalTo(TEST_CONTENT));
    }

    @Test
    public void testIsEmpty() throws Exception
    {
        ExtendedMimeMessage message = new ExtendedMimeMessage();
        assertThat(message.isEmpty(), is(true));

        message.setText(TEST_CONTENT);
        assertThat(message.isEmpty(), is(false));
    }

    @Test
    public void testSetType() throws Exception
    {
        ExtendedMimeMessage message = new ExtendedMimeMessage();
        message.setType(TEST_XMAIL_TYPE);

        assertThat(message.getHeader(XMAIL_TYPE_HEADER, null), equalTo(TEST_XMAIL_TYPE));
    }

    @Test
    public void testGetType() throws Exception
    {
        ExtendedMimeMessage message = new ExtendedMimeMessage();
        message.setHeader(XMAIL_TYPE_HEADER, TEST_XMAIL_TYPE);

        assertThat(message.getType(), equalTo(TEST_XMAIL_TYPE));
    }

    @Test
    public void testGetMessageIdAndEnsureSaved() throws Exception
    {
        ExtendedMimeMessage message = new ExtendedMimeMessage();
        message.setText(TEST_CONTENT);
        message.setMessageId(TEST_MESSAGE_ID);
        message.writeTo(new OutputStream() { @Override public void write(int b) { } });

        assertThat(message.getMessageID(), equalTo(TEST_MESSAGE_ID));
    }

    @Test
    public void testGetUniqueMessageId() throws Exception
    {
        ExtendedMimeMessage message = new ExtendedMimeMessage();
        message.setText(TEST_CONTENT);
        message.setMessageId(TEST_MESSAGE_ID);
        assertThat(message.getUniqueMessageId(), equalTo("wmK5jlxm4kPv2caEGeVtsDOT3zk="));

        message.setRecipients(Message.RecipientType.TO, "john.doe@example.com");
        assertThat(message.getUniqueMessageId(), equalTo("g9tEjV2+qAGNIFaQ44+P+iZtZZw="));

        message.setMessageId("AnotherID");
        assertThat(message.getUniqueMessageId(), equalTo("hdr6yyK2Tq9fKpv5hr5eMOL8XYA="));

        message.setMessageId(TEST_MESSAGE_ID);
        assertThat(message.getUniqueMessageId(), equalTo("g9tEjV2+qAGNIFaQ44+P+iZtZZw="));
    }


}

@@ -1054,16 +1054,28 @@
 g.junit.
+jupiter.api.
 Test;%0A%0Ai
@@ -1113,23 +1113,28 @@
 atchers.
-equalTo
+sameInstance
 ;%0Aimport
@@ -1163,36 +1163,31 @@
 oreMatchers.
-sameInstance
+equalTo
 ;%0Aimport sta
@@ -1207,14 +1207,19 @@
 est.
-core.I
+CoreMatcher
 s.is
@@ -1238,22 +1238,32 @@
 tic org.
-junit.
+hamcrest.Matcher
 Assert.a
@@ -1698,29 +1698,12 @@
 oid 
-testOptimizedWrapping
+wrap
 () t
@@ -1744,33 +1744,41 @@
 ndedMimeMessage 
-m
+extendedM
 imeMessage = new
@@ -1801,17 +1801,16 @@
 sage();%0A
-%0A
         
@@ -1837,33 +1837,41 @@
 imeMessage.wrap(
-m
+extendedM
 imeMessage), sam
@@ -1884,104 +1884,31 @@
 nce(
-mimeMessage));%0A    %7D%0A%0A    @Test%0A    public void testMimeMessageWrapping() throws Exception%0A    %7B
+extendedMimeMessage));%0A
 %0A   
@@ -2014,17 +2014,16 @@
 NTENT);%0A
-%0A
         
@@ -2037,17 +2037,8 @@
 hat(
-(String) 
 Exte
@@ -2146,13 +2146,9 @@
 oid 
-testI
+i
 sEmp
@@ -2416,13 +2416,9 @@
 oid 
-testS
+s
 etTy
@@ -2676,21 +2676,17 @@
 ic void 
-testG
+g
 etType()
@@ -2936,21 +2936,17 @@
 ic void 
-testG
+g
 etMessag
@@ -3335,13 +3335,9 @@
 oid 
-testG
+g
 etUn
@@ -4070,12 +4070,10 @@
 ;%0A    %7D%0A
-%0A%0A
 %7D%0A

Convert to JUnit5