ISIS-1557: adds email configuration keys to allow the to, cc and bcc email 
addresses to be overridden.


Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/7c2ca993
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/7c2ca993
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/7c2ca993

Branch: refs/heads/maint-1.13.3
Commit: 7c2ca9933e9200aee4f93b04f4e08c331575fece
Parents: 07b0ecf
Author: Dan Haywood <d...@haywood-associates.co.uk>
Authored: Wed Dec 21 13:08:45 2016 +0000
Committer: Dan Haywood <d...@haywood-associates.co.uk>
Committed: Wed Dec 21 13:08:45 2016 +0000

----------------------------------------------------------------------
 .../guides/_rgcfg_configuring-core.adoc         | 28 +++++++++
 .../guides/_rgsvc_api_EmailService.adoc         | 12 ++++
 .../services/email/EmailServiceDefault.java     | 53 ++++++++++++++---
 .../email/EmailServiceDefaultTest_actually.java | 61 ++++++++++++++++++++
 .../email/EmailServiceDefaultTest_notEmpty.java | 45 +++++++++++++++
 5 files changed, 191 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/7c2ca993/adocs/documentation/src/main/asciidoc/guides/_rgcfg_configuring-core.adoc
----------------------------------------------------------------------
diff --git 
a/adocs/documentation/src/main/asciidoc/guides/_rgcfg_configuring-core.adoc 
b/adocs/documentation/src/main/asciidoc/guides/_rgcfg_configuring-core.adoc
index 7223385..3a6e1c2 100644
--- a/adocs/documentation/src/main/asciidoc/guides/_rgcfg_configuring-core.adoc
+++ b/adocs/documentation/src/main/asciidoc/guides/_rgcfg_configuring-core.adoc
@@ -247,6 +247,34 @@ If no configuration property is available, then the 
defaults is determined by th
 
 | `isis.service.` +
 `email.` +
+`override.bcc` +
+| email address
+|(`1.13.3-SNAPSHOT`) intended to simplify testing, if specified then the 
email's `bcc` address will be that specified (rather than the email address(es) 
passed in as an argument to `EmailService#send(...)`).  +
+
+NB: note that the key is mis-spelt, (`isis.service.email` rather than 
`isis.services.email`)
+
+
+| `isis.service.` +
+`email.` +
+`override.cc` +
+| email address
+|(`1.13.3-SNAPSHOT`) intended to simplify testing, if specified then the 
email's `cc` address will be that specified (rather than the email address(es) 
passed in as an argument to `EmailService#send(...)`).  +
+
+NB: note that the key is mis-spelt, (`isis.service.email` rather than 
`isis.services.email`)
+
+
+| `isis.service.` +
+`email.` +
+`override.to` +
+| email address
+|(`1.13.3-SNAPSHOT`) intended to simplify testing, if specified then the 
email's `to` address will be that specified (rather than the email address(es) 
passed in as an argument to `EmailService#send(...)`).  +
+
+NB: note that the key is mis-spelt, (`isis.service.email` rather than 
`isis.services.email`)
+
+
+
+| `isis.service.` +
+`email.` +
 `port` +
 | port number (`_587_`)
 |The port number for the SMTP service on the the external SMTP host (used by 
xref:rgsvc.adoc#_rgsvc_api_EmailService[`EmailService`]).  +

http://git-wip-us.apache.org/repos/asf/isis/blob/7c2ca993/adocs/documentation/src/main/asciidoc/guides/_rgsvc_api_EmailService.adoc
----------------------------------------------------------------------
diff --git 
a/adocs/documentation/src/main/asciidoc/guides/_rgsvc_api_EmailService.adoc 
b/adocs/documentation/src/main/asciidoc/guides/_rgsvc_api_EmailService.adoc
index 7c58b40..9215dd9 100644
--- a/adocs/documentation/src/main/asciidoc/guides/_rgsvc_api_EmailService.adoc
+++ b/adocs/documentation/src/main/asciidoc/guides/_rgsvc_api_EmailService.adoc
@@ -73,6 +73,18 @@ In addition (as of `1.13.3-SNAPSHOT`), the following 
properties can be set:
 Whether to throw an exception if there the email cannot be sent (probably 
because of some misconfiguration).
 This behaviour is (now) the default; the old behaviour (of just returning 
`false` from the `send()` method) can be re-enabled by setting this property to 
`false`.
 
+* `isis.service.email.override.to` +
++
+Intended to simplify testing, if specified then the email's `to` address will 
be that specified (rather than the email address(es) passed in as an argument 
to `EmailService#send(...)`).
+
+* `isis.service.email.override.cc` +
++
+Similarly, to override the `cc` email address.
+
+* `isis.service.email.override.to` +
++
+Similarly, to override the `bcc` email address.
+
 * `isis.service.email.socketTimeout` +
 +
 The socket timeout, defaulting to 2000ms.

http://git-wip-us.apache.org/repos/asf/isis/blob/7c2ca993/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java
 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java
index 76d7d02..5b01331 100644
--- 
a/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java
+++ 
b/core/runtime/src/main/java/org/apache/isis/core/runtime/services/email/EmailServiceDefault.java
@@ -18,6 +18,7 @@
  */
 package org.apache.isis.core.runtime.services.email;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.Properties;
 
@@ -79,6 +80,10 @@ public class EmailServiceDefault implements EmailService {
     private static final String ISIS_SERVICE_EMAIL_SOCKET_CONNECTION_TIMEOUT = 
"isis.service.email.socketConnectionTimeout";
     private static final int 
ISIS_SERVICE_EMAIL_SOCKET_CONNECTION_TIMEOUT_DEFAULT = 2000;
 
+    private static final String ISIS_SERVICE_EMAIL_OVERRIDE_TO = 
"isis.service.email.override.to";
+    private static final String ISIS_SERVICE_EMAIL_OVERRIDE_CC = 
"isis.service.email.override.cc";
+    private static final String ISIS_SERVICE_EMAIL_OVERRIDE_BCC = 
"isis.service.email.override.bcc";
+
     //endregion
 
     //region > init
@@ -135,6 +140,19 @@ public class EmailServiceDefault implements EmailService {
     protected int getSocketConnectionTimeout() {
         return 
configuration.getInteger(ISIS_SERVICE_EMAIL_SOCKET_CONNECTION_TIMEOUT, 
ISIS_SERVICE_EMAIL_SOCKET_CONNECTION_TIMEOUT_DEFAULT);
     }
+
+    protected String getEmailOverrideTo() {
+        return configuration.getString(ISIS_SERVICE_EMAIL_OVERRIDE_TO);
+    }
+
+    protected String getEmailOverrideCc() {
+        return configuration.getString(ISIS_SERVICE_EMAIL_OVERRIDE_CC);
+    }
+
+    protected String getEmailOverrideBcc() {
+        return configuration.getString(ISIS_SERVICE_EMAIL_OVERRIDE_BCC);
+    }
+
     //endregion
 
     //region > isConfigured
@@ -194,14 +212,22 @@ public class EmailServiceDefault implements EmailService {
                 }
             }
 
-            if(notEmpty(toList)) {
-                email.addTo(toList.toArray(new String[toList.size()]));
+
+            final String overrideTo = getEmailOverrideTo();
+            final String overrideCc = getEmailOverrideCc();
+            final String overrideBcc = getEmailOverrideBcc();
+
+            final String[] toListElseOverride = actually(toList, overrideTo);
+            if(notEmpty(toListElseOverride)) {
+                email.addTo(toListElseOverride);
             }
-            if(notEmpty(ccList)) {
-                email.addCc(ccList.toArray(new String[ccList.size()]));
+            final String[] ccListElseOverride = actually(ccList, overrideCc);
+            if(notEmpty(ccListElseOverride)) {
+                email.addCc(ccListElseOverride);
             }
-            if(notEmpty(bccList)) {
-                email.addBcc(bccList.toArray(new String[bccList.size()]));
+            final String[] bccListElseOverride = actually(bccList, 
overrideBcc);
+            if(notEmpty(bccListElseOverride)) {
+                email.addBcc(bccListElseOverride);
             }
 
             email.send();
@@ -219,9 +245,20 @@ public class EmailServiceDefault implements EmailService {
     }
     //endregion
 
+
     //region > helper methods
-    private boolean notEmpty(final List<String> toList) {
-        return toList != null && !toList.isEmpty();
+
+    static String[] actually(final List<String> original, final String 
overrideIfAny) {
+        final List<String> addresses = Strings.isNullOrEmpty(overrideIfAny)
+                ? original == null
+                    ? Collections.<String>emptyList()
+                    : original
+                : Collections.singletonList(overrideIfAny);
+        return addresses.toArray(new String[addresses.size()]);
+    }
+
+    static boolean notEmpty(final String[] addresses) {
+        return addresses != null && addresses.length > 0;
     }
     //endregion
 

http://git-wip-us.apache.org/repos/asf/isis/blob/7c2ca993/core/runtime/src/test/java/org/apache/isis/core/runtime/services/email/EmailServiceDefaultTest_actually.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/email/EmailServiceDefaultTest_actually.java
 
b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/email/EmailServiceDefaultTest_actually.java
new file mode 100644
index 0000000..cfbd748
--- /dev/null
+++ 
b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/email/EmailServiceDefaultTest_actually.java
@@ -0,0 +1,61 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.core.runtime.services.email;
+
+import com.google.common.collect.Lists;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+public class EmailServiceDefaultTest_actually {
+
+    @Test
+    public void when_null() throws Exception {
+
+        final String[] actually = EmailServiceDefault.actually(null, null);
+
+        assertThat(actually, is(not(nullValue())));
+        assertThat(actually.length, is(0));
+    }
+
+    @Test
+    public void when_not_null_but_no_override() throws Exception {
+
+        final String[] actually = 
EmailServiceDefault.actually(Lists.newArrayList("j...@tribiani.com", 
"rac...@green.com"), null);
+
+        assertThat(actually, is(not(nullValue())));
+        assertThat(actually.length, is(2));
+    }
+
+    @Test
+    public void when_not_null_but_with_override() throws Exception {
+
+        final String[] actually = 
EmailServiceDefault.actually(Lists.newArrayList("j...@tribiani.com", 
"rac...@green.com"), "r...@geller.com");
+
+        assertThat(actually, is(not(nullValue())));
+        assertThat(actually.length, is(1));
+        assertThat(actually[0], is(equalTo("r...@geller.com")));
+    }
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/isis/blob/7c2ca993/core/runtime/src/test/java/org/apache/isis/core/runtime/services/email/EmailServiceDefaultTest_notEmpty.java
----------------------------------------------------------------------
diff --git 
a/core/runtime/src/test/java/org/apache/isis/core/runtime/services/email/EmailServiceDefaultTest_notEmpty.java
 
b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/email/EmailServiceDefaultTest_notEmpty.java
new file mode 100644
index 0000000..e4b00ff
--- /dev/null
+++ 
b/core/runtime/src/test/java/org/apache/isis/core/runtime/services/email/EmailServiceDefaultTest_notEmpty.java
@@ -0,0 +1,45 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.core.runtime.services.email;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class EmailServiceDefaultTest_notEmpty {
+
+    @Test
+    public void when_not_empty() throws Exception {
+        assertThat(EmailServiceDefault.notEmpty(new String[] { 
"j...@tribiani.com", "rac...@green.com" }), is(true));
+        assertThat(EmailServiceDefault.notEmpty(new String[] { 
"rac...@green.com" }), is(true));
+    }
+
+    @Test
+    public void when_null() throws Exception {
+        assertThat(EmailServiceDefault.notEmpty(null), is(false));
+    }
+
+    @Test
+    public void when_empty() throws Exception {
+        assertThat(EmailServiceDefault.notEmpty(new String[]{}), is(false));
+    }
+
+
+}
\ No newline at end of file

Reply via email to