added svcapi ui and camunda code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / camunda / delegate / otf / common / LogTestResultDelegate.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 package org.oran.otf.camunda.delegate.otf.common;\r
18 \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
26 \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
40 \r
41 @Component\r
42 public class LogTestResultDelegate implements JavaDelegate {\r
43 \r
44   private static Logger logger = LoggerFactory.getLogger(LogTestResultDelegate.class);\r
45 \r
46   @Autowired\r
47   private TestExecutionRepository testExecutionRepository;\r
48   @Autowired\r
49   private MongoTemplate mongoOperation;\r
50   @Autowired\r
51   private WorkflowUtility utility;\r
52 \r
53   @Override\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
59 \r
60     // Set the end time right after retrieving the execution. This will not include the save time\r
61     // to the database.\r
62     testExecution.setEndTime(new Date(System.currentTimeMillis()));\r
63 \r
64     // Set the processInstanceId because the user may have modified it through a script task.\r
65     testExecution.setProcessInstanceId(execution.getProcessInstanceId());\r
66 \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
71     }\r
72     if(Arrays.asList(ExecutionConstants.getAllTestResultStr()).contains(testResult.toUpperCase()))\r
73       testExecution.setTestResult(testResult.toUpperCase());\r
74     else{\r
75       testExecution.setTestResult(ExecutionConstants.TestResult.OTHER);\r
76     }\r
77 \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
81 \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
87 \r
88 \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
104           String.format(\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
109     } else {\r
110       logger.info(\r
111           logPrefix + execution.getProcessInstanceId() + ": Saved test result to the database.");\r
112     }\r
113   }\r
114 }\r