1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 package org.oran.otf.camunda.delegate.otf.common;
\r
19 import org.oran.otf.camunda.exception.TestExecutionException;
\r
20 import org.oran.otf.camunda.model.ExecutionConstants;
\r
21 import org.oran.otf.camunda.workflow.utility.WorkflowUtility;
\r
22 import org.oran.otf.common.model.TestExecution;
\r
23 import org.oran.otf.common.repository.TestExecutionRepository;
\r
24 import org.oran.otf.common.utility.Utility;
\r
25 import com.mongodb.client.result.UpdateResult;
\r
27 import java.util.Arrays;
\r
28 import java.util.Date;
\r
29 import java.util.Map;
\r
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
\r
31 import org.camunda.bpm.engine.delegate.JavaDelegate;
\r
32 import org.slf4j.Logger;
\r
33 import org.slf4j.LoggerFactory;
\r
34 import org.springframework.beans.factory.annotation.Autowired;
\r
35 import org.springframework.data.mongodb.core.MongoTemplate;
\r
36 import org.springframework.data.mongodb.core.query.Criteria;
\r
37 import org.springframework.data.mongodb.core.query.Query;
\r
38 import org.springframework.data.mongodb.core.query.Update;
\r
39 import org.springframework.stereotype.Component;
\r
42 public class LogTestResultDelegate implements JavaDelegate {
\r
44 private static Logger logger = LoggerFactory.getLogger(LogTestResultDelegate.class);
\r
47 private TestExecutionRepository testExecutionRepository;
\r
49 private MongoTemplate mongoOperation;
\r
51 private WorkflowUtility utility;
\r
54 public void execute(DelegateExecution execution) throws Exception {
\r
55 logger.info("[LogTestResult] Starting to log test result.");
\r
56 final String logPrefix = Utility.getLoggerPrefix();
\r
57 // Get the current test execution object.
\r
58 TestExecution testExecution = utility.getTestExecution(execution.getVariables(), logPrefix);
\r
60 // Set the end time right after retrieving the execution. This will not include the save time
\r
62 testExecution.setEndTime(new Date(System.currentTimeMillis()));
\r
64 // Set the processInstanceId because the user may have modified it through a script task.
\r
65 testExecution.setProcessInstanceId(execution.getProcessInstanceId());
\r
67 // Get the test result from the execution.
\r
68 String testResult = utility.getTestResult(execution.getVariables(), logPrefix).toUpperCase();
\r
69 if(testResult.equalsIgnoreCase(ExecutionConstants.TestResult.WORKFLOW_ERROR)){
\r
70 testResult = ExecutionConstants.TestResult.ERROR;
\r
72 if(Arrays.asList(ExecutionConstants.getAllTestResultStr()).contains(testResult.toUpperCase()))
\r
73 testExecution.setTestResult(testResult.toUpperCase());
\r
75 testExecution.setTestResult(ExecutionConstants.TestResult.OTHER);
\r
78 //Get the test result message from the execution
\r
79 String testResultMessage = utility.getTestResultMessage(execution.getVariables(), logPrefix);
\r
80 testExecution.setTestResultMessage(testResultMessage);
\r
82 // Get test details as a String because it can be saved as one of many "JSON" types. Then try
\r
83 // to convert it to a generic map.
\r
84 Map<String, Object> testDetails = utility.getTestDetails(execution.getVariables(), logPrefix);
\r
85 // Save the converted object to the test execution.
\r
86 testExecution.setTestDetails(testDetails);
\r
89 // Update the Test Execution object to save the result. Find the existing test execution by the
\r
90 // processBusinessKey from the delegate execution because it is saved to the database before the
\r
91 // user can modify the value.
\r
92 Query query = new Query();
\r
93 query.addCriteria(Criteria.where("businessKey").is(execution.getProcessBusinessKey()));
\r
94 Update update = new Update();
\r
95 update.set("testResult", testExecution.getTestResult());
\r
96 update.set("testResultMessage", testExecution.getTestResultMessage());
\r
97 update.set("testDetails", testExecution.getTestDetails());
\r
98 update.set("endTime", testExecution.getEndTime());
\r
99 update.set("processInstanceId", execution.getProcessInstanceId());
\r
100 UpdateResult result = mongoOperation.updateFirst(query, update, TestExecution.class);
\r
101 // Check the status of the findAndUpdate database, and appropriately handle the errors.
\r
102 if (result.getMatchedCount() == 0) {
\r
103 throw new TestExecutionException(
\r
105 "Unable to log the test result because a testExecution associated with businessKey, %s, was not found.",
\r
106 execution.getProcessBusinessKey()));
\r
107 } else if (result.getModifiedCount() == 0) {
\r
108 throw new TestExecutionException("Unable to persist the testExecution to the database.");
\r
111 logPrefix + execution.getProcessInstanceId() + ": Saved test result to the database.");
\r