added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / camunda / delegate / otf / common / LogTestResultDelegate.java
diff --git a/otf-camunda/src/main/java/org/oran/otf/camunda/delegate/otf/common/LogTestResultDelegate.java b/otf-camunda/src/main/java/org/oran/otf/camunda/delegate/otf/common/LogTestResultDelegate.java
new file mode 100644 (file)
index 0000000..0ecb37e
--- /dev/null
@@ -0,0 +1,114 @@
+/*  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.model.ExecutionConstants;\r
+import org.oran.otf.camunda.workflow.utility.WorkflowUtility;\r
+import org.oran.otf.common.model.TestExecution;\r
+import org.oran.otf.common.repository.TestExecutionRepository;\r
+import org.oran.otf.common.utility.Utility;\r
+import com.mongodb.client.result.UpdateResult;\r
+\r
+import java.util.Arrays;\r
+import java.util.Date;\r
+import java.util.Map;\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.data.mongodb.core.MongoTemplate;\r
+import org.springframework.data.mongodb.core.query.Criteria;\r
+import org.springframework.data.mongodb.core.query.Query;\r
+import org.springframework.data.mongodb.core.query.Update;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class LogTestResultDelegate implements JavaDelegate {\r
+\r
+  private static Logger logger = LoggerFactory.getLogger(LogTestResultDelegate.class);\r
+\r
+  @Autowired\r
+  private TestExecutionRepository testExecutionRepository;\r
+  @Autowired\r
+  private MongoTemplate mongoOperation;\r
+  @Autowired\r
+  private WorkflowUtility utility;\r
+\r
+  @Override\r
+  public void execute(DelegateExecution execution) throws Exception {\r
+    logger.info("[LogTestResult] Starting to log test result.");\r
+    final String logPrefix = Utility.getLoggerPrefix();\r
+    // Get the current test execution object.\r
+    TestExecution testExecution = utility.getTestExecution(execution.getVariables(), logPrefix);\r
+\r
+    // Set the end time right after retrieving the execution. This will not include the save time\r
+    // to the database.\r
+    testExecution.setEndTime(new Date(System.currentTimeMillis()));\r
+\r
+    // Set the processInstanceId because the user may have modified it through a script task.\r
+    testExecution.setProcessInstanceId(execution.getProcessInstanceId());\r
+\r
+    // Get the test result from the execution.\r
+    String testResult = utility.getTestResult(execution.getVariables(), logPrefix).toUpperCase();\r
+    if(testResult.equalsIgnoreCase(ExecutionConstants.TestResult.WORKFLOW_ERROR)){\r
+      testResult = ExecutionConstants.TestResult.ERROR;\r
+    }\r
+    if(Arrays.asList(ExecutionConstants.getAllTestResultStr()).contains(testResult.toUpperCase()))\r
+      testExecution.setTestResult(testResult.toUpperCase());\r
+    else{\r
+      testExecution.setTestResult(ExecutionConstants.TestResult.OTHER);\r
+    }\r
+\r
+    //Get the test result message from the execution\r
+    String testResultMessage = utility.getTestResultMessage(execution.getVariables(), logPrefix);\r
+    testExecution.setTestResultMessage(testResultMessage);\r
+\r
+    // Get test details as a String because it can be saved as one of many "JSON" types. Then try\r
+    // to convert it to a generic map.\r
+    Map<String, Object> testDetails = utility.getTestDetails(execution.getVariables(), logPrefix);\r
+    // Save the converted object to the test execution.\r
+    testExecution.setTestDetails(testDetails);\r
+\r
+\r
+    // Update the Test Execution object to save the result. Find the existing test execution by the\r
+    // processBusinessKey from the delegate execution because it is saved to the database before the\r
+    // user can modify the value.\r
+    Query query = new Query();\r
+    query.addCriteria(Criteria.where("businessKey").is(execution.getProcessBusinessKey()));\r
+    Update update = new Update();\r
+    update.set("testResult", testExecution.getTestResult());\r
+    update.set("testResultMessage", testExecution.getTestResultMessage());\r
+    update.set("testDetails", testExecution.getTestDetails());\r
+    update.set("endTime", testExecution.getEndTime());\r
+    update.set("processInstanceId", execution.getProcessInstanceId());\r
+    UpdateResult result = mongoOperation.updateFirst(query, update, TestExecution.class);\r
+    // Check the status of the findAndUpdate database, and appropriately handle the errors.\r
+    if (result.getMatchedCount() == 0) {\r
+      throw new TestExecutionException(\r
+          String.format(\r
+              "Unable to log the test result because a testExecution associated with businessKey, %s, was not found.",\r
+              execution.getProcessBusinessKey()));\r
+    } else if (result.getModifiedCount() == 0) {\r
+      throw new TestExecutionException("Unable to persist the testExecution to the database.");\r
+    } else {\r
+      logger.info(\r
+          logPrefix + execution.getProcessInstanceId() + ": Saved test result to the database.");\r
+    }\r
+  }\r
+}\r