🚧 Add runtime parameters to Action interface
authorDavid Roden <bombe@pterodactylus.net>
Fri, 20 Dec 2024 11:07:02 +0000 (12:07 +0100)
committerDavid Roden <bombe@pterodactylus.net>
Fri, 20 Dec 2024 11:07:02 +0000 (12:07 +0100)
src/main/java/net/pterodactylus/rhynodge/Action.java
src/main/java/net/pterodactylus/rhynodge/actions/EmailAction.java
src/test/java/net/pterodactylus/rhynodge/actions/EmailActionTest.java
src/test/kotlin/net/pterodactylus/rhynodge/Testing.kt
src/test/kotlin/net/pterodactylus/rhynodge/loader/ReactionLoaderTest.kt

index 157f555..c2c6e9b 100644 (file)
@@ -33,6 +33,10 @@ public interface Action {
         * @param output
         *            The output for the action
         */
-       void execute(Output output);
+       default void execute(Output output) {
+               execute(output, new String[0]);
+       }
+
+       void execute(Output output, String... arguments);
 
 }
index 89798d0..6baa0a2 100644 (file)
@@ -96,12 +96,15 @@ public class EmailAction implements Action {
         * {@inheritDoc}
         */
        @Override
-       public void execute(Output output) {
+       public void execute(Output output, String... additionalRecipients) {
                MimeMessage message = new MimeMessage(session);
                try {
                        /* create message. */
                        message.setFrom(new InternetAddress(sender));
                        message.setRecipient(RecipientType.TO, new InternetAddress(recipient));
+                       for (String additionalEmail : additionalRecipients) {
+                               message.addRecipient(RecipientType.TO, new InternetAddress(additionalEmail));
+                       }
                        message.setSubject(output.summary(), "UTF-8");
 
                        /* create text and html parts. */
index c866923..97e504d 100644 (file)
@@ -1,15 +1,18 @@
 package net.pterodactylus.rhynodge.actions;
 
+import static org.hamcrest.Matchers.hasItemInArray;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
+import static org.mockito.hamcrest.MockitoHamcrest.argThat;
 
 import javax.mail.Address;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
 
 import net.pterodactylus.rhynodge.output.DefaultOutput;
 import net.pterodactylus.rhynodge.output.Output;
@@ -42,6 +45,12 @@ public class EmailActionTest {
        }
 
        @Test
+       public void emailContainsAllRecipients() throws MessagingException {
+               emailAction.execute(output, "test1@test.com");
+               verify(transport).sendMessage(any(Message.class), argThat(hasItemInArray(new InternetAddress("test1@test.com"))));
+       }
+
+       @Test
        public void exceptionWhenSendingIsSwallowed() throws MessagingException {
                doThrow(MessagingException.class).doNothing().when(transport).sendMessage(any(Message.class), any(Address[].class));
                emailAction.execute(output);
index 87a618a..2a58324 100644 (file)
@@ -11,5 +11,5 @@ class TestQuery(private val state: State) : Query {
 }
 
 class TestAction : Action {
-       override fun execute(output: Output?) = Unit
+       override fun execute(output: Output, vararg arguments: String) = Unit
 }
index e177d09..767bf33 100644 (file)
@@ -144,7 +144,7 @@ class TestMerger1(val one: String) : TestMerger0()
 class TestMerger3(val one: String, val two: String, val three: String) : TestMerger0()
 
 open class TestAction0 : Action {
-       override fun execute(output: Output) = Unit
+       override fun execute(output: Output, vararg arguments: String) = Unit
 }
 
 class TestAction2(val foo: String, val bar: String) : TestAction0()