|
@@ -0,0 +1,171 @@
|
|
|
+package com.mooctest.crowd.site.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.druid.util.StringUtils;
|
|
|
+import com.mooctest.crowd.domain.dao.CrowdTestProjectDao;
|
|
|
+import com.mooctest.crowd.domain.dao.TaskToUserDao;
|
|
|
+import com.mooctest.crowd.domain.domainobject.*;
|
|
|
+import com.mooctest.crowd.domain.env.TestCaseExamStatus;
|
|
|
+import com.mooctest.crowd.domain.env.TestStatus;
|
|
|
+import com.mooctest.crowd.domain.exception.BaseException;
|
|
|
+import com.mooctest.crowd.domain.exception.CrowdTestProjectNotExistException;
|
|
|
+import com.mooctest.crowd.domain.exception.CrowdTestTaskNotExistException;
|
|
|
+import com.mooctest.crowd.domain.model.CrowdTestProjectPO;
|
|
|
+import com.mooctest.crowd.domain.model.TaskToUserPO;
|
|
|
+import com.mooctest.crowd.domain.page.DOPage;
|
|
|
+import com.mooctest.crowd.domain.repository.*;
|
|
|
+import com.mooctest.crowd.domain.util.Converter;
|
|
|
+import com.mooctest.crowd.site.command.TestCaseAddedCommand;
|
|
|
+import com.mooctest.crowd.site.command.TestCaseUpdatedCommand;
|
|
|
+import com.mooctest.crowd.site.data.dto.TestCaseExamCommand;
|
|
|
+import com.mooctest.crowd.site.data.dto.TestCaseSearchDTO;
|
|
|
+import com.mooctest.crowd.site.service.TestCaseService;
|
|
|
+import com.mooctest.crowd.site.util.GenerateFlowCodeUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class TestCaseServiceImpl implements TestCaseService {
|
|
|
+ @Autowired
|
|
|
+ private ITestCaseRepo testCaseRepo;
|
|
|
+ @Autowired
|
|
|
+ private ICrowdTaskRepo crowdTaskRepo;
|
|
|
+ @Autowired
|
|
|
+ private ITestCaseCodeRepo testCaseCodeRepo;
|
|
|
+ @Autowired
|
|
|
+ private DefectRepo defectRepo;
|
|
|
+ @Autowired
|
|
|
+ private CrowdTestProjectDao crowdTestProjectDao;
|
|
|
+ @Autowired
|
|
|
+ private TaskToUserDao taskToUserDao;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public TestCase add(TestCaseAddedCommand testCaseAddedCommand) {
|
|
|
+ Long designerId = testCaseAddedCommand.getDesignerId();
|
|
|
+ String taskCode = testCaseAddedCommand.getTaskCode();
|
|
|
+ CrowdTestTask task = crowdTaskRepo.findByCode(taskCode);
|
|
|
+ if (task == null) {
|
|
|
+ throw new CrowdTestTaskNotExistException();
|
|
|
+ }
|
|
|
+ List<TaskToUser> taskToUsers = crowdTaskRepo.findAllTaskToUserByTaskCode(taskCode);
|
|
|
+ if(!taskToUsers.stream().anyMatch(taskToUser -> taskToUser.getUserId().equals(designerId))) {
|
|
|
+ throw new BaseException("当前用户没有此任务的操作权限!");
|
|
|
+ }
|
|
|
+ String projectCode = task.getCrowdTestProjectCode();
|
|
|
+ List<CrowdTestTask> tasks = crowdTaskRepo.findAllByCrowdTestProjectCode(projectCode);
|
|
|
+ int index = -1;
|
|
|
+ for(int i = 0; i < tasks.size(); i++) {
|
|
|
+ if (tasks.get(i).getCode().equals(taskCode)) {
|
|
|
+ index = i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (index < 0) {
|
|
|
+ throw new RuntimeException("找不到匹配的任务");
|
|
|
+ }
|
|
|
+ int ind = testCaseCodeRepo.getNewestIndByTaskCode(taskCode);
|
|
|
+ String code1 = GenerateFlowCodeUtil.getSequence(index + 1, 2);
|
|
|
+ String code2 = GenerateFlowCodeUtil.getSequence(ind, 4);
|
|
|
+ String testCaseCode = "TC-".concat(code1).concat(code2);
|
|
|
+ TestCase testCase = Converter.convert(TestCase.class, testCaseAddedCommand);
|
|
|
+ testCase.setCode(testCaseCode);
|
|
|
+ testCase.setProjectCode(task.getCrowdTestProjectCode());
|
|
|
+ testCase.setDesignTime(new Timestamp(System.currentTimeMillis()));
|
|
|
+ testCase.setExamStatus(TestCaseExamStatus.WAIT);
|
|
|
+ testCaseRepo.save(testCase);
|
|
|
+ return testCase;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void delete(Long designerId, Long id) {
|
|
|
+ TestCase testCase = testCaseRepo.findById(id);
|
|
|
+ if (!testCase.getDesignerId().equals(designerId)) {
|
|
|
+ throw new BaseException("当前用户无权限操作该用例");
|
|
|
+ }
|
|
|
+ testCaseRepo.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TestCase update(TestCaseUpdatedCommand testCaseUpdatedCommand) {
|
|
|
+ TestCase testCase = testCaseRepo.findById(testCaseUpdatedCommand.getId());
|
|
|
+ if (testCase == null) {
|
|
|
+ throw new RuntimeException("用例不存在");
|
|
|
+ }
|
|
|
+ if (!testCase.getDesignerId().equals(testCaseUpdatedCommand.getDesignerId())) {
|
|
|
+ throw new BaseException("当前用户无权限操作该用例");
|
|
|
+ }
|
|
|
+ Converter.copy(testCase, testCaseUpdatedCommand);
|
|
|
+ testCaseRepo.save(testCase);
|
|
|
+ return testCase;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DOPage<TestCase> find(Long designerId, Long readUserId, String taskCode, Integer pageNo, Integer pageSize, TestCaseSearchDTO testCaseSearchDTO) {
|
|
|
+ if (!readUserId.equals(designerId)) {
|
|
|
+ CrowdTestTask crowdTestTask = crowdTaskRepo.findByCode(taskCode);
|
|
|
+ if (crowdTestTask == null) {
|
|
|
+ throw new CrowdTestTaskNotExistException();
|
|
|
+ }
|
|
|
+ CrowdTestProjectPO crowdTestProjectPO = crowdTestProjectDao.findByCodeAndIsDeleted(crowdTestTask.getCrowdTestProjectCode(), DeletedStatus.isNotDeleted);
|
|
|
+ if (crowdTestProjectPO == null) {
|
|
|
+ throw new CrowdTestProjectNotExistException();
|
|
|
+ }
|
|
|
+ if (!crowdTestProjectPO.getRegionalManagerId().equals(readUserId)) {
|
|
|
+ throw new BaseException("当前用户无权获取该数据");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DOPage<TestCase> page = testCaseRepo.find(designerId, taskCode, pageNo, pageSize, testCaseSearchDTO.getTestStatus(), testCaseSearchDTO.getExamStatus());
|
|
|
+ Map<String, TestCase> testCaseCodeMap = new HashMap();
|
|
|
+ page.getDatas().stream().forEach(testCase -> {
|
|
|
+ testCase.setDefects(new ArrayList());
|
|
|
+ testCaseCodeMap.put(testCase.getCode(), testCase);
|
|
|
+ });
|
|
|
+ List<Defect> defects = defectRepo.findAllByTaskCodeAndTestCaseCodeIn(taskCode, testCaseCodeMap.keySet());
|
|
|
+ defects.stream().forEach(defect -> {
|
|
|
+ testCaseCodeMap.get(defect.getTestCaseCode()).getDefects().add(defect);
|
|
|
+ });
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void exam(TestCaseExamCommand testCaseExamCommand) {
|
|
|
+ if (testCaseExamCommand.getExamStatus().equals(TestCaseExamStatus.INVALID.toString())) {
|
|
|
+ if (StringUtils.isEmpty(testCaseExamCommand.getExamDescr())) {
|
|
|
+ throw new BaseException("审核结果为无效时,需要填写审核结果说明!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ TestCase testCase = testCaseRepo.findById(testCaseExamCommand.getId());
|
|
|
+ if (testCase == null) {
|
|
|
+ throw new BaseException("测试用例不存在!");
|
|
|
+ }
|
|
|
+ CrowdTestTask task = crowdTaskRepo.findByCode(testCase.getTaskCode());
|
|
|
+ if (task == null) {
|
|
|
+ throw new CrowdTestTaskNotExistException();
|
|
|
+ }
|
|
|
+ CrowdTestProjectPO projectPO = crowdTestProjectDao.findByCodeAndIsDeleted(task.getCrowdTestProjectCode(), DeletedStatus.isNotDeleted);
|
|
|
+ if (projectPO == null) {
|
|
|
+ throw new CrowdTestProjectNotExistException();
|
|
|
+ }
|
|
|
+ if (!testCaseExamCommand.getExamerId().equals(projectPO.getRegionalManagerId())) {
|
|
|
+ throw new BaseException("当前用户无权审核该测试用例!");
|
|
|
+ }
|
|
|
+ TaskToUserPO taskToUserPO = taskToUserDao.findByUserIdAndTaskCode(testCase.getDesignerId(), testCase.getTaskCode());
|
|
|
+ if (taskToUserPO.getIsCommitted() != 1) {
|
|
|
+ throw new BaseException("当前测试用例还未提交,不能审核!");
|
|
|
+ }
|
|
|
+ if (task.getStatus() == CrowdTestTask.HAS_FINISHED) {
|
|
|
+ throw new BaseException("当前任务已经结束,不能审核该任务的测试用例!");
|
|
|
+ }
|
|
|
+ testCaseRepo.exam(testCaseExamCommand.getId(), TestCaseExamStatus.valueOf(testCaseExamCommand.getExamStatus()),
|
|
|
+ testCaseExamCommand.getExamDescr());
|
|
|
+ }
|
|
|
+}
|