package com.elorating.service.email;

import com.elorating.model.League;
import com.elorating.model.Match;
import com.elorating.model.Player;
import com.elorating.model.User;
import com.elorating.service.BaseServiceTest;
import com.elorating.utils.MatchTestUtils;
import com.elorating.utils.PlayerTestUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

public class EmailGeneratorTest extends BaseServiceTest {

    @Autowired
    EmailGenerator emailGenerator;

    League league;
    List<Player> players = PlayerTestUtils.generatePlayerList(2, league);
    Match match = MatchTestUtils.generateMatch(league, players.get(0), players.get(1), false);
    String originUrl = "originUrl";

    @Before
    public void setup() {
        league = new League("test");
        players = PlayerTestUtils.generatePlayerList(2, league);
        User user = new User("testUser", "test@email.com");
        for (int i = 0; i < players.size(); i++) {
            Player player = players.get(i);
            player.setUser(user);
            players.set(i, player);
        }
        match = MatchTestUtils.generateMatch(league, players.get(0), players.get(1), false);
    }

    @Test
    public void test_generateScheduleMatches_shouldReturnTwoEmails_success() throws Exception {
        Set<EmailBuilder> scheduleMatchEmails = emailGenerator.generateEmails(match, emailGenerator.SCHEDULE_MATCH, originUrl);

        Assert.assertNotNull(scheduleMatchEmails);
        Assert.assertTrue(scheduleMatchEmails.size() == 2);
        for (EmailBuilder email : scheduleMatchEmails) {
            Assert.assertTrue(email.getClass() == ScheduledMatchEmail.class);
        }
    }

    @Test
    public void test_generateEditMatches_shouldReturnTwoEmails_success() throws Exception {
        Set<EmailBuilder> scheduleMatchEmails = emailGenerator.generateEmails(match, emailGenerator.EDIT_MATCH, originUrl);

        Assert.assertNotNull(scheduleMatchEmails);
        Assert.assertTrue(scheduleMatchEmails.size() == 2);
        for (EmailBuilder email : scheduleMatchEmails) {
            Assert.assertTrue(email.getClass() == EditMatchEmail.class);
        }
    }

    @Test
    public void test_generateCancelMatches_shouldReturnTwoEmails_success() throws Exception {
        Set<EmailBuilder> scheduleMatchEmails = emailGenerator.generateEmails(match, emailGenerator.CANCEL_MATCH, originUrl);

        Assert.assertNotNull(scheduleMatchEmails);
        Assert.assertTrue(scheduleMatchEmails.size() == 2);
        for (EmailBuilder email : scheduleMatchEmails) {
            Assert.assertTrue(email.getClass() == CancelledMatchEmail.class);
        }
    }
}

@@ -62,116 +62,9 @@
 del.
-League;%0Aimport com.elorating.model.Match;%0Aimport com.elorating.model.Player;%0Aimport com.elorating.model.User
+*
 ;%0Aim
@@ -710,16 +710,72 @@
 ginUrl%22;
+%0A    User user = new User(%22testUser%22, %22test@email.com%22);
 %0A%0A    @B
@@ -912,68 +912,8 @@
 e);%0A
-        User user = new User(%22testUser%22, %22test@email.com%22);%0A
     
@@ -1175,32 +1175,366 @@
  false);%0A    %7D%0A%0A
+    private EmailsNotifications setEmailNotifications(boolean scheduledNotifications, boolean editedNotifications, boolean canceledNotifications) %7B%0A        EmailsNotifications emailsNotifications = new EmailsNotifications(scheduledNotifications, editedNotifications, canceledNotifications);%0A        return emailsNotifications;%0A    %7D%0A%0A
     @Test%0A    pu
@@ -1615,32 +1615,111 @@
 ows Exception %7B%0A
+        user.setEmailNotifications(setEmailNotifications(true, false, false));%0A
         Set%3CEmai
@@ -2188,32 +2188,111 @@
 ows Exception %7B%0A
+        user.setEmailNotifications(setEmailNotifications(false, true, false));%0A
         Set%3CEmai
@@ -2297,32 +2297,28 @@
 ailBuilder%3E 
-schedule
+edit
 MatchEmails 
@@ -2421,32 +2421,28 @@
 sertNotNull(
-schedule
+edit
 MatchEmails)
@@ -2465,32 +2465,28 @@
 .assertTrue(
-schedule
+edit
 MatchEmails.
@@ -2529,32 +2529,28 @@
 der email : 
-schedule
+edit
 MatchEmails)
@@ -2738,32 +2738,111 @@
 ows Exception %7B%0A
+        user.setEmailNotifications(setEmailNotifications(false, false, true));%0A
         Set%3CEmai
@@ -2847,32 +2847,30 @@
 ailBuilder%3E 
-schedule
+cancel
 MatchEmails 
@@ -2975,32 +2975,30 @@
 sertNotNull(
-schedule
+cancel
 MatchEmails)
@@ -3021,32 +3021,30 @@
 .assertTrue(
-schedule
+cancel
 MatchEmails.
@@ -3087,32 +3087,30 @@
 der email : 
-schedule
+cancel
 MatchEmails)
@@ -3205,11 +3205,762 @@
  %7D%0A    %7D
+%0A%0A    @Test%0A    public void test_emailNotifications_allEmailsSetToFalse_shouldNotGenerateEmails() throws Exception %7B%0A        user.setEmailNotifications(new EmailsNotifications(false, false, false));%0A        Set%3CEmailBuilder%3E scheduleMatchEmails = emailGenerator.generateEmails(match, emailGenerator.SCHEDULE_MATCH, originUrl);%0A        Set%3CEmailBuilder%3E editMatchEmails = emailGenerator.generateEmails(match, emailGenerator.EDIT_MATCH, originUrl);%0A        Set%3CEmailBuilder%3E cancelMatchEmails = emailGenerator.generateEmails(match, emailGenerator.CANCEL_MATCH, originUrl);%0A%0A        Assert.assertTrue(scheduleMatchEmails.size() == 0);%0A        Assert.assertTrue(editMatchEmails.size() == 0);%0A        Assert.assertTrue(cancelMatchEmails.size() == 0);%0A    %7D
 %0A%7D%0A

Fix EmailGenerator tests