From 8da31607be38c4fcc7ddd0560f3f16d2564c0d62 Mon Sep 17 00:00:00 2001 From: EvgenySafronov <32324614+EvgenySafronov@users.noreply.github.com> Date: Wed, 3 Apr 2024 17:58:08 +0300 Subject: [PATCH 01/18] fixed connections leak, added synchronization to avoid deadlocks --- src/main/java/main/model/db/dao/DAO.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/java/main/model/db/dao/DAO.java b/src/main/java/main/model/db/dao/DAO.java index b731ed1..b1dfbbe 100644 --- a/src/main/java/main/model/db/dao/DAO.java +++ b/src/main/java/main/model/db/dao/DAO.java @@ -213,8 +213,9 @@ public boolean updateMultiply(List entities) throws AqualityException { protected JSONArray CallStoredProcedure(String sql, List> parameters) throws AqualityException { JSONArray json = null; - CallableStatement callableStatement = executeCallableStatement(sql, parameters, null); + CallableStatement callableStatement = null; try { + callableStatement = executeCallableStatement(sql, parameters, null); ResultSet rs = callableStatement.getResultSet(); if (rs != null) { json = RS_Converter.convertToJSON(rs); @@ -284,18 +285,14 @@ private CallableStatement executeCallableStatement(String sql, List Date: Tue, 9 Apr 2024 17:54:18 +0300 Subject: [PATCH 02/18] Improved performance for endpoints: /public/test/create-or-update, /public/test/result/start, /public/test/result/finish; Fixed INSERT_TEST_RESULT final_result_id comparison --- CHANGELOG.md | 7 + pom.xml | 2 +- .../controllers/Project/ResultController.java | 102 +++++++++------ .../controllers/Project/TestController.java | 120 ++++++++++-------- src/main/java/main/model/db/dao/DAO.java | 2 +- .../main/model/db/dao/project/TestDao.java | 5 +- .../model/db/dao/project/TestResultDao.java | 45 ++++++- src/main/java/main/utils/DateUtils.java | 5 + .../PublicTestResultFinishServlet.java | 23 +--- .../PublicTestResultStartServlet.java | 10 +- .../view/publicApi/PublicTestServlet.java | 6 +- src/main/resources/db_changelog/changelog.xml | 1 + .../db_changelog/db.changelog-1.5.1.xml | 115 +++++++++++++++++ .../dao/project/TestResultDaoTest.java | 8 ++ 14 files changed, 324 insertions(+), 127 deletions(-) create mode 100644 src/main/resources/db_changelog/db.changelog-1.5.1.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 2aef5ee..5e99f54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## 1.5.1 (2024-04-12) +Bugfixes: +- Fixed 500 (Entity is locked) issue +- Fixed SQL connections leak +- Improved /public/test/create-or-update, /public/test/result/start, /public/test/result/finish performance +- Fixed INSERT_TEST_RESULT final_result_id comparison + ## 1.5.0 (2023-07-28) - Added `/issues/assign` endpoint, which allows to check previously created Test Runs and Test Results and assign Issues to them if there's a RegEx match diff --git a/pom.xml b/pom.xml index e232c60..3f52ae1 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ unifi_reporting_api api war - 1.5.0 + 1.5.1 UTF-8 diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index 09d52aa..87e014f 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -14,6 +14,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; public class ResultController extends BaseController { @@ -56,15 +57,29 @@ public TestResultDto create(TestResultDto template) throws AqualityException { } } - @Override - public List get(TestResultDto template) throws AqualityException { - if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(template.getProject_id()).isViewer()) { - return fillResults(testResultDao.searchAll(template)); + public TestResultDto updateWithFinalResultIdAndFailReason(TestResultDto testResult) throws AqualityException { + if (baseUser.isManager() || baseUser.getProjectUser(testResult.getProject_id()).isEditor()) { + return testResultDao.updateFinalResultIdAndFailReason( + testResult.getId(), + testResult.getFinal_result_id(), + testResult.getFail_reason(), + testResult.getFinish_date()); } else { - throw new AqualityPermissionsException("Account is not allowed to view Test Results", baseUser); + throw new AqualityPermissionsException("Account is not allowed to update Test Result", baseUser); } } + @Override + public List get(TestResultDto template) throws AqualityException { + checkReadPermissions(template.getProject_id()); + return fillResults(testResultDao.searchAll(template)); + } + + public List getRaw(TestResultDto template) throws AqualityException { + checkReadPermissions(template.getProject_id()); + return testResultDao.searchAll(template); + } + @Override public boolean delete(TestResultDto template) throws AqualityException { if (baseUser.isManager() || baseUser.getProjectUser(template.getProject_id()).isEditor()) { @@ -76,8 +91,11 @@ public boolean delete(TestResultDto template) throws AqualityException { public List getOnlyFailedResults(TestResultDto testResultTemplate) throws AqualityException { List testResults = this.get(testResultTemplate); - return testResults.stream().filter(x -> x.getFinal_result_id() != FAILED_STATUS_ID && x.getFail_reason() != null - && x.getIssue_id() == null).collect(Collectors.toList()); + return testResults.stream() + .filter(x -> !Objects.equals(x.getFinal_result_id(), FAILED_STATUS_ID) && + x.getFail_reason() != null && + x.getIssue_id() == null) + .collect(Collectors.toList()); } public boolean createMultiple(List listOfAttachments) throws AqualityException { @@ -98,7 +116,7 @@ public List getLatestResultsByMilestone(Integer projectId, Intege } public boolean updateMultipleTestResults(List entities) throws AqualityException { - if (entities.size() > 0 + if (!entities.isEmpty() && (baseUser.isManager() || baseUser.getProjectUser(entities.get(0).getProject_id()).isEditor())) { return testResultDao.updateMultiply(entities); } else { @@ -114,6 +132,38 @@ public List get(TestResultStatDto template) throws AqualityEx } } + public Map matchIssues(Integer testResultId) throws AqualityException { + TestResultDto testResultTemplate = new TestResultDto(); + testResultTemplate.setId(testResultId); + List testResults = this.getOnlyFailedResults(testResultTemplate); + if (testResults.isEmpty()) { + throw new AqualityParametersException("No test result found to update. Wrong ID might be provided."); + } + IssueDto issueTemplate = new IssueDto(); + issueTemplate.setProject_id(testResults.get(0).getProject_id()); + List issues = issueController.get(issueTemplate); + Integer count = assignIssuesToResults(issues, testResults); + Map results = new HashMap<>(); + results.put("Issues assigned", count); + return results; + } + + public Integer assignIssuesToResults(List issues, List testResults) + throws AqualityException { + Integer count = 0; + for (TestResultDto testResult : testResults) { + for (IssueDto issue : issues) { + if (issue.getExpression() != null + && RegexpUtil.match(testResult.getFail_reason(), issue.getExpression())) { + testResult.setIssue_id(issue.getId()); + this.create(testResult); + count++; + } + } + } + return count; + } + private void createPendingStepResults(TestResultDto template) throws AqualityException { Step2TestDto step2TestTemplate = new Step2TestDto(); step2TestTemplate.setProject_id(template.getProject_id()); @@ -133,7 +183,7 @@ private void createPendingStepResults(TestResultDto template) throws AqualityExc private List fillResults(List results) throws AqualityException { - if (results.size() > 0) { + if (!results.isEmpty()) { int projectId = results.get(0).getProject_id(); List finalResults = finalResultController.get(new FinalResultDto()); IssueDto issueDto = new IssueDto(); @@ -163,7 +213,7 @@ private List fillResults(List results) throws Aqua } private void fillResult(TestResultDto result, List finalResults, List tests, - List issues, List attachments, boolean isStepsEnabled) + List issues, List attachments, boolean isStepsEnabled) throws AqualityException { if (isStepsEnabled) { fillResultSteps(result); @@ -187,35 +237,9 @@ private void fillResultSteps(TestResultDto result) throws AqualityException { result.setSteps(stepResultController.get(stepResultTemplate)); } - public Map matchIssues(Integer testResultId) throws AqualityException { - TestResultDto testResultTemplate = new TestResultDto(); - testResultTemplate.setId(testResultId); - List testResults = this.getOnlyFailedResults(testResultTemplate); - if (testResults.isEmpty()) { - throw new AqualityParametersException("No test result found to update. Wrong ID might be provided."); - } - IssueDto issueTemplate = new IssueDto(); - issueTemplate.setProject_id(testResults.get(0).getProject_id()); - List issues = issueController.get(issueTemplate); - Integer count = assignIssuesToResults(issues, testResults); - Map results = new HashMap<>(); - results.put("Issues assigned", count); - return results; - } - - public Integer assignIssuesToResults(List issues, List testResults) - throws AqualityException { - Integer count = 0; - for (TestResultDto testResult : testResults) { - for (IssueDto issue : issues) { - if (issue.getExpression() != null - && RegexpUtil.match(testResult.getFail_reason(), issue.getExpression())) { - testResult.setIssue_id(issue.getId()); - this.create(testResult); - count++; - } - } + private void checkReadPermissions(Integer projectId) throws AqualityException { + if (!(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer())) { + throw new AqualityPermissionsException("Account is not allowed to view Test Results", baseUser); } - return count; } } diff --git a/src/main/java/main/controllers/Project/TestController.java b/src/main/java/main/controllers/Project/TestController.java index ce7d292..fdc2ac9 100644 --- a/src/main/java/main/controllers/Project/TestController.java +++ b/src/main/java/main/controllers/Project/TestController.java @@ -30,45 +30,20 @@ public TestController(UserDto user) { } public TestDto create(TestDto template, boolean updateSuites) throws AqualityException { - if (baseUser.isManager() || baseUser.getProjectUser(template.getProject_id()).isEditor()) { - TestDto test = testDao.create(template); - if (updateSuites) { - test.setSuites(template.getSuites()); - updateSuites(test); - test = get(test).get(0); - } - return test; - } else { - throw new AqualityPermissionsException("Account is not allowed to create Test", baseUser); + checkCreatePermissions(template.getProject_id()); + TestDto test = testDao.create(template); + if (updateSuites) { + test.setSuites(template.getSuites()); + updateSuites(test); + test = get(test).get(0); } + return test; } public TestDto createOrUpdate(TestDto test) throws AqualityException { - TestDto searchTemplate = new TestDto(); - searchTemplate.setId(test.getId()); - searchTemplate.setProject_id(test.getProject_id()); - searchTemplate.setName(test.getName()); - - List existingTests = get(searchTemplate); - - if(!existingTests.isEmpty()) { - TestDto existingTest = existingTests.get(0); - if(existingTest.getSuites() != null) { - TestSuiteDto existingSuite = existingTest.getSuites().stream() - .filter(suite -> suite.getId().equals(test.getSuites().get(0).getId())) - .findFirst().orElse(null); - if(existingSuite == null) { - List listOfSuites = existingTest.getSuites(); - listOfSuites.add(test.getSuites().get(0)); - existingTest.setSuites(listOfSuites); - } - }else { - existingTest.setSuites(test.getSuites()); - } - return create(existingTest, true); - } else { - return create(test, true); - } + checkCreatePermissions(test.getProject_id()); + TestDto rawTest = getOrCreateRawTest(test); + return updateTestSuites(rawTest, test.getSuites().get(0).getId()); } @Override @@ -77,22 +52,15 @@ public TestDto create(TestDto template) throws AqualityException { } public List get(TestDto template) throws AqualityException { - if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(template.getProject_id()).isViewer()) { - return fillTests(testDao.searchAll(template)); - } else { - throw new AqualityPermissionsException("Account is not allowed to view Tests", baseUser); - } + checkReadPermissions(template.getProject_id()); + return fillTests(testDao.searchAll(template)); } public List get(Integer issueId, Integer projectId) throws AqualityException { - if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer()) { - return fillTests(testDao.getTestsAffectedByIssue(issueId)); - } else { - throw new AqualityPermissionsException("Account is not allowed to view Tests", baseUser); - } + checkReadPermissions(projectId); + return fillTests(testDao.getTestsAffectedByIssue(issueId)); } - @Override public boolean delete(TestDto template) throws AqualityException { if (baseUser.isManager() || baseUser.getProjectUser(template.getProject_id()).isEditor()) { @@ -103,7 +71,7 @@ public boolean delete(TestDto template) throws AqualityException { } public void updateMultipleTests(List entities) throws AqualityException { - if (entities.size() > 0 && (baseUser.isManager() || baseUser.getProjectUser(entities.get(0).getProject_id()).isEditor())) { + if (!entities.isEmpty() && (baseUser.isManager() || baseUser.getProjectUser(entities.get(0).getProject_id()).isEditor())) { for (TestDto test : entities) { updateSuites(test); } @@ -157,14 +125,12 @@ protected List getResultsToMove(TestDto from, TestDto to) { private List fillTests(List tests) throws AqualityException { List filledTests = new ArrayList<>(); - if (tests.size() > 0) { + if (!tests.isEmpty()) { Integer projectId = tests.get(0).getProject_id(); ProjectUserDto projectUserDto = new ProjectUserDto(); projectUserDto.setProject_id(tests.get(0).getProject_id()); List projectUsers = projectUserController.get(projectUserDto); - TestSuiteDto testSuiteDto = new TestSuiteDto(); - testSuiteDto.setProject_id(projectId); - List testSuites = suiteDao.searchAll(testSuiteDto); + List testSuites = getProjectTestSuites(projectId); ProjectDto projectDto = new ProjectDto(); projectDto.setId(tests.get(0).getProject_id()); @@ -189,7 +155,7 @@ private void updateSuites(TestDto test) throws AqualityException { Test2SuiteDto test2SuiteDto = new Test2SuiteDto(); test2SuiteDto.setTest_id(test.getId()); List oldSuites = test2SuiteController.get(test2SuiteDto); - if (test.getSuites() != null && test.getSuites().size() > 0) { + if (test.getSuites() != null && !test.getSuites().isEmpty()) { List suites = test.getSuites(); for (TestSuiteDto newSuite : suites) { Test2SuiteDto alreadyExists = oldSuites.stream().filter(x -> Objects.equals(x.getSuite_id(), newSuite.getId())).findAny().orElse(null); @@ -204,10 +170,58 @@ private void updateSuites(TestDto test) throws AqualityException { } } - if (oldSuites.size() > 0) { + if (!oldSuites.isEmpty()) { for (Test2SuiteDto oldSuite : oldSuites) { test2SuiteController.delete(oldSuite, test.getProject_id()); } } } + + private void checkReadPermissions(Integer projectId) throws AqualityException { + if (!(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer())) { + throw new AqualityPermissionsException("Account is not allowed to view Tests", baseUser); + } + } + + private void checkCreatePermissions(Integer projectId) throws AqualityException { + if (!(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isEditor())) { + throw new AqualityPermissionsException("Account is not allowed to create Test", baseUser); + } + } + + private List getProjectTestSuites(Integer projectId) throws AqualityException { + TestSuiteDto testSuiteDto = new TestSuiteDto(); + testSuiteDto.setProject_id(projectId); + return suiteDao.searchAll(testSuiteDto); + } + + private TestDto getOrCreateRawTest(TestDto test) throws AqualityException { + TestDto searchTemplate = new TestDto(); + searchTemplate.setId(test.getId()); + searchTemplate.setProject_id(test.getProject_id()); + searchTemplate.setName(test.getName()); + + List existingTests = testDao.searchAll(searchTemplate); + + return existingTests.isEmpty() ? testDao.create(test) : existingTests.get(0); + } + + private TestDto updateTestSuites(TestDto testDto, Integer testSuiteId) throws AqualityException { + Integer projectId = testDto.getProject_id(); + Test2SuiteDto test2SuiteDto = new Test2SuiteDto(); + test2SuiteDto.setProject_id(projectId); + test2SuiteDto.setTest_id(testDto.getId()); + + List existingTest2Suites = test2SuiteController.get(test2SuiteDto); + if (existingTest2Suites.stream().noneMatch(test2Suite -> test2Suite.getSuite_id().equals(testSuiteId))) { + test2SuiteDto.setSuite_id(testSuiteId); + Test2SuiteDto createdTest2Suite = test2SuiteController.create(test2SuiteDto, projectId); + existingTest2Suites.add(createdTest2Suite); + } + + List projectTestSuites = getProjectTestSuites(projectId); + testDto.setSuites(test2SuiteController.convertToSuites(existingTest2Suites, projectTestSuites)); + + return testDto; + } } diff --git a/src/main/java/main/model/db/dao/DAO.java b/src/main/java/main/model/db/dao/DAO.java index b1dfbbe..607633b 100644 --- a/src/main/java/main/model/db/dao/DAO.java +++ b/src/main/java/main/model/db/dao/DAO.java @@ -229,7 +229,7 @@ protected JSONArray CallStoredProcedure(String sql, List> p return json; } - private T getSingleResult(List allResults, Integer id) throws AqualityException { + protected T getSingleResult(List allResults, Integer id) throws AqualityException { if (!allResults.isEmpty()) { return allResults.get(0); } else { diff --git a/src/main/java/main/model/db/dao/project/TestDao.java b/src/main/java/main/model/db/dao/project/TestDao.java index e71501c..9afe027 100644 --- a/src/main/java/main/model/db/dao/project/TestDao.java +++ b/src/main/java/main/model/db/dao/project/TestDao.java @@ -9,17 +9,18 @@ import java.util.List; public class TestDao extends DAO { + private static final String TESTS_AFFECTED_BY_ISSUE_QUERY = "{call SELECT_ISSUE_TESTS(?)}"; + public TestDao() { super(TestDto.class); select = "{call SELECT_TEST(?,?,?,?,?,?)}"; insert = "{call INSERT_TEST(?,?,?,?,?,?)}"; remove = "{call REMOVE_TEST(?)}"; } - private String testsAffectedByIssueQuery = "{call SELECT_ISSUE_TESTS(?)}"; public List getTestsAffectedByIssue(Integer issueId) throws AqualityException { List> parameters = new ArrayList<>(); parameters.add(new Pair<>("request_issue_id", issueId.toString())); - return dtoMapper.mapObjects(CallStoredProcedure(testsAffectedByIssueQuery, parameters).toString()); + return dtoMapper.mapObjects(CallStoredProcedure(TESTS_AFFECTED_BY_ISSUE_QUERY, parameters).toString()); } } diff --git a/src/main/java/main/model/db/dao/project/TestResultDao.java b/src/main/java/main/model/db/dao/project/TestResultDao.java index a9fc9d7..9df415d 100644 --- a/src/main/java/main/model/db/dao/project/TestResultDao.java +++ b/src/main/java/main/model/db/dao/project/TestResultDao.java @@ -4,12 +4,20 @@ import main.exceptions.AqualityException; import main.model.db.dao.DAO; import main.model.dto.project.TestResultDto; +import main.utils.DateUtils; +import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.Date; import java.util.List; public class TestResultDao extends DAO { + private static final String SYNC_SUITE_QUERY = "{call SELECT_LEGACY_RESULTS(?,?)}"; + private static final String SELECT_LATEST_RESULTS_BY_MILESTONE = "{call SELECT_LATEST_RESULTS_BY_MILESTONE(?)}"; + private static final String FINISH_TEST_RESULT_QUERY = + "{call UPDATE_TEST_RESULT_WITH_FINAL_RESULT_ID_AND_FAIL_REASON(?,?,?,?)}"; + public TestResultDao() { super(TestResultDto.class); select = "{call SELECT_TEST_RESULT(?,?,?,?,?,?,?,?,?,?)}"; @@ -17,26 +25,49 @@ public TestResultDao() { remove = "{call REMOVE_TEST_RESULT(?)}"; } - - private String syncSuiteQuery = "{call SELECT_LEGACY_RESULTS(?,?)}"; - private String latestResultsByMilestone = "{call SELECT_LATEST_RESULTS_BY_MILESTONE(?)}"; - /** * @param suiteId suite id for search * @param testId test id for search * @return all not executed results till first executed * @throws AqualityException */ - public List selectSuiteLegacyResults(@NotNull Integer suiteId, @NotNull Integer testId) throws AqualityException { + public List selectSuiteLegacyResults( + @NotNull Integer suiteId, + @NotNull Integer testId) throws AqualityException { List> parameters = new ArrayList<>(); parameters.add(new Pair<>("request_test_suite_id", suiteId.toString())); parameters.add(new Pair<>("request_test_id", testId.toString())); - return dtoMapper.mapObjects(CallStoredProcedure(syncSuiteQuery, parameters).toString()); + return dtoMapper.mapObjects(CallStoredProcedure(SYNC_SUITE_QUERY, parameters).toString()); } public List selectLatestResultsByMilestone(@NotNull Integer milestoneId) throws AqualityException { List> parameters = new ArrayList<>(); parameters.add(new Pair<>("request_milestone_id", milestoneId.toString())); - return dtoMapper.mapObjects(CallStoredProcedure(latestResultsByMilestone, parameters).toString()); + return dtoMapper.mapObjects(CallStoredProcedure(SELECT_LATEST_RESULTS_BY_MILESTONE, parameters).toString()); + } + + /** + * @param id test result id + * @param finalResultId ID of final result - Failed: 1, Passed: 2, Not Executed: 3, Pending: 5 + * @param failReason fail reason message + * @param finishDate test finish date + * @return updated test result + */ + public TestResultDto updateFinalResultIdAndFailReason( + @NotNull Integer id, + @NotNull Integer finalResultId, + String failReason, + Date finishDate) throws AqualityException { + List> parameters = new ArrayList<>(); + parameters.add(new Pair<>("request_id", id.toString())); + parameters.add(new Pair<>("request_final_result_id", finalResultId.toString())); + parameters.add(new Pair<>("request_fail_reason", failReason)); + parameters.add(new Pair<>( + "request_finish_date", + finishDate == null ? StringUtils.EMPTY : String.valueOf(DateUtils.toUnixTime(finishDate)))); + + return getSingleResult( + dtoMapper.mapObjects(CallStoredProcedure(FINISH_TEST_RESULT_QUERY, parameters).toString()), + id); } } diff --git a/src/main/java/main/utils/DateUtils.java b/src/main/java/main/utils/DateUtils.java index ca70bb7..6c147ce 100644 --- a/src/main/java/main/utils/DateUtils.java +++ b/src/main/java/main/utils/DateUtils.java @@ -7,6 +7,7 @@ import java.util.Date; public class DateUtils { + private static final int MILLISECONDS_PER_SECOND = 1000; public Date addMS(Date date, Integer ms) { @@ -64,4 +65,8 @@ public Date fromyyyyMMdd(String date){ return null; } } + + public static Long toUnixTime(Date date) { + return date != null ? date.getTime() / MILLISECONDS_PER_SECOND : null; + } } diff --git a/src/main/java/main/view/publicApi/PublicTestResultFinishServlet.java b/src/main/java/main/view/publicApi/PublicTestResultFinishServlet.java index 2990fb5..c6968e1 100644 --- a/src/main/java/main/view/publicApi/PublicTestResultFinishServlet.java +++ b/src/main/java/main/view/publicApi/PublicTestResultFinishServlet.java @@ -1,6 +1,7 @@ package main.view.publicApi; import main.Session; +import main.controllers.Project.ResultController; import main.exceptions.AqualityParametersException; import main.model.dto.project.TestResultDto; import main.view.BaseServlet; @@ -10,7 +11,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Date; -import java.util.List; @WebServlet("/public/test/result/finish") public class PublicTestResultFinishServlet extends BaseServlet implements IPost { @@ -25,25 +25,12 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) { TestResultDto testResult = mapper.mapObject(TestResultDto.class, requestedJson); validatePost(testResult); + testResult.setFinish_date(new Date()); - TestResultDto testResultSearchTemplate = new TestResultDto(); - testResultSearchTemplate.setProject_id(testResult.getProject_id()); - testResultSearchTemplate.setId(testResult.getId()); + ResultController testResultController = session.controllerFactory.getHandler(testResult); + TestResultDto updatedTestResult = testResultController.updateWithFinalResultIdAndFailReason(testResult); - List oldResults = session.controllerFactory.getHandler(testResult).get(testResultSearchTemplate); - - if(oldResults.size() > 0) { - TestResultDto currentResult = oldResults.get(0); - currentResult.setFinal_result_id(testResult.getFinal_result_id()); - currentResult.setFail_reason(testResult.getFail_reason()); - currentResult.setFinish_date(new Date()); - - testResult = session.controllerFactory.getHandler(testResult).create(currentResult); - } else { - throw new AqualityParametersException("Test Result with %s id was not found!", testResult.getId()); - } - - setResponseBody(resp, testResult); + setResponseBody(resp, updatedTestResult); } catch (Exception e) { handleException(resp, e); } diff --git a/src/main/java/main/view/publicApi/PublicTestResultStartServlet.java b/src/main/java/main/view/publicApi/PublicTestResultStartServlet.java index 214b0fc..092b51c 100644 --- a/src/main/java/main/view/publicApi/PublicTestResultStartServlet.java +++ b/src/main/java/main/view/publicApi/PublicTestResultStartServlet.java @@ -26,11 +26,13 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) { validateGet(testResult); - List oldResults = session.controllerFactory.getHandler(testResult).get(testResult); + List oldResults = session.controllerFactory.getHandler(testResult).getRaw(testResult); - if(oldResults.size() > 0) { - TestResultDto pending = oldResults.stream().filter(result -> result.getPending().equals(1)).findFirst().orElse(null); - if(pending != null) { + if(!oldResults.isEmpty()) { + TestResultDto pending = oldResults.stream() + .filter(result -> result.getPending().equals(1)) + .findFirst().orElse(null); + if (pending != null) { testResult.setId(pending.getId()); } } diff --git a/src/main/java/main/view/publicApi/PublicTestServlet.java b/src/main/java/main/view/publicApi/PublicTestServlet.java index 8b13a2c..781bf42 100644 --- a/src/main/java/main/view/publicApi/PublicTestServlet.java +++ b/src/main/java/main/view/publicApi/PublicTestServlet.java @@ -41,8 +41,10 @@ private void validatePost(TestDto test) throws AqualityParametersException { throw new AqualityParametersException("You should specify 'id' or/and 'name' suite parameters!"); } - if(test.getSuites() == null || test.getSuites().size() != 1) { - throw new AqualityParametersException("You should specify 'suite' array with one element as {id: suite_id}. So the API will ensure this test is assigned to Suite."); + if(test.getSuites() == null || test.getSuites().size() != 1 || test.getSuites().get(0).getId() == null) { + throw new AqualityParametersException( + "You should specify 'suite' array with one element as {id: suite_id}. " + + "So the API will ensure this test is assigned to Suite."); } } diff --git a/src/main/resources/db_changelog/changelog.xml b/src/main/resources/db_changelog/changelog.xml index 4b5d986..31fdd41 100644 --- a/src/main/resources/db_changelog/changelog.xml +++ b/src/main/resources/db_changelog/changelog.xml @@ -38,4 +38,5 @@ + diff --git a/src/main/resources/db_changelog/db.changelog-1.5.1.xml b/src/main/resources/db_changelog/db.changelog-1.5.1.xml new file mode 100644 index 0000000..512e80b --- /dev/null +++ b/src/main/resources/db_changelog/db.changelog-1.5.1.xml @@ -0,0 +1,115 @@ + + + + + + DROP procedure IF EXISTS `UPDATE_TEST_RESULT_WITH_FINAL_RESULT_ID_AND_FAIL_REASON`; + + # + + CREATE PROCEDURE `UPDATE_TEST_RESULT_WITH_FINAL_RESULT_ID_AND_FAIL_REASON`( + IN request_id VARCHAR(11), + IN request_final_result_id VARCHAR(11), + IN request_fail_reason mediumtext, + IN request_finish_date VARCHAR(500) + ) + BEGIN + UPDATE test_results + SET + finish_date = If(request_finish_date = '', finish_date, FROM_UNIXTIME(request_finish_date)), + final_result_updated = IF(request_final_result_id != final_result_id, NOW(), final_result_updated), + fail_reason = IF( + request_fail_reason = '', + fail_reason, + IF(request_fail_reason = '$blank', '', request_fail_reason) + ), + final_result_id = request_final_result_id, + updated = NOW() + WHERE id = request_id; + + SELECT * FROM test_results WHERE id = request_id; + END + + + + + + + + + + + + + DROP PROCEDURE IF EXISTS `INSERT_TEST_RESULT`; + + # + CREATE PROCEDURE `INSERT_TEST_RESULT`( + IN request_project_id VARCHAR(10), + IN request_id VARCHAR(10), + IN request_test_id VARCHAR(500), + IN request_final_result_id VARCHAR(500), + IN request_test_run_id VARCHAR(500), + IN request_log longtext, + IN request_debug VARCHAR(1), + IN request_finish_date VARCHAR(500), + IN request_start_date VARCHAR(500), + IN request_final_result_updated VARCHAR(500), + IN request_fail_reason mediumtext, + IN request_issue_id VARCHAR(11) + ) + BEGIN + INSERT INTO test_results ( + project_id, id, test_id, final_result_id, test_run_id, log, debug, start_date, + finish_date, final_result_updated, fail_reason, issue_id + ) + VALUES ( + request_project_id, + IF(request_id='', null, request_id), + request_test_id, + IF(request_final_result_id='',1,request_final_result_id), + IF(request_test_run_id = '', null, request_test_run_id), + IF(request_log = '', null, request_log), + IF(request_debug = '', 0, request_debug), + If(request_start_date = '', null, FROM_UNIXTIME(request_start_date)), + If(request_finish_date = '', null, FROM_UNIXTIME(request_finish_date)), + NOW(), + IF(request_fail_reason = '', null, request_fail_reason), + replace_empty(request_issue_id, null) + ) + ON DUPLICATE KEY UPDATE + test_id=IF(request_test_id = '',test_id,request_test_id), + log = IF(request_log = '', log, IF(request_log = '$blank', '', request_log)), + debug = IF(request_debug = '', debug, request_debug), + start_date = If(request_start_date = '', start_date, FROM_UNIXTIME(request_start_date)), + finish_date = If(request_finish_date = '', finish_date, FROM_UNIXTIME(request_finish_date)), + final_result_updated = IF( + request_final_result_id != final_result_id AND request_final_result_updated = '', + NOW(), + IF( + request_final_result_updated = '', + final_result_updated, + FROM_UNIXTIME(request_final_result_updated) + ) + ), + fail_reason = IF( + request_fail_reason = '', + fail_reason, + IF(request_fail_reason = '$blank', '', request_fail_reason) + ), + final_result_id = IF(request_final_result_id = '', final_result_id, request_final_result_id), + issue_id = apply_or_remove_id(request_issue_id, issue_id); + + SET @insert_id = IF(request_id = '', (SELECT LAST_INSERT_ID()), request_id); + + SELECT * from test_results where id = @insert_id; + END + + + + + diff --git a/src/test/java/tests/workers/dao/project/TestResultDaoTest.java b/src/test/java/tests/workers/dao/project/TestResultDaoTest.java index f46cab4..ca34936 100644 --- a/src/test/java/tests/workers/dao/project/TestResultDaoTest.java +++ b/src/test/java/tests/workers/dao/project/TestResultDaoTest.java @@ -12,6 +12,7 @@ import tests.workers.dao.IDaoTest; import java.util.ArrayList; +import java.util.Date; import java.util.List; import static org.testng.Assert.assertEquals; @@ -62,6 +63,13 @@ public void selectLatestResultsByMilestoneTest() throws AqualityException { assertSQLToParams(currentSql, currentParameters); } + @Test + public void updateFinalResultIdAndFailReasonTest() throws AqualityException { + resultList.add(new TestResultDto()); + updateFinalResultIdAndFailReason(1, 1, "failReason", new Date()); + assertSQLToParams(currentSql, currentParameters); + } + @Override protected JSONArray CallStoredProcedure(String sql, List> parameters){ currentSql = sql; From 81bbf3462f32d03200be17f49eac25da122d2cbd Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Sun, 4 Aug 2024 21:14:55 +0300 Subject: [PATCH 03/18] add test data --- .../main/controllers/Project/ResultController.java | 14 +++++++++++--- src/main/java/main/model/db/dao/DAO.java | 7 ++++++- src/main/java/main/view/Project/TestResult.java | 5 +++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index 87e014f..96b3a1d 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -184,29 +184,37 @@ private void createPendingStepResults(TestResultDto template) throws AqualityExc private List fillResults(List results) throws AqualityException { if (!results.isEmpty()) { + long start1 = System.currentTimeMillis(); int projectId = results.get(0).getProject_id(); List finalResults = finalResultController.get(new FinalResultDto()); IssueDto issueDto = new IssueDto(); issueDto.setProject_id(projectId); List issues = issueController.get(issueDto); - + long start2 = System.currentTimeMillis(); + System.out.println("get issue = " + (start2-start1)); TestDto testTemplate = new TestDto(); testTemplate.setProject_id(projectId); List tests = testController.get(testTemplate); - + long start3 = System.currentTimeMillis(); + System.out.println("get test template(project_id) = " + (start3-start2)); TestResultAttachmentDto testResultAttachmentTemplate = new TestResultAttachmentDto(); testResultAttachmentTemplate.setProject_id(projectId); List testResultAttachments = testResultAttachmentController .get(testResultAttachmentTemplate); - + long start4 = System.currentTimeMillis(); + System.out.println("get test attachments = " + (start4-start3)); ProjectUserDto projectUserDto = new ProjectUserDto(); projectUserDto.setProject_id(projectId); boolean isStepsEnabled = projectController.isStepsEnabled(projectId); + long start5 = System.currentTimeMillis(); for (TestResultDto result : results) { fillResult(result, finalResults, tests, issues, testResultAttachments, isStepsEnabled); } + long start6 = System.currentTimeMillis(); + System.out.println("Exact filling results " + (start6-start5)); + System.out.println("Whole filling function " + (start6-start1)); } return results; diff --git a/src/main/java/main/model/db/dao/DAO.java b/src/main/java/main/model/db/dao/DAO.java index 607633b..1f89820 100644 --- a/src/main/java/main/model/db/dao/DAO.java +++ b/src/main/java/main/model/db/dao/DAO.java @@ -78,9 +78,14 @@ public List getAll() throws AqualityException { * @return List of entities */ public List searchAll(T entity) throws AqualityException { + long start1 = System.currentTimeMillis(); List> parameters = entity.getSearchParameters(); checkSelectProcedure(); - return dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString()); + + List result = dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString()); + long start2 = System.currentTimeMillis(); + System.out.println("Search all by entity = " + (start2-start1)); + return result; } /** diff --git a/src/main/java/main/view/Project/TestResult.java b/src/main/java/main/view/Project/TestResult.java index 50d31d5..79e30cb 100644 --- a/src/main/java/main/view/Project/TestResult.java +++ b/src/main/java/main/view/Project/TestResult.java @@ -37,6 +37,11 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) { setPostResponseHeaders(resp); setEncoding(resp); try { + System.out.println(); + System.out.println(); + System.out.println(); + System.out.println("request = " + req.getRequestURL()); + System.out.println("query = " + req.getQueryString()); Session session = createSession(req); TestResultDto testResultTemplate = new TestResultDto(); testResultTemplate.getSearchTemplateFromRequestParameters(req); From 56c3ea8333dac6a8d69ece0ea1d448a9b77c7f3b Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Sun, 4 Aug 2024 22:41:31 +0300 Subject: [PATCH 04/18] add entity class name --- src/main/java/main/model/db/dao/DAO.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/main/model/db/dao/DAO.java b/src/main/java/main/model/db/dao/DAO.java index 1f89820..15c7c8e 100644 --- a/src/main/java/main/model/db/dao/DAO.java +++ b/src/main/java/main/model/db/dao/DAO.java @@ -84,7 +84,7 @@ public List searchAll(T entity) throws AqualityException { List result = dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString()); long start2 = System.currentTimeMillis(); - System.out.println("Search all by entity = " + (start2-start1)); + System.out.println("Search all by entity " + entity.getClass().getName() + "=" + (start2-start1)); return result; } From c17e7b9a64ab40913f95dddc1350eb6989b66d5e Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Mon, 5 Aug 2024 20:22:09 +0300 Subject: [PATCH 05/18] add map search instead list --- .../controllers/Project/ResultController.java | 71 +++++++++++++++---- 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index 96b3a1d..a35f9c0 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -7,14 +7,12 @@ import main.model.db.dao.project.TestResultAttachmentDao; import main.model.db.dao.project.TestResultDao; import main.model.db.dao.project.TestResultStatDao; +import main.model.dto.AttachmentDto; import main.model.dto.project.*; import main.model.dto.settings.UserDto; import main.utils.RegexpUtil; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; public class ResultController extends BaseController { @@ -187,20 +185,59 @@ private List fillResults(List results) throws Aqua long start1 = System.currentTimeMillis(); int projectId = results.get(0).getProject_id(); List finalResults = finalResultController.get(new FinalResultDto()); + + Map finalResultsMap = new HashMap<>(); + for(FinalResultDto resultDto : finalResults) { + finalResultsMap.put(resultDto.getId(), resultDto); + } + IssueDto issueDto = new IssueDto(); issueDto.setProject_id(projectId); List issues = issueController.get(issueDto); + + Map issuesMap = new HashMap<>(); + for(IssueDto issueDto2 : issues) { + issuesMap.put(issueDto2.getId(), issueDto2); + } + + long start2 = System.currentTimeMillis(); System.out.println("get issue = " + (start2-start1)); TestDto testTemplate = new TestDto(); testTemplate.setProject_id(projectId); List tests = testController.get(testTemplate); + + Map testsMap = new HashMap<>(); + for(TestDto testDto : tests) { + testsMap.put(testDto.getId(), testDto); + } + + + long start3 = System.currentTimeMillis(); System.out.println("get test template(project_id) = " + (start3-start2)); TestResultAttachmentDto testResultAttachmentTemplate = new TestResultAttachmentDto(); testResultAttachmentTemplate.setProject_id(projectId); List testResultAttachments = testResultAttachmentController .get(testResultAttachmentTemplate); + + + Map> attachmentsMap = new HashMap<>(); + + + for(TestResultAttachmentDto attachmentDto : testResultAttachments) { + if(!attachmentsMap.containsKey(attachmentDto.getId())) { + List res = new ArrayList<>(); + res.add(attachmentDto); + attachmentsMap.put(attachmentDto.getTest_result_id(), res); + } + else { + attachmentsMap.get(attachmentDto.getTest_result_id()).add(attachmentDto); + } + + + } + long start4 = System.currentTimeMillis(); System.out.println("get test attachments = " + (start4-start3)); ProjectUserDto projectUserDto = new ProjectUserDto(); @@ -210,7 +247,7 @@ private List fillResults(List results) throws Aqua long start5 = System.currentTimeMillis(); for (TestResultDto result : results) { - fillResult(result, finalResults, tests, issues, testResultAttachments, isStepsEnabled); + fillResult(result, finalResultsMap, testsMap, issuesMap, attachmentsMap, isStepsEnabled); } long start6 = System.currentTimeMillis(); System.out.println("Exact filling results " + (start6-start5)); @@ -220,22 +257,28 @@ private List fillResults(List results) throws Aqua return results; } - private void fillResult(TestResultDto result, List finalResults, List tests, - List issues, List attachments, boolean isStepsEnabled) + private void fillResult(TestResultDto result, Map finalResults, Map tests, + Map issues, Map> attachments, boolean isStepsEnabled) throws AqualityException { if (isStepsEnabled) { fillResultSteps(result); } - result.setFinal_result(finalResults.stream().filter(x -> x.getId().equals(result.getFinal_result_id())) - .findFirst().orElse(null)); - result.setTest(tests.stream().filter(x -> x.getId().equals(result.getTest_id())).findFirst().orElse(null)); + // result.setFinal_result(finalResults.stream().filter(x -> x.getId().equals(result.getFinal_result_id())) + // .findFirst().orElse(null)); + result.setFinal_result(finalResults.get(result.getFinal_result_id())); + + // result.setTest(tests.stream().filter(x -> x.getId().equals(result.getTest_id())).findFirst().orElse(null)); + result.setTest(tests.get(result.getTest_id())); + if (result.getIssue_id() != null) { - result.setIssue( - issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null)); + //result.setIssue( + // issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null)); + result.setIssue(issues.get(result.getIssue_id())); } - result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) - .collect(Collectors.toList())); + // result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) + // .collect(Collectors.toList())); + result.setAttachments(attachments.get(result.getId())); } private void fillResultSteps(TestResultDto result) throws AqualityException { From 37e61160b64196e0dd1a7b5004055045b0e24ae7 Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Mon, 5 Aug 2024 20:41:35 +0300 Subject: [PATCH 06/18] null pointer debug --- .../main/controllers/Project/ResultController.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index a35f9c0..1c365a7 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -247,7 +247,7 @@ private List fillResults(List results) throws Aqua long start5 = System.currentTimeMillis(); for (TestResultDto result : results) { - fillResult(result, finalResultsMap, testsMap, issuesMap, attachmentsMap, isStepsEnabled); + fillResult(result, finalResultsMap, testsMap, issuesMap, testResultAttachments, isStepsEnabled); } long start6 = System.currentTimeMillis(); System.out.println("Exact filling results " + (start6-start5)); @@ -258,7 +258,7 @@ private List fillResults(List results) throws Aqua } private void fillResult(TestResultDto result, Map finalResults, Map tests, - Map issues, Map> attachments, boolean isStepsEnabled) + Map issues, List attachments, boolean isStepsEnabled) throws AqualityException { if (isStepsEnabled) { fillResultSteps(result); @@ -276,9 +276,9 @@ private void fillResult(TestResultDto result, Map final // issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null)); result.setIssue(issues.get(result.getIssue_id())); } - // result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) - // .collect(Collectors.toList())); - result.setAttachments(attachments.get(result.getId())); + result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) + .collect(Collectors.toList())); + //result.setAttachments(attachments.get(result.getId())); } private void fillResultSteps(TestResultDto result) throws AqualityException { From 7c3caf7d2869aa7600def6c6111a28d00ace1fe7 Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Mon, 5 Aug 2024 20:42:32 +0300 Subject: [PATCH 07/18] null pointer debug --- .../controllers/Project/ResultController.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index 1c365a7..b7738a4 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -225,18 +225,18 @@ private List fillResults(List results) throws Aqua Map> attachmentsMap = new HashMap<>(); - for(TestResultAttachmentDto attachmentDto : testResultAttachments) { - if(!attachmentsMap.containsKey(attachmentDto.getId())) { - List res = new ArrayList<>(); - res.add(attachmentDto); - attachmentsMap.put(attachmentDto.getTest_result_id(), res); - } - else { - attachmentsMap.get(attachmentDto.getTest_result_id()).add(attachmentDto); - } - - - } +// for(TestResultAttachmentDto attachmentDto : testResultAttachments) { +// if(!attachmentsMap.containsKey(attachmentDto.getId())) { +// List res = new ArrayList<>(); +// res.add(attachmentDto); +// attachmentsMap.put(attachmentDto.getTest_result_id(), res); +// } +// else { +// attachmentsMap.get(attachmentDto.getTest_result_id()).add(attachmentDto); +// } +// +// +// } long start4 = System.currentTimeMillis(); System.out.println("get test attachments = " + (start4-start3)); From a6fd7d431d4a24db83231ab099de1e6184b223ce Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Mon, 5 Aug 2024 21:12:11 +0300 Subject: [PATCH 08/18] null pointer debug --- .../controllers/Project/ResultController.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index b7738a4..df2f862 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -225,18 +225,18 @@ private List fillResults(List results) throws Aqua Map> attachmentsMap = new HashMap<>(); -// for(TestResultAttachmentDto attachmentDto : testResultAttachments) { -// if(!attachmentsMap.containsKey(attachmentDto.getId())) { -// List res = new ArrayList<>(); -// res.add(attachmentDto); -// attachmentsMap.put(attachmentDto.getTest_result_id(), res); -// } -// else { -// attachmentsMap.get(attachmentDto.getTest_result_id()).add(attachmentDto); -// } -// -// -// } + for(TestResultAttachmentDto attachmentDto : testResultAttachments) { + if(!attachmentsMap.containsKey(attachmentDto.getTest_result_id())) { + List res = new ArrayList<>(); + res.add(attachmentDto); + attachmentsMap.put(attachmentDto.getTest_result_id(), res); + } + else { + attachmentsMap.get(attachmentDto.getTest_result_id()).add(attachmentDto); + } + + + } long start4 = System.currentTimeMillis(); System.out.println("get test attachments = " + (start4-start3)); @@ -247,7 +247,7 @@ private List fillResults(List results) throws Aqua long start5 = System.currentTimeMillis(); for (TestResultDto result : results) { - fillResult(result, finalResultsMap, testsMap, issuesMap, testResultAttachments, isStepsEnabled); + fillResult(result, finalResultsMap, testsMap, issuesMap, attachmentsMap, isStepsEnabled); } long start6 = System.currentTimeMillis(); System.out.println("Exact filling results " + (start6-start5)); @@ -258,7 +258,7 @@ private List fillResults(List results) throws Aqua } private void fillResult(TestResultDto result, Map finalResults, Map tests, - Map issues, List attachments, boolean isStepsEnabled) + Map issues, Map> attachments, boolean isStepsEnabled) throws AqualityException { if (isStepsEnabled) { fillResultSteps(result); @@ -276,9 +276,9 @@ private void fillResult(TestResultDto result, Map final // issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null)); result.setIssue(issues.get(result.getIssue_id())); } - result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) - .collect(Collectors.toList())); - //result.setAttachments(attachments.get(result.getId())); +// result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) +// .collect(Collectors.toList())); + result.setAttachments(attachments.get(result.getId())); } private void fillResultSteps(TestResultDto result) throws AqualityException { From 51eb6af01ff160ae0369a20cd1267ec604df1d1a Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Mon, 5 Aug 2024 21:28:23 +0300 Subject: [PATCH 09/18] count --- .../controllers/Project/ResultController.java | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index df2f862..d23fca5 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -186,6 +186,8 @@ private List fillResults(List results) throws Aqua int projectId = results.get(0).getProject_id(); List finalResults = finalResultController.get(new FinalResultDto()); + System.out.println("Final results count = " + (long) finalResults.size()); + Map finalResultsMap = new HashMap<>(); for(FinalResultDto resultDto : finalResults) { finalResultsMap.put(resultDto.getId(), resultDto); @@ -195,6 +197,8 @@ private List fillResults(List results) throws Aqua issueDto.setProject_id(projectId); List issues = issueController.get(issueDto); + System.out.println("Count issues = "+ (long) issues.size()); + Map issuesMap = new HashMap<>(); for(IssueDto issueDto2 : issues) { issuesMap.put(issueDto2.getId(), issueDto2); @@ -207,6 +211,8 @@ private List fillResults(List results) throws Aqua testTemplate.setProject_id(projectId); List tests = testController.get(testTemplate); + System.out.println("Count tests = " + (long) tests.size()); + Map testsMap = new HashMap<>(); for(TestDto testDto : tests) { testsMap.put(testDto.getId(), testDto); @@ -221,22 +227,23 @@ private List fillResults(List results) throws Aqua List testResultAttachments = testResultAttachmentController .get(testResultAttachmentTemplate); + System.out.println("Test result attachments = " + testResultAttachments.size()); Map> attachmentsMap = new HashMap<>(); - for(TestResultAttachmentDto attachmentDto : testResultAttachments) { - if(!attachmentsMap.containsKey(attachmentDto.getTest_result_id())) { - List res = new ArrayList<>(); - res.add(attachmentDto); - attachmentsMap.put(attachmentDto.getTest_result_id(), res); - } - else { - attachmentsMap.get(attachmentDto.getTest_result_id()).add(attachmentDto); - } - - - } +// for(TestResultAttachmentDto attachmentDto : testResultAttachments) { +// if(!attachmentsMap.containsKey(attachmentDto.getTest_result_id())) { +// List res = new ArrayList<>(); +// res.add(attachmentDto); +// attachmentsMap.put(attachmentDto.getTest_result_id(), res); +// } +// else { +// attachmentsMap.get(attachmentDto.getTest_result_id()).add(attachmentDto); +// } +// +// +// } long start4 = System.currentTimeMillis(); System.out.println("get test attachments = " + (start4-start3)); @@ -246,8 +253,9 @@ private List fillResults(List results) throws Aqua boolean isStepsEnabled = projectController.isStepsEnabled(projectId); long start5 = System.currentTimeMillis(); + System.out.println("Count results = "+ (long) results.size()); for (TestResultDto result : results) { - fillResult(result, finalResultsMap, testsMap, issuesMap, attachmentsMap, isStepsEnabled); + fillResult(result, finalResultsMap, testsMap, issuesMap, testResultAttachments, isStepsEnabled); } long start6 = System.currentTimeMillis(); System.out.println("Exact filling results " + (start6-start5)); @@ -258,7 +266,7 @@ private List fillResults(List results) throws Aqua } private void fillResult(TestResultDto result, Map finalResults, Map tests, - Map issues, Map> attachments, boolean isStepsEnabled) + Map issues, List attachments, boolean isStepsEnabled) throws AqualityException { if (isStepsEnabled) { fillResultSteps(result); @@ -276,9 +284,9 @@ private void fillResult(TestResultDto result, Map final // issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null)); result.setIssue(issues.get(result.getIssue_id())); } -// result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) -// .collect(Collectors.toList())); - result.setAttachments(attachments.get(result.getId())); + result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) + .collect(Collectors.toList())); + // result.setAttachments(attachments.get(result.getId())); } private void fillResultSteps(TestResultDto result) throws AqualityException { From 95536753edaa645098c69411db2336154385e13f Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Sun, 11 Aug 2024 13:56:26 +0300 Subject: [PATCH 10/18] add search by test_id --- .../controllers/Project/ResultController.java | 59 ++++++++++--------- .../dto/project/TestResultAttachmentDto.java | 4 ++ .../db_changelog/db.changelog-1.5.2.xml | 33 +++++++++++ 3 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 src/main/resources/db_changelog/db.changelog-1.5.2.xml diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index d23fca5..58179ba 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -70,7 +70,7 @@ public TestResultDto updateWithFinalResultIdAndFailReason(TestResultDto testResu @Override public List get(TestResultDto template) throws AqualityException { checkReadPermissions(template.getProject_id()); - return fillResults(testResultDao.searchAll(template)); + return fillResults(testResultDao.searchAll(template), template); } public List getRaw(TestResultDto template) throws AqualityException { @@ -107,7 +107,7 @@ public boolean createMultiple(List listOfAttachments) t public List getLatestResultsByMilestone(Integer projectId, Integer milestoneId) throws AqualityException { if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer()) { - return fillResults(testResultDao.selectLatestResultsByMilestone(milestoneId)); + return fillResults(testResultDao.selectLatestResultsByMilestone(milestoneId), null); } else { throw new AqualityPermissionsException("Account is not allowed to view Test Results", baseUser); } @@ -179,7 +179,7 @@ private void createPendingStepResults(TestResultDto template) throws AqualityExc } } - private List fillResults(List results) throws AqualityException { + private List fillResults(List results, TestResultDto searchTemplate) throws AqualityException { if (!results.isEmpty()) { long start1 = System.currentTimeMillis(); @@ -188,10 +188,10 @@ private List fillResults(List results) throws Aqua System.out.println("Final results count = " + (long) finalResults.size()); - Map finalResultsMap = new HashMap<>(); - for(FinalResultDto resultDto : finalResults) { - finalResultsMap.put(resultDto.getId(), resultDto); - } +// Map finalResultsMap = new HashMap<>(); +// for(FinalResultDto resultDto : finalResults) { +// finalResultsMap.put(resultDto.getId(), resultDto); +// } IssueDto issueDto = new IssueDto(); issueDto.setProject_id(projectId); @@ -199,10 +199,10 @@ private List fillResults(List results) throws Aqua System.out.println("Count issues = "+ (long) issues.size()); - Map issuesMap = new HashMap<>(); - for(IssueDto issueDto2 : issues) { - issuesMap.put(issueDto2.getId(), issueDto2); - } +// Map issuesMap = new HashMap<>(); +// for(IssueDto issueDto2 : issues) { +// issuesMap.put(issueDto2.getId(), issueDto2); +// } long start2 = System.currentTimeMillis(); @@ -213,10 +213,10 @@ private List fillResults(List results) throws Aqua System.out.println("Count tests = " + (long) tests.size()); - Map testsMap = new HashMap<>(); - for(TestDto testDto : tests) { - testsMap.put(testDto.getId(), testDto); - } +// Map testsMap = new HashMap<>(); +// for(TestDto testDto : tests) { +// testsMap.put(testDto.getId(), testDto); +// } @@ -224,12 +224,17 @@ private List fillResults(List results) throws Aqua System.out.println("get test template(project_id) = " + (start3-start2)); TestResultAttachmentDto testResultAttachmentTemplate = new TestResultAttachmentDto(); testResultAttachmentTemplate.setProject_id(projectId); + if(searchTemplate != null) { + testResultAttachmentTemplate.setTest_run_id(searchTemplate.getTest_run_id()); + testResultAttachmentTemplate.setTest_id(searchTemplate.getTest_id()); + } + // testResultAttachmentTemplate.set List testResultAttachments = testResultAttachmentController .get(testResultAttachmentTemplate); System.out.println("Test result attachments = " + testResultAttachments.size()); - Map> attachmentsMap = new HashMap<>(); + // Map> attachmentsMap = new HashMap<>(); // for(TestResultAttachmentDto attachmentDto : testResultAttachments) { @@ -255,7 +260,7 @@ private List fillResults(List results) throws Aqua long start5 = System.currentTimeMillis(); System.out.println("Count results = "+ (long) results.size()); for (TestResultDto result : results) { - fillResult(result, finalResultsMap, testsMap, issuesMap, testResultAttachments, isStepsEnabled); + fillResult(result, finalResults, tests, issues, testResultAttachments, isStepsEnabled); } long start6 = System.currentTimeMillis(); System.out.println("Exact filling results " + (start6-start5)); @@ -265,24 +270,24 @@ private List fillResults(List results) throws Aqua return results; } - private void fillResult(TestResultDto result, Map finalResults, Map tests, - Map issues, List attachments, boolean isStepsEnabled) + private void fillResult(TestResultDto result, List< FinalResultDto> finalResults, List tests, + List issues, List attachments, boolean isStepsEnabled) throws AqualityException { if (isStepsEnabled) { fillResultSteps(result); } - // result.setFinal_result(finalResults.stream().filter(x -> x.getId().equals(result.getFinal_result_id())) - // .findFirst().orElse(null)); - result.setFinal_result(finalResults.get(result.getFinal_result_id())); + result.setFinal_result(finalResults.stream().filter(x -> x.getId().equals(result.getFinal_result_id())) + .findFirst().orElse(null)); + //result.setFinal_result(finalResults.get(result.getFinal_result_id())); - // result.setTest(tests.stream().filter(x -> x.getId().equals(result.getTest_id())).findFirst().orElse(null)); - result.setTest(tests.get(result.getTest_id())); + result.setTest(tests.stream().filter(x -> x.getId().equals(result.getTest_id())).findFirst().orElse(null)); + // result.setTest(tests.get(result.getTest_id())); if (result.getIssue_id() != null) { - //result.setIssue( - // issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null)); - result.setIssue(issues.get(result.getIssue_id())); + result.setIssue( + issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null)); + // result.setIssue(issues.get(result.getIssue_id())); } result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) .collect(Collectors.toList())); diff --git a/src/main/java/main/model/dto/project/TestResultAttachmentDto.java b/src/main/java/main/model/dto/project/TestResultAttachmentDto.java index c5bb7bd..245921d 100644 --- a/src/main/java/main/model/dto/project/TestResultAttachmentDto.java +++ b/src/main/java/main/model/dto/project/TestResultAttachmentDto.java @@ -24,6 +24,10 @@ public class TestResultAttachmentDto extends AttachmentDto { @DataBaseSearchable private Integer test_run_id; + @DataBaseName(name = "request_test_id") + @DataBaseSearchable + private Integer test_id; + private String url; private byte[] attachment; diff --git a/src/main/resources/db_changelog/db.changelog-1.5.2.xml b/src/main/resources/db_changelog/db.changelog-1.5.2.xml new file mode 100644 index 0000000..18c6608 --- /dev/null +++ b/src/main/resources/db_changelog/db.changelog-1.5.2.xml @@ -0,0 +1,33 @@ + + + + + + DROP procedure IF EXISTS `SELECT_TEST_RESULT_ATTACH`; + + # + + create procedure SELECT_TEST_RESULT_ATTACH2( + In request_id varchar(11), + In request_test_result_id varchar(11), + In request_test_run_id varchar(11), + In request_project_id varchar(11), + In request_test_id varchar(11) + ) + BEGIN + SELECT * from test_result_attachments + WHERE (request_test_result_id = '' OR test_result_id=request_test_result_id) + AND (request_test_run_id = '' OR test_run_id=request_test_run_id) + AND (request_id = '' OR id=request_id) + AND (request_project_id = '' OR project_id=request_project_id) + AND (request_test_id = '' OR test_result_attachments.test_result_id in (select id from test_results where test_results.test_id=request_test_id)); + END + + + + + From 2b66b72c3e9bcdb070ab678aa5d81c25418382fb Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Sun, 11 Aug 2024 14:54:20 +0300 Subject: [PATCH 11/18] add search by test_id --- .../java/main/model/db/dao/project/TestResultAttachmentDao.java | 2 +- src/main/resources/db_changelog/changelog.xml | 1 + src/main/resources/db_changelog/db.changelog-1.5.2.xml | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/main/model/db/dao/project/TestResultAttachmentDao.java b/src/main/java/main/model/db/dao/project/TestResultAttachmentDao.java index 0e962a9..1890469 100644 --- a/src/main/java/main/model/db/dao/project/TestResultAttachmentDao.java +++ b/src/main/java/main/model/db/dao/project/TestResultAttachmentDao.java @@ -6,7 +6,7 @@ public class TestResultAttachmentDao extends DAO { public TestResultAttachmentDao() { super(TestResultAttachmentDto.class); - select = "{call SELECT_TEST_RESULT_ATTACH(?,?,?,?)}"; + select = "{call SELECT_TEST_RESULT_ATTACH(?,?,?,?,?)}"; insert = "{call INSERT_TEST_RESULT_ATTACH(?,?,?)}"; remove = "{call REMOVE_TEST_RESULT_ATTACH(?)}"; } diff --git a/src/main/resources/db_changelog/changelog.xml b/src/main/resources/db_changelog/changelog.xml index 31fdd41..1f49e3f 100644 --- a/src/main/resources/db_changelog/changelog.xml +++ b/src/main/resources/db_changelog/changelog.xml @@ -39,4 +39,5 @@ + diff --git a/src/main/resources/db_changelog/db.changelog-1.5.2.xml b/src/main/resources/db_changelog/db.changelog-1.5.2.xml index 18c6608..8543d46 100644 --- a/src/main/resources/db_changelog/db.changelog-1.5.2.xml +++ b/src/main/resources/db_changelog/db.changelog-1.5.2.xml @@ -11,7 +11,7 @@ # - create procedure SELECT_TEST_RESULT_ATTACH2( + create procedure SELECT_TEST_RESULT_ATTACH( In request_id varchar(11), In request_test_result_id varchar(11), In request_test_run_id varchar(11), From d1c7fb317d724d83d6676f4a4b5b0afb38e8eb16 Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Sun, 11 Aug 2024 15:37:42 +0300 Subject: [PATCH 12/18] add search by test_id --- src/main/java/main/controllers/Project/ResultController.java | 3 +++ src/main/java/main/exceptions/AqualitySQLException.java | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index 58179ba..1d0378e 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -225,6 +225,9 @@ private List fillResults(List results, TestResultD TestResultAttachmentDto testResultAttachmentTemplate = new TestResultAttachmentDto(); testResultAttachmentTemplate.setProject_id(projectId); if(searchTemplate != null) { + System.out.println("Test run id = " + searchTemplate.getTest_run_id()); + System.out.println("Test id = " + searchTemplate.getTest_id()); + // System.out.println("type = " + ); testResultAttachmentTemplate.setTest_run_id(searchTemplate.getTest_run_id()); testResultAttachmentTemplate.setTest_id(searchTemplate.getTest_id()); } diff --git a/src/main/java/main/exceptions/AqualitySQLException.java b/src/main/java/main/exceptions/AqualitySQLException.java index e31f8eb..fcc302f 100644 --- a/src/main/java/main/exceptions/AqualitySQLException.java +++ b/src/main/java/main/exceptions/AqualitySQLException.java @@ -11,6 +11,8 @@ public AqualitySQLException(SQLException exception) { private static String getErrorMessage(SQLException exception){ + System.out.println(exception.getMessage()); + System.out.println(exception); switch (exception.getSQLState()){ case "45000": return exception.getMessage(); From 10bcd3a7ae6890795091e20e85a8e2027e618891 Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Sun, 11 Aug 2024 16:06:29 +0300 Subject: [PATCH 13/18] add filling attachments by test_result_id --- .../controllers/Project/ResultController.java | 42 +------------------ 1 file changed, 1 insertion(+), 41 deletions(-) diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index 1d0378e..b1b2f2d 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -7,7 +7,6 @@ import main.model.db.dao.project.TestResultAttachmentDao; import main.model.db.dao.project.TestResultDao; import main.model.db.dao.project.TestResultStatDao; -import main.model.dto.AttachmentDto; import main.model.dto.project.*; import main.model.dto.settings.UserDto; import main.utils.RegexpUtil; @@ -188,10 +187,6 @@ private List fillResults(List results, TestResultD System.out.println("Final results count = " + (long) finalResults.size()); -// Map finalResultsMap = new HashMap<>(); -// for(FinalResultDto resultDto : finalResults) { -// finalResultsMap.put(resultDto.getId(), resultDto); -// } IssueDto issueDto = new IssueDto(); issueDto.setProject_id(projectId); @@ -199,11 +194,6 @@ private List fillResults(List results, TestResultD System.out.println("Count issues = "+ (long) issues.size()); -// Map issuesMap = new HashMap<>(); -// for(IssueDto issueDto2 : issues) { -// issuesMap.put(issueDto2.getId(), issueDto2); -// } - long start2 = System.currentTimeMillis(); System.out.println("get issue = " + (start2-start1)); @@ -213,46 +203,20 @@ private List fillResults(List results, TestResultD System.out.println("Count tests = " + (long) tests.size()); -// Map testsMap = new HashMap<>(); -// for(TestDto testDto : tests) { -// testsMap.put(testDto.getId(), testDto); -// } - - - long start3 = System.currentTimeMillis(); System.out.println("get test template(project_id) = " + (start3-start2)); TestResultAttachmentDto testResultAttachmentTemplate = new TestResultAttachmentDto(); testResultAttachmentTemplate.setProject_id(projectId); if(searchTemplate != null) { - System.out.println("Test run id = " + searchTemplate.getTest_run_id()); - System.out.println("Test id = " + searchTemplate.getTest_id()); - // System.out.println("type = " + ); testResultAttachmentTemplate.setTest_run_id(searchTemplate.getTest_run_id()); testResultAttachmentTemplate.setTest_id(searchTemplate.getTest_id()); + testResultAttachmentTemplate.setTest_result_id(searchTemplate.getId()); } - // testResultAttachmentTemplate.set List testResultAttachments = testResultAttachmentController .get(testResultAttachmentTemplate); System.out.println("Test result attachments = " + testResultAttachments.size()); - // Map> attachmentsMap = new HashMap<>(); - - -// for(TestResultAttachmentDto attachmentDto : testResultAttachments) { -// if(!attachmentsMap.containsKey(attachmentDto.getTest_result_id())) { -// List res = new ArrayList<>(); -// res.add(attachmentDto); -// attachmentsMap.put(attachmentDto.getTest_result_id(), res); -// } -// else { -// attachmentsMap.get(attachmentDto.getTest_result_id()).add(attachmentDto); -// } -// -// -// } - long start4 = System.currentTimeMillis(); System.out.println("get test attachments = " + (start4-start3)); ProjectUserDto projectUserDto = new ProjectUserDto(); @@ -282,19 +246,15 @@ private void fillResult(TestResultDto result, List< FinalResultDto> finalResults result.setFinal_result(finalResults.stream().filter(x -> x.getId().equals(result.getFinal_result_id())) .findFirst().orElse(null)); - //result.setFinal_result(finalResults.get(result.getFinal_result_id())); result.setTest(tests.stream().filter(x -> x.getId().equals(result.getTest_id())).findFirst().orElse(null)); - // result.setTest(tests.get(result.getTest_id())); if (result.getIssue_id() != null) { result.setIssue( issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null)); - // result.setIssue(issues.get(result.getIssue_id())); } result.setAttachments(attachments.stream().filter(x -> x.getTest_result_id().equals(result.getId())) .collect(Collectors.toList())); - // result.setAttachments(attachments.get(result.getId())); } private void fillResultSteps(TestResultDto result) throws AqualityException { From 6686c8e6fe26a1919ede3a85b881dc4c0b5cd93b Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Sun, 11 Aug 2024 16:55:04 +0300 Subject: [PATCH 14/18] decrease the response time for test result by test_id and by test_result_id --- CHANGELOG.md | 4 +++ .../controllers/Project/ResultController.java | 26 ++----------------- .../main/exceptions/AqualitySQLException.java | 3 --- src/main/java/main/model/db/dao/DAO.java | 7 +---- .../java/main/view/Project/TestResult.java | 5 ---- 5 files changed, 7 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e99f54..79503cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # CHANGELOG +## 1.5.2 (2024-08-11) +Performance: +- Decrease time of the response for test result by test_id +- Decrease time of the response for test result by test_result_id ## 1.5.1 (2024-04-12) Bugfixes: diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index b1b2f2d..129dbbb 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -181,57 +181,35 @@ private void createPendingStepResults(TestResultDto template) throws AqualityExc private List fillResults(List results, TestResultDto searchTemplate) throws AqualityException { if (!results.isEmpty()) { - long start1 = System.currentTimeMillis(); int projectId = results.get(0).getProject_id(); List finalResults = finalResultController.get(new FinalResultDto()); - - System.out.println("Final results count = " + (long) finalResults.size()); - - IssueDto issueDto = new IssueDto(); issueDto.setProject_id(projectId); List issues = issueController.get(issueDto); - - System.out.println("Count issues = "+ (long) issues.size()); - - - long start2 = System.currentTimeMillis(); - System.out.println("get issue = " + (start2-start1)); TestDto testTemplate = new TestDto(); testTemplate.setProject_id(projectId); List tests = testController.get(testTemplate); - System.out.println("Count tests = " + (long) tests.size()); - - long start3 = System.currentTimeMillis(); - System.out.println("get test template(project_id) = " + (start3-start2)); TestResultAttachmentDto testResultAttachmentTemplate = new TestResultAttachmentDto(); testResultAttachmentTemplate.setProject_id(projectId); + if(searchTemplate != null) { testResultAttachmentTemplate.setTest_run_id(searchTemplate.getTest_run_id()); testResultAttachmentTemplate.setTest_id(searchTemplate.getTest_id()); testResultAttachmentTemplate.setTest_result_id(searchTemplate.getId()); } + List testResultAttachments = testResultAttachmentController .get(testResultAttachmentTemplate); - System.out.println("Test result attachments = " + testResultAttachments.size()); - - long start4 = System.currentTimeMillis(); - System.out.println("get test attachments = " + (start4-start3)); ProjectUserDto projectUserDto = new ProjectUserDto(); projectUserDto.setProject_id(projectId); boolean isStepsEnabled = projectController.isStepsEnabled(projectId); - long start5 = System.currentTimeMillis(); - System.out.println("Count results = "+ (long) results.size()); for (TestResultDto result : results) { fillResult(result, finalResults, tests, issues, testResultAttachments, isStepsEnabled); } - long start6 = System.currentTimeMillis(); - System.out.println("Exact filling results " + (start6-start5)); - System.out.println("Whole filling function " + (start6-start1)); } return results; diff --git a/src/main/java/main/exceptions/AqualitySQLException.java b/src/main/java/main/exceptions/AqualitySQLException.java index fcc302f..564410c 100644 --- a/src/main/java/main/exceptions/AqualitySQLException.java +++ b/src/main/java/main/exceptions/AqualitySQLException.java @@ -9,10 +9,7 @@ public AqualitySQLException(SQLException exception) { this.responseCode = getErrorCode(exception.getSQLState()); } - private static String getErrorMessage(SQLException exception){ - System.out.println(exception.getMessage()); - System.out.println(exception); switch (exception.getSQLState()){ case "45000": return exception.getMessage(); diff --git a/src/main/java/main/model/db/dao/DAO.java b/src/main/java/main/model/db/dao/DAO.java index 15c7c8e..bc91a3b 100644 --- a/src/main/java/main/model/db/dao/DAO.java +++ b/src/main/java/main/model/db/dao/DAO.java @@ -78,14 +78,9 @@ public List getAll() throws AqualityException { * @return List of entities */ public List searchAll(T entity) throws AqualityException { - long start1 = System.currentTimeMillis(); List> parameters = entity.getSearchParameters(); checkSelectProcedure(); - - List result = dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString()); - long start2 = System.currentTimeMillis(); - System.out.println("Search all by entity " + entity.getClass().getName() + "=" + (start2-start1)); - return result; + return dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString()); } /** diff --git a/src/main/java/main/view/Project/TestResult.java b/src/main/java/main/view/Project/TestResult.java index 79e30cb..50d31d5 100644 --- a/src/main/java/main/view/Project/TestResult.java +++ b/src/main/java/main/view/Project/TestResult.java @@ -37,11 +37,6 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) { setPostResponseHeaders(resp); setEncoding(resp); try { - System.out.println(); - System.out.println(); - System.out.println(); - System.out.println("request = " + req.getRequestURL()); - System.out.println("query = " + req.getQueryString()); Session session = createSession(req); TestResultDto testResultTemplate = new TestResultDto(); testResultTemplate.getSearchTemplateFromRequestParameters(req); From 70979795f4db9f7ea995e4c542e387c9276a54ec Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Sun, 11 Aug 2024 17:03:38 +0300 Subject: [PATCH 15/18] refactor code --- pom.xml | 2 +- src/main/java/main/controllers/Project/ResultController.java | 4 +--- src/main/java/main/model/db/dao/DAO.java | 2 +- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 3f52ae1..c836db8 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ unifi_reporting_api api war - 1.5.1 + 1.5.2 UTF-8 diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index 129dbbb..c08489f 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -215,7 +215,7 @@ private List fillResults(List results, TestResultD return results; } - private void fillResult(TestResultDto result, List< FinalResultDto> finalResults, List tests, + private void fillResult(TestResultDto result, List finalResults, List tests, List issues, List attachments, boolean isStepsEnabled) throws AqualityException { if (isStepsEnabled) { @@ -224,9 +224,7 @@ private void fillResult(TestResultDto result, List< FinalResultDto> finalResults result.setFinal_result(finalResults.stream().filter(x -> x.getId().equals(result.getFinal_result_id())) .findFirst().orElse(null)); - result.setTest(tests.stream().filter(x -> x.getId().equals(result.getTest_id())).findFirst().orElse(null)); - if (result.getIssue_id() != null) { result.setIssue( issues.stream().filter(x -> x.getId().equals(result.getIssue_id())).findFirst().orElse(null)); diff --git a/src/main/java/main/model/db/dao/DAO.java b/src/main/java/main/model/db/dao/DAO.java index bc91a3b..607633b 100644 --- a/src/main/java/main/model/db/dao/DAO.java +++ b/src/main/java/main/model/db/dao/DAO.java @@ -80,7 +80,7 @@ public List getAll() throws AqualityException { public List searchAll(T entity) throws AqualityException { List> parameters = entity.getSearchParameters(); checkSelectProcedure(); - return dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString()); + return dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString()); } /** From 05d6d73eb3badd86c76fc7ed416e52af277e5087 Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Mon, 12 Aug 2024 14:05:50 +0300 Subject: [PATCH 16/18] refactor code --- .../main/controllers/Project/ResultController.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index c08489f..bfee94f 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -106,7 +106,7 @@ public boolean createMultiple(List listOfAttachments) t public List getLatestResultsByMilestone(Integer projectId, Integer milestoneId) throws AqualityException { if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer()) { - return fillResults(testResultDao.selectLatestResultsByMilestone(milestoneId), null); + return fillResults(testResultDao.selectLatestResultsByMilestone(milestoneId), new TestResultDto()); } else { throw new AqualityPermissionsException("Account is not allowed to view Test Results", baseUser); } @@ -192,12 +192,9 @@ private List fillResults(List results, TestResultD TestResultAttachmentDto testResultAttachmentTemplate = new TestResultAttachmentDto(); testResultAttachmentTemplate.setProject_id(projectId); - - if(searchTemplate != null) { - testResultAttachmentTemplate.setTest_run_id(searchTemplate.getTest_run_id()); - testResultAttachmentTemplate.setTest_id(searchTemplate.getTest_id()); - testResultAttachmentTemplate.setTest_result_id(searchTemplate.getId()); - } + testResultAttachmentTemplate.setTest_run_id(searchTemplate.getTest_run_id()); + testResultAttachmentTemplate.setTest_id(searchTemplate.getTest_id()); + testResultAttachmentTemplate.setTest_result_id(searchTemplate.getId()); List testResultAttachments = testResultAttachmentController .get(testResultAttachmentTemplate); From 7bdc0ec5dfd147e9b8a6a6f1f273151a91cff332 Mon Sep 17 00:00:00 2001 From: EvgenySafronov <32324614+EvgenySafronov@users.noreply.github.com> Date: Tue, 9 Apr 2024 17:54:18 +0300 Subject: [PATCH 17/18] Improved performance for endpoints: /public/test/create-or-update, /public/test/result/start, /public/test/result/finish; Fixed INSERT_TEST_RESULT final_result_id comparison --- CHANGELOG.md | 7 + pom.xml | 2 +- .../controllers/Project/ResultController.java | 102 +++++++++------ .../controllers/Project/TestController.java | 120 ++++++++++-------- src/main/java/main/model/db/dao/DAO.java | 2 +- .../main/model/db/dao/project/TestDao.java | 5 +- .../model/db/dao/project/TestResultDao.java | 45 ++++++- src/main/java/main/utils/DateUtils.java | 5 + .../PublicTestResultFinishServlet.java | 23 +--- .../PublicTestResultStartServlet.java | 10 +- .../view/publicApi/PublicTestServlet.java | 6 +- src/main/resources/db_changelog/changelog.xml | 1 + .../db_changelog/db.changelog-1.5.1.xml | 115 +++++++++++++++++ .../dao/project/TestResultDaoTest.java | 8 ++ 14 files changed, 324 insertions(+), 127 deletions(-) create mode 100644 src/main/resources/db_changelog/db.changelog-1.5.1.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 2aef5ee..5e99f54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## 1.5.1 (2024-04-12) +Bugfixes: +- Fixed 500 (Entity is locked) issue +- Fixed SQL connections leak +- Improved /public/test/create-or-update, /public/test/result/start, /public/test/result/finish performance +- Fixed INSERT_TEST_RESULT final_result_id comparison + ## 1.5.0 (2023-07-28) - Added `/issues/assign` endpoint, which allows to check previously created Test Runs and Test Results and assign Issues to them if there's a RegEx match diff --git a/pom.xml b/pom.xml index e232c60..3f52ae1 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ unifi_reporting_api api war - 1.5.0 + 1.5.1 UTF-8 diff --git a/src/main/java/main/controllers/Project/ResultController.java b/src/main/java/main/controllers/Project/ResultController.java index 09d52aa..87e014f 100644 --- a/src/main/java/main/controllers/Project/ResultController.java +++ b/src/main/java/main/controllers/Project/ResultController.java @@ -14,6 +14,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; public class ResultController extends BaseController { @@ -56,15 +57,29 @@ public TestResultDto create(TestResultDto template) throws AqualityException { } } - @Override - public List get(TestResultDto template) throws AqualityException { - if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(template.getProject_id()).isViewer()) { - return fillResults(testResultDao.searchAll(template)); + public TestResultDto updateWithFinalResultIdAndFailReason(TestResultDto testResult) throws AqualityException { + if (baseUser.isManager() || baseUser.getProjectUser(testResult.getProject_id()).isEditor()) { + return testResultDao.updateFinalResultIdAndFailReason( + testResult.getId(), + testResult.getFinal_result_id(), + testResult.getFail_reason(), + testResult.getFinish_date()); } else { - throw new AqualityPermissionsException("Account is not allowed to view Test Results", baseUser); + throw new AqualityPermissionsException("Account is not allowed to update Test Result", baseUser); } } + @Override + public List get(TestResultDto template) throws AqualityException { + checkReadPermissions(template.getProject_id()); + return fillResults(testResultDao.searchAll(template)); + } + + public List getRaw(TestResultDto template) throws AqualityException { + checkReadPermissions(template.getProject_id()); + return testResultDao.searchAll(template); + } + @Override public boolean delete(TestResultDto template) throws AqualityException { if (baseUser.isManager() || baseUser.getProjectUser(template.getProject_id()).isEditor()) { @@ -76,8 +91,11 @@ public boolean delete(TestResultDto template) throws AqualityException { public List getOnlyFailedResults(TestResultDto testResultTemplate) throws AqualityException { List testResults = this.get(testResultTemplate); - return testResults.stream().filter(x -> x.getFinal_result_id() != FAILED_STATUS_ID && x.getFail_reason() != null - && x.getIssue_id() == null).collect(Collectors.toList()); + return testResults.stream() + .filter(x -> !Objects.equals(x.getFinal_result_id(), FAILED_STATUS_ID) && + x.getFail_reason() != null && + x.getIssue_id() == null) + .collect(Collectors.toList()); } public boolean createMultiple(List listOfAttachments) throws AqualityException { @@ -98,7 +116,7 @@ public List getLatestResultsByMilestone(Integer projectId, Intege } public boolean updateMultipleTestResults(List entities) throws AqualityException { - if (entities.size() > 0 + if (!entities.isEmpty() && (baseUser.isManager() || baseUser.getProjectUser(entities.get(0).getProject_id()).isEditor())) { return testResultDao.updateMultiply(entities); } else { @@ -114,6 +132,38 @@ public List get(TestResultStatDto template) throws AqualityEx } } + public Map matchIssues(Integer testResultId) throws AqualityException { + TestResultDto testResultTemplate = new TestResultDto(); + testResultTemplate.setId(testResultId); + List testResults = this.getOnlyFailedResults(testResultTemplate); + if (testResults.isEmpty()) { + throw new AqualityParametersException("No test result found to update. Wrong ID might be provided."); + } + IssueDto issueTemplate = new IssueDto(); + issueTemplate.setProject_id(testResults.get(0).getProject_id()); + List issues = issueController.get(issueTemplate); + Integer count = assignIssuesToResults(issues, testResults); + Map results = new HashMap<>(); + results.put("Issues assigned", count); + return results; + } + + public Integer assignIssuesToResults(List issues, List testResults) + throws AqualityException { + Integer count = 0; + for (TestResultDto testResult : testResults) { + for (IssueDto issue : issues) { + if (issue.getExpression() != null + && RegexpUtil.match(testResult.getFail_reason(), issue.getExpression())) { + testResult.setIssue_id(issue.getId()); + this.create(testResult); + count++; + } + } + } + return count; + } + private void createPendingStepResults(TestResultDto template) throws AqualityException { Step2TestDto step2TestTemplate = new Step2TestDto(); step2TestTemplate.setProject_id(template.getProject_id()); @@ -133,7 +183,7 @@ private void createPendingStepResults(TestResultDto template) throws AqualityExc private List fillResults(List results) throws AqualityException { - if (results.size() > 0) { + if (!results.isEmpty()) { int projectId = results.get(0).getProject_id(); List finalResults = finalResultController.get(new FinalResultDto()); IssueDto issueDto = new IssueDto(); @@ -163,7 +213,7 @@ private List fillResults(List results) throws Aqua } private void fillResult(TestResultDto result, List finalResults, List tests, - List issues, List attachments, boolean isStepsEnabled) + List issues, List attachments, boolean isStepsEnabled) throws AqualityException { if (isStepsEnabled) { fillResultSteps(result); @@ -187,35 +237,9 @@ private void fillResultSteps(TestResultDto result) throws AqualityException { result.setSteps(stepResultController.get(stepResultTemplate)); } - public Map matchIssues(Integer testResultId) throws AqualityException { - TestResultDto testResultTemplate = new TestResultDto(); - testResultTemplate.setId(testResultId); - List testResults = this.getOnlyFailedResults(testResultTemplate); - if (testResults.isEmpty()) { - throw new AqualityParametersException("No test result found to update. Wrong ID might be provided."); - } - IssueDto issueTemplate = new IssueDto(); - issueTemplate.setProject_id(testResults.get(0).getProject_id()); - List issues = issueController.get(issueTemplate); - Integer count = assignIssuesToResults(issues, testResults); - Map results = new HashMap<>(); - results.put("Issues assigned", count); - return results; - } - - public Integer assignIssuesToResults(List issues, List testResults) - throws AqualityException { - Integer count = 0; - for (TestResultDto testResult : testResults) { - for (IssueDto issue : issues) { - if (issue.getExpression() != null - && RegexpUtil.match(testResult.getFail_reason(), issue.getExpression())) { - testResult.setIssue_id(issue.getId()); - this.create(testResult); - count++; - } - } + private void checkReadPermissions(Integer projectId) throws AqualityException { + if (!(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer())) { + throw new AqualityPermissionsException("Account is not allowed to view Test Results", baseUser); } - return count; } } diff --git a/src/main/java/main/controllers/Project/TestController.java b/src/main/java/main/controllers/Project/TestController.java index ce7d292..fdc2ac9 100644 --- a/src/main/java/main/controllers/Project/TestController.java +++ b/src/main/java/main/controllers/Project/TestController.java @@ -30,45 +30,20 @@ public TestController(UserDto user) { } public TestDto create(TestDto template, boolean updateSuites) throws AqualityException { - if (baseUser.isManager() || baseUser.getProjectUser(template.getProject_id()).isEditor()) { - TestDto test = testDao.create(template); - if (updateSuites) { - test.setSuites(template.getSuites()); - updateSuites(test); - test = get(test).get(0); - } - return test; - } else { - throw new AqualityPermissionsException("Account is not allowed to create Test", baseUser); + checkCreatePermissions(template.getProject_id()); + TestDto test = testDao.create(template); + if (updateSuites) { + test.setSuites(template.getSuites()); + updateSuites(test); + test = get(test).get(0); } + return test; } public TestDto createOrUpdate(TestDto test) throws AqualityException { - TestDto searchTemplate = new TestDto(); - searchTemplate.setId(test.getId()); - searchTemplate.setProject_id(test.getProject_id()); - searchTemplate.setName(test.getName()); - - List existingTests = get(searchTemplate); - - if(!existingTests.isEmpty()) { - TestDto existingTest = existingTests.get(0); - if(existingTest.getSuites() != null) { - TestSuiteDto existingSuite = existingTest.getSuites().stream() - .filter(suite -> suite.getId().equals(test.getSuites().get(0).getId())) - .findFirst().orElse(null); - if(existingSuite == null) { - List listOfSuites = existingTest.getSuites(); - listOfSuites.add(test.getSuites().get(0)); - existingTest.setSuites(listOfSuites); - } - }else { - existingTest.setSuites(test.getSuites()); - } - return create(existingTest, true); - } else { - return create(test, true); - } + checkCreatePermissions(test.getProject_id()); + TestDto rawTest = getOrCreateRawTest(test); + return updateTestSuites(rawTest, test.getSuites().get(0).getId()); } @Override @@ -77,22 +52,15 @@ public TestDto create(TestDto template) throws AqualityException { } public List get(TestDto template) throws AqualityException { - if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(template.getProject_id()).isViewer()) { - return fillTests(testDao.searchAll(template)); - } else { - throw new AqualityPermissionsException("Account is not allowed to view Tests", baseUser); - } + checkReadPermissions(template.getProject_id()); + return fillTests(testDao.searchAll(template)); } public List get(Integer issueId, Integer projectId) throws AqualityException { - if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer()) { - return fillTests(testDao.getTestsAffectedByIssue(issueId)); - } else { - throw new AqualityPermissionsException("Account is not allowed to view Tests", baseUser); - } + checkReadPermissions(projectId); + return fillTests(testDao.getTestsAffectedByIssue(issueId)); } - @Override public boolean delete(TestDto template) throws AqualityException { if (baseUser.isManager() || baseUser.getProjectUser(template.getProject_id()).isEditor()) { @@ -103,7 +71,7 @@ public boolean delete(TestDto template) throws AqualityException { } public void updateMultipleTests(List entities) throws AqualityException { - if (entities.size() > 0 && (baseUser.isManager() || baseUser.getProjectUser(entities.get(0).getProject_id()).isEditor())) { + if (!entities.isEmpty() && (baseUser.isManager() || baseUser.getProjectUser(entities.get(0).getProject_id()).isEditor())) { for (TestDto test : entities) { updateSuites(test); } @@ -157,14 +125,12 @@ protected List getResultsToMove(TestDto from, TestDto to) { private List fillTests(List tests) throws AqualityException { List filledTests = new ArrayList<>(); - if (tests.size() > 0) { + if (!tests.isEmpty()) { Integer projectId = tests.get(0).getProject_id(); ProjectUserDto projectUserDto = new ProjectUserDto(); projectUserDto.setProject_id(tests.get(0).getProject_id()); List projectUsers = projectUserController.get(projectUserDto); - TestSuiteDto testSuiteDto = new TestSuiteDto(); - testSuiteDto.setProject_id(projectId); - List testSuites = suiteDao.searchAll(testSuiteDto); + List testSuites = getProjectTestSuites(projectId); ProjectDto projectDto = new ProjectDto(); projectDto.setId(tests.get(0).getProject_id()); @@ -189,7 +155,7 @@ private void updateSuites(TestDto test) throws AqualityException { Test2SuiteDto test2SuiteDto = new Test2SuiteDto(); test2SuiteDto.setTest_id(test.getId()); List oldSuites = test2SuiteController.get(test2SuiteDto); - if (test.getSuites() != null && test.getSuites().size() > 0) { + if (test.getSuites() != null && !test.getSuites().isEmpty()) { List suites = test.getSuites(); for (TestSuiteDto newSuite : suites) { Test2SuiteDto alreadyExists = oldSuites.stream().filter(x -> Objects.equals(x.getSuite_id(), newSuite.getId())).findAny().orElse(null); @@ -204,10 +170,58 @@ private void updateSuites(TestDto test) throws AqualityException { } } - if (oldSuites.size() > 0) { + if (!oldSuites.isEmpty()) { for (Test2SuiteDto oldSuite : oldSuites) { test2SuiteController.delete(oldSuite, test.getProject_id()); } } } + + private void checkReadPermissions(Integer projectId) throws AqualityException { + if (!(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer())) { + throw new AqualityPermissionsException("Account is not allowed to view Tests", baseUser); + } + } + + private void checkCreatePermissions(Integer projectId) throws AqualityException { + if (!(baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isEditor())) { + throw new AqualityPermissionsException("Account is not allowed to create Test", baseUser); + } + } + + private List getProjectTestSuites(Integer projectId) throws AqualityException { + TestSuiteDto testSuiteDto = new TestSuiteDto(); + testSuiteDto.setProject_id(projectId); + return suiteDao.searchAll(testSuiteDto); + } + + private TestDto getOrCreateRawTest(TestDto test) throws AqualityException { + TestDto searchTemplate = new TestDto(); + searchTemplate.setId(test.getId()); + searchTemplate.setProject_id(test.getProject_id()); + searchTemplate.setName(test.getName()); + + List existingTests = testDao.searchAll(searchTemplate); + + return existingTests.isEmpty() ? testDao.create(test) : existingTests.get(0); + } + + private TestDto updateTestSuites(TestDto testDto, Integer testSuiteId) throws AqualityException { + Integer projectId = testDto.getProject_id(); + Test2SuiteDto test2SuiteDto = new Test2SuiteDto(); + test2SuiteDto.setProject_id(projectId); + test2SuiteDto.setTest_id(testDto.getId()); + + List existingTest2Suites = test2SuiteController.get(test2SuiteDto); + if (existingTest2Suites.stream().noneMatch(test2Suite -> test2Suite.getSuite_id().equals(testSuiteId))) { + test2SuiteDto.setSuite_id(testSuiteId); + Test2SuiteDto createdTest2Suite = test2SuiteController.create(test2SuiteDto, projectId); + existingTest2Suites.add(createdTest2Suite); + } + + List projectTestSuites = getProjectTestSuites(projectId); + testDto.setSuites(test2SuiteController.convertToSuites(existingTest2Suites, projectTestSuites)); + + return testDto; + } } diff --git a/src/main/java/main/model/db/dao/DAO.java b/src/main/java/main/model/db/dao/DAO.java index b1dfbbe..607633b 100644 --- a/src/main/java/main/model/db/dao/DAO.java +++ b/src/main/java/main/model/db/dao/DAO.java @@ -229,7 +229,7 @@ protected JSONArray CallStoredProcedure(String sql, List> p return json; } - private T getSingleResult(List allResults, Integer id) throws AqualityException { + protected T getSingleResult(List allResults, Integer id) throws AqualityException { if (!allResults.isEmpty()) { return allResults.get(0); } else { diff --git a/src/main/java/main/model/db/dao/project/TestDao.java b/src/main/java/main/model/db/dao/project/TestDao.java index e71501c..9afe027 100644 --- a/src/main/java/main/model/db/dao/project/TestDao.java +++ b/src/main/java/main/model/db/dao/project/TestDao.java @@ -9,17 +9,18 @@ import java.util.List; public class TestDao extends DAO { + private static final String TESTS_AFFECTED_BY_ISSUE_QUERY = "{call SELECT_ISSUE_TESTS(?)}"; + public TestDao() { super(TestDto.class); select = "{call SELECT_TEST(?,?,?,?,?,?)}"; insert = "{call INSERT_TEST(?,?,?,?,?,?)}"; remove = "{call REMOVE_TEST(?)}"; } - private String testsAffectedByIssueQuery = "{call SELECT_ISSUE_TESTS(?)}"; public List getTestsAffectedByIssue(Integer issueId) throws AqualityException { List> parameters = new ArrayList<>(); parameters.add(new Pair<>("request_issue_id", issueId.toString())); - return dtoMapper.mapObjects(CallStoredProcedure(testsAffectedByIssueQuery, parameters).toString()); + return dtoMapper.mapObjects(CallStoredProcedure(TESTS_AFFECTED_BY_ISSUE_QUERY, parameters).toString()); } } diff --git a/src/main/java/main/model/db/dao/project/TestResultDao.java b/src/main/java/main/model/db/dao/project/TestResultDao.java index a9fc9d7..9df415d 100644 --- a/src/main/java/main/model/db/dao/project/TestResultDao.java +++ b/src/main/java/main/model/db/dao/project/TestResultDao.java @@ -4,12 +4,20 @@ import main.exceptions.AqualityException; import main.model.db.dao.DAO; import main.model.dto.project.TestResultDto; +import main.utils.DateUtils; +import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.Date; import java.util.List; public class TestResultDao extends DAO { + private static final String SYNC_SUITE_QUERY = "{call SELECT_LEGACY_RESULTS(?,?)}"; + private static final String SELECT_LATEST_RESULTS_BY_MILESTONE = "{call SELECT_LATEST_RESULTS_BY_MILESTONE(?)}"; + private static final String FINISH_TEST_RESULT_QUERY = + "{call UPDATE_TEST_RESULT_WITH_FINAL_RESULT_ID_AND_FAIL_REASON(?,?,?,?)}"; + public TestResultDao() { super(TestResultDto.class); select = "{call SELECT_TEST_RESULT(?,?,?,?,?,?,?,?,?,?)}"; @@ -17,26 +25,49 @@ public TestResultDao() { remove = "{call REMOVE_TEST_RESULT(?)}"; } - - private String syncSuiteQuery = "{call SELECT_LEGACY_RESULTS(?,?)}"; - private String latestResultsByMilestone = "{call SELECT_LATEST_RESULTS_BY_MILESTONE(?)}"; - /** * @param suiteId suite id for search * @param testId test id for search * @return all not executed results till first executed * @throws AqualityException */ - public List selectSuiteLegacyResults(@NotNull Integer suiteId, @NotNull Integer testId) throws AqualityException { + public List selectSuiteLegacyResults( + @NotNull Integer suiteId, + @NotNull Integer testId) throws AqualityException { List> parameters = new ArrayList<>(); parameters.add(new Pair<>("request_test_suite_id", suiteId.toString())); parameters.add(new Pair<>("request_test_id", testId.toString())); - return dtoMapper.mapObjects(CallStoredProcedure(syncSuiteQuery, parameters).toString()); + return dtoMapper.mapObjects(CallStoredProcedure(SYNC_SUITE_QUERY, parameters).toString()); } public List selectLatestResultsByMilestone(@NotNull Integer milestoneId) throws AqualityException { List> parameters = new ArrayList<>(); parameters.add(new Pair<>("request_milestone_id", milestoneId.toString())); - return dtoMapper.mapObjects(CallStoredProcedure(latestResultsByMilestone, parameters).toString()); + return dtoMapper.mapObjects(CallStoredProcedure(SELECT_LATEST_RESULTS_BY_MILESTONE, parameters).toString()); + } + + /** + * @param id test result id + * @param finalResultId ID of final result - Failed: 1, Passed: 2, Not Executed: 3, Pending: 5 + * @param failReason fail reason message + * @param finishDate test finish date + * @return updated test result + */ + public TestResultDto updateFinalResultIdAndFailReason( + @NotNull Integer id, + @NotNull Integer finalResultId, + String failReason, + Date finishDate) throws AqualityException { + List> parameters = new ArrayList<>(); + parameters.add(new Pair<>("request_id", id.toString())); + parameters.add(new Pair<>("request_final_result_id", finalResultId.toString())); + parameters.add(new Pair<>("request_fail_reason", failReason)); + parameters.add(new Pair<>( + "request_finish_date", + finishDate == null ? StringUtils.EMPTY : String.valueOf(DateUtils.toUnixTime(finishDate)))); + + return getSingleResult( + dtoMapper.mapObjects(CallStoredProcedure(FINISH_TEST_RESULT_QUERY, parameters).toString()), + id); } } diff --git a/src/main/java/main/utils/DateUtils.java b/src/main/java/main/utils/DateUtils.java index ca70bb7..6c147ce 100644 --- a/src/main/java/main/utils/DateUtils.java +++ b/src/main/java/main/utils/DateUtils.java @@ -7,6 +7,7 @@ import java.util.Date; public class DateUtils { + private static final int MILLISECONDS_PER_SECOND = 1000; public Date addMS(Date date, Integer ms) { @@ -64,4 +65,8 @@ public Date fromyyyyMMdd(String date){ return null; } } + + public static Long toUnixTime(Date date) { + return date != null ? date.getTime() / MILLISECONDS_PER_SECOND : null; + } } diff --git a/src/main/java/main/view/publicApi/PublicTestResultFinishServlet.java b/src/main/java/main/view/publicApi/PublicTestResultFinishServlet.java index 2990fb5..c6968e1 100644 --- a/src/main/java/main/view/publicApi/PublicTestResultFinishServlet.java +++ b/src/main/java/main/view/publicApi/PublicTestResultFinishServlet.java @@ -1,6 +1,7 @@ package main.view.publicApi; import main.Session; +import main.controllers.Project.ResultController; import main.exceptions.AqualityParametersException; import main.model.dto.project.TestResultDto; import main.view.BaseServlet; @@ -10,7 +11,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Date; -import java.util.List; @WebServlet("/public/test/result/finish") public class PublicTestResultFinishServlet extends BaseServlet implements IPost { @@ -25,25 +25,12 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp) { TestResultDto testResult = mapper.mapObject(TestResultDto.class, requestedJson); validatePost(testResult); + testResult.setFinish_date(new Date()); - TestResultDto testResultSearchTemplate = new TestResultDto(); - testResultSearchTemplate.setProject_id(testResult.getProject_id()); - testResultSearchTemplate.setId(testResult.getId()); + ResultController testResultController = session.controllerFactory.getHandler(testResult); + TestResultDto updatedTestResult = testResultController.updateWithFinalResultIdAndFailReason(testResult); - List oldResults = session.controllerFactory.getHandler(testResult).get(testResultSearchTemplate); - - if(oldResults.size() > 0) { - TestResultDto currentResult = oldResults.get(0); - currentResult.setFinal_result_id(testResult.getFinal_result_id()); - currentResult.setFail_reason(testResult.getFail_reason()); - currentResult.setFinish_date(new Date()); - - testResult = session.controllerFactory.getHandler(testResult).create(currentResult); - } else { - throw new AqualityParametersException("Test Result with %s id was not found!", testResult.getId()); - } - - setResponseBody(resp, testResult); + setResponseBody(resp, updatedTestResult); } catch (Exception e) { handleException(resp, e); } diff --git a/src/main/java/main/view/publicApi/PublicTestResultStartServlet.java b/src/main/java/main/view/publicApi/PublicTestResultStartServlet.java index 214b0fc..092b51c 100644 --- a/src/main/java/main/view/publicApi/PublicTestResultStartServlet.java +++ b/src/main/java/main/view/publicApi/PublicTestResultStartServlet.java @@ -26,11 +26,13 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) { validateGet(testResult); - List oldResults = session.controllerFactory.getHandler(testResult).get(testResult); + List oldResults = session.controllerFactory.getHandler(testResult).getRaw(testResult); - if(oldResults.size() > 0) { - TestResultDto pending = oldResults.stream().filter(result -> result.getPending().equals(1)).findFirst().orElse(null); - if(pending != null) { + if(!oldResults.isEmpty()) { + TestResultDto pending = oldResults.stream() + .filter(result -> result.getPending().equals(1)) + .findFirst().orElse(null); + if (pending != null) { testResult.setId(pending.getId()); } } diff --git a/src/main/java/main/view/publicApi/PublicTestServlet.java b/src/main/java/main/view/publicApi/PublicTestServlet.java index 8b13a2c..781bf42 100644 --- a/src/main/java/main/view/publicApi/PublicTestServlet.java +++ b/src/main/java/main/view/publicApi/PublicTestServlet.java @@ -41,8 +41,10 @@ private void validatePost(TestDto test) throws AqualityParametersException { throw new AqualityParametersException("You should specify 'id' or/and 'name' suite parameters!"); } - if(test.getSuites() == null || test.getSuites().size() != 1) { - throw new AqualityParametersException("You should specify 'suite' array with one element as {id: suite_id}. So the API will ensure this test is assigned to Suite."); + if(test.getSuites() == null || test.getSuites().size() != 1 || test.getSuites().get(0).getId() == null) { + throw new AqualityParametersException( + "You should specify 'suite' array with one element as {id: suite_id}. " + + "So the API will ensure this test is assigned to Suite."); } } diff --git a/src/main/resources/db_changelog/changelog.xml b/src/main/resources/db_changelog/changelog.xml index 4b5d986..31fdd41 100644 --- a/src/main/resources/db_changelog/changelog.xml +++ b/src/main/resources/db_changelog/changelog.xml @@ -38,4 +38,5 @@ + diff --git a/src/main/resources/db_changelog/db.changelog-1.5.1.xml b/src/main/resources/db_changelog/db.changelog-1.5.1.xml new file mode 100644 index 0000000..ffb324a --- /dev/null +++ b/src/main/resources/db_changelog/db.changelog-1.5.1.xml @@ -0,0 +1,115 @@ + + + + + + DROP procedure IF EXISTS `UPDATE_TEST_RESULT_WITH_FINAL_RESULT_ID_AND_FAIL_REASON`; + + # + + CREATE PROCEDURE `UPDATE_TEST_RESULT_WITH_FINAL_RESULT_ID_AND_FAIL_REASON`( + IN request_id VARCHAR(11), + IN request_final_result_id VARCHAR(11), + IN request_fail_reason mediumtext, + IN request_finish_date VARCHAR(500) + ) + BEGIN + UPDATE test_results + SET + finish_date = If(request_finish_date = '', finish_date, FROM_UNIXTIME(request_finish_date)), + final_result_updated = IF(request_final_result_id != final_result_id, NOW(), final_result_updated), + fail_reason = IF( + request_fail_reason = '', + fail_reason, + IF(request_fail_reason = '$blank', '', request_fail_reason) + ), + final_result_id = request_final_result_id, + updated = NOW() + WHERE id = request_id; + + SELECT * FROM test_results WHERE id = request_id; + END + + + + + + + + + + + + + DROP PROCEDURE IF EXISTS `INSERT_TEST_RESULT`; + + # + CREATE PROCEDURE `INSERT_TEST_RESULT`( + IN request_project_id VARCHAR(10), + IN request_id VARCHAR(10), + IN request_test_id VARCHAR(500), + IN request_final_result_id VARCHAR(500), + IN request_test_run_id VARCHAR(500), + IN request_log longtext, + IN request_debug VARCHAR(1), + IN request_finish_date VARCHAR(500), + IN request_start_date VARCHAR(500), + IN request_final_result_updated VARCHAR(500), + IN request_fail_reason mediumtext, + IN request_issue_id VARCHAR(11) + ) + BEGIN + INSERT INTO test_results ( + project_id, id, test_id, final_result_id, test_run_id, log, debug, start_date, + finish_date, final_result_updated, fail_reason, issue_id + ) + VALUES ( + request_project_id, + IF(request_id='', null, request_id), + request_test_id, + IF(request_final_result_id='',1,request_final_result_id), + IF(request_test_run_id = '', null, request_test_run_id), + IF(request_log = '', null, request_log), + IF(request_debug = '', 0, request_debug), + If(request_start_date = '', null, FROM_UNIXTIME(request_start_date)), + If(request_finish_date = '', null, FROM_UNIXTIME(request_finish_date)), + NOW(), + IF(request_fail_reason = '', null, request_fail_reason), + replace_empty(request_issue_id, null) + ) + ON DUPLICATE KEY UPDATE + test_id=IF(request_test_id = '',test_id,request_test_id), + log = IF(request_log = '', log, IF(request_log = '$blank', '', request_log)), + debug = IF(request_debug = '', debug, request_debug), + start_date = If(request_start_date = '', start_date, FROM_UNIXTIME(request_start_date)), + finish_date = If(request_finish_date = '', finish_date, FROM_UNIXTIME(request_finish_date)), + final_result_updated = IF( + request_final_result_id != final_result_id AND request_final_result_updated = '', + NOW(), + IF( + request_final_result_updated = '', + final_result_updated, + FROM_UNIXTIME(request_final_result_updated) + ) + ), + fail_reason = IF( + request_fail_reason = '', + fail_reason, + IF(request_fail_reason = '$blank', '', request_fail_reason) + ), + final_result_id = IF(request_final_result_id = '', final_result_id, request_final_result_id), + issue_id = apply_or_remove_id(request_issue_id, issue_id); + + SET @insert_id = IF(request_id = '', (SELECT LAST_INSERT_ID()), request_id); + + SELECT * FROM test_results WHERE id = @insert_id; + END + + + + + diff --git a/src/test/java/tests/workers/dao/project/TestResultDaoTest.java b/src/test/java/tests/workers/dao/project/TestResultDaoTest.java index f46cab4..ca34936 100644 --- a/src/test/java/tests/workers/dao/project/TestResultDaoTest.java +++ b/src/test/java/tests/workers/dao/project/TestResultDaoTest.java @@ -12,6 +12,7 @@ import tests.workers.dao.IDaoTest; import java.util.ArrayList; +import java.util.Date; import java.util.List; import static org.testng.Assert.assertEquals; @@ -62,6 +63,13 @@ public void selectLatestResultsByMilestoneTest() throws AqualityException { assertSQLToParams(currentSql, currentParameters); } + @Test + public void updateFinalResultIdAndFailReasonTest() throws AqualityException { + resultList.add(new TestResultDto()); + updateFinalResultIdAndFailReason(1, 1, "failReason", new Date()); + assertSQLToParams(currentSql, currentParameters); + } + @Override protected JSONArray CallStoredProcedure(String sql, List> parameters){ currentSql = sql; From 7a298302fdb9e13f297ec2f3ecc343a7e1b95436 Mon Sep 17 00:00:00 2001 From: Andrei Pavar Date: Mon, 26 Aug 2024 10:16:14 +0300 Subject: [PATCH 18/18] refactor code --- .../db_changelog/db.changelog-1.5.2.xml | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/resources/db_changelog/db.changelog-1.5.2.xml b/src/main/resources/db_changelog/db.changelog-1.5.2.xml index 8543d46..538d162 100644 --- a/src/main/resources/db_changelog/db.changelog-1.5.2.xml +++ b/src/main/resources/db_changelog/db.changelog-1.5.2.xml @@ -12,19 +12,19 @@ # create procedure SELECT_TEST_RESULT_ATTACH( - In request_id varchar(11), - In request_test_result_id varchar(11), - In request_test_run_id varchar(11), - In request_project_id varchar(11), - In request_test_id varchar(11) + IN request_id varchar(11), + IN request_test_result_id varchar(11), + IN request_test_run_id varchar(11), + IN request_project_id varchar(11), + IN request_test_id varchar(11) ) BEGIN - SELECT * from test_result_attachments - WHERE (request_test_result_id = '' OR test_result_id=request_test_result_id) - AND (request_test_run_id = '' OR test_run_id=request_test_run_id) - AND (request_id = '' OR id=request_id) - AND (request_project_id = '' OR project_id=request_project_id) - AND (request_test_id = '' OR test_result_attachments.test_result_id in (select id from test_results where test_results.test_id=request_test_id)); + SELECT * from test_result_attachments + WHERE (request_test_result_id = '' OR test_result_id=request_test_result_id) + AND (request_test_run_id = '' OR test_run_id=request_test_run_id) + AND (request_id = '' OR id=request_id) + AND (request_project_id = '' OR project_id=request_project_id) + AND (request_test_id = '' OR test_result_attachments.test_result_id IN (SELECT id FROM test_results WHERE test_results.test_id=request_test_id)); END