Search in sources :

Example 61 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project 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 62 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.

the class ProjectUsersControllerTest method testRemoveUserFromProject.

@Test
public void testRemoveUserFromProject() throws ProjectWithoutOwnerException {
    Project p = TestDataFactory.constructProject();
    User u = TestDataFactory.constructUser();
    when(projectService.read(p.getId())).thenReturn(p);
    when(userService.getUserByUsername(u.getUsername())).thenReturn(u);
    ModelMap modelMap = controller.removeUserFromProject(p.getId(), u.getUsername());
    verify(projectService).read(p.getId());
    verify(userService).getUserByUsername(u.getUsername());
    verify(projectService).removeUserFromProject(p, u);
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertTrue(o instanceof RootResource);
    RootResource r = (RootResource) o;
    // confirm that a project link exists
    Link projectLink = r.getLink(RESTProjectsController.REL_PROJECT);
    assertNotNull(projectLink);
    assertEquals("http://localhost/api/projects/" + p.getId(), projectLink.getHref());
    // confirm that a project users link exists
    Link projectUsersLink = r.getLink(RESTProjectUsersController.REL_PROJECT_USERS);
    assertNotNull(projectUsersLink);
    assertEquals("http://localhost/api/projects/" + p.getId() + "/users", projectUsersLink.getHref());
}
Also used : RootResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.RootResource) Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) ModelMap(org.springframework.ui.ModelMap) Link(org.springframework.hateoas.Link) Test(org.junit.Test)

Example 63 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.

the class RESTProjectSamplesControllerTest method testRemoveSampleFromProject.

@Test
public void testRemoveSampleFromProject() {
    Project p = TestDataFactory.constructProject();
    Sample s = TestDataFactory.constructSample();
    when(projectService.read(p.getId())).thenReturn(p);
    when(sampleService.read(s.getId())).thenReturn(s);
    ModelMap modelMap = controller.removeSampleFromProject(p.getId(), s.getId());
    // verify that we actually tried to remove the sample from the project.
    verify(projectService, times(1)).removeSampleFromProject(p, s);
    verify(projectService, times(1)).read(p.getId());
    verify(sampleService, times(1)).read(s.getId());
    // confirm that the response looks right.
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertTrue(o instanceof RootResource);
    RootResource resource = (RootResource) o;
    List<Link> links = resource.getLinks();
    // should be two links in the response, one back to the individual
    // project, the other to the samples collection
    Set<String> rels = Sets.newHashSet(RESTProjectsController.REL_PROJECT, RESTProjectSamplesController.REL_PROJECT_SAMPLES);
    for (Link link : links) {
        assertTrue(rels.contains(link.getRel()));
        assertNotNull(rels.remove(link.getRel()));
    }
    assertTrue(rels.isEmpty());
}
Also used : RootResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.RootResource) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) Link(org.springframework.hateoas.Link) Test(org.junit.Test)

Example 64 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.

the class RESTProjectSamplesControllerTest method testGetProjectSamples.

@Test
public void testGetProjectSamples() {
    Project p = TestDataFactory.constructProject();
    Sample s = TestDataFactory.constructSample();
    Join<Project, Sample> r = new ProjectSampleJoin(p, s, true);
    @SuppressWarnings("unchecked") List<Join<Project, Sample>> relationships = Lists.newArrayList(r);
    when(sampleService.getSamplesForProject(p)).thenReturn(relationships);
    when(projectService.read(p.getId())).thenReturn(p);
    ModelMap modelMap = controller.getProjectSamples(p.getId());
    verify(sampleService, times(1)).getSamplesForProject(p);
    verify(projectService, times(1)).read(p.getId());
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertTrue(o instanceof ResourceCollection);
    @SuppressWarnings("unchecked") ResourceCollection<Sample> samples = (ResourceCollection<Sample>) o;
    assertEquals(1, samples.size());
    List<Link> resourceLinks = samples.getLinks();
    assertEquals(1, resourceLinks.size());
    Link self = resourceLinks.iterator().next();
    assertEquals("self", self.getRel());
    assertEquals("http://localhost/api/projects/" + p.getId() + "/samples", self.getHref());
    Sample resource = samples.iterator().next();
    assertEquals(s.getSampleName(), resource.getSampleName());
    // assertEquals(1, resource.getSequenceFileCount());
    List<Link> links = resource.getLinks();
    Set<String> rels = Sets.newHashSet(Link.REL_SELF, RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES, RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILE_PAIRS, RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILE_UNPAIRED, RESTProjectSamplesController.REL_PROJECT, RESTProjectSamplesController.REL_PROJECT_SAMPLE, RESTSampleMetadataController.METADATA_REL);
    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) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Link(org.springframework.hateoas.Link) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) Test(org.junit.Test)

Example 65 with Project

use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.

the class RESTProjectSamplesControllerTest method testAlreadyCopiedSampleToProject.

@Test(expected = EntityExistsException.class)
public void testAlreadyCopiedSampleToProject() {
    final Project p = TestDataFactory.constructProject();
    final Sample s = TestDataFactory.constructSample();
    when(projectService.read(p.getId())).thenReturn(p);
    when(sampleService.read(s.getId())).thenReturn(s);
    when(projectService.addSampleToProject(p, s, false)).thenThrow(new EntityExistsException("sample already exists!"));
    controller.copySampleToProject(p.getId(), Lists.newArrayList(s.getId()), new MockHttpServletResponse());
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

Project (ca.corefacility.bioinformatics.irida.model.project.Project)331 Test (org.junit.Test)190 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)120 User (ca.corefacility.bioinformatics.irida.model.user.User)88 WithMockUser (org.springframework.security.test.context.support.WithMockUser)80 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)71 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)62 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)51 RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)37 ArrayList (java.util.ArrayList)34 ProjectUserJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin)30 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)30 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)27 ProjectRole (ca.corefacility.bioinformatics.irida.model.enums.ProjectRole)25 ReferenceFile (ca.corefacility.bioinformatics.irida.model.project.ReferenceFile)23 ProjectEvent (ca.corefacility.bioinformatics.irida.model.event.ProjectEvent)22 ProjectAnalysisSubmissionJoin (ca.corefacility.bioinformatics.irida.model.workflow.submission.ProjectAnalysisSubmissionJoin)22 List (java.util.List)22 UserRoleSetProjectEvent (ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent)21 ImmutableMap (com.google.common.collect.ImmutableMap)21