EmailWorkflowOperationHandler extends AbstractWorkflowOperationHandler { @Override public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException { WorkflowOperationInstance operation = workflowInstance.getCurrentOperation(); MediaPackage srcPackage = workflowInstance.getMediaPackage(); String to = processDestination(workflowInstance, operation, TO_PROPERTY); String cc = processDestination(workflowInstance, operation, CC_PROPERTY); String bcc = processDestination(workflowInstance, operation, BCC_PROPERTY); String subject = applyTemplateIfNecessary(workflowInstance, operation, SUBJECT_PROPERTY); String bodyText = null; String body = operation.getConfiguration(BODY_PROPERTY); Boolean isHTML = BooleanUtils.toBoolean(operation.getConfiguration(IS_HTML)); String bodyTemplateFile = operation.getConfiguration(BODY_TEMPLATE_FILE_PROPERTY); if (body == null && bodyTemplateFile == null) { bodyText = srcPackage.getTitle() + "(" + srcPackage.getIdentifier().toString() + ")"; } else if (body != null) { bodyText = applyTemplateIfNecessary(workflowInstance, operation, BODY_PROPERTY); } else { bodyText = applyTemplateIfNecessary(workflowInstance, operation, BODY_TEMPLATE_FILE_PROPERTY); } try { logger.debug( "Sending e-mail notification with subject {} and body {} to {}, CC addresses {} and BCC addresses {}", subject, bodyText, to, cc, bcc); smtpService.send(to, cc, bcc, subject, bodyText, isHTML); logger.info("E-mail notification sent to {}, CC addresses {} and BCC addresses {}", to, cc, bcc); } catch (MessagingException e) { throw new WorkflowOperationException(e); } return createResult(srcPackage, Action.CONTINUE); }  @Override WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context);  }
@Test public void testDefaultBody() throws Exception { operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, DEFAULT_TO); operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, DEFAULT_CC); operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, DEFAULT_BCC); operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); Assert.assertEquals(DEFAULT_TO, capturedTo.getValue()); Assert.assertEquals(DEFAULT_CC, capturedCC.getValue()); Assert.assertEquals(DEFAULT_BCC, capturedBCC.getValue()); Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue()); Assert.assertEquals("Test Media Package(3e7bb56d-2fcc-4efe-9f0e-d6e56422f557)", capturedBody.getValue()); }
@Test public void testTemplateInBody() throws Exception { operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, DEFAULT_TO); operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, DEFAULT_CC); operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, DEFAULT_BCC); operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT); operation.setConfiguration(EmailWorkflowOperationHandler.BODY_PROPERTY, "This is the media package: ${mediaPackage.identifier}"); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); Assert.assertEquals(DEFAULT_TO, capturedTo.getValue()); Assert.assertEquals(DEFAULT_CC, capturedCC.getValue()); Assert.assertEquals(DEFAULT_BCC, capturedBCC.getValue()); Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue()); Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue()); }
@Test public void testTemplateInFile() throws Exception { operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, DEFAULT_TO); operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, DEFAULT_CC); operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, DEFAULT_BCC); operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT); operation.setConfiguration(EmailWorkflowOperationHandler.BODY_TEMPLATE_FILE_PROPERTY, "template1"); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); Assert.assertEquals(DEFAULT_TO, capturedTo.getValue()); Assert.assertEquals(DEFAULT_CC, capturedCC.getValue()); Assert.assertEquals(DEFAULT_BCC, capturedBCC.getValue()); Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue()); Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue()); }
@Test public void testTemplateNotFound() throws Exception { operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, DEFAULT_TO); operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, DEFAULT_CC); operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, DEFAULT_BCC); operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT); operation.setConfiguration(EmailWorkflowOperationHandler.BODY_TEMPLATE_FILE_PROPERTY, "templateNotFound"); operationHandler.start(workflowInstance, null); Assert.assertEquals(DEFAULT_TO, capturedTo.getValue()); Assert.assertEquals(DEFAULT_CC, capturedCC.getValue()); Assert.assertEquals(DEFAULT_BCC, capturedBCC.getValue()); Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue()); Assert.assertEquals("TEMPLATE NOT FOUND!", capturedBody.getValue()); }
@Test public void testDestinationAsUserName() throws Exception { operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, USER_NAME); operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, USER_NAME); operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, USER_NAME); operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT); operation.setConfiguration(EmailWorkflowOperationHandler.BODY_PROPERTY, "This is the media package: ${mediaPackage.identifier}"); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); Assert.assertEquals(DEFAULT_TO, capturedTo.getValue()); Assert.assertEquals(DEFAULT_TO, capturedCC.getValue()); Assert.assertEquals(DEFAULT_TO, capturedBCC.getValue()); Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue()); Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue()); }
@Test public void testManyDestinationEmails() throws Exception { operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, USER_NAME + "," + DEFAULT_BCC + " " + DEFAULT_CC); operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, null); operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, null); operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT); operation.setConfiguration(EmailWorkflowOperationHandler.BODY_PROPERTY, "This is the media package: ${mediaPackage.identifier}"); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); Assert.assertEquals(DEFAULT_TO + "," + DEFAULT_BCC + "," + DEFAULT_CC, capturedTo.getValue()); Assert.assertNull(capturedCC.getValue()); Assert.assertNull(capturedBCC.getValue()); Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue()); Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue()); }
@Test public void testUserNameIsAnEmail() throws Exception { operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, USER_NAME_EMAIL); operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, USER_NAME_EMAIL); operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, USER_NAME_EMAIL); operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT); operation.setConfiguration(EmailWorkflowOperationHandler.BODY_PROPERTY, "This is the media package: ${mediaPackage.identifier}"); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); Assert.assertEquals(DEFAULT_TO, capturedTo.getValue()); Assert.assertEquals(DEFAULT_TO, capturedCC.getValue()); Assert.assertEquals(DEFAULT_TO, capturedBCC.getValue()); Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue()); Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue()); }
@Test(expected = WorkflowOperationException.class) public void testUserNoEmail() throws Exception { operation.setConfiguration(EmailWorkflowOperationHandler.TO_PROPERTY, USER_NAME_NO_EMAIL); operation.setConfiguration(EmailWorkflowOperationHandler.CC_PROPERTY, USER_NAME_NO_EMAIL); operation.setConfiguration(EmailWorkflowOperationHandler.BCC_PROPERTY, USER_NAME_NO_EMAIL); operation.setConfiguration(EmailWorkflowOperationHandler.SUBJECT_PROPERTY, DEFAULT_SUBJECT); operation.setConfiguration(EmailWorkflowOperationHandler.BODY_PROPERTY, "This is the media package: ${mediaPackage.identifier}"); WorkflowOperationResult result = operationHandler.start(workflowInstance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); Assert.assertEquals(DEFAULT_TO, capturedTo.getValue()); Assert.assertEquals(DEFAULT_TO, capturedCC.getValue()); Assert.assertEquals(DEFAULT_TO, capturedBCC.getValue()); Assert.assertEquals(DEFAULT_SUBJECT, capturedSubject.getValue()); Assert.assertEquals("This is the media package: 3e7bb56d-2fcc-4efe-9f0e-d6e56422f557", capturedBody.getValue()); }