SecretsResource { protected SanitizedSecret retrieveSecret(@Auth User user, String name) { logger.info("User '{}' retrieving secret name={}.", user, name); return sanitizedSecretFromName(name); } @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 includesTheSecret() { when(secretController.getSecretById(22)).thenReturn(Optional.of(secret)); when(aclDAO.getGroupsFor(secret)).thenReturn(Collections.emptySet()); when(aclDAO.getClientsFor(secret)).thenReturn(Collections.emptySet()); SecretDetailResponse response = resource.retrieveSecret(user, new LongParam("22")); assertThat(response.id).isEqualTo(secret.getId()); assertThat(response.name).isEqualTo(secret.getName()); assertThat(response.description).isEqualTo(secret.getDescription()); assertThat(response.createdAt).isEqualTo(secret.getCreatedAt()); assertThat(response.createdBy).isEqualTo(secret.getCreatedBy()); assertThat(response.updatedAt).isEqualTo(secret.getUpdatedAt()); assertThat(response.updatedBy).isEqualTo(secret.getUpdatedBy()); assertThat(response.metadata).isEqualTo(secret.getMetadata()); }
@Test public void handlesNoAssociations() { when(secretController.getSecretById(22)).thenReturn(Optional.of(secret)); when(aclDAO.getGroupsFor(secret)).thenReturn(Collections.emptySet()); when(aclDAO.getClientsFor(secret)).thenReturn(Collections.emptySet()); SecretDetailResponse response = resource.retrieveSecret(user, new LongParam("22")); assertThat(response.groups).isEmpty(); assertThat(response.clients).isEmpty(); }
@Test public void includesAssociations() { Client client = new Client(0, "client", null, null, null, null, null, null, null, null, false, false ); Group group1 = new Group(0, "group1", null, null, null, null, null, null); Group group2 = new Group(0, "group2", null, null, null, null, null, null); when(secretController.getSecretById(22)).thenReturn(Optional.of(secret)); when(aclDAO.getGroupsFor(secret)).thenReturn(Sets.newHashSet(group1, group2)); when(aclDAO.getClientsFor(secret)).thenReturn(Sets.newHashSet(client)); SecretDetailResponse response = resource.retrieveSecret(user, new LongParam("22")); assertThat(response.groups).containsOnly(group1, group2); assertThat(response.clients).containsOnly(client); }
@Test(expected = NotFoundException.class) public void badIdNotFound() { when(secretController.getSecretById(0xbad1d)).thenReturn(Optional.empty()); resource.retrieveSecret(user, new LongParam(Long.toString(0xbad1d))); }
@Test public void findSecretByNameAndVersion() { when(secretController.getSecretByName(secret.getName())) .thenReturn(Optional.of(secret)); assertThat(resource.retrieveSecret(user, "name")) .isEqualTo(SanitizedSecret.fromSecret(secret)); }
@Test(expected = NotFoundException.class) public void badNameNotFound() { when(secretController.getSecretByName("non-existent")).thenReturn(Optional.empty()); resource.retrieveSecret(user, "non-existent"); }