Skip to content

Commit

Permalink
Improved performance for endpoints: /public/test/create-or-update, /p…
Browse files Browse the repository at this point in the history
…ublic/test/result/start, /public/test/result/finish;

Fixed INSERT_TEST_RESULT final_result_id comparison
  • Loading branch information
EvgenySafronov committed Apr 17, 2024
1 parent 8da3160 commit 53c9bf1
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 127 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>unifi_reporting_api</groupId>
<artifactId>api</artifactId>
<packaging>war</packaging>
<version>1.5.0</version>
<version>1.5.1</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
102 changes: 63 additions & 39 deletions src/main/java/main/controllers/Project/ResultController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestResultDto> {
Expand Down Expand Up @@ -56,15 +57,29 @@ public TestResultDto create(TestResultDto template) throws AqualityException {
}
}

@Override
public List<TestResultDto> 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<TestResultDto> get(TestResultDto template) throws AqualityException {
checkReadPermissions(template.getProject_id());
return fillResults(testResultDao.searchAll(template));
}

public List<TestResultDto> 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()) {
Expand All @@ -76,8 +91,11 @@ public boolean delete(TestResultDto template) throws AqualityException {

public List<TestResultDto> getOnlyFailedResults(TestResultDto testResultTemplate) throws AqualityException {
List<TestResultDto> 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<TestResultAttachmentDto> listOfAttachments) throws AqualityException {
Expand All @@ -98,7 +116,7 @@ public List<TestResultDto> getLatestResultsByMilestone(Integer projectId, Intege
}

public boolean updateMultipleTestResults(List<TestResultDto> 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 {
Expand All @@ -114,6 +132,38 @@ public List<TestResultStatDto> get(TestResultStatDto template) throws AqualityEx
}
}

public Map<String, Integer> matchIssues(Integer testResultId) throws AqualityException {
TestResultDto testResultTemplate = new TestResultDto();
testResultTemplate.setId(testResultId);
List<TestResultDto> 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<IssueDto> issues = issueController.get(issueTemplate);
Integer count = assignIssuesToResults(issues, testResults);
Map<String, Integer> results = new HashMap<>();
results.put("Issues assigned", count);
return results;
}

public Integer assignIssuesToResults(List<IssueDto> issues, List<TestResultDto> 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());
Expand All @@ -133,7 +183,7 @@ private void createPendingStepResults(TestResultDto template) throws AqualityExc

private List<TestResultDto> fillResults(List<TestResultDto> results) throws AqualityException {

if (results.size() > 0) {
if (!results.isEmpty()) {
int projectId = results.get(0).getProject_id();
List<FinalResultDto> finalResults = finalResultController.get(new FinalResultDto());
IssueDto issueDto = new IssueDto();
Expand Down Expand Up @@ -163,7 +213,7 @@ private List<TestResultDto> fillResults(List<TestResultDto> results) throws Aqua
}

private void fillResult(TestResultDto result, List<FinalResultDto> finalResults, List<TestDto> tests,
List<IssueDto> issues, List<TestResultAttachmentDto> attachments, boolean isStepsEnabled)
List<IssueDto> issues, List<TestResultAttachmentDto> attachments, boolean isStepsEnabled)
throws AqualityException {
if (isStepsEnabled) {
fillResultSteps(result);
Expand All @@ -187,35 +237,9 @@ private void fillResultSteps(TestResultDto result) throws AqualityException {
result.setSteps(stepResultController.get(stepResultTemplate));
}

public Map<String, Integer> matchIssues(Integer testResultId) throws AqualityException {
TestResultDto testResultTemplate = new TestResultDto();
testResultTemplate.setId(testResultId);
List<TestResultDto> 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<IssueDto> issues = issueController.get(issueTemplate);
Integer count = assignIssuesToResults(issues, testResults);
Map<String, Integer> results = new HashMap<>();
results.put("Issues assigned", count);
return results;
}

public Integer assignIssuesToResults(List<IssueDto> issues, List<TestResultDto> 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;
}
}
120 changes: 67 additions & 53 deletions src/main/java/main/controllers/Project/TestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestDto> 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<TestSuiteDto> 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
Expand All @@ -77,22 +52,15 @@ public TestDto create(TestDto template) throws AqualityException {
}

public List<TestDto> 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<TestDto> 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()) {
Expand All @@ -103,7 +71,7 @@ public boolean delete(TestDto template) throws AqualityException {
}

public void updateMultipleTests(List<TestDto> 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);
}
Expand Down Expand Up @@ -157,14 +125,12 @@ protected List<TestResultDto> getResultsToMove(TestDto from, TestDto to) {

private List<TestDto> fillTests(List<TestDto> tests) throws AqualityException {
List<TestDto> 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<ProjectUserDto> projectUsers = projectUserController.get(projectUserDto);
TestSuiteDto testSuiteDto = new TestSuiteDto();
testSuiteDto.setProject_id(projectId);
List<TestSuiteDto> testSuites = suiteDao.searchAll(testSuiteDto);
List<TestSuiteDto> testSuites = getProjectTestSuites(projectId);
ProjectDto projectDto = new ProjectDto();
projectDto.setId(tests.get(0).getProject_id());

Expand All @@ -189,7 +155,7 @@ private void updateSuites(TestDto test) throws AqualityException {
Test2SuiteDto test2SuiteDto = new Test2SuiteDto();
test2SuiteDto.setTest_id(test.getId());
List<Test2SuiteDto> oldSuites = test2SuiteController.get(test2SuiteDto);
if (test.getSuites() != null && test.getSuites().size() > 0) {
if (test.getSuites() != null && !test.getSuites().isEmpty()) {
List<TestSuiteDto> suites = test.getSuites();
for (TestSuiteDto newSuite : suites) {
Test2SuiteDto alreadyExists = oldSuites.stream().filter(x -> Objects.equals(x.getSuite_id(), newSuite.getId())).findAny().orElse(null);
Expand All @@ -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<TestSuiteDto> 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<TestDto> 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<Test2SuiteDto> 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<TestSuiteDto> projectTestSuites = getProjectTestSuites(projectId);
testDto.setSuites(test2SuiteController.convertToSuites(existingTest2Suites, projectTestSuites));

return testDto;
}
}
2 changes: 1 addition & 1 deletion src/main/java/main/model/db/dao/DAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ protected JSONArray CallStoredProcedure(String sql, List<Pair<String, String>> p
return json;
}

private T getSingleResult(List<T> allResults, Integer id) throws AqualityException {
protected T getSingleResult(List<T> allResults, Integer id) throws AqualityException {
if (!allResults.isEmpty()) {
return allResults.get(0);
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/main/model/db/dao/project/TestDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
import java.util.List;

public class TestDao extends DAO<TestDto> {
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<TestDto> getTestsAffectedByIssue(Integer issueId) throws AqualityException {
List<Pair<String, String>> 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());
}
}
Loading

0 comments on commit 53c9bf1

Please sign in to comment.