added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / camunda / delegate / otf / common / SendMailDelegate.java
diff --git a/otf-camunda/src/main/java/org/oran/otf/camunda/delegate/otf/common/SendMailDelegate.java b/otf-camunda/src/main/java/org/oran/otf/camunda/delegate/otf/common/SendMailDelegate.java
new file mode 100644 (file)
index 0000000..9755214
--- /dev/null
@@ -0,0 +1,170 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
+#                                                                              #\r
+#   Licensed under the Apache License, Version 2.0 (the "License");            #\r
+#   you may not use this file except in compliance with the License.           #\r
+#   You may obtain a copy of the License at                                    #\r
+#                                                                              #\r
+#       http://www.apache.org/licenses/LICENSE-2.0                             #\r
+#                                                                              #\r
+#   Unless required by applicable law or agreed to in writing, software        #\r
+#   distributed under the License is distributed on an "AS IS" BASIS,          #\r
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #\r
+#   See the License for the specific language governing permissions and        #\r
+#   limitations under the License.                                             #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.camunda.delegate.otf.common;\r
+\r
+import org.oran.otf.camunda.exception.TestExecutionException;\r
+import org.oran.otf.camunda.workflow.utility.WorkflowUtility;\r
+import org.oran.otf.common.model.TestExecution;\r
+import org.oran.otf.common.utility.Utility;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Properties;\r
+import javax.mail.Message;\r
+import javax.mail.MessagingException;\r
+import javax.mail.Session;\r
+import javax.mail.Transport;\r
+import javax.mail.internet.InternetAddress;\r
+import javax.mail.internet.MimeMessage;\r
+import org.assertj.core.util.Strings;\r
+import org.camunda.bpm.engine.delegate.DelegateExecution;\r
+import org.camunda.bpm.engine.delegate.JavaDelegate;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.stereotype.Service;\r
+\r
+@Service\r
+public class SendMailDelegate implements JavaDelegate {\r
+\r
+  @Autowired private WorkflowUtility utility;\r
+\r
+  private static final String logPrefix = Utility.getLoggerPrefix();\r
+  private static final Logger log = LoggerFactory.getLogger(SendMailDelegate.class);\r
+\r
+  @Override\r
+  public void execute(DelegateExecution execution) throws Exception {\r
+    Map<String, Object> variables = execution.getVariables();\r
+    Map<String, Object> testData = utility.getTestData(variables, logPrefix);\r
+\r
+    Map<String, Object> sendMailData = null;\r
+    try {\r
+      sendMailData =\r
+          (Map<String, Object>) testData.getOrDefault(execution.getCurrentActivityId(), null);\r
+    } catch (Exception e) {\r
+      log.error(e.getMessage());\r
+      throw new TestExecutionException(e);\r
+    }\r
+\r
+    if (sendMailData == null) {\r
+      String err =\r
+          String.format(\r
+              "%sMissing parameters for activityId, %s.",\r
+              logPrefix, execution.getCurrentActivityId());\r
+      log.error(err);\r
+      throw new TestExecutionException(err);\r
+    }\r
+\r
+    // Get the recipient(s)\r
+    Object oRecipients = sendMailData.get("to");\r
+    if (oRecipients == null) {\r
+      String err = String.format("%sRecipients array cannot be null or empty.", logPrefix);\r
+      log.error(err);\r
+      throw new TestExecutionException(err);\r
+    }\r
+    List<String> recipients = null;\r
+    try {\r
+      recipients = (ArrayList<String>) (oRecipients);\r
+      if (recipients.size() == 0) {\r
+        String err = String.format("%sRecipients array cannot be null or empty.", logPrefix);\r
+        log.error(err);\r
+        throw new TestExecutionException(err);\r
+      }\r
+    } catch (Exception e) {\r
+      throw new TestExecutionException(e);\r
+    }\r
+\r
+    for (String recipient : recipients) {\r
+      if (Strings.isNullOrEmpty(recipient)) {\r
+        String err = String.format("%sRecipient cannot be null or empty.", logPrefix);\r
+        log.error(err);\r
+        throw new TestExecutionException(err);\r
+      }\r
+    }\r
+\r
+    // Get the email subject.\r
+    String subject = (String) sendMailData.get("subject");\r
+    if (Strings.isNullOrEmpty(subject.trim())) {\r
+      String err = String.format("%sSubject cannot be null or empty.", logPrefix);\r
+      log.error(err);\r
+      throw new TestExecutionException(err);\r
+    }\r
+\r
+    // Get the body contents.\r
+    String body = (String) sendMailData.get("body");\r
+    if (Strings.isNullOrEmpty(body.trim())) {\r
+      String err = String.format("%sBody cannot be null or empty.", logPrefix);\r
+      log.error(err);\r
+      throw new TestExecutionException(err);\r
+    }\r
+\r
+    TestExecution testExecution = utility.getTestExecution(variables, logPrefix);\r
+    String sender = testExecution.getHistoricEmail();\r
+    String hTestInstanceId = testExecution.getHistoricTestInstance().get_id().toString();\r
+    String processInstanceId = execution.getProcessInstanceId();\r
+\r
+    sendMail(recipients, subject, body, sender, processInstanceId, hTestInstanceId);\r
+  }\r
+\r
+  public void sendMail(\r
+      List<String> recipients,\r
+      String subject,\r
+      String body,\r
+      String sender,\r
+      String processInstanceId,\r
+      String testInstanceId)\r
+      throws Exception {\r
+    // Get the system properties.\r
+    Properties properties = System.getProperties();\r
+\r
+    // Set the SMTP host.\r
+    properties.setProperty("mail.smtp.host", "localhost");\r
+\r
+    // creating session object to get properties\r
+    Session session = Session.getDefaultInstance(properties);\r
+\r
+    try {\r
+      // MimeMessage object.\r
+      MimeMessage message = new MimeMessage(session);\r
+\r
+      // Set From Field: adding senders email to from field.\r
+      message.setFrom(new InternetAddress("OTF_EMAIL-ALERT@localhost"));\r
+\r
+      // Set Subject: subject of the email\r
+      message.setSubject(subject);\r
+\r
+      // set body of the email.\r
+      StringBuffer sb = new StringBuffer();\r
+      sb.append("************************OTF Alerting System************************");\r
+      sb.append("\n\n");\r
+      sb.append(String.format("This message was sent by %s via the Open Test Framework\n", sender));\r
+      sb.append(String.format("processInstanceId: %s\n", processInstanceId));\r
+      sb.append(String.format("testInstanceId: %s", testInstanceId));\r
+      sb.append("\n\n");\r
+      sb.append("******************************************************************");\r
+      sb.append("\n\n");\r
+      sb.append(body);\r
+\r
+      message.setText(sb.toString());\r
+\r
+      // Send email.\r
+      Transport.send(message);\r
+    } catch (MessagingException mex) {\r
+      mex.printStackTrace();\r
+    }\r
+  }\r
+}\r