AutomationSecretResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) public AutomationSecretResponse createSecret( @Auth AutomationClient automationClient, @Valid CreateSecretRequest request) { SecretController.SecretBuilder builder = secretController.builder(request.name, request.content, automationClient.getName(), request.expiry) .withDescription(nullToEmpty(request.description)); if (request.metadata != null) { builder.withMetadata(request.metadata); } Secret secret; try { secret = builder.create(); } catch (DataAccessException e) { logger.info(format("Cannot create secret %s", request.name), e); throw new ConflictException(format("Cannot create secret %s.", request.name)); } ImmutableList<Group> groups = ImmutableList.copyOf(aclDAO.getGroupsFor(secret)); Map<String, String> extraInfo = new HashMap<>(); extraInfo.put("deprecated", "true"); if (request.description != null) { extraInfo.put("description", request.description); } if (request.metadata != null) { extraInfo.put("metadata", request.metadata.toString()); } extraInfo.put("expiry", Long.toString(request.expiry)); auditLog.recordEvent(new Event(Instant.now(), EventTag.SECRET_CREATE, automationClient.getName(), request.name, extraInfo)); return AutomationSecretResponse.fromSecret(secret, groups); } @Inject  AutomationSecretResource(SecretController secretController,
                                          SecretDAOFactory secretDAOFactory, AclDAOFactory aclDAOFactory, AuditLog auditLog); @VisibleForTesting  AutomationSecretResource(SecretController secretController,
                                              SecretDAO secretDAO, AclDAO aclDAO, AuditLog auditLog); @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) AutomationSecretResponse createSecret(
      @Auth AutomationClient automationClient,
      @Valid CreateSecretRequest request); @Timed @ExceptionMetered @GET ImmutableList<AutomationSecretResponse> readSecrets(
      @Auth AutomationClient automationClient, @QueryParam("name") String name); @Path("{secretId}") @Timed @ExceptionMetered @GET AutomationSecretResponse readSecretById(
      @Auth AutomationClient automationClient,
      @PathParam("secretId") LongParam secretId); @Path("{secretName}") @Timed @ExceptionMetered @DELETE Response deleteSecretSeries(
      @Auth AutomationClient automationClient,
      @PathParam("secretName") String secretName);  }
@Test public void addSecret() { CreateSecretRequest request = new CreateSecretRequest("mySecret", "some secret", "ponies", null, 0); Secret secret = new Secret(0, request.name, request.description, () -> Base64.getUrlEncoder().encodeToString(request.content.getBytes(UTF_8)), "checksum", NOW, automation.getName(), NOW, automation.getName(), request.metadata, null, null, 0, 1L, NOW, automation.getName()); when(secretBuilder.create()).thenReturn(secret); when(secretController.getSecretByName(eq(request.name))) .thenReturn(Optional.of(secret)); AutomationSecretResponse response = resource.createSecret(automation, request); assertThat(response.id()).isEqualTo(secret.getId()); assertThat(response.secret()).isEqualTo(secret.getSecret()); assertThat(response.name()).isEqualTo(secret.getDisplayName()); assertThat(response.metadata()).isEqualTo(secret.getMetadata()); }
@Test(expected = ConflictException.class) public void triesToCreateDuplicateSecret() throws Exception { DataAccessException exception = new DataAccessException(""); ImmutableMap<String,String> emptyMap = ImmutableMap.of(); doThrow(exception).when(secretBuilder).create(); CreateSecretRequest req = new CreateSecretRequest("name", "desc", "content", emptyMap, 0); resource.createSecret(automation, req); }