AutomationClientResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) public ClientDetailResponse createClient( @Auth AutomationClient automationClient, @Valid CreateClientRequest clientRequest) { Optional<Client> client = clientDAO.getClientByName(clientRequest.name); if (client.isPresent()) { logger.info("Automation ({}) - Client {} already exists", automationClient.getName(), clientRequest.name); throw new ConflictException("Client name already exists."); } long id = clientDAO.createClient(clientRequest.name, automationClient.getName(), "", null); client = clientDAO.getClientById(id); if (client.isPresent()) { Map<String, String> extraInfo = new HashMap<>(); extraInfo.put("deprecated", "true"); auditLog.recordEvent( new Event(Instant.now(), EventTag.CLIENT_CREATE, automationClient.getName(), client.get().getName(), extraInfo)); } return ClientDetailResponse.fromClient(client.get(), ImmutableList.of(), ImmutableList.of()); } @Inject  AutomationClientResource(ClientDAOFactory clientDAOFactory, AclDAOFactory aclDAOFactory,
      AuditLog auditLog); @VisibleForTesting  AutomationClientResource(ClientDAO clientDAO, AclDAO aclDAO,
      AuditLog auditLog); @Timed @ExceptionMetered @GET @Path("{clientId}") Response findClientById(
      @Auth AutomationClient automationClient,
      @PathParam("clientId") LongParam clientId); @Timed @ExceptionMetered @GET Response findClient(
      @Auth AutomationClient automationClient,
      @QueryParam("name") Optional<String> name); @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) ClientDetailResponse createClient(
      @Auth AutomationClient automationClient,
      @Valid CreateClientRequest clientRequest); @Timed @ExceptionMetered @DELETE @Path("{clientId}") Response deleteClient(@Auth AutomationClient automationClient,
      @PathParam("clientId") LongParam clientId);  }
@Test public void createNewClient() { Client client = new Client(543L, "client", "2nd client", null, now, "test", now, "test", null, null, true, false); CreateClientRequest request = new CreateClientRequest("client"); when(clientDAO.getClientByName("client")).thenReturn(Optional.empty()); when(clientDAO.createClient("client", automation.getName(), "", null)).thenReturn(543L); when(clientDAO.getClientById(543L)).thenReturn(Optional.of(client)); when(aclDAO.getGroupsFor(client)).thenReturn(ImmutableSet.of()); ClientDetailResponse response = ClientDetailResponse.fromClient(client, ImmutableList.of(), ImmutableList.of()); ClientDetailResponse response1 = resource.createClient(automation, request); assertThat(response.name).isEqualTo(response1.name); }
@Test public void createNewClientAlreadyExists() { Client client = new Client(543L, "client", "2nd client", null, now, "test", now, "test", null, null, true, false); CreateClientRequest request = new CreateClientRequest("client"); when(clientDAO.getClientByName("client")).thenReturn(Optional.empty()); when(clientDAO.createClient("client", automation.getName(), "", null)).thenReturn(543L); when(clientDAO.getClientById(543L)).thenReturn(Optional.of(client)); ClientDetailResponse response = ClientDetailResponse.fromClient(client, ImmutableList.of(), ImmutableList.of()); ClientDetailResponse response1 = resource.createClient(automation, request); assertThat(response.name).isEqualTo(response1.name); }