package org.opensingular.server.commons.service;

import org.junit.Assert;
import org.junit.Test;
import org.opensingular.server.commons.exception.SingularServerException;
import org.opensingular.server.commons.service.dto.Email;
import org.opensingular.server.commons.test.SingularCommonsBaseTest;
import org.springframework.test.annotation.Rollback;

import javax.inject.Inject;
import javax.transaction.Transactional;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class EmailTest extends SingularCommonsBaseTest {

    @Inject
    private EmailPersistenceService emailPersistenceService;

    @Test
    @Transactional
    @Rollback
    public void testSendEmail() throws IOException {
        Email e = new Email();
        e.setCreationDate(new Date());
        e.addTo("nada@mada.com");
        e.withSubject("Teste e-mail");
        e.withContent("super corpo de e-mail, ativar!");
        File f = File.createTempFile("nada","bada");
        f.deleteOnExit();
        e.addAttachment(f, "lada");
        Assert.assertTrue(emailPersistenceService.send(e));
    }

    @Test(expected = SingularServerException.class)
    @Rollback
    public void testEmailNaoExiste(){
        Email e = new Email();
        emailPersistenceService.send(e);
    }

    @Test
    @Transactional
    @Rollback
    public void testCountPending(){
        generateMockEmailAddresseEntitties();
        Assert.assertEquals(2, emailPersistenceService.countPendingRecipients());
    }

    @Test
    @Transactional
    @Rollback
    public void testListPending(){
        generateMockEmailAddresseEntitties();
        List<Email.Addressee> list = emailPersistenceService.listPendingRecipients(0, 10);

        Assert.assertEquals(2, list.size());
    }

    private void generateMockEmailAddresseEntitties() {
        Email email = new Email();
        email.withSubject("teste");
        email.withContent("conteudo de teste");
        email.setCreationDate(new Date());
        email.addTo("mock.entity@teste.com", "mock.entity2@teste.com");

        emailPersistenceService.send(email);
    }
}

@@ -89,86 +89,319 @@
 nit.
-Test;%0Aimport org.opensingular.server.commons.exception.SingularServerException
+FixMethodOrder;%0Aimport org.junit.Test;%0Aimport org.junit.runners.MethodSorters;%0Aimport org.opensingular.server.commons.exception.SingularServerException;%0Aimport org.opensingular.server.commons.persistence.dao.EmailAddresseeDao;%0Aimport org.opensingular.server.commons.persistence.entity.email.EmailAddresseeEntity
 ;%0Aim
@@ -743,16 +743,62 @@
 .List;%0A%0A
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)%0A
 public c
@@ -921,16 +921,100 @@
 rvice;%0A%0A
+    @Inject%0A    private EmailAddresseeDao%3CEmailAddresseeEntity%3E emailAddresseeDao;%0A%0A
     @Tes
@@ -1030,38 +1030,24 @@
 ansactional%0A
-    @Rollback%0A
     public v
@@ -2130,24 +2130,341 @@
 ());%0A    %7D%0A%0A
+    @Test%0A    @Rollback%0A    public void testSentMarked()%7B%0A        List%3CEmail.Addressee%3E list = emailPersistenceService.listPendingRecipients(0, 1);%0A%0A        Email.Addressee addressee = list.get(0);%0A%0A        emailPersistenceService.markAsSent(addressee);%0A%0A        Assert.assertNotNull(addressee.getSentDate());%0A    %7D%0A%0A
     private 

Teste markAsSend EmailTest