UserRest { @PUT @Path("{username}/groups") @RolesAllowed("admin") @ApiOperation("Add a Mobi user to a group") public Response addUserGroup(@PathParam("username") String username, @QueryParam("group") String groupTitle) { if (StringUtils.isEmpty(username) || StringUtils.isEmpty(groupTitle)) { throw ErrorUtils.sendError("Both username and group name must be provided", Response.Status.BAD_REQUEST); } try { User savedUser = engineManager.retrieveUser(username).orElseThrow(() -> ErrorUtils.sendError("User " + username + " not found", Response.Status.BAD_REQUEST)); Group savedGroup = engineManager.retrieveGroup(rdfEngine.getEngineName(), groupTitle).orElseThrow(() -> ErrorUtils.sendError("Group " + groupTitle + " not found", Response.Status.BAD_REQUEST)); savedGroup.addMember(savedUser); engineManager.updateGroup(rdfEngine.getEngineName(), savedGroup); logger.info("Added user " + username + " 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 Users") Response getUsers(); @POST @RolesAllowed("admin") @ApiOperation("Create a Mobi User account") @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.MULTIPART_FORM_DATA) Response createUser(@FormDataParam("username") String username,
                        @FormDataParam("password") String password,
                        @FormDataParam("roles") List<FormDataBodyPart> roles,
                        @FormDataParam("firstName") String firstName,
                        @FormDataParam("lastName") String lastName,
                        @FormDataParam("email") String email); @GET @Path("{username}") @RolesAllowed("user") @Produces(MediaType.APPLICATION_JSON) @ApiOperation("Get a single Mobi User") Response getUser(@PathParam("username") String username); @PUT @Path("{username}") @RolesAllowed("user") @ApiOperation("Update a Mobi user's information") @Consumes(MediaType.APPLICATION_JSON) Response updateUser(@Context ContainerRequestContext context,
                        @PathParam("username") String username,
                        String newUserStr); @POST @Path("{username}/password") @RolesAllowed("user") @ApiOperation("Changes a Mobi User's password if it is the User making the request") Response changePassword(@Context ContainerRequestContext context,
                            @PathParam("username") String username,
                            @QueryParam("currentPassword") String currentPassword,
                            @QueryParam("newPassword") String newPassword); @PUT @Path("{username}/password") @RolesAllowed("admin") @ApiOperation("Resets a Mobi User's password if User making request is the admin") Response resetPassword(@Context ContainerRequestContext context,
                           @PathParam("username") String username,
                           @QueryParam("newPassword") String newPassword); @DELETE @Path("{username}") @RolesAllowed("user") @ApiOperation("Remove a Mobi user's account") Response deleteUser(@Context ContainerRequestContext context,
                        @PathParam("username") String username); @GET @Path("{username}/roles") @RolesAllowed("user") @Produces(MediaType.APPLICATION_JSON) @ApiOperation("List roles of a Mobi User") Response getUserRoles(@PathParam("username") String username,
                          @DefaultValue("false") @QueryParam("includeGroups") boolean includeGroups); @PUT @Path("{username}/roles") @RolesAllowed("admin") @ApiOperation("Add roles to a Mobi User") Response addUserRoles(@PathParam("username") String username, @QueryParam("roles") List<String> roles); @DELETE @Path("{username}/roles") @RolesAllowed("admin") @ApiOperation("Remove role from a Mobi User") Response removeUserRole(@PathParam("username") String username, @QueryParam("role") String role); @GET @Path("{username}/groups") @RolesAllowed("user") @Produces(MediaType.APPLICATION_JSON) @ApiOperation("List groups of a Mobi User") Response listUserGroups(@PathParam("username") String username); @PUT @Path("{username}/groups") @RolesAllowed("admin") @ApiOperation("Add a Mobi user to a group") Response addUserGroup(@PathParam("username") String username, @QueryParam("group") String groupTitle); @DELETE @Path("{username}/groups") @RolesAllowed("admin") @ApiOperation("Remove a Mobi User from a group") Response removeUserGroup(@PathParam("username") String username, @QueryParam("group") String groupTitle); @GET @Path("username") @RolesAllowed("user") @Produces(MediaType.TEXT_PLAIN) @ApiOperation("Retrieve a username based on the passed User IRI") Response getUsername(@QueryParam("iri") String userIri);  }
@Test public void addUserGroupTest() { Group newGroup = groupFactory.createNew(vf.createIRI("http: when(engineManager.retrieveGroup(ENGINE_NAME, "anothergroup")).thenReturn(Optional.of(newGroup)); Response response = target().path("users/" + UsernameTestFilter.USERNAME + "/groups").queryParam("group", "anothergroup") .request().put(Entity.entity("", MediaType.MULTIPART_FORM_DATA)); assertEquals(response.getStatus(), 200); verify(engineManager).retrieveUser(UsernameTestFilter.USERNAME); verify(engineManager).retrieveGroup(ENGINE_NAME, "anothergroup"); 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(1, updatedMembers.size()); assertTrue(updatedMembers.contains(user.getResource())); }