--- /dev/null
+/* 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.api.service.impl;\r
+\r
+import org.oran.otf.api.Utilities;\r
+import org.oran.otf.api.service.TestExecutionService;\r
+import org.oran.otf.common.model.Group;\r
+import org.oran.otf.common.model.TestExecution;\r
+import org.oran.otf.common.model.User;\r
+import org.oran.otf.common.repository.GroupRepository;\r
+import org.oran.otf.common.repository.TestExecutionRepository;\r
+import org.oran.otf.common.repository.UserRepository;\r
+import org.oran.otf.common.utility.permissions.PermissionChecker;\r
+import org.oran.otf.common.utility.permissions.UserPermission;\r
+import com.google.gson.JsonElement;\r
+import com.google.gson.JsonObject;\r
+import com.google.gson.JsonParser;\r
+import java.util.Optional;\r
+import java.util.UUID;\r
+import javax.ws.rs.core.Response;\r
+\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 TestExecutionServiceImpl implements TestExecutionService {\r
+ private static final Logger logger = LoggerFactory.getLogger(TestExecutionServiceImpl.class);\r
+ @Autowired\r
+ private UserRepository userRepository;\r
+ @Autowired private TestExecutionRepository testExecutionRepository;\r
+ @Autowired private GroupRepository groupRepository;\r
+\r
+ @Override\r
+ public Response getExecutionStatus(String authorization, String executionId) {\r
+ if (authorization == null) {\r
+ return Utilities.Http.BuildResponse.unauthorizedWithMessage("Missing authorization header.");\r
+ }\r
+ // check if the executionId is a valid UUID\r
+ try {\r
+ UUID.fromString(executionId);\r
+ } catch (IllegalArgumentException e) {\r
+ return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+ "Invalid execution identifier. Expected type is UUID (v4).");\r
+ }\r
+\r
+ // try to find the test execution\r
+ Optional<TestExecution> optionalTestExecution =\r
+ testExecutionRepository.findFirstByProcessInstanceId(executionId);\r
+ TestExecution testExecution = Utilities.resolveOptional(optionalTestExecution);\r
+ if (testExecution == null) {\r
+ return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+ String.format("An execution with identifier %s was not found.", executionId));\r
+ }\r
+\r
+ // try to find the group of the test execution\r
+ String testExecutionGroupId = testExecution.getGroupId().toString();\r
+ Optional<Group> optionalGroup = groupRepository.findById(testExecutionGroupId);\r
+ Group group = Utilities.resolveOptional(optionalGroup);\r
+ if (group == null) {\r
+ return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+ String.format(\r
+ "The group (id: %s) associated with the test execution does not exist.",\r
+ testExecutionGroupId));\r
+ }\r
+\r
+ // try to find the user for the mechanizedId used to make this request\r
+ User user = Utilities.findUserByAuthHeader(authorization, userRepository);\r
+ if (user == null) {\r
+ return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+ "No user associated with mechanized identifier used for this request.");\r
+ }\r
+ // if it doesnt have read permission then return bad request\r
+ if (!PermissionChecker.hasPermissionTo(user,group, UserPermission.Permission.READ,groupRepository)){\r
+ return Utilities.Http.BuildResponse.unauthorizedWithMessage(\r
+ "Unauthorized to view this test execution.");\r
+ }\r
+ // Used the build the final response to be returned\r
+ JsonObject res = new JsonObject();\r
+ try {\r
+ // Parsing is required to prevent Gson from escaping all the characters of the json\r
+ JsonElement testExecutionParsed = new JsonParser().parse(testExecution.toString());\r
+ res.add("testExecution", testExecutionParsed);\r
+ // Get the state of the process instance using the Camunda REST API\r
+ JsonObject procInstStatus = Utilities.Camunda.processInstanceStatus(executionId);\r
+ // Extract the state of the process instance from the JSON response\r
+ String processInstanceState =\r
+ procInstStatus.getAsJsonObject("historicProcessInstance").get("state").getAsString();\r
+ // Add the result to the final response\r
+ res.addProperty("state", processInstanceState);\r
+ } catch (NullPointerException npe) {\r
+ // In the case of a null pointer exception, make it clear that the state was not able\r
+ // to be determined using the Camunda API.\r
+ logger.error("Unable to determine the live status of the test execution.");\r
+ res.addProperty("state", "Unable to determine");\r
+ }\r
+ // Send the response\r
+ return Response.ok(res.toString()).build();\r
+ }\r
+\r
+ @Override\r
+ public Response getExecution(String authorization, String processInstanceId) {\r
+ try {\r
+ UUID.fromString(processInstanceId);\r
+ } catch (IllegalArgumentException e) {\r
+ return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+ "Invalid execution identifier. Expected type is UUID (v4).");\r
+ }\r
+\r
+ // try to find the test execution\r
+ Optional<TestExecution> optionalTestExecution =\r
+ testExecutionRepository.findFirstByProcessInstanceId(processInstanceId);\r
+ TestExecution testExecution = Utilities.resolveOptional(optionalTestExecution);\r
+ if (testExecution == null) {\r
+ return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+ String.format("An execution with identifier %s was not found.", processInstanceId));\r
+ }\r
+\r
+ // try to find the group of the test execution\r
+ String testExecutionGroupId = testExecution.getGroupId().toString();\r
+ Optional<Group> optionalGroup = groupRepository.findById(testExecutionGroupId);\r
+ Group group = Utilities.resolveOptional(optionalGroup);\r
+ if (group == null) {\r
+ return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+ String.format(\r
+ "The group (id: %s) associated with the test execution does not exist.",\r
+ testExecutionGroupId));\r
+ }\r
+\r
+ // try to find the user for the mechanizedId used to make this request\r
+ User user = Utilities.findUserByAuthHeader(authorization, userRepository);\r
+ if (user == null) {\r
+ return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+ "No user associated with mechanized identifier used" + " for this request.");\r
+ }\r
+\r
+ // if it doesnt have read permission then return bad request\r
+ if (!PermissionChecker.hasPermissionTo(user,group,UserPermission.Permission.READ,groupRepository)){\r
+ return Utilities.Http.BuildResponse.unauthorizedWithMessage(\r
+ "Unauthorized to view this test execution.");\r
+ }\r
+\r
+ return Response.ok(testExecution.toString()).build();\r
+ }\r
+}\r