### Question:
MembersOperation implements PostOperation { @Override public void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors) { final List<Modification> changes = new ArrayList<>(); try { List<String> auths = new ArrayList<>(); Optional.ofNullable(request.getParameterValues(PN_MEMBERS)).ifPresent(p -> auths.addAll(Arrays.asList(p))); AuthorizableWrapper groupWrapper = Optional .ofNullable(request.getResource().adaptTo(AuthorizableWrapper.class)) .orElseThrow(() -> new RepositoryException("Failed to get group")); if (!groupWrapper.getAuthorizable().isGroup()) { throw new RepositoryException("Provided authorizable is not a group"); } Group group = (Group) groupWrapper.getAuthorizable(); response.setPath(group.getPath()); changes.add(Modification.onModified(group.getPath())); group.getDeclaredMembers().forEachRemaining(member -> { try { if (!auths.contains(member.getPath())) { log.debug("Removing member {} from {}", member, group); group.removeMember(member); changes.add(Modification.onModified(member.getPath())); } else { auths.remove(member.getPath()); } } catch (RepositoryException e) { log.warn("Failed to remove members", e); } }); for (String path : auths) { Resource resource = request.getResourceResolver().getResource(path); if (resource == null) { throw new RepositoryException("Failed to resolve authorizable at " + path); } Authorizable authorizable = Optional.ofNullable(resource.adaptTo(AuthorizableWrapper.class)) .map(AuthorizableWrapper::getAuthorizable) .orElseThrow(() -> new RepositoryException("Failed to get authorizable from: " + resource)); group.addMember(authorizable); changes.add(Modification.onModified(authorizable.getPath())); log.debug("Adding member {} to {}", authorizable, group); } if (processors != null) { for (SlingPostProcessor processor : processors) { processor.process(request, changes); } } request.getResourceResolver().commit(); response.onModified(group.getPath()); } catch (Exception e) { log.warn("Failed to update members", e); response.setError(e); } }  @Override void run(SlingHttpServletRequest request, PostResponse response, SlingPostProcessor[] processors); static final String PN_MEMBERS; }### Answer:
@Test public void testModifyOperation() throws RepositoryException { MembersOperation membersOperation = new MembersOperation(); PostResponse response = new JSONResponse(); context.currentResource("/home/groups/sling-cms/authors"); context.request().setParameterMap(ImmutableMap.<String, Object>builder() .put(":members", new String[] { "/home/users/test2", "/home/users/test3" }).build()); membersOperation.run(context.request(), response, null); assertNull(response.getError()); assertEquals("/home/groups/sling-cms/authors", response.getPath()); assertTrue(added.size() == 1); assertEquals("/home/users/test2", added.get(0)); assertTrue(removed.size() == 1); assertEquals("/home/users/test", removed.get(0)); }
@Test public void testNotGroup() throws RepositoryException { MembersOperation membersOperation = new MembersOperation(); PostResponse response = new JSONResponse(); context.currentResource("/home/users/test2"); context.request().setParameterMap(ImmutableMap.<String, Object>builder() .put(":members", new String[] { "/home/users/test2", "/home/users/test3" }).build()); membersOperation.run(context.request(), response, null); assertNotNull(response.getError()); }
@Test public void testInvalidPath() throws RepositoryException { MembersOperation membersOperation = new MembersOperation(); PostResponse response = new JSONResponse(); context.currentResource("/home/groups/sling-cms/authors"); context.request().setParameterMap(ImmutableMap.<String, Object>builder() .put(":members", new String[] { "/home/users/test2", "/home/users/test4" }).build()); membersOperation.run(context.request(), response, null); assertNotNull(response.getError()); }