Skip to content

Commit 7a4aab4

Browse files
Merge pull request #70 from aquality-automation/add_attachments_for_result
add attachment
2 parents 835929e + 111dd2f commit 7a4aab4

File tree

12 files changed

+344
-31
lines changed

12 files changed

+344
-31
lines changed

src/main/java/main/controllers/AuditController.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
import java.util.stream.Collectors;
2222

2323
public class AuditController extends BaseController<AuditDto> {
24-
private AuditDao auditDao;
25-
private AuditStatusDao auditStatusDao;
26-
private ServiceDao serviceDao;
27-
private ProjectDao projectDao;
28-
private AuditorsDao auditorsDao;
29-
private AuditCommentsDao auditCommentsDao;
30-
private AuditAttachmentsDao auditAttachmentsDao;
31-
private AuditStatisticDao auditStatisticDao;
32-
private UserDao userDao;
33-
private SimpleDateFormat formatter = new SimpleDateFormat("MM.dd.yyyy");
24+
private final AuditDao auditDao;
25+
private final AuditStatusDao auditStatusDao;
26+
private final ServiceDao serviceDao;
27+
private final ProjectDao projectDao;
28+
private final AuditorsDao auditorsDao;
29+
private final AuditCommentsDao auditCommentsDao;
30+
private final AuditAttachmentsDao auditAttachmentsDao;
31+
private final AuditStatisticDao auditStatisticDao;
32+
private final UserDao userDao;
33+
private final SimpleDateFormat formatter = new SimpleDateFormat("MM.dd.yyyy");
3434

3535
public AuditController(UserDto user) {
3636
super(user);

src/main/java/main/controllers/Project/ResultController.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,31 @@
33
import main.controllers.BaseController;
44
import main.exceptions.AqualityException;
55
import main.exceptions.AqualityPermissionsException;
6+
import main.model.db.dao.project.TestResultAttachmentDao;
67
import main.model.db.dao.project.TestResultDao;
78
import main.model.db.dao.project.TestResultStatDao;
89
import main.model.dto.project.*;
910
import main.model.dto.settings.UserDto;
1011

12+
import java.util.ArrayList;
1113
import java.util.List;
1214

1315
public class ResultController extends BaseController<TestResultDto> {
14-
private TestResultDao testResultDao;
15-
private TestResultStatDao testResultStatDao;
16-
private TestController testController;
17-
private FinalResultController finalResultController;
18-
private ProjectController projectController;
19-
private StepController stepController;
20-
private StepResultController stepResultController;
21-
private IssueController issueController;
16+
private final TestResultDao testResultDao;
17+
private final TestResultStatDao testResultStatDao;
18+
private final TestResultAttachmentDao testResultAttachmentDao;
19+
private final TestController testController;
20+
private final FinalResultController finalResultController;
21+
private final ProjectController projectController;
22+
private final StepController stepController;
23+
private final StepResultController stepResultController;
24+
private final IssueController issueController;
2225

2326
public ResultController(UserDto user) {
2427
super(user);
2528
testResultDao = new TestResultDao();
26-
testResultStatDao = new TestResultStatDao();
29+
testResultStatDao = new TestResultStatDao();;
30+
testResultAttachmentDao = new TestResultAttachmentDao();
2731
testController = new TestController(user);
2832
finalResultController = new FinalResultController(user);
2933
projectController = new ProjectController(user);
@@ -54,7 +58,6 @@ public List<TestResultDto> get(TestResultDto template) throws AqualityException
5458
}
5559
}
5660

57-
5861
@Override
5962
public boolean delete(TestResultDto template) throws AqualityException {
6063
if (baseUser.isManager() || baseUser.getProjectUser(template.getProject_id()).isEditor()) {
@@ -64,6 +67,14 @@ public boolean delete(TestResultDto template) throws AqualityException {
6467
}
6568
}
6669

70+
public boolean createMultiple(List<TestResultAttachmentDto> listOfAttachments) throws AqualityException {
71+
if (baseUser.isManager() || baseUser.getProjectUser(listOfAttachments.get(0).getProject_id()).isEditor()) {
72+
return testResultAttachmentDao.createMultiply(listOfAttachments);
73+
} else {
74+
throw new AqualityPermissionsException("Account is not allowed to add Test Result Attachment", baseUser);
75+
}
76+
}
77+
6778
public List<TestResultDto> getLatestResultsByMilestone(Integer projectId, Integer milestoneId) throws AqualityException {
6879
if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(projectId).isViewer()) {
6980
return fillResults(testResultDao.selectLatestResultsByMilestone(milestoneId));
@@ -88,6 +99,30 @@ public List<TestResultStatDto> get(TestResultStatDto template) throws AqualityEx
8899
}
89100
}
90101

102+
public TestResultAttachmentDto create(TestResultAttachmentDto attachment) throws AqualityException {
103+
if (baseUser.isManager() || baseUser.getProjectUser(attachment.getProject_id()).isEditor()) {
104+
return testResultAttachmentDao.create(attachment);
105+
} else {
106+
throw new AqualityPermissionsException("Account is not allowed to add Test Result Attachment", baseUser);
107+
}
108+
}
109+
110+
public List<TestResultAttachmentDto> get(TestResultAttachmentDto testResultAttachment) throws AqualityException {
111+
if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(testResultAttachment.getProject_id()).isViewer()) {
112+
return testResultAttachmentDao.searchAll(testResultAttachment);
113+
} else {
114+
throw new AqualityPermissionsException("Account is not allowed to view Test Result Attachment", baseUser);
115+
}
116+
}
117+
118+
public boolean delete(TestResultAttachmentDto attachment) throws AqualityException {
119+
if (baseUser.isManager() || baseUser.getProjectUser(attachment.getProject_id()).isEditor()) {
120+
return testResultAttachmentDao.delete(attachment);
121+
} else {
122+
throw new AqualityPermissionsException("Account is not allowed to delete Test Result Attachment", baseUser);
123+
}
124+
}
125+
91126
private void createPendingStepResults(TestResultDto template) throws AqualityException {
92127
Step2TestDto step2TestTemplate = new Step2TestDto();
93128
step2TestTemplate.setProject_id(template.getProject_id());
@@ -149,5 +184,4 @@ private void fillResultSteps(TestResultDto result) throws AqualityException {
149184
stepResultTemplate.setProject_id(result.getProject_id());
150185
result.setSteps(stepResultController.get(stepResultTemplate));
151186
}
152-
153187
}

src/main/java/main/controllers/Project/TestController.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,17 @@ public TestDto createOrUpdate(TestDto test) throws AqualityException {
5151

5252
List<TestDto> existingTests = get(searchTemplate);
5353

54-
if(existingTests.size() > 0) {
54+
if(!existingTests.isEmpty()) {
5555
TestDto existingTest = existingTests.get(0);
5656
if(existingTest.getSuites() != null) {
57-
existingTest.getSuites().stream()
58-
.filter(suite -> suite.getId().equals(test.getSuites().get(0).getId())).findFirst().ifPresent(testSuite -> existingTest.getSuites().add(test.getSuites().get(0)));
57+
TestSuiteDto existingSuite = existingTest.getSuites().stream()
58+
.filter(suite -> suite.getId().equals(test.getSuites().get(0).getId()))
59+
.findFirst().orElse(null);
60+
if(existingSuite == null) {
61+
List<TestSuiteDto> listOfSuites = existingTest.getSuites();
62+
listOfSuites.add(test.getSuites().get(0));
63+
existingTest.setSuites(listOfSuites);
64+
}
5965
}else {
6066
existingTest.setSuites(test.getSuites());
6167
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main.model.db.dao.project;
2+
3+
import main.model.db.dao.DAO;
4+
import main.model.dto.project.TestResultAttachmentDto;
5+
6+
public class TestResultAttachmentDao extends DAO<TestResultAttachmentDto> {
7+
public TestResultAttachmentDao() {
8+
super(TestResultAttachmentDto.class);
9+
select = "{call SELECT_TEST_RESULT_ATTACH(?,?)}";
10+
insert = "{call INSERT_TEST_RESULT_ATTACH(?,?,?)}";
11+
remove = "{call REMOVE_TEST_RESULT_ATTACH(?)}";
12+
}
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main.model.dto.project;
2+
3+
import lombok.Data;
4+
import lombok.EqualsAndHashCode;
5+
import main.annotations.DataBaseInsert;
6+
import main.annotations.DataBaseName;
7+
import main.annotations.DataBaseSearchable;
8+
import main.model.dto.AttachmentDto;
9+
10+
@Data
11+
@EqualsAndHashCode(callSuper = true)
12+
public class TestResultAttachmentDto extends AttachmentDto {
13+
@DataBaseName(name = "request_test_result_id")
14+
@DataBaseSearchable
15+
@DataBaseInsert
16+
private Integer test_result_id;
17+
18+
@DataBaseName(name = "request_project_id")
19+
@DataBaseSearchable
20+
@DataBaseInsert
21+
private Integer project_id;
22+
23+
@DataBaseName(name = "request_test_run_id")
24+
@DataBaseSearchable
25+
private Integer test_run_id;
26+
}

src/main/java/main/view/BaseServlet.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected Boolean getBooleanQueryParameter(@NotNull HttpServletRequest req, Stri
8181
}
8282

8383
protected void setPostResponseHeaders(@NotNull HttpServletResponse resp) {
84-
resp.addHeader("Access-Control-Allow-Methods", "Post");
84+
resp.addHeader("Access-Control-Allow-Methods", "POST");
8585
resp.addHeader("Access-Control-Allow-Origin", "*");
8686
resp.addHeader("Access-Control-Allow-Headers", "Authorization");
8787
}
@@ -95,13 +95,13 @@ protected void setJSONContentType(@NotNull HttpServletResponse resp) {
9595
}
9696

9797
protected void setDeleteResponseHeaders(@NotNull HttpServletResponse resp) {
98-
resp.addHeader("Access-Control-Allow-Methods", "Delete");
98+
resp.addHeader("Access-Control-Allow-Methods", "DELETE");
9999
resp.addHeader("Access-Control-Allow-Origin", "*");
100100
resp.addHeader("Access-Control-Allow-Headers", "Authorization");
101101
}
102102

103103
protected void setGetResponseHeaders(@NotNull HttpServletResponse resp) {
104-
resp.addHeader("Access-Control-Allow-Methods", "Get");
104+
resp.addHeader("Access-Control-Allow-Methods", "GET");
105105
resp.addHeader("Access-Control-Allow-Origin", "*");
106106
resp.addHeader("Access-Control-Allow-Headers", "Authorization");
107107
resp.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main.view.publicApi;
2+
3+
import main.Session;
4+
import main.exceptions.AqualityParametersException;
5+
import main.model.dto.audit.AuditAttachmentDto;
6+
import main.model.dto.project.TestResultAttachmentDto;
7+
import main.model.dto.project.TestResultDto;
8+
import main.model.dto.project.TestSuiteDto;
9+
import main.utils.FileUtils;
10+
import main.utils.PathUtils;
11+
import main.view.BaseServlet;
12+
import main.view.IDelete;
13+
import main.view.IGet;
14+
import main.view.IPost;
15+
import main.view.Project.TestResult;
16+
17+
import javax.servlet.annotation.MultipartConfig;
18+
import javax.servlet.annotation.WebServlet;
19+
import javax.servlet.http.HttpServletRequest;
20+
import javax.servlet.http.HttpServletResponse;
21+
import java.nio.charset.StandardCharsets;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
26+
@WebServlet("/public/test/result/attachment")
27+
@MultipartConfig
28+
public class PublicTestResultAttachment extends BaseServlet implements IPost {
29+
30+
@Override
31+
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
32+
try {
33+
req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
34+
setEncoding(resp);
35+
setPostResponseHeaders(resp);
36+
Session session = createSession(req);
37+
TestResultAttachmentDto attachment = new TestResultAttachmentDto();
38+
attachment.getSearchTemplateFromRequestParameters(req);
39+
validatePost(attachment);
40+
41+
FileUtils fileUtils = new FileUtils();
42+
List<String> filePaths = fileUtils.doUpload(req, resp, PathUtils.createPathToBin(
43+
"project",
44+
getStringQueryParameter(req, "project_id"),
45+
"result_attachments",
46+
getStringQueryParameter(req, "test_result_id")));
47+
List<TestResultAttachmentDto> listOfAttachments = new ArrayList<>();
48+
for (String path : filePaths) {
49+
TestResultAttachmentDto newAttachment = new TestResultAttachmentDto();
50+
newAttachment.getSearchTemplateFromRequestParameters(req);
51+
newAttachment.setPath(path);
52+
listOfAttachments.add(newAttachment);
53+
}
54+
55+
session.controllerFactory.getHandler(new TestResultDto()).createMultiple(listOfAttachments);
56+
} catch (Exception e) {
57+
handleException(resp, e);
58+
}
59+
}
60+
61+
62+
63+
private void validatePost(TestResultAttachmentDto attachment) throws AqualityParametersException {
64+
if(attachment.getProject_id() == null) {
65+
throw new AqualityParametersException("You should specify 'project_id'!");
66+
}
67+
if(attachment.getTest_result_id() == null ) {
68+
throw new AqualityParametersException("You should specify 'test_result_id!");
69+
}
70+
}
71+
72+
@Override
73+
public void doOptions(HttpServletRequest req, HttpServletResponse resp){
74+
setOptionsResponseHeaders(resp);
75+
}
76+
}
77+
78+

src/main/java/main/view/publicApi/PublicTestServlet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private void validatePost(TestDto test) throws AqualityParametersException {
4141
throw new AqualityParametersException("You should specify 'id' or/and 'name' suite parameters!");
4242
}
4343

44-
if(test.getSuites() == null) {
45-
throw new AqualityParametersException("You should specify 'suites' array with single suite like `[{id: test_suite_id}]`");
44+
if(test.getSuites() == null || test.getSuites().size() != 1) {
45+
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.");
4646
}
4747
}
4848

src/main/resources/db_changelog/changelog.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
<include file="db.changelog-0.3.8.xml" relativeToChangelogFile="true"/>
3434
<include file="db.changelog-0.3.9.xml" relativeToChangelogFile="true"/>
3535
<include file="db.changelog-0.3.10.xml" relativeToChangelogFile="true"/>
36+
<include file="db.changelog-0.3.11.xml" relativeToChangelogFile="true"/>
3637
</databaseChangeLog>

0 commit comments

Comments
 (0)