package uk.ac.ox.zoo.seeg.abraid.mp.publicsite.web.tools;

import org.apache.commons.mail.Email;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.format.DateTimeFormat;
import org.junit.Before;
import org.junit.Test;
import uk.ac.ox.zoo.seeg.abraid.mp.common.config.SmtpConfiguration;
import uk.ac.ox.zoo.seeg.abraid.mp.common.service.core.EmailService;
import uk.ac.ox.zoo.seeg.abraid.mp.common.service.core.EmailServiceImpl;
import uk.ac.ox.zoo.seeg.abraid.mp.common.util.EmailFactory;
import uk.ac.ox.zoo.seeg.abraid.mp.dataacquisition.acquirers.DataAcquisitionException;
import uk.ac.ox.zoo.seeg.abraid.mp.dataacquisition.service.DataAcquisitionService;

import java.io.File;

import static org.mockito.Mockito.*;

/**
 * Tests the UploadCsvControllerHelper class.
 *
 * Copyright (c) 2014 University of Oxford
 */
public class UploadCsvControllerHelperTest {
    private DataAcquisitionService dataAcquisitionService;
    private EmailService emailService;
    private Email email;
    private UploadCsvControllerHelper helper;

    private static final String EMAIL_TEMPLATE_PATH = "PublicSite/src/uk/ac/ox/zoo/seeg/abraid/mp/publicsite/web/tools";

    @Before
    public void setUp() throws Exception {
        dataAcquisitionService = mock(DataAcquisitionService.class);
        emailService = createEmailService();
        helper = new UploadCsvControllerHelper(dataAcquisitionService, emailService);

        DateTimeUtils.setCurrentMillisFixed(DateTime.now().getMillis());
    }

    private EmailService createEmailService() throws Exception {
        // Create a real e-mail service with a mock Email class, and specifying the path to our e-mail template
        email = mock(Email.class);
        EmailFactory factory = mock(EmailFactory.class);
        when(factory.createEmail()).thenReturn(email);
        return new EmailServiceImpl(factory, "", "", mock(SmtpConfiguration.class),
                new File[] {new File(EMAIL_TEMPLATE_PATH)});
    }

    @Test
    public void acquireCsvDataSendsCorrectEmailWhenAcquisitionSucceeds() throws Exception {
        // Arrange
        String csv = "Test csv";
        String message = "Saved 10 disease occurrence(s) in 8 location(s) (of which 7 location(s) passed QC)";
        String expectedSubject = "CSV upload succeeded";
        String expectedEmailEnd =
                "The upload succeeded with message:\r\n" +
                "\r\n" +
                "Saved 10 disease occurrence(s) in 8 location(s) (of which 7 location(s) passed QC)\r\n";

        when(dataAcquisitionService.acquireCsvData(csv)).thenReturn(message);

        // Act and assert
        acquireCsvDataSendsCorrectEmail(message, expectedEmailEnd, expectedSubject);
    }

    @Test
    public void acquireCsvDataSendsCorrectEmailWhenAcquisitionFails() throws Exception {
        // Arrange
        String csv = "Test csv";
        String message = "Some error during data acquisition";
        String expectedSubject = "CSV upload failed";
        String expectedEmailEnd =
                "The upload failed with message:\r\n" +
                "\r\n" +
                "Some error during data acquisition\r\n";

        when(dataAcquisitionService.acquireCsvData(csv)).thenThrow(new DataAcquisitionException(message));

        // Act and assert
        acquireCsvDataSendsCorrectEmail(message, expectedEmailEnd, expectedSubject);
    }

    private void acquireCsvDataSendsCorrectEmail(String message, String expectedEmailEnding, String expectedSubject)
            throws Exception {
        // Arrange
        String csv = "Test csv";
        String userEmailAddress = "user@email.com";
        String filePath = "/path/to/test.csv";

        // Act
        helper.acquireCsvData(csv, userEmailAddress, filePath);

        // Assert
        String nowString = DateTimeFormat.longDateTime().print(DateTime.now());
        verify(email).setMsg("Here are the results of the CSV upload that you submitted.\r\n" +
                "\r\n" +
                "File: /path/to/test.csv\r\n" +
                "Submitted on: " + nowString + "\r\n" +
                "Completed on: " + nowString + "\r\n" +
                "\r\n" + expectedEmailEnding);
        verify(email).setSubject(expectedSubject);
        verify(email).addTo(userEmailAddress);
    }
}

@@ -709,24 +709,87 @@
 a.io.File;%0A%0A
+import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;%0A
 import stati
@@ -2463,34 +2463,32 @@
 d with message:%5C
-r%5C
 n%22 +%0A           
@@ -2486,34 +2486,32 @@
               %22%5C
-r%5C
 n%22 +%0A           
@@ -2599,18 +2599,16 @@
 sed QC)%5C
-r%5C
 n%22;%0A%0A   
@@ -2745,33 +2745,24 @@
 orrectEmail(
-message, 
 expectedEmai
@@ -3143,18 +3143,16 @@
 essage:%5C
-r%5C
 n%22 +%0A   
@@ -3158,34 +3158,32 @@
               %22%5C
-r%5C
 n%22 +%0A           
@@ -3223,18 +3223,16 @@
 isition%5C
-r%5C
 n%22;%0A%0A   
@@ -3402,25 +3402,16 @@
 ctEmail(
-message, 
 expected
@@ -3505,24 +3505,8 @@
 ing 
-message, String 
 expe
@@ -3936,16 +3936,67 @@
 .setMsg(
+argThat(equalToIgnoringWhiteSpace(%0A                
 %22Here ar
@@ -4047,18 +4047,16 @@
 mitted.%5C
-r%5C
 n%22 +%0A   
@@ -4066,26 +4066,24 @@
           %22%5C
-r%5C
 n%22 +%0A       
@@ -4116,18 +4116,16 @@
 est.csv%5C
-r%5C
 n%22 +%0A   
@@ -4162,34 +4162,32 @@
 + nowString + %22%5C
-r%5C
 n%22 +%0A           
@@ -4224,18 +4224,16 @@
 ing + %22%5C
-r%5C
 n%22 +%0A   
@@ -4251,10 +4251,8 @@
   %22%5C
-r%5C
 n%22 +
@@ -4272,16 +4272,18 @@
 lEnding)
+))
 ;%0A      
@@ -4309,16 +4309,19 @@
 Subject(
+eq(
 expected
@@ -4324,24 +4324,25 @@
 ctedSubject)
+)
 ;%0A        ve
@@ -4359,16 +4359,19 @@
 ).addTo(
+eq(
 userEmai
@@ -4379,16 +4379,17 @@
 Address)
+)
 ;%0A    %7D%0A

Fix following Travis