use of ca.corefacility.bioinformatics.irida.model.enums.ProjectRole in project irida by phac-nml.
the class ProjectServiceImplTest method testAddUserToProjectTwice.
@Test(expected = EntityExistsException.class)
public void testAddUserToProjectTwice() {
User u = new User("test", "test@nowhere.com", "PASSWOD!1", "Test", "User", "1234");
u.setId(new Long(1111));
Project p = project();
ProjectRole r = ProjectRole.PROJECT_USER;
ProjectUserJoin join = new ProjectUserJoin(p, u, r);
when(pujRepository.save(join)).thenThrow(new DataIntegrityViolationException("Duplicates."));
projectService.addUserToProject(p, u, r);
}
use of ca.corefacility.bioinformatics.irida.model.enums.ProjectRole in project irida by phac-nml.
the class ProjectUsersControllerTest method testAddUserToProject.
@Test
public void testAddUserToProject() throws ProjectWithoutOwnerException {
Project p = TestDataFactory.constructProject();
User u = TestDataFactory.constructUser();
ProjectRole r = ProjectRole.PROJECT_USER;
ProjectUserJoin j = new ProjectUserJoin(p, u, r);
MockHttpServletResponse response = new MockHttpServletResponse();
when(projectService.read(p.getId())).thenReturn(p);
when(userService.getUserByUsername(u.getUsername())).thenReturn(u);
when(projectService.addUserToProject(p, u, r)).thenReturn(j);
// prepare the "user" for addition to the project, just a map of userId and a username.
Map<String, String> userMap = ImmutableMap.of(RESTProjectUsersController.USER_ID_KEY, u.getUsername());
// add the user to the project
ModelMap map = controller.addUserToProject(p.getId(), userMap, response);
// confirm that the service method was called
verify(projectService, times(1)).addUserToProject(p, u, ProjectRole.PROJECT_USER);
verify(projectService, times(1)).read(p.getId());
verify(userService, times(1)).getUserByUsername(u.getUsername());
// check that the response is as expected:
assertEquals("Response must be CREATED", HttpStatus.CREATED.value(), response.getStatus());
// check for a correct user link
String location = response.getHeader(HttpHeaders.LOCATION);
assertNotNull("location must not be null", location);
assertFalse("location must not be empty", location.isEmpty());
assertEquals("location must be correct", "http://localhost/api/projects/" + p.getId() + "/users/" + u.getUsername(), location);
// check the ModelMap's resource type
Object o = map.get(RESTGenericController.RESOURCE_NAME);
assertNotNull("object must not be null", o);
assertTrue("object must be an instance of LabelledRelationshipResource", o instanceof LabelledRelationshipResource);
@SuppressWarnings("unchecked") LabelledRelationshipResource<Project, User> lrr = (LabelledRelationshipResource<Project, User>) o;
Object o2 = lrr.getResource();
assertNotNull("object must not be null", o2);
assertTrue("object must be an instance of ProjectUserJoin", o2 instanceof ProjectUserJoin);
ProjectUserJoin pj = (ProjectUserJoin) o2;
Object o3 = pj.getObject();
assertNotNull("object must not be null", o3);
assertTrue("object must be an instance of User", o3 instanceof User);
User user = (User) o3;
assertEquals("Username must be correct", user.getUsername(), u.getUsername());
// check for a correct relationship link
assertTrue("relationship link must be correct", lrr.getLink("self").getHref().endsWith(u.getUsername()));
Link relationship = lrr.getLink(RESTGenericController.REL_RELATIONSHIP);
assertNotNull("relationship link must exist", relationship);
assertEquals("relationship link must be correct", "http://localhost/api/projects/" + p.getId() + "/users/" + u.getUsername(), relationship.getHref());
// confirm that a project link exists
Link projectLink = lrr.getLink(RESTProjectsController.REL_PROJECT);
assertNotNull("project link must exist", projectLink);
assertEquals("project link must be correct", "http://localhost/api/projects/" + p.getId(), projectLink.getHref());
// confirm that a project users link exists
Link projectUsersLink = lrr.getLink(RESTProjectUsersController.REL_PROJECT_USERS);
assertNotNull("project users link must exist", projectUsersLink);
assertEquals("project users link must be correct", "http://localhost/api/projects/" + p.getId() + "/users", projectUsersLink.getHref());
}
use of ca.corefacility.bioinformatics.irida.model.enums.ProjectRole in project irida by phac-nml.
the class RESTProjectUsersController method addUserToProject.
/**
* Add a relationship between a {@link Project} and a {@link User}.
*
* @param projectId
* the project ID to add the user to.
* @param representation
* the JSON key-value pair that contains the identifier for the
* project and the identifier for the user.
* @param response
* a reference to the servlet response.
* @return a response indicating that the collection was modified.
* @throws ProjectWithoutOwnerException
* this cannot actually be thrown, it's an artifact of using
* spring HATEOAS {@code linkTo} and {@code methodOn}.
*/
@RequestMapping(value = "/api/projects/{projectId}/users", method = RequestMethod.POST)
public ModelMap addUserToProject(@PathVariable Long projectId, @RequestBody Map<String, String> representation, HttpServletResponse response) throws ProjectWithoutOwnerException {
// first, get the project
Project p = projectService.read(projectId);
String username = representation.get(USER_ID_KEY);
// then, get the user
User u = userService.getUserByUsername(username);
ProjectRole r = ProjectRole.PROJECT_USER;
// then add the user to the project with the specified role.
Join<Project, User> joined = projectService.addUserToProject(p, u, r);
LabelledRelationshipResource<Project, User> lrr = new LabelledRelationshipResource<Project, User>(joined.getLabel(), joined);
// prepare a link to the user
lrr.add(linkTo(RESTUsersController.class).slash(u.getUsername()).withSelfRel());
// prepare a link to the user as added to the project
lrr.add(linkTo(methodOn(RESTProjectUsersController.class).removeUserFromProject(projectId, u.getUsername())).withRel(RESTGenericController.REL_RELATIONSHIP));
// prepare a link back to the user collection of the project
lrr.add(linkTo(methodOn(RESTProjectUsersController.class).getUsersForProject(projectId)).withRel(REL_PROJECT_USERS));
// prepare a link back to the project
lrr.add(linkTo(RESTProjectsController.class).slash(projectId).withRel(RESTProjectsController.REL_PROJECT));
String location = linkTo(RESTProjectsController.class).slash(projectId).slash("users").slash(username).withSelfRel().getHref();
// add a location header and set the response status.
response.addHeader(HttpHeaders.LOCATION, location);
response.setStatus(HttpStatus.CREATED.value());
// prepare the response for the client
ModelMap modelMap = new ModelMap();
modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, lrr);
return modelMap;
}
use of ca.corefacility.bioinformatics.irida.model.enums.ProjectRole in project irida by phac-nml.
the class ProjectMembersControllerTest method testUdateUserSelfRole.
public void testUdateUserSelfRole() throws ProjectWithoutOwnerException {
Long projectId = 1L;
Long userId = 2L;
Project project = new Project();
User user = new User(userId, USER_NAME, null, null, "Tom", "Matthews", null);
ProjectRole projectRole = ProjectRole.PROJECT_USER;
when(projectService.read(projectId)).thenReturn(project);
when(userService.read(userId)).thenReturn(user);
when(messageSource.getMessage(any(), any(), any())).thenReturn("");
final Map<String, String> result = controller.updateUserRole(projectId, userId, projectRole.toString(), null);
assertTrue("should have failure message.", result.containsKey("failure"));
}
use of ca.corefacility.bioinformatics.irida.model.enums.ProjectRole in project irida by phac-nml.
the class ProjectMembersControllerTest method testAddProjectMember.
@Test
public void testAddProjectMember() {
Long projectId = 1L;
Long userId = 2L;
Project project = new Project();
User user = new User(userId, "tom", null, null, "Tom", "Matthews", null);
ProjectRole projectRole = ProjectRole.PROJECT_USER;
when(projectService.read(projectId)).thenReturn(project);
when(userService.read(userId)).thenReturn(user);
when(messageSource.getMessage(any(), any(), any())).thenReturn("My random string");
controller.addProjectMember(projectId, userId, projectRole.toString(), Locale.US);
verify(projectService).read(projectId);
verify(userService).read(userId);
verify(projectService).addUserToProject(project, user, projectRole);
}
Aggregations