<commit_before>package org.motechproject.admin.notification;

import org.apache.velocity.app.VelocityEngine;
import org.hamcrest.core.IsEqual;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.motechproject.admin.domain.StatusMessage;
import org.motechproject.admin.messages.Level;
import org.motechproject.email.model.Mail;
import org.motechproject.email.service.EmailSenderService;
import org.motechproject.server.config.service.PlatformSettingsService;
import org.motechproject.server.config.settings.MotechSettings;

import java.util.Map;

import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

public class EmailNotifierTest {
    @Mock
    private PlatformSettingsService settingsService;

    @Mock
    private VelocityEngine velocityEngine;

    @Mock
    private EmailSenderService emailSenderService;

    @InjectMocks
    EmailNotifier emailNotifier;
    private MotechSettings motechSettings;

    @Before
    public void setUp() {
        emailNotifier = new EmailNotifier();
        MockitoAnnotations.initMocks(this);

        motechSettings = mock(MotechSettings.class);
        when(settingsService.getPlatformSettings()).thenReturn(motechSettings);
    }

    @Test
    public void shouldSendNotification() throws Exception {
        String text = "Mail msg";
        DateTime datetime = new DateTime(2010, 12, 20, 10, 50);
        String moduleName = "admin";
        StatusMessage statusMessage = statusMessage(text, datetime, moduleName);

        when(motechSettings.getServerUrl()).thenReturn("http://serverurl");
        when(motechSettings.getServerHost()).thenReturn("serverurl");

        EmailNotifier emailNotifierSpy = spy(emailNotifier);

        ArgumentCaptor<Map> velocityArgumentCaptor = ArgumentCaptor.forClass(Map.class);
        doReturn(text).when(emailNotifierSpy).mergeTemplateIntoString(velocityArgumentCaptor.capture());

        emailNotifierSpy.send(statusMessage, "recipients");

        Assert.assertEquals(text, velocityArgumentCaptor.getValue().get("msg"));

        String msgLink = "http://serverurl/module/server/?moduleName=admin#/messages";
        Assert.assertEquals(msgLink, velocityArgumentCaptor.getValue().get("msgLink"));
        Assert.assertEquals("12/20/10 10:50 AM", velocityArgumentCaptor.getValue().get("dateTime"));
        Assert.assertEquals(moduleName, velocityArgumentCaptor.getValue().get("module"));

        ArgumentCaptor<Mail> argument = ArgumentCaptor.forClass(Mail.class);
        Mockito.verify(emailSenderService).send(argument.capture());
        Mail mail = argument.getValue();

        Assert.assertEquals(text, mail.getMessage());
        Assert.assertEquals("recipients", mail.getToAddress());
        Assert.assertEquals("noreply@serverurl",mail.getFromAddress());
    }

<commit_msg>856 | Fixed BUG: shouldSendNotification is locale dependent<commit_after>package org.motechproject.admin.notification;

import org.apache.velocity.app.VelocityEngine;
import org.hamcrest.core.IsEqual;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.motechproject.admin.domain.StatusMessage;
import org.motechproject.admin.messages.Level;
import org.motechproject.email.model.Mail;
import org.motechproject.email.service.EmailSenderService;
import org.motechproject.server.config.service.PlatformSettingsService;
import org.motechproject.server.config.settings.MotechSettings;

import java.util.Map;

import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

public class EmailNotifierTest {
    @Mock
    private PlatformSettingsService settingsService;

    @Mock
    private VelocityEngine velocityEngine;

    @Mock
    private EmailSenderService emailSenderService;

    @InjectMocks
    EmailNotifier emailNotifier;
    private MotechSettings motechSettings;

    @Before
    public void setUp() {
        emailNotifier = new EmailNotifier();
        MockitoAnnotations.initMocks(this);

        motechSettings = mock(MotechSettings.class);
        when(settingsService.getPlatformSettings()).thenReturn(motechSettings);
    }

    @Test
    public void shouldSendNotification() throws Exception {
        String text = "Mail msg";
        DateTime datetime = new DateTime(2010, 12, 20, 10, 50);
        String moduleName = "admin";
        StatusMessage statusMessage = statusMessage(text, datetime, moduleName);

        when(motechSettings.getServerUrl()).thenReturn("http://serverurl");
        when(motechSettings.getServerHost()).thenReturn("serverurl");

        EmailNotifier emailNotifierSpy = spy(emailNotifier);

        ArgumentCaptor<Map> velocityArgumentCaptor = ArgumentCaptor.forClass(Map.class);
        doReturn(text).when(emailNotifierSpy).mergeTemplateIntoString(velocityArgumentCaptor.capture());

        emailNotifierSpy.send(statusMessage, "recipients");

        Assert.assertEquals(text, velocityArgumentCaptor.getValue().get("msg"));

        String msgLink = "http://serverurl/module/server/?moduleName=admin#/messages";
        Assert.assertEquals(msgLink, velocityArgumentCaptor.getValue().get("msgLink"));
        Assert.assertTrue(velocityArgumentCaptor.getValue().get("dateTime").toString().matches("^(12[./]20|20[./]12)[./]10 10:50(| AM)$"));
        Assert.assertEquals(moduleName, velocityArgumentCaptor.getValue().get("module"));

        ArgumentCaptor<Mail> argument = ArgumentCaptor.forClass(Mail.class);
        Mockito.verify(emailSenderService).send(argument.capture());
        Mail mail = argument.getValue();

        Assert.assertEquals(text, mail.getMessage());
        Assert.assertEquals("recipients", mail.getToAddress());
        Assert.assertEquals("noreply@serverurl",mail.getFromAddress());
    }


{'fraction_non_alphanumeric': 0.1111111111111111, 'fraction_numerical': 0.007992477668077104, 'mean_word_length': 4.61301671064204, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1798434', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1734, 'n_words': 299}