SecretsResource { @Path("{name}") @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) public Response createOrUpdateSecret(@Auth User user, @PathParam("name") String secretName, @Valid CreateOrUpdateSecretRequestV2 request) { logger.info("User '{}' createOrUpdate secret '{}'.", user, secretName); Secret secret = secretController .builder(secretName, request.content(), user.getName(), request.expiry()) .withDescription(request.description()) .withMetadata(request.metadata()) .withType(request.type()) .createOrUpdate(); URI uri = UriBuilder.fromResource(SecretsResource.class).path(secretName).build(); Response response = Response.created(uri).entity(secretDetailResponseFromId(secret.getId())).build(); if (response.getStatus() == HttpStatus.SC_CREATED) { Map<String, String> extraInfo = new HashMap<>(); if (request.description() != null && !request.description().isEmpty()) { extraInfo.put("description", request.description()); } if (request.metadata() != null && !request.metadata().isEmpty()) { extraInfo.put("metadata", request.metadata().toString()); } extraInfo.put("expiry", Long.toString(request.expiry())); auditLog.recordEvent( new Event(Instant.now(), EventTag.SECRET_CREATEORUPDATE, user.getName(), secretName, extraInfo)); } return response; } @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 createOrUpdateSecret() throws Exception { when(secretController.getSecretById(secret.getId())).thenReturn(Optional.of(secret)); SecretController.SecretBuilder secretBuilder = mock(SecretController.SecretBuilder.class); when(secretController.builder(secret.getName(), secret.getSecret(), user.getName(), 0)) .thenReturn(secretBuilder); when(secretBuilder.withDescription(any())).thenReturn(secretBuilder); when(secretBuilder.withMetadata(any())).thenReturn(secretBuilder); when(secretBuilder.withType(any())).thenReturn(secretBuilder); when(secretBuilder.createOrUpdate()).thenReturn(secret); CreateOrUpdateSecretRequestV2 req = CreateOrUpdateSecretRequestV2.builder() .description(secret.getDescription()) .content(secret.getSecret()) .build(); Response response = resource.createOrUpdateSecret(user, secret.getName(), req); assertThat(response.getStatus()).isEqualTo(201); assertThat(response.getMetadata().get(HttpHeaders.LOCATION)) .containsExactly(new URI("/admin/secrets/" + secret.getName())); }