summaryrefslogtreecommitdiff
path: root/src/test/java/com/p4square/grow/backend/SESNotificationServiceTest.java
blob: 6b61f8e9694bd44f2312399f08019094002cdb75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.p4square.grow.backend;

import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.model.SendEmailRequest;
import com.amazonaws.services.simpleemail.model.SendEmailResult;
import com.p4square.grow.config.Config;
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Tests for {@link SESNotificationService}.
 */
public class SESNotificationServiceTest {

    private SESNotificationService service;

    private AmazonSimpleEmailService mockSES;
    private SendEmailResult emailResult;

    @Before
    public void setup() {
        mockSES = EasyMock.createMock(AmazonSimpleEmailService.class);
        emailResult = new SendEmailResult().withMessageId("1234");
    }

    @Test
    public void notificationsDisabled() {
        // Setup
        Config testConfig = new Config();
        service = new SESNotificationService(testConfig, mockSES);

        EasyMock.replay(mockSES);

        // Run test
        service.sendNotification("Hello World");

        // Verify
        EasyMock.verify(mockSES);

    }

    @Test
    public void sendNotification() throws Exception {
        // Setup
        Config testConfig = new Config();
        testConfig.setString("notificationSourceEmail", "from@example.com");
        testConfig.setString("notificationEmail", "to@example.com");
        service = new SESNotificationService(testConfig, mockSES);

        Capture<SendEmailRequest> requestCapture = EasyMock.newCapture();
        EasyMock.expect(mockSES.sendEmail(EasyMock.capture(requestCapture))).andReturn(emailResult).once();
        EasyMock.replay(mockSES);

        // Run test
        service.sendNotification("Hello World");

        // Verify
        EasyMock.verify(mockSES);
        SendEmailRequest request = requestCapture.getValue();
        assertEquals("from@example.com", request.getSource());
        assertEquals(1, request.getDestination().getToAddresses().size());
        assertEquals("to@example.com", request.getDestination().getToAddresses().get(0));
    }

    @Test
    public void testMultipleDestinations() {
        // Setup
        Config testConfig = new Config();
        testConfig.setString("notificationSourceEmail", "from@example.com");
        testConfig.setString("notificationEmail", "to@example.com,another@example.com");
        service = new SESNotificationService(testConfig, mockSES);

        Capture<SendEmailRequest> requestCapture = EasyMock.newCapture();
        EasyMock.expect(mockSES.sendEmail(EasyMock.capture(requestCapture))).andReturn(emailResult).once();
        EasyMock.replay(mockSES);


        // Run test
        service.sendNotification("Hello World");

        // Verify
        EasyMock.verify(mockSES);
        SendEmailRequest request = requestCapture.getValue();
        assertEquals("from@example.com", request.getSource());
        assertEquals(2, request.getDestination().getToAddresses().size());
        assertEquals("to@example.com", request.getDestination().getToAddresses().get(0));
        assertEquals("another@example.com", request.getDestination().getToAddresses().get(1));
    }

}