package com.atlassian.stash.internal.mail;

import com.atlassian.bitbucket.mail.MailHostConfiguration;
import com.atlassian.bitbucket.mail.MailProtocol;
import org.junit.Test;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.util.Properties;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;

public class DefaultJavaMailSenderFactoryTest {

    @Test
    public void testCreate() {
        MailHostConfiguration configuration = new MailHostConfiguration.Builder().hostname("localhost").build();

        JavaMailSenderImpl sender = new DefaultJavaMailSenderFactory(null, null).create(configuration, 0, 0);
        assertEquals("smtp", sender.getProtocol());
        assertEquals("UTF-8", sender.getDefaultEncoding());
        assertEquals("localhost", sender.getHost());
        assertEquals(JavaMailSenderImpl.DEFAULT_PORT, sender.getPort());
        assertNull(sender.getPassword());
        assertNull(sender.getUsername());

        Properties properties = sender.getJavaMailProperties();
        assertTrue(properties.isEmpty());
    }

    @Test
    public void testCreateWithNullConfiguration() {
        assertNull(new DefaultJavaMailSenderFactory(null, null).create(null, 0, 0));
    }

    @Test
    public void testCreateWithNullHost() {
        MailHostConfiguration configuration = mock(MailHostConfiguration.class);

        assertNull(new DefaultJavaMailSenderFactory(null, null).create(configuration, 0, 0));
    }

    @Test
    public void testCreateWithTimeouts() {
        MailHostConfiguration configuration = new MailHostConfiguration.Builder().hostname("localhost").port(25).build();

        JavaMailSenderImpl sender = new DefaultJavaMailSenderFactory(null, null).create(configuration, 1000, 2000);
        assertEquals("smtp", sender.getProtocol());
        assertEquals("UTF-8", sender.getDefaultEncoding());
        assertEquals("localhost", sender.getHost());
        assertEquals(25, sender.getPort());
        assertNull(sender.getPassword());
        assertNull(sender.getUsername());

        Properties properties = sender.getJavaMailProperties();
        assertEquals("1000", properties.get("mail.smtp.connectiontimeout"));
        assertEquals("2000", properties.get("mail.smtp.timeout"));
    }

    @Test
    public void testCreateWithUsernameAndPasswordAndOptionalTlS() {
        MailHostConfiguration configuration = new MailHostConfiguration.Builder().hostname("localhost")
                .username("user").password("password").protocol(MailProtocol.SMTP).useStartTls(true).build();

        JavaMailSenderImpl sender = new DefaultJavaMailSenderFactory(null, null).create(configuration, 0, 0);
        assertEquals("smtp", sender.getProtocol());
        assertEquals("UTF-8", sender.getDefaultEncoding());
        assertEquals("localhost", sender.getHost());
        assertEquals("password", sender.getPassword());
        assertEquals(JavaMailSenderImpl.DEFAULT_PORT, sender.getPort());
        assertEquals("user", sender.getUsername());

        Properties properties = sender.getJavaMailProperties();
        assertEquals("true", properties.get("mail.smtp.auth"));
        assertEquals("true", properties.get("mail.smtp.starttls.enable"));
    }

    @Test
    public void testCreateWithUsernameAndPasswordAndMandatoryStartTLS() {
        MailHostConfiguration configuration = new MailHostConfiguration.Builder().hostname("localhost")
                .username("user").password("password").protocol(MailProtocol.SMTP).requireStartTls(true).build();

        JavaMailSenderImpl sender = new DefaultJavaMailSenderFactory(null, null).create(configuration, 0, 0);
        assertEquals("smtp", sender.getProtocol());
        assertEquals("UTF-8", sender.getDefaultEncoding());
        assertEquals("localhost", sender.getHost());
        assertEquals("password", sender.getPassword());
        assertEquals(JavaMailSenderImpl.DEFAULT_PORT, sender.getPort());
        assertEquals("user", sender.getUsername());

        Properties properties = sender.getJavaMailProperties();
        assertEquals("true", properties.get("mail.smtp.auth"));
        assertEquals("true", properties.get("mail.smtp.starttls.enable"));
        assertEquals("true", properties.get("mail.smtp.starttls.required"));
    }

    @Test
    public void testCreateWithUsernameAndPasswordAndSMTPS() {
        MailHostConfiguration configuration = new MailHostConfiguration.Builder().hostname("localhost")
                .username("user").password("password").protocol(MailProtocol.SMTPS).build();

        JavaMailSenderImpl sender = new DefaultJavaMailSenderFactory(null, null).create(configuration, 0, 0);
        assertEquals("smtps", sender.getProtocol());
        assertEquals("UTF-8", sender.getDefaultEncoding());
        assertEquals("localhost", sender.getHost());
        assertEquals("password", sender.getPassword());
        assertEquals(JavaMailSenderImpl.DEFAULT_PORT, sender.getPort());
        assertEquals("user", sender.getUsername());

        Properties properties = sender.getJavaMailProperties();
        assertEquals("true", properties.get("mail.smtps.auth"));
    }
}
