Skip to content

Commit b46d066

Browse files
Merge pull request #56 from aquality-automation/feature/public_api_tests
Feature/public api tests
2 parents 4983d6e + 50fc5f2 commit b46d066

File tree

18 files changed

+399
-27
lines changed

18 files changed

+399
-27
lines changed

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>0.3.7</version>
9+
<version>0.3.8</version>
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main.annotations;
2+
3+
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Target(value= ElementType.FIELD)
10+
@Retention(value= RetentionPolicy.RUNTIME)
11+
public @interface OverrideIDName {
12+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ private List<AuditCommentDto> completeComments(List<AuditCommentDto> comments) t
292292

293293
private UserDto getUser(UserDto template) throws AqualityUserException {
294294
try {
295-
return userDao.getEntityById(template);
295+
return userDao.getEntityById(template.getId());
296296
} catch (AqualityException e) {
297297
throw new AqualityUserException("Cannot get Author for the audit comment.");
298298
}

src/main/java/main/model/db/dao/DAO.java

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main.model.db.dao;
22

33
import com.mysql.cj.core.conf.url.ConnectionUrlParser.Pair;
4+
import main.exceptions.AqualityParametersException;
45
import main.exceptions.AqualitySQLException;
56
import main.exceptions.AqualityException;
67
import main.model.db.RS_Converter;
@@ -82,30 +83,58 @@ public List<T> searchAll(T entity) throws AqualityException {
8283

8384
/**
8485
* Get single entity by id
85-
* @param entity entity id
86+
* @param id entity id
8687
* @return entity
8788
*/
88-
public T getEntityById(T entity) throws AqualityException {
89-
List<T> all = searchAll(entity);
90-
if(all.size() > 0) {
91-
return all.get(0);
92-
}
93-
else{
94-
throw new AqualityException("No Entities was found by %s", entity.getIDParameters());
89+
public T getEntityById(Integer id) throws AqualityException {
90+
T entity;
91+
try {
92+
entity = type.newInstance();
93+
} catch (InstantiationException | IllegalAccessException e) {
94+
throw new AqualityException("Cannot Create new Instance of entity");
9595
}
96+
97+
List<Pair<String, String>> parameters = entity.getIdSearchParameters(id);
98+
List<T> all = dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString());
99+
100+
return getSingleResult(all, id);
96101
}
97102

103+
/**
104+
* Get single entity by id and project_id
105+
* @param entity entity with id and project_id
106+
* @return entity
107+
*/
108+
public T getEntityById(T entity) throws AqualityException {
109+
List<Pair<String, String>> parameters = entity.getIdAndProjectIdSearchParameters();
110+
List<T> all = dtoMapper.mapObjects(CallStoredProcedure(select, parameters).toString());
111+
112+
return getSingleResult(all, entity.getIdOrOverrideId());
113+
}
98114

99115
/**
100116
* Update entity
101117
* @param entity with fields that should be updated (id is required)
102118
* @return Updated entity
103119
*/
104120
public T update(T entity) throws AqualityException {
105-
if(entity.getIDParameters().size() > 0){
106-
return create(entity);
121+
try {
122+
if(entity.hasProjectId()){
123+
getEntityById(entity);
124+
} else {
125+
getEntityById(entity.getIdOrOverrideId());
126+
}
127+
} catch (AqualityException e) {
128+
throw new AqualityParametersException("Entity with specified '%s' id does not exist!", entity.getIdOrOverrideId());
129+
}
130+
131+
List<Pair<String, String>> parameters = entity.getParameters();
132+
List<T> results = dtoMapper.mapObjects(CallStoredProcedure(insert, parameters).toString());
133+
if (!results.isEmpty()) {
134+
return results.get(0);
107135
}
108-
throw new AqualityException("Cannot update entity without id!");
136+
137+
throw new AqualityException("Was not able to update entity!");
109138
}
110139

111140
/**
@@ -114,7 +143,7 @@ public T update(T entity) throws AqualityException {
114143
* @return true if was able to remove entity
115144
*/
116145
public boolean delete(T entity) throws AqualityException {
117-
List<Pair<String, String>> parameters = entity.getIDParameters();
146+
List<Pair<String, String>> parameters = entity.getDataBaseIDParameters();
118147

119148
CallStoredProcedure(remove, parameters);
120149
return true;
@@ -126,10 +155,22 @@ public boolean delete(T entity) throws AqualityException {
126155
* @return created entity
127156
*/
128157
public T create(T entity) throws AqualityException {
129-
List<Pair<String, String>> parameters = entity.getParameters();
130-
List<T> results = dtoMapper.mapObjects(CallStoredProcedure(insert, parameters).toString());
131-
if(results.size() > 0){
132-
return results.get(0);
158+
Integer id = null;
159+
try {
160+
id = entity.getIdOrOverrideId();
161+
} catch (AqualityException e) {
162+
// entity has no id
163+
}
164+
165+
if(id == null){
166+
List<Pair<String, String>> parameters = entity.getParameters();
167+
List<T> results = dtoMapper.mapObjects(CallStoredProcedure(insert, parameters).toString());
168+
if(!results.isEmpty()){
169+
return results.get(0);
170+
}
171+
}
172+
else {
173+
return update(entity);
133174
}
134175

135176
throw new AqualitySQLException(new SQLException("Possible duplicate error.", "23505"));
@@ -176,6 +217,15 @@ protected JSONArray CallStoredProcedure(String sql, List<Pair<String, String>> p
176217
return json;
177218
}
178219

220+
private T getSingleResult(List<T> allResults, Integer id) throws AqualityException {
221+
if(!allResults.isEmpty()) {
222+
return allResults.get(0);
223+
}
224+
else{
225+
throw new AqualityException("No Entities was found by '%s' id", id);
226+
}
227+
}
228+
179229
private void getConnection() throws AqualityException {
180230
InitialContext initialContext;
181231
try {
@@ -261,4 +311,14 @@ private String getSqlName(String storedProcedure){
261311
}
262312
return "";
263313
}
314+
315+
private boolean areParametersEmpty(List<Pair<String, String>> parameters) {
316+
for (Pair<String, String> pair : parameters) {
317+
if(!pair.right.equals("")){
318+
return false;
319+
}
320+
}
321+
322+
return true;
323+
}
264324
}

src/main/java/main/model/db/dao/audit/AuditorsDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class AuditorsDao extends DAO<AuditorDto> {
1212
public AuditorsDao() {
1313
super(AuditorDto.class);
14-
select = "{call SELECT_AUDITOR(?,?)}";
14+
select = "{call SELECT_AUDITOR(?,?,?)}";
1515
insert = "{call INSERT_AUDITOR(?,?)}";
1616
remove = "{call REMOVE_AUDITOR(?)}";
1717
}

src/main/java/main/model/db/dao/settings/AppSettingsDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ public class AppSettingsDao extends DAO<AppSettingsDto> {
88
public AppSettingsDao(){
99
super(AppSettingsDto.class);
1010
insert = "{call INSERT_APP_SETTINGS(?,?,?,?)}";
11-
select = "{call SELECT_APP_SETTINGS()}";
11+
select = "{call SELECT_APP_SETTINGS(?)}";
1212
}
1313
}

src/main/java/main/model/db/dao/settings/LdapDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class LdapDao extends DAO<LdapDto>{
77
public LdapDao() {
88
super(LdapDto.class);
9-
select = "{call SELECT_LDAP_SETTING()}";
9+
select = "{call SELECT_LDAP_SETTING(?)}";
1010
insert = "{call INSERT_LDAP_SETTING(?,?,?,?,?,?,?,?,?,?,?,?)}";
1111
}
1212
}

src/main/java/main/model/db/imports/BaseImporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private void updateResultWithSimilarError(TestResultDto result) throws AqualityE
229229
try{
230230
ProjectDto project = new ProjectDto();
231231
project.setId(result.getProject_id());
232-
project = projectDao.getEntityById(project);
232+
project = projectDao.getEntityById(project.getId());
233233

234234
TestResultDto testResultTemplate = new TestResultDto();
235235
testResultTemplate.setProject_id(result.getProject_id());

src/main/java/main/model/dto/AppSettingsDto.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public class AppSettingsDto extends BaseDto{
99
@DataBaseName(name = "request_id")
1010
@DataBaseInsert
11+
@DataBaseSearchable
1112
private Integer id;
1213
@DataBaseName(name = "request_ldap_auth")
1314
@DataBaseInsert

src/main/java/main/model/dto/AuditorDto.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class AuditorDto extends UserDto {
1515
@DataBaseInsert
1616
@DataBaseSearchable
1717
private Integer id;
18+
@DataBaseSearchable
19+
@OverrideIDName
1820
@DataBaseName(name="request_auditor_id")
1921
@DataBaseID
2022
private Integer auditor_id;

0 commit comments

Comments
 (0)