SecretsResource { @Path("{secretId}") @Timed @ExceptionMetered @DELETE public Response deleteSecret(@Auth User user, @PathParam("secretId") LongParam secretId) { Optional<Secret> secret = secretController.getSecretById(secretId.get()); if (!secret.isPresent()) { logger.info("User '{}' tried deleting a secret which was not found (id={})", user, secretId.get()); throw new NotFoundException("Secret not found."); } logger.info("User '{}' deleting secret id={}, name='{}'", user, secretId, secret.get().getName()); Set<String> groups = aclDAOReadOnly.getGroupsFor(secret.get()).stream().map(Group::getName).collect(toSet()); secretDAOReadWrite.deleteSecretsByName(secret.get().getName()); Map<String, String> extraInfo = new HashMap<>(); extraInfo.put("groups", groups.toString()); extraInfo.put("current version", secret.get().getVersion().toString()); auditLog.recordEvent( new Event(Instant.now(), EventTag.SECRET_DELETE, user.getName(), secret.get().getName(), extraInfo)); return Response.noContent().build(); } @SuppressWarnings("unused") @Inject  SecretsResource(SecretController secretController, AclDAOFactory aclDAOFactory,
      SecretDAOFactory secretDAOFactory, AuditLog auditLog); @VisibleForTesting  SecretsResource(SecretController secretController, AclDAO aclDAOReadOnly,
      SecretDAO secretDAOReadWrite, AuditLog auditLog); @Timed @ExceptionMetered @GET Response findSecrets(@Auth User user, @DefaultValue("") @QueryParam("name") String name,
      @DefaultValue("") @QueryParam("nameOnly") String nameOnly, @QueryParam("idx") Integer idx,
      @QueryParam("num") Integer num,
      @DefaultValue("true") @QueryParam("newestFirst") Boolean newestFirst); @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Response createSecret(@Auth User user, @Valid CreateSecretRequestV2 request); @Path("{name}") @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Response createOrUpdateSecret(@Auth User user, @PathParam("name") String secretName,
      @Valid CreateOrUpdateSecretRequestV2 request); @Path("{name}/partialupdate") @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Response partialUpdateSecret(@Auth User user, @PathParam("name") String secretName, @Valid
      PartialUpdateSecretRequestV2 request); @Path("{secretId}") @Timed @ExceptionMetered @GET SecretDetailResponse retrieveSecret(@Auth User user,
      @PathParam("secretId") LongParam secretId); @Timed @ExceptionMetered @GET @Path("versions/{name}") @Produces(APPLICATION_JSON) List<SanitizedSecret> secretVersions(@Auth User user,
      @PathParam("name") String name, @QueryParam("versionIdx") int versionIdx,
      @QueryParam("numVersions") int numVersions); @Path("rollback/{secretName}/{versionId}") @Timed @ExceptionMetered @POST Response resetSecretVersion(@Auth User user, @PathParam("secretName") String secretName,
      @PathParam("versionId") LongParam versionId); @Path("{secretId}") @Timed @ExceptionMetered @DELETE Response deleteSecret(@Auth User user, @PathParam("secretId") LongParam secretId);  }
@Test public void canDelete() { when(secretController.getSecretById(0xdeadbeef)).thenReturn(Optional.of(secret)); HashSet<Group> groups = new HashSet<>(); groups.add(new Group(0, "group1", "", NOW, null, NOW, null, null)); groups.add(new Group(0, "group2", "", NOW, null, NOW, null, null)); when(aclDAO.getGroupsFor(secret)).thenReturn(groups); Response response = resource.deleteSecret(user, new LongParam(Long.toString(0xdeadbeef))); verify(secretDAO).deleteSecretsByName("name"); assertThat(response.getStatus()).isEqualTo(204); }
@Test (expected = NotFoundException.class) public void deleteErrorsOnNotFound() { when(secretController.getSecretById(0xdeadbeef)).thenReturn(Optional.empty()); resource.deleteSecret(user, new LongParam(Long.toString(0xdeadbeef))); }