Skip to content

Commit 81b01e2

Browse files
committed
Refactors to introduce DraftServiceDAO and DraftFileStorageServiceDAO so that two level mocks aren't needed for GCSService
1 parent e47d249 commit 81b01e2

File tree

11 files changed

+493
-393
lines changed

11 files changed

+493
-393
lines changed

src/main/java/org/broadinstitute/consent/http/ConsentApplication.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import org.broadinstitute.consent.http.service.DatasetRegistrationService;
8585
import org.broadinstitute.consent.http.service.DatasetService;
8686
import org.broadinstitute.consent.http.service.DraftService;
87+
import org.broadinstitute.consent.http.service.dao.DraftServiceDAO;
8788
import org.broadinstitute.consent.http.service.ElasticSearchService;
8889
import org.broadinstitute.consent.http.service.ElectionService;
8990
import org.broadinstitute.consent.http.service.EmailService;

src/main/java/org/broadinstitute/consent/http/ConsentModule.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
import org.broadinstitute.consent.http.service.DataAccessRequestService;
4545
import org.broadinstitute.consent.http.service.DatasetRegistrationService;
4646
import org.broadinstitute.consent.http.service.DatasetService;
47-
import org.broadinstitute.consent.http.service.DraftFileStorageService;
48-
import org.broadinstitute.consent.http.service.DraftService;
47+
import org.broadinstitute.consent.http.service.dao.DraftFileStorageServiceDAO;
48+
import org.broadinstitute.consent.http.service.dao.DraftServiceDAO;
4949
import org.broadinstitute.consent.http.service.ElasticSearchService;
5050
import org.broadinstitute.consent.http.service.ElectionService;
5151
import org.broadinstitute.consent.http.service.EmailService;
@@ -628,14 +628,14 @@ DraftDAO providesDraftDAO() {
628628
}
629629

630630
@Provides
631-
DraftFileStorageService providesDraftFileStorageService() {
632-
return new DraftFileStorageService(providesJdbi(), providesGCSService(),
631+
DraftFileStorageServiceDAO providesDraftFileStorageService() {
632+
return new DraftFileStorageServiceDAO(providesJdbi(), providesGCSService(),
633633
providesFileStorageObjectDAO());
634634
}
635635

636636
@Provides
637-
DraftService providesDraftService() {
638-
return new DraftService(providesJdbi(), providesDraftDAO(),
637+
DraftServiceDAO providesDraftService() {
638+
return new DraftServiceDAO(providesJdbi(), providesDraftDAO(),
639639
providesDraftFileStorageService());
640640
}
641641
}

src/main/java/org/broadinstitute/consent/http/service/DraftService.java

Lines changed: 36 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -2,173 +2,75 @@
22

33
import com.google.gson.Gson;
44
import com.google.inject.Inject;
5-
import jakarta.ws.rs.BadRequestException;
6-
import jakarta.ws.rs.NotAuthorizedException;
7-
import jakarta.ws.rs.NotFoundException;
85
import jakarta.ws.rs.core.StreamingOutput;
96
import java.io.InputStream;
107
import java.sql.SQLException;
11-
import java.util.Date;
128
import java.util.List;
139
import java.util.Map;
14-
import java.util.Objects;
15-
import java.util.Optional;
1610
import java.util.Set;
1711
import java.util.UUID;
18-
import org.broadinstitute.consent.http.db.DraftDAO;
19-
import org.broadinstitute.consent.http.enumeration.UserRoles;
12+
import org.broadinstitute.consent.http.cloudstore.GCSService;
2013
import org.broadinstitute.consent.http.models.DraftInterface;
2114
import org.broadinstitute.consent.http.models.DraftSummary;
2215
import org.broadinstitute.consent.http.models.FileStorageObject;
2316
import org.broadinstitute.consent.http.models.User;
17+
import org.broadinstitute.consent.http.service.dao.DraftServiceDAO;
18+
import org.broadinstitute.consent.http.util.ConsentLogger;
2419
import org.broadinstitute.consent.http.util.gson.GsonUtil;
2520
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
26-
import org.jdbi.v3.core.Jdbi;
2721

28-
public class DraftService {
29-
30-
private final Jdbi jdbi;
31-
private final DraftDAO draftDAO;
32-
private final DraftFileStorageService draftFileStorageService;
22+
public class DraftService implements ConsentLogger {
23+
DraftServiceDAO draftServiceDAO;
24+
GCSService gcsService;
3325

3426
@Inject
35-
public DraftService(Jdbi jdbi, DraftDAO draftDAO,
36-
DraftFileStorageService draftFileStorageService) {
37-
this.jdbi = jdbi;
38-
this.draftDAO = draftDAO;
39-
this.draftFileStorageService = draftFileStorageService;
40-
}
41-
42-
public void insertDraft(DraftInterface draft)
43-
throws SQLException, BadRequestException {
44-
jdbi.useHandle(handle -> {
45-
handle.getConnection().setAutoCommit(false);
46-
try {
47-
draftDAO.insert(draft.getName(), draft.getCreateDate().toInstant(),
48-
draft.getCreateUser().getUserId(), draft.getJson(), draft.getUUID(),
49-
draft.getType().getValue());
50-
} catch (Exception e) {
51-
handle.rollback();
52-
throw new BadRequestException(
53-
"Error submitting draft. Drafts require valid json to be submitted.");
54-
}
55-
handle.commit();
56-
});
57-
}
58-
59-
public DraftInterface updateDraft(DraftInterface draft, User user) throws SQLException {
60-
draft.setUpdateUser(user);
61-
draft.setUpdateDate(new Date());
62-
jdbi.useHandle(handle -> {
63-
handle.getConnection().setAutoCommit(false);
64-
try {
65-
draftDAO.updateDraftByDraftUUID(draft.getName(),
66-
draft.getUpdateDate().toInstant(), draft.getUpdateUser().getUserId(), draft.getJson(),
67-
draft.getUUID(), draft.getType().getValue());
68-
} catch (Exception e) {
69-
handle.rollback();
70-
}
71-
handle.commit();
72-
});
73-
return getAuthorizedDraft(draft.getUUID(), user);
27+
public DraftService(DraftServiceDAO draftServiceDAO, GCSService gcsService) {
28+
this.draftServiceDAO = draftServiceDAO;
29+
this.gcsService = gcsService;
7430
}
7531

76-
public DraftInterface getAuthorizedDraft(UUID draftUUID, User user) {
77-
DraftInterface draft;
78-
try {
79-
draft = findDraftByDraftUUID(draftUUID);
80-
} catch (SQLException e) {
81-
throw new NotFoundException(
82-
String.format("Draft with UUID %s not found.", draftUUID.toString()));
83-
}
84-
if (Objects.isNull(draft)) {
85-
throw new NotFoundException(
86-
String.format("Draft with UUID %s not found.", draftUUID.toString()));
87-
}
88-
if (!user.getUserId().equals(draft.getCreateUser().getUserId()) && !user.hasUserRole(
89-
UserRoles.ADMIN)) {
90-
throw new NotAuthorizedException("User not authorized to modify resource.");
91-
}
92-
return draft;
32+
public DraftInterface getAuthorizedDraft(UUID uuid, User user) {
33+
return draftServiceDAO.getAuthorizedDraft(uuid, user);
9334
}
9435

95-
public void deleteDraftsByUser(User user) {
96-
Set<DraftInterface> userDrafts = findDraftsForUser(user);
97-
for (DraftInterface draft : userDrafts) {
98-
deleteDraft(draft, user);
99-
}
100-
}
101-
102-
public Set<DraftSummary> findDraftSummariesForUser(User user) {
103-
return draftDAO.findDraftSummariesByUserId(user.getUserId());
36+
public List<FileStorageObject> addAttachments(DraftInterface draft, User user, Map<String, FormDataBodyPart> files)
37+
throws SQLException {
38+
return draftServiceDAO.addAttachments(draft, user, files);
10439
}
10540

106-
public Set<DraftInterface> findDraftsForUser(User user) {
107-
return draftDAO.findDraftsByUserId(user.getUserId());
41+
public InputStream getDraftAttachmentStream(FileStorageObject targetAttachment) {
42+
return gcsService.getDocument(targetAttachment.getBlobId());
10843
}
10944

110-
private DraftInterface findDraftByDraftUUID(
111-
UUID draftUUID) throws SQLException {
112-
return draftDAO.findDraftById(draftUUID);
45+
public void deleteDraftAttachment(DraftInterface draft, User user, Integer fileId)
46+
throws SQLException {
47+
draftServiceDAO.deleteDraftAttachment(draft, user, fileId);
11348
}
11449

115-
public List<FileStorageObject> addAttachments(DraftInterface draft, User user,
116-
Map<String, FormDataBodyPart> files) throws SQLException {
117-
List<FileStorageObject> storedFiles = draftFileStorageService.storeDraftFiles(draft.getUUID(), user, files);
118-
draftDAO.updateDraftByDraftUUID(draft.getUUID(),
119-
new Date().toInstant(), user.getUserId());
120-
return storedFiles;
50+
public StreamingOutput draftAsJson(DraftInterface draft) {
51+
Gson gson = GsonUtil.buildGson();
52+
return output -> {
53+
output.write("{ \"document\":".getBytes());
54+
output.write(draft.getJson().getBytes());
55+
output.write(", \"meta\":".getBytes());
56+
output.write(gson.toJson(draft).getBytes());
57+
output.write("}".getBytes());
58+
};
12159
}
12260

123-
public void deleteDraftAttachment(DraftInterface draft, User user, Integer fileId)
124-
throws SQLException {
125-
Optional<FileStorageObject> fileStorageObjectToDelete = draft.getStoredFiles().stream()
126-
.filter(fileStorageObject -> fileStorageObject.getFileStorageObjectId().equals(fileId))
127-
.findFirst();
128-
if (fileStorageObjectToDelete.isPresent()) {
129-
draftFileStorageService.deleteStoredFile(fileStorageObjectToDelete.get(), user);
130-
draftDAO.updateDraftByDraftUUID(draft.getUUID(),
131-
new Date().toInstant(), user.getUserId());
132-
} else {
133-
throw new NotFoundException(
134-
String.format("Draft attachment is not found. Draft: %s, Attachment: %d",
135-
draft.getUUID(), fileId));
136-
}
61+
public void insertDraft(DraftInterface draft) throws SQLException {
62+
draftServiceDAO.insertDraft(draft);
13763
}
13864

139-
public void deleteDraft(DraftInterface draft, User user)
140-
throws RuntimeException {
141-
jdbi.useHandle(handle -> {
142-
try {
143-
handle.useTransaction(handler -> {
144-
draftDAO.deleteDraftByUUIDList(List.of(draft.getUUID()));
145-
draft.getStoredFiles().forEach(fileStorageObject -> {
146-
try {
147-
draftFileStorageService.deleteStoredFile(fileStorageObject, user);
148-
} catch (SQLException e) {
149-
throw new RuntimeException(e);
150-
}
151-
});
152-
});
153-
} catch (Exception e) {
154-
handle.rollback();
155-
}
156-
handle.commit();
157-
});
65+
public Set<DraftSummary> findDraftSummariesForUser(User user) {
66+
return draftServiceDAO.findDraftSummariesForUser(user);
15867
}
15968

160-
public StreamingOutput draftAsJson(DraftInterface draft) {
161-
Gson gson = GsonUtil.buildGson();
162-
return output -> {
163-
output.write("{ \"document\":".getBytes());
164-
output.write(draft.getJson().getBytes());
165-
output.write(", \"meta\":".getBytes());
166-
output.write(gson.toJson(draft).getBytes());
167-
output.write("}".getBytes());
168-
};
69+
public DraftInterface updateDraft(DraftInterface draft, User user) throws SQLException {
70+
return draftServiceDAO.updateDraft(draft, user);
16971
}
17072

171-
public InputStream getDraftAttachmentStream(FileStorageObject targetAttachment) {
172-
return draftFileStorageService.get(targetAttachment);
73+
public void deleteDraft(DraftInterface draft, User user) {
74+
draftServiceDAO.deleteDraft(draft, user);
17375
}
17476
}

src/main/java/org/broadinstitute/consent/http/service/UserService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.broadinstitute.consent.http.models.UserUpdateFields;
4040
import org.broadinstitute.consent.http.models.Vote;
4141
import org.broadinstitute.consent.http.resources.Resource;
42+
import org.broadinstitute.consent.http.service.dao.DraftServiceDAO;
4243
import org.broadinstitute.consent.http.service.dao.UserServiceDAO;
4344
import org.broadinstitute.consent.http.util.ConsentLogger;
4445
import org.broadinstitute.consent.http.util.gson.GsonUtil;
@@ -61,14 +62,14 @@ public class UserService implements ConsentLogger {
6162
private final UserServiceDAO userServiceDAO;
6263
private final DaaDAO daaDAO;
6364
private final EmailService emailService;
64-
private final DraftService draftService;
65+
private final DraftServiceDAO draftServiceDAO;
6566

6667
@Inject
6768
public UserService(UserDAO userDAO, UserPropertyDAO userPropertyDAO, UserRoleDAO userRoleDAO,
6869
VoteDAO voteDAO, InstitutionDAO institutionDAO, LibraryCardDAO libraryCardDAO,
6970
AcknowledgementDAO acknowledgementDAO, FileStorageObjectDAO fileStorageObjectDAO,
7071
SamDAO samDAO, UserServiceDAO userServiceDAO, DaaDAO daaDAO, EmailService emailService,
71-
DraftService draftService) {
72+
DraftServiceDAO draftServiceDAO) {
7273
this.userDAO = userDAO;
7374
this.userPropertyDAO = userPropertyDAO;
7475
this.userRoleDAO = userRoleDAO;
@@ -81,7 +82,7 @@ public UserService(UserDAO userDAO, UserPropertyDAO userPropertyDAO, UserRoleDAO
8182
this.userServiceDAO = userServiceDAO;
8283
this.daaDAO = daaDAO;
8384
this.emailService = emailService;
84-
this.draftService = draftService;
85+
this.draftServiceDAO = draftServiceDAO;
8586
}
8687

8788
/**
@@ -268,7 +269,7 @@ public void deleteUserByEmail(String email) {
268269
List<Integer> voteIds = votes.stream().map(Vote::getVoteId).collect(Collectors.toList());
269270
voteDAO.removeVotesByIds(voteIds);
270271
}
271-
draftService.deleteDraftsByUser(user);
272+
draftServiceDAO.deleteDraftsByUser(user);
272273
institutionDAO.deleteAllInstitutionsByUser(userId);
273274
userPropertyDAO.deleteAllPropertiesByUser(userId);
274275
libraryCardDAO.deleteAllLibraryCardsByUser(userId);

src/main/java/org/broadinstitute/consent/http/service/DraftFileStorageService.java renamed to src/main/java/org/broadinstitute/consent/http/service/dao/DraftFileStorageServiceDAO.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.broadinstitute.consent.http.service;
1+
package org.broadinstitute.consent.http.service.dao;
22

33
import com.google.cloud.storage.BlobId;
44
import com.google.inject.Inject;
@@ -20,14 +20,14 @@
2020
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
2121
import org.jdbi.v3.core.Jdbi;
2222

23-
public class DraftFileStorageService implements ConsentLogger {
23+
public class DraftFileStorageServiceDAO implements ConsentLogger {
2424

2525
Jdbi jdbi;
2626
GCSService gcsService;
2727
FileStorageObjectDAO fileStorageObjectDAO;
2828

2929
@Inject
30-
public DraftFileStorageService(Jdbi jdbi, GCSService gcsService,
30+
public DraftFileStorageServiceDAO(Jdbi jdbi, GCSService gcsService,
3131
FileStorageObjectDAO fileStorageObjectDAO) {
3232
this.jdbi = jdbi;
3333
this.gcsService = gcsService;
@@ -107,8 +107,4 @@ private FileStorageObject store(FormDataBodyPart file, User user, UUID draftId)
107107
throw new RuntimeException(e);
108108
}
109109
}
110-
111-
public InputStream get(FileStorageObject fileStorageObject) {
112-
return gcsService.getDocument(fileStorageObject.getBlobId());
113-
}
114110
}

0 commit comments

Comments
 (0)