Skip to content

Commit f965c8a

Browse files
Merge pull request #49 from aquality-automation/feature/public_api
Feature/public api
2 parents 30a51ec + 6153e00 commit f965c8a

15 files changed

+874
-132
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
Features:
66
- Test Run view: Add stability indicator -> [View Issue](https://github.com/aquality-automation/aquality-tracking/issues/60)
7+
- Provide public API -> [View Issue](https://github.com/aquality-automation/aquality-tracking/issues/56)
78

89
Bugfixes:
910
- Fixed for downloading audits results from Audits Dashboard -> [View Issue](https://github.com/aquality-automation/aquality-tracking/issues/51)

src/main/java/main/controllers/Project/SuiteController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import main.exceptions.AqualityPermissionsException;
66
import main.model.db.dao.project.*;
77
import main.model.dto.*;
8+
import org.jetbrains.annotations.NotNull;
89

910
import java.util.List;
1011
import java.util.stream.Collectors;
@@ -36,6 +37,17 @@ public TestSuiteDto create(TestSuiteDto template) throws AqualityException {
3637
}
3738
}
3839

40+
public TestSuiteDto createOrUpdate(@NotNull TestSuiteDto testSuite) throws AqualityException {
41+
if(testSuite.getId() == null) {
42+
List<TestSuiteDto> testSuites = get(testSuite);
43+
if(testSuites.size() > 0) {
44+
return testSuites.get(0);
45+
}
46+
}
47+
48+
return create(testSuite);
49+
}
50+
3951
public List<TestSuiteDto> get(TestSuiteDto template, boolean withChildren) throws AqualityException {
4052
if (baseUser.isFromGlobalManagement() || baseUser.getProjectUser(template.getProject_id()).isViewer()) {
4153
return fillTestSuites(testSuiteDao.searchAll(template), withChildren);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,38 @@ public TestDto create(TestDto template, boolean updateSuites) throws AqualityExc
3636
if (updateSuites) {
3737
test.setSuites(template.getSuites());
3838
updateSuites(test);
39+
test = get(test).get(0);
3940
}
4041
return test;
4142
} else {
4243
throw new AqualityPermissionsException("Account is not allowed to create Test", baseUser);
4344
}
4445
}
4546

47+
public TestDto createOrUpdate(TestDto test) throws AqualityException {
48+
TestDto searchTemplate = new TestDto();
49+
searchTemplate.setId(test.getId());
50+
searchTemplate.setProject_id(test.getProject_id());
51+
searchTemplate.setName(test.getName());
52+
53+
List<TestDto> existingTests = get(searchTemplate);
54+
55+
if(existingTests.size() > 0) {
56+
TestDto existingTest = existingTests.get(0);
57+
if(existingTest.getSuites() != null) {
58+
TestSuiteDto testSuite = existingTest.getSuites().stream().filter(suite -> suite.getId().equals(test.getSuites().get(0).getId())).findFirst().orElse(null);
59+
if(testSuite != null) {
60+
existingTest.getSuites().add(test.getSuites().get(0));
61+
}
62+
}else {
63+
existingTest.setSuites(test.getSuites());
64+
}
65+
return create(existingTest, true);
66+
} else {
67+
return create(test, true);
68+
}
69+
}
70+
4671
@Override
4772
public TestDto create(TestDto template) throws AqualityException {
4873
return create(template, false);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main.exceptions;
2+
3+
public class AqualityParametersException extends AqualityException {
4+
5+
public AqualityParametersException(String error) {
6+
super(error);
7+
this.responseCode = 400;
8+
}
9+
10+
public AqualityParametersException(String error, Object... args) {
11+
super(error, args);
12+
this.responseCode = 400;
13+
}
14+
}

src/main/java/main/model/dto/DtoMapperGeneral.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.fasterxml.jackson.databind.ObjectMapper;
1111
import com.fasterxml.jackson.databind.type.CollectionType;
1212
import main.exceptions.AqualityException;
13+
import main.exceptions.AqualityParametersException;
1314

1415
import java.io.IOException;
1516
import java.util.ArrayList;
@@ -31,16 +32,20 @@ public String serialize(Object object) throws JsonProcessingException {
3132
return mapper.writeValueAsString(object);
3233
}
3334

34-
public <T extends BaseDto> T mapObject(Class<T> clazz, String objectJsonString) throws IOException {
35-
return mapper.readValue(objectJsonString, clazz);
35+
public <T extends BaseDto> T mapObject(Class<T> clazz, String objectJsonString) throws AqualityParametersException {
36+
try {
37+
return mapper.readValue(objectJsonString, clazz);
38+
} catch (IOException e) {
39+
throw new AqualityParametersException("Cannot map Object to " + clazz.getName());
40+
}
3641
}
3742

38-
public <T> List<T> mapObjects(Class<T> clazz, String arrayJsonString) throws AqualityException {
43+
public <T> List<T> mapObjects(Class<T> clazz, String arrayJsonString) throws AqualityParametersException {
3944
CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz);
4045
try {
4146
return mapper.readValue(arrayJsonString, listType);
4247
} catch (IOException e) {
43-
throw new AqualityException("Cannot map Object to " + clazz.getName());
48+
throw new AqualityParametersException("Cannot map Object to " + clazz.getName());
4449
}
4550
}
4651
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main.model.dto;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class ErrorDto {
7+
public ErrorDto (String message) {
8+
setMessage(message);
9+
}
10+
private String message;
11+
}

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

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import main.exceptions.AqualityException;
55
import main.exceptions.AqualityQueryParameterException;
66
import main.model.dto.DtoMapperGeneral;
7+
import main.model.dto.ErrorDto;
78
import org.jetbrains.annotations.NotNull;
89

910
import javax.naming.AuthenticationException;
@@ -132,9 +133,10 @@ protected void setOptionsResponseHeaders(@NotNull HttpServletResponse resp) {
132133
resp.setStatus(204);
133134
}
134135

135-
private void setAuthorizationProblem(@NotNull HttpServletResponse resp, @NotNull Exception e) {
136+
private void setAuthorizationProblem(@NotNull HttpServletResponse resp, @NotNull Exception e) throws AqualityException {
136137
resp.setStatus(401);
137138
resp.addHeader("ErrorMessage", !Objects.equals(e.getMessage(), "") ? e.getMessage() : "Are you sure you logged in?");
139+
setResponseBody(resp, !Objects.equals(e.getMessage(), "") ? e.getMessage() : "Are you sure you logged in?");
138140
}
139141

140142
protected void setAuthorizationProblem(@NotNull HttpServletResponse resp) {
@@ -191,37 +193,54 @@ protected void processResponse(HttpServletResponse response, String filePath) {
191193
}
192194
}
193195

196+
protected void setResponseBody(HttpServletResponse resp, Object object) throws AqualityException {
197+
try {
198+
setJSONContentType(resp);
199+
resp.getWriter().write(mapper.serialize(object));
200+
} catch (IOException e) {
201+
throw new AqualityException("System was not able to write a response! Raise an Issue.");
202+
}
203+
}
204+
194205
protected void handleException(HttpServletResponse resp, @NotNull Exception e) {
195206
e.printStackTrace();
196-
switch (e.getClass().getSimpleName()) {
197-
case "UnsupportedOperationException":
198-
setNotImplementedFunction(resp, e);
199-
return;
200-
case "AuthenticationException":
201-
setAuthorizationProblem(resp, e);
202-
return;
203-
case "AqualityPermissionsException":
204-
case "AqualityException":
205-
case "InvalidFormatException":
206-
case "AqualityQueryParameterException":
207-
case "AqualitySQLException":
208-
AqualityException exception = (AqualityException) e;
209-
resp.setStatus(exception.getResponseCode());
210-
resp.addHeader("ErrorMessage", exception.getMessage());
211-
return;
212-
default:
213-
setUnknownIssue(resp);
207+
try {
208+
switch (e.getClass().getSimpleName()) {
209+
case "UnsupportedOperationException":
210+
setNotImplementedFunction(resp, e);
211+
return;
212+
case "AuthenticationException":
213+
setAuthorizationProblem(resp, e);
214+
return;
215+
case "AqualityParametersException":
216+
case "AqualityPermissionsException":
217+
case "AqualityException":
218+
case "InvalidFormatException":
219+
case "AqualityQueryParameterException":
220+
case "AqualitySQLException":
221+
AqualityException exception = (AqualityException) e;
222+
resp.setStatus(exception.getResponseCode());
223+
resp.addHeader("ErrorMessage", exception.getMessage());
224+
setResponseBody(resp, new ErrorDto(exception.getMessage()));
225+
return;
226+
default:
227+
setUnknownIssue(resp);
228+
}
229+
} catch (AqualityException ex) {
230+
handleException(resp, ex);
214231
}
215232
}
216233

217-
private void setNotImplementedFunction(@NotNull HttpServletResponse resp, @NotNull Exception e) {
234+
private void setNotImplementedFunction(@NotNull HttpServletResponse resp, @NotNull Exception e) throws AqualityException {
218235
resp.setStatus(501);
219236
resp.addHeader("ErrorMessage", e.getMessage());
237+
setResponseBody(resp, new ErrorDto(e.getMessage()));
220238
}
221239

222-
private void setUnknownIssue(@NotNull HttpServletResponse resp) {
240+
private void setUnknownIssue(@NotNull HttpServletResponse resp) throws AqualityException {
223241
resp.setStatus(500);
224242
resp.addHeader("ErrorMessage", "Unknown Issue.");
243+
setResponseBody(resp, new ErrorDto("Unknown Issue."));
225244
}
226245

227246
protected void assertRequiredField(HttpServletRequest request, String fieldName) throws AqualityException {

src/main/java/main/view/Project/TestServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) {
2424
TestDto test = new TestDto();
2525
test.getSearchTemplateFromRequestParameters(req);
2626
List<TestDto> tests = session.controllerFactory.getHandler(test).get(test);
27-
resp.getWriter().write(mapper.serialize(tests));
27+
setResponseBody(resp, tests);
2828
}catch (Exception e) {
2929
handleException(resp, e);
3030
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main.view.publicApi;
2+
3+
import main.Session;
4+
import main.exceptions.AqualityParametersException;
5+
import main.model.dto.TestSuiteDto;
6+
import main.view.BaseServlet;
7+
import main.view.IPost;
8+
9+
import javax.servlet.annotation.WebServlet;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
13+
@WebServlet("/public/suite/create-or-update")
14+
public class PublicSuiteServlet extends BaseServlet implements IPost {
15+
16+
@Override
17+
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
18+
setPostResponseHeaders(resp);
19+
setEncoding(resp);
20+
try {
21+
Session session = createSession(req);
22+
String requestedJson = getRequestJson(req);
23+
24+
TestSuiteDto testSuite = mapper.mapObject(TestSuiteDto.class, requestedJson);
25+
validatePost(testSuite);
26+
testSuite = session.controllerFactory.getHandler(testSuite).createOrUpdate(testSuite);
27+
28+
setResponseBody(resp, testSuite);
29+
} catch (Exception e) {
30+
handleException(resp, e);
31+
}
32+
}
33+
34+
private void validatePost(TestSuiteDto testSuite) throws AqualityParametersException {
35+
if(testSuite.getProject_id() == null) {
36+
throw new AqualityParametersException("You should specify 'project_id'!");
37+
}
38+
if(testSuite.getId() == null && testSuite.getName() == null) {
39+
throw new AqualityParametersException("You should specify 'id' or/and 'name' suite parameters!");
40+
}
41+
}
42+
43+
@Override
44+
public void doOptions(HttpServletRequest req, HttpServletResponse resp) {
45+
setOptionsResponseHeaders(resp);
46+
}
47+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main.view.publicApi;
2+
3+
import main.Session;
4+
import main.exceptions.AqualityParametersException;
5+
import main.model.dto.TestResultDto;
6+
import main.view.BaseServlet;
7+
import main.view.IPost;
8+
9+
import javax.servlet.annotation.WebServlet;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
import java.util.Date;
13+
import java.util.List;
14+
15+
@WebServlet("/public/test/result/finish")
16+
public class PublicTestResultFinishServlet extends BaseServlet implements IPost {
17+
18+
@Override
19+
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
20+
setPostResponseHeaders(resp);
21+
setEncoding(resp);
22+
try {
23+
Session session = createSession(req);
24+
String requestedJson = getRequestJson(req);
25+
26+
TestResultDto testResult = mapper.mapObject(TestResultDto.class, requestedJson);
27+
validatePost(testResult);
28+
29+
TestResultDto testResultSearchTemplate = new TestResultDto();
30+
testResultSearchTemplate.setProject_id(testResult.getProject_id());
31+
testResultSearchTemplate.setId(testResult.getId());
32+
33+
List<TestResultDto> oldResults = session.controllerFactory.getHandler(testResult).get(testResultSearchTemplate);
34+
35+
if(oldResults.size() > 0) {
36+
TestResultDto currentResult = oldResults.get(0);
37+
currentResult.setFinal_result_id(testResult.getFinal_result_id());
38+
currentResult.setFail_reason(testResult.getFail_reason());
39+
currentResult.setFinish_date(new Date());
40+
41+
testResult = session.controllerFactory.getHandler(testResult).create(currentResult);
42+
} else {
43+
throw new AqualityParametersException("Test Result with %s id was not found!", testResult.getId());
44+
}
45+
46+
setResponseBody(resp, testResult);
47+
} catch (Exception e) {
48+
handleException(resp, e);
49+
}
50+
}
51+
52+
private void validatePost(TestResultDto testResult) throws AqualityParametersException {
53+
if(testResult.getProject_id() == null) {
54+
throw new AqualityParametersException("You should specify 'project_id'!");
55+
}
56+
57+
if(testResult.getId() == null) {
58+
throw new AqualityParametersException("You should specify 'id'!");
59+
}
60+
61+
if(testResult.getFinal_result_id() == null) {
62+
throw new AqualityParametersException("You should specify 'final_result_id' - Failed: 1, Passed: 2, Not Executed: 3, Pending: 5.");
63+
}
64+
}
65+
66+
@Override
67+
public void doOptions(HttpServletRequest req, HttpServletResponse resp) {
68+
setOptionsResponseHeaders(resp);
69+
}
70+
}

0 commit comments

Comments
 (0)