AutomationClientResource { @Timed @ExceptionMetered @GET public Response findClient( @Auth AutomationClient automationClient, @QueryParam("name") Optional<String> name) { logger.info("Automation ({}) - Looking up a name {}", automationClient.getName(), name); if (name.isPresent()) { Client client = clientDAO.getClientByName(name.get()).orElseThrow(NotFoundException::new); ImmutableList<Group> groups = ImmutableList.copyOf(aclDAO.getGroupsFor(client)); return Response.ok() .entity(ClientDetailResponse.fromClient(client, groups, ImmutableList.of())) .build(); } List<ClientDetailResponse> clients = clientDAO.getClients().stream() .map(c -> ClientDetailResponse.fromClient(c, ImmutableList.copyOf(aclDAO.getGroupsFor(c)), ImmutableList.of())) .collect(toList()); return Response.ok().entity(clients).build(); } @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 findClientByName() { Client client = new Client(2, "client", "2nd client", null, now, "test", now, "test", null, null, true, false); Group firstGroup = new Group(1, "first Group", "testing group", now, "client", now, "client", ImmutableMap.of("app", "keywhiz")); Group secondGroup = new Group(2, "second Group", "testing group", now, "client", now, "client", ImmutableMap.of("app", "keywhiz")); ClientDetailResponse expectedClient = ClientDetailResponse.fromClient(client, ImmutableList.of(firstGroup, secondGroup), ImmutableList.of()); when(clientDAO.getClientByName("client")).thenReturn(Optional.of(client)); when(aclDAO.getGroupsFor(client)).thenReturn(ImmutableSet.of(firstGroup, secondGroup)); Response response = resource.findClient(automation, Optional.of("client")); assertThat(response.getEntity()).hasSameClassAs(expectedClient); ClientDetailResponse actualResponse = (ClientDetailResponse) response.getEntity(); assertThat(actualResponse).isEqualToComparingFieldByField(expectedClient); }
@Test(expected = NotFoundException.class) public void findClientByNameNotFound() { when(clientDAO.getClientByName("client")).thenReturn(Optional.empty()); resource.findClient(automation, Optional.of("client")); }