RoleResource { @POST @ApiOperation(value = "Create a new role", response = RoleDTO.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of created RoleDTO"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response createRole( @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") String workspaceId, @ApiParam(required = true, value = "Role to create") RoleDTO roleDTO) throws EntityNotFoundException, EntityAlreadyExistsException, UserNotActiveException, AccessRightException, CreationException, WorkspaceNotEnabledException { List<UserDTO> userDTOs = roleDTO.getDefaultAssignedUsers(); List<UserGroupDTO> groupDTOs = roleDTO.getDefaultAssignedGroups(); List<String> userLogins = new ArrayList<>(); List<String> userGroupIds = new ArrayList<>(); if (userDTOs != null) { userLogins.addAll(userDTOs.stream().map(UserDTO::getLogin).collect(Collectors.toList())); } if (groupDTOs != null) { userGroupIds.addAll(groupDTOs.stream().map(UserGroupDTO::getId).collect(Collectors.toList())); } Role roleCreated = roleService.createRole(roleDTO.getName(), roleDTO.getWorkspaceId(), userLogins, userGroupIds); RoleDTO roleCreatedDTO = mapRoleToDTO(roleCreated); return Tools.prepareCreatedResponse(roleCreatedDTO.getName(), roleCreatedDTO); } RoleResource(); @PostConstruct void init(); @GET @ApiOperation(value = "Get roles in given workspace", response = RoleDTO.class, responseContainer = "List") @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of RoleDTOs. It can be an empty list."), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Produces(MediaType.APPLICATION_JSON) Response getRolesInWorkspace(
            @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") String workspaceId); @GET @ApiOperation(value = "Get roles in use in given workspace", response = RoleDTO.class, responseContainer = "List") @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of RoleDTOs. It can be an empty list."), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Path("inuse") @Produces(MediaType.APPLICATION_JSON) Response getRolesInUseInWorkspace(
            @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") String workspaceId); @POST @ApiOperation(value = "Create a new role", response = RoleDTO.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of created RoleDTO"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) Response createRole(
            @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") String workspaceId,
            @ApiParam(required = true, value = "Role to create") RoleDTO roleDTO); @PUT @ApiOperation(value = "Update a role", response = RoleDTO.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful retrieval of updated RoleDTO"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Path("{roleName}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) Response updateRole(
            @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") String workspaceId,
            @ApiParam(required = true, value = "Role name") @PathParam("roleName") String roleName,
            @ApiParam(required = true, value = "Role to update") RoleDTO roleDTO); @DELETE @ApiOperation(value = "Delete a role", response = Response.class) @ApiResponses(value = { @ApiResponse(code = 204, message = "Successful deletion of RoleDTO"), @ApiResponse(code = 401, message = "Unauthorized"), @ApiResponse(code = 403, message = "Forbidden"), @ApiResponse(code = 500, message = "Internal server error") }) @Path("{roleName}") @Produces(MediaType.APPLICATION_JSON) Response deleteRole(
            @ApiParam(required = true, value = "Workspace id") @PathParam("workspaceId") String workspaceId,
            @ApiParam(required = true, value = "Role name") @PathParam("roleName") String roleName);  }
@Test public void createRoleTest() throws ApplicationException{ Role role = new Role(); role.setWorkspace(workspace); role.setName("role"); RoleDTO roleDTO = new RoleDTO(); roleDTO.setName("role"); roleDTO.setWorkspaceId(workspaceId); UserDTO user = new UserDTO(); String login ="foo"; user.setLogin(login); List<UserDTO> users = Collections.singletonList(user); roleDTO.setDefaultAssignedUsers(users); UserGroupDTO group = new UserGroupDTO(); String groupId = "bar"; group.setId(groupId); List<UserGroupDTO> groups = Collections.singletonList(group); roleDTO.setDefaultAssignedGroups(groups); List<String> userLogins = Collections.singletonList(login); List<String> userGroupIds = Collections.singletonList(groupId); Mockito.when(roleService.createRole(roleDTO.getName(), roleDTO.getWorkspaceId(), userLogins, userGroupIds)) .thenReturn(role); Response res = roleResource.createRole(workspaceId, roleDTO); Assert.assertEquals(Response.Status.CREATED.getStatusCode(), res.getStatus()); }