Skip to content

Commit 89c271b

Browse files
authored
Add attachments for result (#78)
* small fixes for result attachment and add unit test * fix result attach * add download attachment servlet * refactor sending of test result attachment * hide path parameter from responses add save same file with name like 'filename (1).txt' * remove path from responce * revert db.changelog-0.3.11.xml * add change log
1 parent 9ac2b13 commit 89c271b

File tree

14 files changed

+184
-76
lines changed

14 files changed

+184
-76
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## 1.0.3 (2020-08-27)
4+
5+
Features:
6+
- API: fixes in test result attachments -> [View Issue](https://github.com/aquality-automation/aquality-tracking/issues/102)
7+
38
## 1.0.2 (2020-08-18)
49

510
Changes:

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>unifi_reporting_api</groupId>
77
<artifactId>api</artifactId>
88
<packaging>war</packaging>
9-
<version>1.0.2</version>
9+
<version>1.0.3</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/main/Session.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import main.controllers.Administration.AppSettingsController;
66
import main.controllers.Project.ProjectController;
77
import main.controllers.Project.ProjectUserController;
8+
import main.controllers.Project.TestResultAttachmentController;
89
import main.exceptions.AqualityException;
910
import main.model.db.dao.project.UserDao;
1011
import main.model.db.imports.Importer;
@@ -60,6 +61,10 @@ public AuditController getAuditController() {
6061
return new AuditController(user);
6162
}
6263

64+
public TestResultAttachmentController getTestResultAttachmentController() {
65+
return new TestResultAttachmentController(user);
66+
}
67+
6368
public AdministrationController getAdministrationController() {
6469
return new AdministrationController(user);
6570
}

src/main/java/main/controllers/ControllerFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,7 @@ public PredefinedResolutionController getHandler(PredefinedResolutionDto entity)
7878
public IssueController getHandler(IssueDto entity) {
7979
return new IssueController(user);
8080
}
81+
public TestResultAttachmentController getHandler(TestResultAttachmentDto entity) {
82+
return new TestResultAttachmentController(user);
83+
}
8184
}

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

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import main.model.dto.project.*;
1010
import main.model.dto.settings.UserDto;
1111

12-
import java.util.ArrayList;
1312
import java.util.List;
1413
import java.util.stream.Collectors;
1514

@@ -23,6 +22,7 @@ public class ResultController extends BaseController<TestResultDto> {
2322
private final StepController stepController;
2423
private final StepResultController stepResultController;
2524
private final IssueController issueController;
25+
private final TestResultAttachmentController testResultAttachmentController;
2626

2727
public ResultController(UserDto user) {
2828
super(user);
@@ -35,6 +35,7 @@ public ResultController(UserDto user) {
3535
stepController = new StepController(user);
3636
stepResultController = new StepResultController(user);
3737
issueController = new IssueController(user);
38+
testResultAttachmentController = new TestResultAttachmentController(user);
3839
}
3940

4041
@Override
@@ -100,30 +101,6 @@ public List<TestResultStatDto> get(TestResultStatDto template) throws AqualityEx
100101
}
101102
}
102103

103-
public TestResultAttachmentDto create(TestResultAttachmentDto attachment) throws AqualityException {
104-
if (baseUser.isManager() || baseUser.getProjectUser(attachment.getProject_id()).isEditor()) {
105-
return testResultAttachmentDao.create(attachment);
106-
} else {
107-
throw new AqualityPermissionsException("Account is not allowed to add Test Result Attachment", baseUser);
108-
}
109-
}
110-
111-
public List<TestResultAttachmentDto> get(TestResultAttachmentDto testResultAttachment) throws AqualityException {
112-
if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(testResultAttachment.getProject_id()).isViewer()) {
113-
return testResultAttachmentDao.searchAll(testResultAttachment);
114-
} else {
115-
throw new AqualityPermissionsException("Account is not allowed to view Test Result Attachment", baseUser);
116-
}
117-
}
118-
119-
public boolean delete(TestResultAttachmentDto attachment) throws AqualityException {
120-
if (baseUser.isManager() || baseUser.getProjectUser(attachment.getProject_id()).isEditor()) {
121-
return testResultAttachmentDao.delete(attachment);
122-
} else {
123-
throw new AqualityPermissionsException("Account is not allowed to delete Test Result Attachment", baseUser);
124-
}
125-
}
126-
127104
private void createPendingStepResults(TestResultDto template) throws AqualityException {
128105
Step2TestDto step2TestTemplate = new Step2TestDto();
129106
step2TestTemplate.setProject_id(template.getProject_id());
@@ -156,7 +133,7 @@ private List<TestResultDto> fillResults(List<TestResultDto> results) throws Aqua
156133

157134
TestResultAttachmentDto testResultAttachmentTemplate = new TestResultAttachmentDto();
158135
testResultAttachmentTemplate.setProject_id(projectId);
159-
List<TestResultAttachmentDto> testResultAttachments = get(testResultAttachmentTemplate);
136+
List<TestResultAttachmentDto> testResultAttachments = testResultAttachmentController.get(testResultAttachmentTemplate);
160137

161138
ProjectUserDto projectUserDto = new ProjectUserDto();
162139
projectUserDto.setProject_id(projectId);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main.controllers.Project;
2+
3+
import main.controllers.BaseController;
4+
import main.exceptions.AqualityException;
5+
import main.model.db.dao.project.TestResultAttachmentDao;
6+
import main.model.dto.project.TestResultAttachmentDto;
7+
import main.model.dto.settings.UserDto;
8+
import org.apache.commons.io.FilenameUtils;
9+
10+
import java.util.List;
11+
12+
public class TestResultAttachmentController extends BaseController<TestResultAttachmentDto> {
13+
private final TestResultAttachmentDao testResultAttachmentDao;
14+
15+
public TestResultAttachmentController(UserDto user) {
16+
super(user);
17+
testResultAttachmentDao = new TestResultAttachmentDao();
18+
}
19+
20+
@Override
21+
public List<TestResultAttachmentDto> get(TestResultAttachmentDto entity) throws AqualityException {
22+
return getResultAttachment(testResultAttachmentDao.searchAll(entity));
23+
}
24+
25+
@Override
26+
public TestResultAttachmentDto create(TestResultAttachmentDto entity) throws AqualityException {
27+
return testResultAttachmentDao.create(entity);
28+
}
29+
30+
@Override
31+
public boolean delete(TestResultAttachmentDto entity) throws AqualityException {
32+
return testResultAttachmentDao.delete(entity);
33+
}
34+
35+
private List<TestResultAttachmentDto> getResultAttachment(List<TestResultAttachmentDto> testResultAttachmentDto) {
36+
testResultAttachmentDto.forEach(attachmentDto -> {
37+
attachmentDto.setName(FilenameUtils.getName(attachmentDto.getPath()));
38+
});
39+
return testResultAttachmentDto;
40+
}
41+
}

src/main/java/main/model/dto/AttachmentDto.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import lombok.Data;
44
import lombok.EqualsAndHashCode;
5-
import main.annotations.*;
5+
import main.annotations.DataBaseInsert;
6+
import main.annotations.DataBaseName;
7+
import main.annotations.DataBaseSearchable;
68

79

810
@Data @EqualsAndHashCode(callSuper = true)
@@ -13,4 +15,6 @@ public class AttachmentDto extends BaseDto {
1315
@DataBaseName(name = "request_path")
1416
@DataBaseInsert
1517
private String path;
18+
19+
private String name;
1620
}

src/main/java/main/model/dto/project/TestResultAttachmentDto.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import lombok.Data;
44
import lombok.EqualsAndHashCode;
5-
import main.annotations.DataBaseID;
65
import main.annotations.DataBaseInsert;
76
import main.annotations.DataBaseName;
87
import main.annotations.DataBaseSearchable;
@@ -16,7 +15,6 @@ public class TestResultAttachmentDto extends AttachmentDto {
1615
@DataBaseInsert
1716
private Integer test_result_id;
1817

19-
@DataBaseID
2018
@DataBaseName(name = "request_project_id")
2119
@DataBaseSearchable
2220
@DataBaseInsert
@@ -26,9 +24,9 @@ public class TestResultAttachmentDto extends AttachmentDto {
2624
@DataBaseSearchable
2725
private Integer test_run_id;
2826

29-
@DataBaseID
30-
@DataBaseName(name = "request_id")
31-
private Integer id;
32-
3327
private String url;
28+
29+
private byte[] attachment;
30+
31+
private String mimeType;
3432
}

src/main/java/main/utils/FileUtils.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
import javax.servlet.http.HttpServletResponse;
99
import javax.servlet.http.Part;
1010
import java.io.*;
11-
import java.nio.file.Files;
12-
import java.nio.file.Paths;
13-
import java.nio.file.attribute.BasicFileAttributes;
1411
import java.util.ArrayList;
1512
import java.util.Collection;
16-
import java.util.Date;
1713
import java.util.List;
1814

1915
public class FileUtils {
@@ -22,15 +18,15 @@ public List<String> doUpload(HttpServletRequest request, HttpServletResponse res
2218
List<String> files = new ArrayList<>();
2319
new File(destination).mkdirs();
2420
Collection<Part> parts = request.getParts();
25-
for (Part filePart:parts) {
26-
System.out.printf("part Type: %s",filePart.getContentType());
21+
for (Part filePart : parts) {
22+
System.out.printf("part Type: %s", filePart.getContentType());
2723
OutputStream out = null;
2824
InputStream fileContent = null;
2925
PrintWriter writer = response.getWriter();
3026
String fileName = getFileName(filePart);
3127

3228
try {
33-
String filePath = PathUtils.createPath(destination, fileName);
29+
String filePath = PathUtils.getUniquePath(PathUtils.createPath(destination, fileName));
3430
out = new FileOutputStream(new File(filePath));
3531
fileContent = filePart.getInputStream();
3632
int read;
@@ -57,13 +53,13 @@ public List<String> doUpload(HttpServletRequest request, HttpServletResponse res
5753
return files;
5854
}
5955

60-
public void removeFiles(List<String> filePaths){
61-
for (String path : filePaths){
56+
public void removeFiles(List<String> filePaths) {
57+
for (String path : filePaths) {
6258
removeFile(path);
6359
}
6460
}
6561

66-
public void removeFile(String path){
62+
public void removeFile(String path) {
6763
new File(path).delete();
6864
}
6965

src/main/java/main/utils/PathUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package main.utils;
22

3+
import org.apache.commons.io.FilenameUtils;
4+
35
import java.io.File;
46
import java.util.ArrayList;
57
import java.util.Arrays;
@@ -15,4 +17,22 @@ public static String createPathToBin(String... parts){
1517
strings.add(0, System.getProperty("user.dir"));
1618
return createPath(strings.toArray(new String[0]));
1719
}
20+
21+
public static String getUniquePath(String filePath) {
22+
File file = new File(filePath);
23+
int fileIndex = 1;
24+
String extension = FilenameUtils.getExtension(filePath);
25+
String newPath = filePath;
26+
while (file.exists()) {
27+
newPath = FilenameUtils.removeExtension(filePath).concat(String.format(" (%s)", fileIndex));
28+
if (!extension.equals("")) {
29+
newPath = newPath.concat(FilenameUtils.EXTENSION_SEPARATOR_STR).concat(extension);
30+
}
31+
32+
file = new File(newPath);
33+
fileIndex++;
34+
}
35+
36+
return newPath;
37+
}
1838
}

0 commit comments

Comments
 (0)