package com.atlassian.bitbucket.mail;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.junit.Test;

import javax.activation.FileDataSource;
import java.io.File;
import java.util.Collections;
import java.util.Set;

import static org.junit.Assert.*;

public class MailMessageTest {

    private static final MailAttachment ATTACHMENT = new MailAttachment("foo.txt", new FileDataSource(new File("/foo.txt")));
    private static final Set<String> BCC = ImmutableSet.of("one@bcc.com", "two@bcc.com");
    private static final Set<String> CC = ImmutableSet.of("one@cc.com", "two@cc.com");
    private static final String FROM = "stash@example.com";
    private static final String SUBJECT = "subject";
    private static final String TEXT = "text";
    private static final Set<String> TO = ImmutableSet.of("one@to.com", "two@to.com");

    @Test
    public void testMailMessageBuilder() {
        MailMessage message = new MailMessage.Builder()
                .attachment(ATTACHMENT.getFileName(), ATTACHMENT.getSource())
                .bcc(Iterables.toArray(BCC, String.class))
                .cc(Iterables.toArray(CC, String.class))
                .from(FROM)
                .subject(SUBJECT)
                .text(TEXT)
                .to(Iterables.toArray(TO, String.class))
                .build();

        assertEquals(ATTACHMENT.getFileName(), message.getAttachments().iterator().next().getFileName());
        assertEquals(BCC, message.getBcc());
        assertEquals(CC, message.getCc());
        assertEquals(FROM, message.getFrom());
        assertEquals(SUBJECT, message.getSubject());
        assertEquals(TEXT, message.getText());
        assertEquals(TO, message.getTo());
        assertEquals(1, message.getAttachments().size());
        assertSame(ATTACHMENT.getSource(), message.getAttachments().iterator().next().getSource());
    }

    @Test(expected = NullPointerException.class)
    public void testNullTo() {
        //noinspection ConstantConditions
        new MailMessage(null, FROM, CC, BCC, Collections.<MailAttachment>emptySet(),
                TEXT, SUBJECT, Collections.<String, String>emptyMap());
    }

    @Test(expected = IllegalArgumentException.class)
    public void testEmptyTo() {
        new MailMessage.Builder().build();
    }

    @Test
    public void testMinimalMailMessage() {
        //This will verify that an empty from address is trimmed to null
        MailMessage message = new MailMessage.Builder().from("").to(Iterables.toArray(TO, String.class)).build();

        assertEquals(TO, message.getTo());
        assertNotNull(message.getCc());
        assertTrue(message.getCc().isEmpty());

        assertNotNull(message.getBcc());
        assertTrue(message.getBcc().isEmpty());

        assertEquals("", message.getSubject());
        assertEquals("", message.getText());

        assertNull(message.getFrom());
    }

    /**
     * Minimal test to test that two identical but different instances are equal and have identical hashcodes
     */
    @Test
    public void testEqualsHashCode() {
        MailMessage.Builder builder = new MailMessage.Builder()
                .bcc(Iterables.toArray(BCC, String.class))
                .cc(Iterables.toArray(CC, String.class))
                .from(FROM)
                .subject(SUBJECT)
                .text(TEXT)
                .to(Iterables.toArray(TO, String.class));

        MailMessage message1 = builder.build();
        MailMessage message2 = builder.build();
        
        assertNotSame(message1, message2);
        assertTrue("expected equals to return true", message1.equals(message2));
        assertTrue("expected equals to return true", message2.equals(message1));

        assertEquals(message1.hashCode(), message2.hashCode());
    }
}
