summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2017-09-04 10:05:38 -0700
committerJesse Morgan <jesse@jesterpm.net>2017-09-04 10:05:38 -0700
commitcbf149af1f07bb98c1f856948a79dcf3fb0c43b3 (patch)
tree7901334e10e181ba2c79c09b3a9a32f16c1abc42
parent72ee0f10ddca0d880e50d13446f9ac0269e542eb (diff)
SESNotificationService to accept multiple destinations.20170904
notificationEmail is now a comma separated list of addresses. Adding unit tests for SESNotificationService. Refactored AWS credential selection into ConfigCredentialProvider.
-rw-r--r--src/main/java/com/p4square/grow/backend/SESNotificationService.java37
-rw-r--r--src/main/java/com/p4square/grow/backend/dynamo/DynamoDatabase.java24
-rw-r--r--src/main/java/com/p4square/grow/config/Config.java32
-rw-r--r--src/main/java/com/p4square/grow/config/ConfigCredentialProvider.java43
-rw-r--r--src/test/java/com/p4square/grow/backend/SESNotificationServiceTest.java94
5 files changed, 180 insertions, 50 deletions
diff --git a/src/main/java/com/p4square/grow/backend/SESNotificationService.java b/src/main/java/com/p4square/grow/backend/SESNotificationService.java
index 58b732d..b42d09b 100644
--- a/src/main/java/com/p4square/grow/backend/SESNotificationService.java
+++ b/src/main/java/com/p4square/grow/backend/SESNotificationService.java
@@ -1,13 +1,12 @@
package com.p4square.grow.backend;
-import com.amazonaws.auth.AWSCredentials;
-import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
import com.amazonaws.services.simpleemail.model.*;
import com.p4square.grow.config.Config;
+import com.p4square.grow.config.ConfigCredentialProvider;
import org.apache.log4j.Logger;
/**
@@ -22,40 +21,22 @@ public class SESNotificationService implements NotificationService {
private final Destination mDestination;
public SESNotificationService(final Config config) {
- AWSCredentials creds;
-
- String awsAccessKey = config.getString("awsAccessKey");
- if (awsAccessKey != null) {
- creds = new AWSCredentials() {
- @Override
- public String getAWSAccessKeyId() {
- return config.getString("awsAccessKey");
- }
- @Override
- public String getAWSSecretKey() {
- return config.getString("awsSecretKey");
- }
- };
- } else {
- creds = new DefaultAWSCredentialsProviderChain().getCredentials();
- }
-
- mClient = new AmazonSimpleEmailServiceClient(creds);
+ this(config, new AmazonSimpleEmailServiceClient(new ConfigCredentialProvider(config)));
+ // Set the AWS region.
String region = config.getString("awsRegion");
if (region != null) {
mClient.setRegion(Region.getRegion(Regions.fromName(region)));
}
+ }
+
+ public SESNotificationService(Config config, AmazonSimpleEmailService client) {
+ mClient = client;
mSourceAddress = config.getString("notificationSourceEmail");
- final String dest = config.getString("notificationEmail");
- if (dest != null) {
- mDestination = new Destination().withToAddresses(dest);
- } else {
- // Notifications are not configured.
- mDestination = null;
- }
+ final String[] dests = config.getString("notificationEmail", "").split(",");
+ mDestination = new Destination().withToAddresses(dests);
}
@Override
diff --git a/src/main/java/com/p4square/grow/backend/dynamo/DynamoDatabase.java b/src/main/java/com/p4square/grow/backend/dynamo/DynamoDatabase.java
index 68a165d..e04933f 100644
--- a/src/main/java/com/p4square/grow/backend/dynamo/DynamoDatabase.java
+++ b/src/main/java/com/p4square/grow/backend/dynamo/DynamoDatabase.java
@@ -4,14 +4,11 @@
package com.p4square.grow.backend.dynamo;
-import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
-import com.amazonaws.auth.AWSCredentials;
-import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
@@ -40,6 +37,7 @@ import com.amazonaws.services.dynamodbv2.model.UpdateTableRequest;
import com.amazonaws.services.dynamodbv2.model.UpdateTableResult;
import com.p4square.grow.config.Config;
+import com.p4square.grow.config.ConfigCredentialProvider;
/**
* A wrapper around the Dynamo API.
@@ -49,25 +47,7 @@ public class DynamoDatabase {
private final String mTablePrefix;
public DynamoDatabase(final Config config) {
- AWSCredentials creds;
-
- String awsAccessKey = config.getString("awsAccessKey");
- if (awsAccessKey != null) {
- creds = new AWSCredentials() {
- @Override
- public String getAWSAccessKeyId() {
- return config.getString("awsAccessKey");
- }
- @Override
- public String getAWSSecretKey() {
- return config.getString("awsSecretKey");
- }
- };
- } else {
- creds = new DefaultAWSCredentialsProviderChain().getCredentials();
- }
-
- mClient = new AmazonDynamoDBClient(creds);
+ mClient = new AmazonDynamoDBClient(new ConfigCredentialProvider(config));
String endpoint = config.getString("dynamoEndpoint");
if (endpoint != null) {
diff --git a/src/main/java/com/p4square/grow/config/Config.java b/src/main/java/com/p4square/grow/config/Config.java
index 2fc2ea3..f810980 100644
--- a/src/main/java/com/p4square/grow/config/Config.java
+++ b/src/main/java/com/p4square/grow/config/Config.java
@@ -200,4 +200,36 @@ public class Config {
return defaultValue;
}
+
+ /**
+ * Set a value in the config.
+ *
+ * @param key Config name
+ * @param value Config value
+ */
+ public void setString(String key, String value) {
+ mProperties.setProperty(mDomain + "." + key, value);
+ }
+
+ /**
+ * Set a value in the config.
+ *
+ * @param key Config name
+ * @param value Config value
+ */
+ public void setInt(String key, int value) {
+ setString(key, String.valueOf(value));
+ }
+
+ /**
+ * Set a value in the config.
+ *
+ * @param key Config name
+ * @param value Config value
+ */
+ public void setBoolean(String key, boolean value) {
+ setString(key, String.valueOf(value));
+ }
+
+
}
diff --git a/src/main/java/com/p4square/grow/config/ConfigCredentialProvider.java b/src/main/java/com/p4square/grow/config/ConfigCredentialProvider.java
new file mode 100644
index 0000000..06abd48
--- /dev/null
+++ b/src/main/java/com/p4square/grow/config/ConfigCredentialProvider.java
@@ -0,0 +1,43 @@
+package com.p4square.grow.config;
+
+import com.amazonaws.auth.AWSCredentials;
+import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
+
+/**
+ * AWSCredentials credentials backed by config.
+ *
+ * Falls back to DefaultAWSCredentialsProviderChain if the credentials are not in the config.
+ */
+public class ConfigCredentialProvider implements AWSCredentials {
+
+ private AWSCredentials mCredentials;
+
+ public ConfigCredentialProvider(final Config config) {
+ String awsAccessKey = config.getString("awsAccessKey");
+ if (awsAccessKey != null) {
+ mCredentials = new AWSCredentials() {
+ @Override
+ public String getAWSAccessKeyId() {
+ return config.getString("awsAccessKey");
+ }
+
+ @Override
+ public String getAWSSecretKey() {
+ return config.getString("awsSecretKey");
+ }
+ };
+ } else {
+ mCredentials = new DefaultAWSCredentialsProviderChain().getCredentials();
+ }
+ }
+
+ @Override
+ public String getAWSAccessKeyId() {
+ return mCredentials.getAWSAccessKeyId();
+ }
+
+ @Override
+ public String getAWSSecretKey() {
+ return mCredentials.getAWSSecretKey();
+ }
+}
diff --git a/src/test/java/com/p4square/grow/backend/SESNotificationServiceTest.java b/src/test/java/com/p4square/grow/backend/SESNotificationServiceTest.java
new file mode 100644
index 0000000..6b61f8e
--- /dev/null
+++ b/src/test/java/com/p4square/grow/backend/SESNotificationServiceTest.java
@@ -0,0 +1,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));
+ }
+
+} \ No newline at end of file