• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DataBiosphere / consent / #5612

25 Mar 2025 08:50PM UTC coverage: 79.273% (-0.06%) from 79.335%
#5612

push

web-flow
[DT-643][risk=no] Optimize tests so they can run locally. (#2471)

Co-authored-by: fboulnois <fboulnois@users.noreply.github.com>

10273 of 12959 relevant lines covered (79.27%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/src/main/java/org/broadinstitute/consent/http/service/dao/DraftFileStorageServiceDAO.java
1
package org.broadinstitute.consent.http.service.dao;
2

3
import com.google.cloud.storage.BlobId;
4
import com.google.inject.Inject;
5
import jakarta.ws.rs.NotFoundException;
6
import java.io.InputStream;
7
import java.sql.SQLException;
8
import java.time.Instant;
9
import java.util.ArrayList;
10
import java.util.Date;
11
import java.util.List;
12
import java.util.Map;
13
import java.util.UUID;
14
import org.broadinstitute.consent.http.cloudstore.GCSService;
15
import org.broadinstitute.consent.http.db.FileStorageObjectDAO;
16
import org.broadinstitute.consent.http.enumeration.FileCategory;
17
import org.broadinstitute.consent.http.models.FileStorageObject;
18
import org.broadinstitute.consent.http.models.User;
19
import org.broadinstitute.consent.http.util.ConsentLogger;
20
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
21
import org.jdbi.v3.core.Jdbi;
22

23
public class DraftFileStorageServiceDAO implements ConsentLogger {
24

25
  Jdbi jdbi;
26
  GCSService gcsService;
27
  FileStorageObjectDAO fileStorageObjectDAO;
28

29
  @Inject
30
  public DraftFileStorageServiceDAO(Jdbi jdbi, GCSService gcsService,
31
      FileStorageObjectDAO fileStorageObjectDAO) {
1✔
32
    this.jdbi = jdbi;
1✔
33
    this.gcsService = gcsService;
1✔
34
    this.fileStorageObjectDAO = fileStorageObjectDAO;
1✔
35
  }
1✔
36

37
  public List<FileStorageObject> storeDraftFiles(UUID associatedId, User user,
38
      Map<String, FormDataBodyPart> files) throws SQLException {
39
    List<FileStorageObject> fileStorageObjects = new ArrayList<>();
1✔
40
    jdbi.useHandle(handle -> {
1✔
41
      handle.getConnection().setAutoCommit(false);
1✔
42
      try {
43
        files.forEach((String key, FormDataBodyPart file) -> {
1✔
44
          fileStorageObjects.add(store(file, user, associatedId));
1✔
45
        });
1✔
46
      } catch (Exception e) {
×
47
        fileStorageObjects.forEach(file -> {
×
48
          try {
49
            deleteStoredFile(file, user);
×
50
          } catch (SQLException ex) {
×
51
            logWarn(String.format("Error rolling back files in GCS for draft: %s, gcsuri: %s",
×
52
                associatedId.toString(), file.getBlobId().toGsUtilUri()));
×
53
            throw new RuntimeException(ex);
×
54
          }
×
55
        });
×
56
        handle.getConnection().rollback();
×
57
      }
1✔
58
      handle.commit();
1✔
59
    });
1✔
60
    return fileStorageObjects;
1✔
61
  }
62

63
  public void deleteStoredFile(FileStorageObject fileStorageObject, User user) throws SQLException, NotFoundException {
64
    jdbi.useHandle(handle -> {
1✔
65
      handle.getConnection().setAutoCommit(false);
1✔
66
      try {
67
        gcsService.deleteDocument(fileStorageObject.getBlobId().getName());
1✔
68
        fileStorageObjectDAO.deleteFileById(fileStorageObject.getFileStorageObjectId(),
1✔
69
            user.getUserId(), new Date().toInstant());
1✔
70
      } catch (Exception e) {
1✔
71
        logWarn(String.format("Error deleting stored file for user: %s, file obj id: %d, error: %s",
1✔
72
            user.getEmail(), fileStorageObject.getFileStorageObjectId(), e));
1✔
73
        handle.rollback();
1✔
74
        throw new NotFoundException("Error deleting stored file for user: " + user.getEmail(), e);
1✔
75
      }
1✔
76
      handle.commit();
1✔
77
    });
1✔
78
  }
1✔
79

80
  private FileStorageObject store(FormDataBodyPart file, User user, UUID draftId)
81
      throws RuntimeException {
82
    BlobId blobId;
83
    try {
84
      // upload to GCS
85
      blobId = gcsService.storeDocument(file.getValueAs(InputStream.class),
1✔
86
          file.getMediaType().toString(), UUID.randomUUID());
1✔
87
      Integer fileStorageObjectId = fileStorageObjectDAO.insertNewFile(file.getName(),
1✔
88
          FileCategory.DRAFT_UPLOADED_FILE.getValue(), blobId.toGsUtilUri(),
1✔
89
          file.getMediaType().toString(), draftId.toString(), user.getUserId(), Instant.now());
1✔
90
      return fileStorageObjectDAO.findFileById(fileStorageObjectId);
1✔
91
    } catch (Exception e) {
×
92
      logWarn(String.format("Error storing file for user: %s, draft id : %s, error: %s",
×
93
          user.getEmail(), draftId.toString(), e));
×
94
      throw new RuntimeException(e);
×
95
    }
96
  }
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc