AutomationGroupResource { @Timed @ExceptionMetered @GET public Response getGroupByName( @Auth AutomationClient automationClient, @QueryParam("name") Optional<String> name) { if (name.isPresent()) { Group group = groupDAO.getGroup(name.get()).orElseThrow(NotFoundException::new); ImmutableList<Client> clients = ImmutableList.copyOf(aclDAO.getClientsFor(group)); ImmutableList<SanitizedSecret> sanitizedSecrets = ImmutableList.copyOf(aclDAO.getSanitizedSecretsFor(group)); return Response.ok() .entity(GroupDetailResponse.fromGroup(group, sanitizedSecrets, clients)) .build(); } ImmutableList<SanitizedSecret> emptySecrets = ImmutableList.of(); ImmutableList<Client> emptyClients = ImmutableList.of(); List<GroupDetailResponse> groups = groupDAO.getGroups().stream() .map((g) -> GroupDetailResponse.fromGroup(g, emptySecrets, emptyClients)) .collect(toList()); return Response.ok() .entity(groups) .build(); } @Inject  AutomationGroupResource(GroupDAOFactory groupDAOFactory, AclDAOFactory aclDAOFactory,
      AuditLog auditLog); @VisibleForTesting  AutomationGroupResource(GroupDAO groupDAO, AclDAO aclDAO, AuditLog auditLog); @Timed @ExceptionMetered @GET @Path("{groupId}") GroupDetailResponse getGroupById(
      @Auth AutomationClient automationClient,
      @PathParam("groupId") LongParam groupId); @Timed @ExceptionMetered @GET Response getGroupByName(
      @Auth AutomationClient automationClient,
      @QueryParam("name") Optional<String> name); @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Group createGroup(
      @Auth AutomationClient automationClient,
      @Valid CreateGroupRequest groupRequest); @Timed @ExceptionMetered @DELETE @Path("{groupId}") Response deleteGroup(
      @Auth AutomationClient automationClient,
      @PathParam("groupId") LongParam groupId);  }
@Test public void findGroupByName() { Group group = new Group(50, "testGroup", "testing group", now, "automation client", now, "automation client", ImmutableMap.of("app", "keywhiz")); when(groupDAO.getGroup("testGroup")).thenReturn(Optional.of(group)); when(aclDAO.getClientsFor(group)).thenReturn(ImmutableSet.of()); when(aclDAO.getSanitizedSecretsFor(group)).thenReturn(ImmutableSet.of()); GroupDetailResponse expectedResponse = GroupDetailResponse.fromGroup(group, ImmutableList.of(), ImmutableList.of()); Response response = resource.getGroupByName(automation, Optional.of("testGroup")); assertThat(response.getEntity()).isEqualTo(expectedResponse); }
@Test public void groupIncludesClientsAndSecrets() { Group group = new Group(50, "testGroup", "testing group", now, "automation client", now, "automation client", ImmutableMap.of("app", "keywhiz")); Client groupClient = new Client(1, "firstClient", "Group client", null, now, "test", now, "test", null, null, true, true); SanitizedSecret firstGroupSecret = SanitizedSecret.of(1, "name1", "desc", "checksum", now, "test", now, "test", null, "", null, 1136214245, 125L, now, "test"); SanitizedSecret secondGroupSecret = SanitizedSecret.of(2, "name2", "desc", "checksum", now, "test", now, "test", null, "", null, 1136214245, 250L, now, "test"); when(groupDAO.getGroup("testGroup")).thenReturn(Optional.of(group)); when(aclDAO.getClientsFor(group)).thenReturn(ImmutableSet.of(groupClient)); when(aclDAO.getSanitizedSecretsFor(group)) .thenReturn(ImmutableSet.of(firstGroupSecret, secondGroupSecret)); GroupDetailResponse expectedResponse = GroupDetailResponse.fromGroup(group, ImmutableList.of(firstGroupSecret, secondGroupSecret), ImmutableList.of(groupClient)); Response response = resource.getGroupByName(automation, Optional.of("testGroup")); assertThat(response.getEntity()).isEqualTo(expectedResponse); }