GroupRest { @PUT @Path("{groupTitle}/users") @RolesAllowed("admin") @ApiOperation("Add a Mobi User to a Group") public Response addGroupUser(@PathParam("groupTitle") String groupTitle, @QueryParam("users") List<String> usernames) { if (StringUtils.isEmpty(groupTitle)) { throw ErrorUtils.sendError("Group title must be provided", Response.Status.BAD_REQUEST); } try { Group savedGroup = engineManager.retrieveGroup(rdfEngine.getEngineName(), groupTitle).orElseThrow(() -> ErrorUtils.sendError("Group " + groupTitle + " not found", Response.Status.BAD_REQUEST)); usernames.stream() .map(username -> engineManager.retrieveUser(username).orElseThrow(() -> ErrorUtils.sendError("User " + username + " not found", Response.Status.BAD_REQUEST))) .forEach(savedGroup::addMember); engineManager.updateGroup(rdfEngine.getEngineName(), savedGroup); logger.info("Added user(s) " + String.join(", ", usernames) + " to group " + groupTitle); return Response.ok().build(); } catch (IllegalArgumentException ex) { throw ErrorUtils.sendError(ex.getMessage(), Response.Status.BAD_REQUEST); } }  @GET @RolesAllowed("user") @Produces(MediaType.APPLICATION_JSON) @ApiOperation("Get all Mobi Groups") Response getGroups(); @POST @RolesAllowed("admin") @ApiOperation("Create a new Mobi Group") @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.MULTIPART_FORM_DATA) Response createGroup(@FormDataParam("title") String title,
                         @FormDataParam("description") String description,
                         @FormDataParam("roles") List<FormDataBodyPart> roles,
                         @FormDataParam("members") List<FormDataBodyPart> members); @GET @Path("{groupTitle}") @RolesAllowed("user") @Produces(MediaType.APPLICATION_JSON) @ApiOperation("Get a single Mobi Group") Response getGroup(@PathParam("groupTitle") String groupTitle); @PUT @Path("{groupTitle}") @RolesAllowed("admin") @ApiOperation("Update a Mobi Group's information") @Consumes(MediaType.APPLICATION_JSON) Response updateGroup(@PathParam("groupTitle") String groupTitle, String newGroupStr); @DELETE @Path("{groupTitle}") @RolesAllowed("admin") @ApiOperation("Remove a Mobi Group") Response deleteGroup(@PathParam("groupTitle") String groupTitle); @GET @Path("{groupTitle}/roles") @RolesAllowed("user") @Produces(MediaType.APPLICATION_JSON) @ApiOperation("List roles of a Mobi Group") Response getGroupRoles(@PathParam("groupTitle") String groupTitle); @PUT @Path("{groupTitle}/roles") @RolesAllowed("admin") @ApiOperation("Add roles to a Mobi Group") Response addGroupRoles(@PathParam("groupTitle") String groupTitle, @QueryParam("roles") List<String> roles); @DELETE @Path("{groupTitle}/roles") @RolesAllowed("admin") @ApiOperation("Remove role from a Mobi Group") Response removeGroupRole(@PathParam("groupTitle") String groupTitle, @QueryParam("role") String role); @GET @Path("{groupTitle}/users") @RolesAllowed("user") @Produces(MediaType.APPLICATION_JSON) @ApiOperation("List users of a Mobi Group") Response getGroupUsers(@PathParam("groupTitle") String groupTitle); @PUT @Path("{groupTitle}/users") @RolesAllowed("admin") @ApiOperation("Add a Mobi User to a Group") Response addGroupUser(@PathParam("groupTitle") String groupTitle, 
                                 @QueryParam("users") List<String> usernames); @DELETE @Path("{groupTitle}/users") @RolesAllowed("admin") @ApiOperation("Remove a Mobi User from a Group") Response removeGroupUser(@PathParam("groupTitle") String groupTitle, @QueryParam("user") String username);  }
@Test public void addGroupUserTest() { Map<String, User> users = new HashMap<>(); IntStream.range(1, 6) .mapToObj(Integer::toString) .forEach(s -> users.put(s, userFactory.createNew(vf.createIRI("http: Group newGroup = groupFactory.createNew(vf.createIRI("http: newGroup.setMember(Collections.singleton(user)); when(engineManager.retrieveUser(anyString())).thenAnswer(i -> Optional.of(users.get(i.getArgumentAt(0, String.class)))); when(engineManager.retrieveGroup(eq(ENGINE_NAME), anyString())).thenReturn(Optional.of(newGroup)); Response response = target().path("groups/testGroup/users").queryParam("users", users.keySet().toArray()) .request().put(Entity.entity("", MediaType.MULTIPART_FORM_DATA)); assertEquals(response.getStatus(), 200); verify(engineManager).retrieveGroup(ENGINE_NAME, "testGroup"); users.keySet().forEach(s -> verify(engineManager).retrieveUser(s)); ArgumentCaptor<Group> captor = ArgumentCaptor.forClass(Group.class); verify(engineManager).updateGroup(eq(ENGINE_NAME), captor.capture()); Group updatedGroup = captor.getValue(); assertEquals(newGroup.getResource(), updatedGroup.getResource()); Set<Resource> updatedMembers = updatedGroup.getMember_resource(); assertEquals(users.size() + 1, updatedMembers.size()); assertTrue(updatedMembers.contains(user.getResource())); users.values().forEach(user -> assertTrue(updatedMembers.contains(user.getResource()))); }