|
3 | 3 | import main.controllers.BaseController;
|
4 | 4 | import main.exceptions.AqualityException;
|
5 | 5 | import main.exceptions.AqualityPermissionsException;
|
6 |
| -import main.model.db.dao.project.SuiteStatisticDao; |
7 |
| -import main.model.db.dao.project.TestSuiteDao; |
8 |
| -import main.model.dto.SuiteStatisticDto; |
9 |
| -import main.model.dto.TestDto; |
10 |
| -import main.model.dto.TestSuiteDto; |
11 |
| -import main.model.dto.UserDto; |
| 6 | +import main.model.db.dao.project.*; |
| 7 | +import main.model.dto.*; |
| 8 | +import main.view.Project.TestResult; |
12 | 9 |
|
13 | 10 | import java.util.List;
|
| 11 | +import java.util.stream.Collectors; |
14 | 12 |
|
15 | 13 | public class SuiteController extends BaseController<TestSuiteDto> {
|
16 | 14 | private TestSuiteDao testSuiteDao;
|
| 15 | + private TestRunDao testRunDao; |
| 16 | + private Test2SuiteDao test2SuiteDao; |
| 17 | + private TestResultDao testResultDao; |
17 | 18 | private SuiteStatisticDao suiteStatisticDao;
|
18 | 19 | private TestController testController;
|
19 | 20 |
|
20 | 21 | public SuiteController(UserDto user) {
|
21 | 22 | super(user);
|
22 | 23 | testSuiteDao = new TestSuiteDao();
|
| 24 | + testRunDao = new TestRunDao(); |
| 25 | + testResultDao = new TestResultDao(); |
23 | 26 | suiteStatisticDao = new SuiteStatisticDao();
|
| 27 | + test2SuiteDao = new Test2SuiteDao(); |
24 | 28 | testController = new TestController(user);
|
25 | 29 | }
|
26 | 30 |
|
@@ -86,4 +90,61 @@ private List<TestSuiteDto> fillTestSuites(List<TestSuiteDto> testSuites, boolean
|
86 | 90 | }
|
87 | 91 | return testSuites;
|
88 | 92 | }
|
| 93 | + |
| 94 | + public void syncLegacyTests(TestSuiteDto template, Integer notExecutedFor, boolean removeNotExecutedResults) throws AqualityException { |
| 95 | + if(baseUser.isManager() || baseUser.getProjectUserBySuiteId(template.getId()).isManager()){ |
| 96 | + TestRunDto testRunTemplate = new TestRunDto(); |
| 97 | + testRunTemplate.setTest_suite_id(template.getId()); |
| 98 | + testRunTemplate.setLimit(notExecutedFor); |
| 99 | + List<TestRunDto> testRuns = testRunDao.searchAll(testRunTemplate); |
| 100 | + List<TestDto> legacyTests = getLegacyTests(testRuns, template.getId()); |
| 101 | + legacyTests.forEach(test -> { |
| 102 | + Test2SuiteDto test2Suite = new Test2SuiteDto(); |
| 103 | + test2Suite.setTest_id(test.getId()); |
| 104 | + test2Suite.setSuite_id(template.getId()); |
| 105 | + try { |
| 106 | + test2SuiteDao.delete(test2Suite); |
| 107 | + if(removeNotExecutedResults) { |
| 108 | + removePendingResultsForTest(test); |
| 109 | + } |
| 110 | + } catch (AqualityException e) { |
| 111 | + e.printStackTrace(); |
| 112 | + } |
| 113 | + }); |
| 114 | + }else{ |
| 115 | + throw new AqualityPermissionsException("Account is not allowed to Sync Test Suite", baseUser); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void removePendingResultsForTest(TestDto test) throws AqualityException { |
| 120 | + TestResultDto testResultTemplate = new TestResultDto(); |
| 121 | + testResultTemplate.setTest_id(test.getId()); |
| 122 | + testResultTemplate.setFinal_result_id(3); |
| 123 | + testResultTemplate.setTest_resolution_id(1); |
| 124 | + List<TestResultDto> testResults = testResultDao.searchAll(testResultTemplate); |
| 125 | + testResults.forEach(testResult -> { |
| 126 | + try { |
| 127 | + testResultDao.delete(testResult); |
| 128 | + } catch (AqualityException e) { |
| 129 | + e.printStackTrace(); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + private List<TestDto> getLegacyTests(List<TestRunDto> testRuns, Integer suiteId) throws AqualityException { |
| 135 | + TestDto testTemplate = new TestDto(); |
| 136 | + testTemplate.setTest_suite_id(suiteId); |
| 137 | + List<TestDto> tests = testController.get(testTemplate); |
| 138 | + for (TestRunDto testRun : testRuns) { |
| 139 | + TestResultDto testResultTemplate = new TestResultDto(); |
| 140 | + testResultTemplate.setTest_run_id(testRun.getId()); |
| 141 | + List<TestResultDto> testResults = testResultDao.searchAll(testResultTemplate); |
| 142 | + tests = tests.stream().filter(test -> { |
| 143 | + TestResultDto currentResult = testResults.stream().filter(result -> result.getTest_id().equals(test.getId())).findFirst().orElse(null); |
| 144 | + return currentResult == null || currentResult.getFinal_result_id() == 3; |
| 145 | + }).collect(Collectors.toList()); |
| 146 | + } |
| 147 | + |
| 148 | + return tests; |
| 149 | + } |
89 | 150 | }
|
0 commit comments