package org.keycloak.services.email;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.MimeMessage;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.ServerSetup;

public class EmailSenderTest {

    private GreenMail greenMail;

    @Before
    public void before() {
        ServerSetup setup = new ServerSetup(3025, "localhost", "smtp");

        greenMail = new GreenMail(setup);
        greenMail.start();

        System.setProperty("keycloak.mail.smtp.from", "auto@keycloak.org");
        System.setProperty("keycloak.mail.smtp.host", "localhost");
        System.setProperty("keycloak.mail.smtp.port", "3025");
    }

    @After
    public void after() throws InterruptedException {
        if (greenMail != null) {
            greenMail.stop();
        }
    }

    @Test
    public void sendMail() throws AddressException, MessagingException, IOException {
        EmailSender emailSender = new EmailSender();
        emailSender.send("test@test.com", "Test subject", "Test body");

        MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
        Assert.assertEquals(1, receivedMessages.length);

        MimeMessage msg = receivedMessages[0];
        Assert.assertEquals(1, msg.getFrom().length);
        Assert.assertEquals("auto@keycloak.org", msg.getFrom()[0].toString());
        Assert.assertEquals("Test subject", msg.getSubject());
        Assert.assertEquals("Test body", ((String) msg.getContent()).trim());
    }

}
