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