Search in sources :

Example 1 with LabelledRelationshipResource

use of ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource 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());
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) ModelMap(org.springframework.ui.ModelMap) ProjectRole(ca.corefacility.bioinformatics.irida.model.enums.ProjectRole) Project(ca.corefacility.bioinformatics.irida.model.project.Project) LabelledRelationshipResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Link(org.springframework.hateoas.Link) Test(org.junit.Test)

Example 2 with LabelledRelationshipResource

use of ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource in project irida by phac-nml.

the class RESTProjectSamplesController method copySampleToProject.

/**
 * Copy an existing sample to a project.
 *
 * @param projectId
 *            the project to copy the sample to.
 * @param sampleIds
 *            the collection of sample IDs to copy.
 * @param response
 *            a reference to the servlet response.
 * @return the response indicating that the sample was joined to the
 *         project.
 */
@RequestMapping(value = "/api/projects/{projectId}/samples", method = RequestMethod.POST, consumes = "application/idcollection+json")
public ModelMap copySampleToProject(@PathVariable final Long projectId, @RequestBody final List<Long> sampleIds, HttpServletResponse response) {
    ModelMap modelMap = new ModelMap();
    Project p = projectService.read(projectId);
    ResourceCollection<LabelledRelationshipResource<Project, Sample>> labeledProjectSampleResources = new ResourceCollection<>(sampleIds.size());
    for (final long sampleId : sampleIds) {
        Sample sample = sampleService.read(sampleId);
        Join<Project, Sample> r = projectService.addSampleToProject(p, sample, false);
        LabelledRelationshipResource<Project, Sample> resource = new LabelledRelationshipResource<Project, Sample>(r.getLabel(), r);
        // add a labeled relationship resource to the resource collection
        // that will fill the body of the response.
        resource.add(linkTo(methodOn(RESTProjectSamplesController.class).getSample(sample.getId())).withSelfRel());
        resource.add(linkTo(methodOn(RESTProjectSamplesController.class).getProjectSample(projectId, sample.getId())).withRel(REL_PROJECT_SAMPLE));
        resource.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).getSampleSequenceFiles(sample.getId())).withRel(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES));
        resource.add(linkTo(RESTProjectsController.class).slash(projectId).withRel(REL_PROJECT));
        labeledProjectSampleResources.add(resource);
        final String location = linkTo(methodOn(RESTProjectSamplesController.class).getProjectSample(projectId, sampleId)).withSelfRel().getHref();
        response.addHeader(HttpHeaders.LOCATION, location);
    }
    // add a link to the project that was copied to.
    labeledProjectSampleResources.add(linkTo(methodOn(RESTProjectSamplesController.class).getProjectSamples(projectId)).withSelfRel());
    modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, labeledProjectSampleResources);
    response.setStatus(HttpStatus.CREATED.value());
    return modelMap;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) LabelledRelationshipResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with LabelledRelationshipResource

use of ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource 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;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) LabelledRelationshipResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource) User(ca.corefacility.bioinformatics.irida.model.user.User) ModelMap(org.springframework.ui.ModelMap) RESTUsersController(ca.corefacility.bioinformatics.irida.web.controller.api.RESTUsersController) ProjectRole(ca.corefacility.bioinformatics.irida.model.enums.ProjectRole) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with LabelledRelationshipResource

use of ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource in project irida by phac-nml.

the class RESTProjectSamplesControllerTest method testCopySampleToProject.

@Test
public void testCopySampleToProject() {
    final Project p = TestDataFactory.constructProject();
    final Sample s = TestDataFactory.constructSample();
    final ProjectSampleJoin r = new ProjectSampleJoin(p, s, true);
    MockHttpServletResponse response = new MockHttpServletResponse();
    when(projectService.read(p.getId())).thenReturn(p);
    when(sampleService.read(s.getId())).thenReturn(s);
    when(projectService.addSampleToProject(p, s, false)).thenReturn(r);
    ModelMap modelMap = controller.copySampleToProject(p.getId(), Lists.newArrayList(s.getId()), response);
    verify(projectService).addSampleToProject(p, s, false);
    assertEquals("response should have CREATED status", HttpStatus.CREATED.value(), response.getStatus());
    final String location = response.getHeader(HttpHeaders.LOCATION);
    assertEquals("location should include sample and project IDs", "http://localhost/api/projects/" + p.getId() + "/samples/" + s.getId(), location);
    // test that the modelMap contains a correct resource collection.
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertTrue("Object should be an instance of ResourceCollection", o instanceof ResourceCollection);
    @SuppressWarnings("unchecked") ResourceCollection<LabelledRelationshipResource<Project, Sample>> labeledRRs = (ResourceCollection<LabelledRelationshipResource<Project, Sample>>) o;
    assertEquals("There should be one item in the resource collection", 1, labeledRRs.size());
    List<Link> resourceLinks = labeledRRs.getLinks();
    assertEquals("There should be one link", 1, resourceLinks.size());
    Link self = resourceLinks.iterator().next();
    assertEquals("Self link should be correct", "self", self.getRel());
    assertEquals("http://localhost/api/projects/" + p.getId() + "/samples", self.getHref());
    LabelledRelationshipResource<Project, Sample> resource = labeledRRs.iterator().next();
    Object o2 = resource.getResource();
    assertTrue("Object should be an instance of ProjectSampleJoin", o2 instanceof ProjectSampleJoin);
    ProjectSampleJoin join = (ProjectSampleJoin) o2;
    Object o3 = join.getObject();
    assertTrue("Object should be an instance of Sample", o3 instanceof Sample);
    Sample sample = (Sample) o3;
    assertEquals("Sample name should be correct", s.getSampleName(), sample.getSampleName());
    List<Link> links = resource.getLinks();
    Set<String> rels = Sets.newHashSet(Link.REL_SELF, RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES, RESTProjectSamplesController.REL_PROJECT, RESTProjectSamplesController.REL_PROJECT_SAMPLE);
    for (Link link : links) {
        assertTrue("Rels should contain link [" + link + "]", rels.contains(link.getRel()));
        assertNotNull("Rels should remove link [" + link + "]", rels.remove(link.getRel()));
    }
    assertTrue("Rels should be empty after removing expected links", rels.isEmpty());
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) LabelledRelationshipResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Link(org.springframework.hateoas.Link) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) Test(org.junit.Test)

Aggregations

Project (ca.corefacility.bioinformatics.irida.model.project.Project)4 LabelledRelationshipResource (ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource)4 ModelMap (org.springframework.ui.ModelMap)4 ProjectRole (ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)2 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)2 User (ca.corefacility.bioinformatics.irida.model.user.User)2 ResourceCollection (ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection)2 Test (org.junit.Test)2 Link (org.springframework.hateoas.Link)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)1 ProjectUserJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin)1 RESTUsersController (ca.corefacility.bioinformatics.irida.web.controller.api.RESTUsersController)1