SecretDAO { public Optional<SecretSeriesAndContent> getSecretById(long secretId) { return dslContext.<Optional<SecretSeriesAndContent>>transactionResult(configuration -> { SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration); SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration); Optional<SecretSeries> series = secretSeriesDAO.getSecretSeriesById(secretId); if (series.isPresent() && series.get().currentVersion().isPresent()) { long secretContentId = series.get().currentVersion().get(); Optional<SecretContent> contents = secretContentDAO.getSecretContentById(secretContentId); if (!contents.isPresent()) { throw new IllegalStateException( format("failed to fetch secret %d, content %d not found.", secretId, secretContentId)); } return Optional.of(SecretSeriesAndContent.of(series.get(), contents.get())); } return Optional.empty(); }); } SecretDAO(DSLContext dslContext, SecretContentDAOFactory secretContentDAOFactory,
      SecretSeriesDAOFactory secretSeriesDAOFactory, ContentCryptographer cryptographer); @VisibleForTesting long createSecret(String name, String encryptedSecret, String hmac,
      String creator, Map<String, String> metadata, long expiry, String description,
      @Nullable String type, @Nullable Map<String, String> generationOptions); @VisibleForTesting long createOrUpdateSecret(String name, String encryptedSecret, String hmac, String creator,
      Map<String, String> metadata, long expiry, String description, @Nullable String type,
      @Nullable Map<String, String> generationOptions); @VisibleForTesting long partialUpdateSecret(String name, String creator,
      PartialUpdateSecretRequestV2 request); boolean setExpiration(String name, Instant expiration); Optional<SecretSeriesAndContent> getSecretById(long secretId); Optional<SecretSeriesAndContent> getSecretByName(String name); List<SecretSeriesAndContent> getSecretsByName(List<String> names); ImmutableList<SecretSeriesAndContent> getSecrets(@Nullable Long expireMaxTime,
      @Nullable Group group, @Nullable Long expireMinTime, @Nullable String minName,
      @Nullable Integer limit); ImmutableList<SimpleEntry<Long, String>> getSecretsNameOnly(); ImmutableList<SecretSeriesAndContent> getSecretsBatched(int idx, int num,
      boolean newestFirst); Optional<ImmutableList<SanitizedSecret>> getSecretVersionsByName(String name,
      int versionIdx, int numVersions); void setCurrentSecretVersionByName(String name, long versionId, String updater); void deleteSecretsByName(String name); int countDeletedSecrets(); int countSecretsDeletedBeforeDate(DateTime deleteBefore); void dangerPermanentlyRemoveSecretsDeletedBeforeDate(DateTime deletedBefore,
      int sleepMillis);  }
@Test public void getSecretById() { assertThat(secretDAO.getSecretById(series2.id())).isEqualTo(Optional.of(secret2b)); }
@Test public void getSecretByIdOneReturnsEmptyWhenCurrentVersionIsNull() { jooqContext.update(SECRETS) .set(SECRETS.CURRENT, (Long) null) .where(SECRETS.ID.eq(series2.id())) .execute(); assertThat(secretDAO.getSecretById(series2.id())).isEmpty(); }
@Test(expected = IllegalStateException.class) public void getSecretByIdOneThrowsExceptionIfCurrentVersionIsInvalid() { jooqContext.update(SECRETS) .set(SECRETS.CURRENT, -1234L) .where(SECRETS.ID.eq(series2.id())) .execute(); secretDAO.getSecretById(series2.id()); }