Search in sources :

Example 16 with ResourceCollection

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

the class UserControllerTest method testGetAllUsers.

@Test
public void testGetAllUsers() {
    String username = "fbristow";
    User u = new User();
    u.setUsername(username);
    List<User> users = Lists.newArrayList(u);
    when(userService.findAll()).thenReturn(users);
    when(userService.count()).thenReturn(1L);
    ModelMap output = controller.listAllResources();
    @SuppressWarnings("unchecked") ResourceCollection<User> usersCollection = (ResourceCollection<User>) output.get(RESTGenericController.RESOURCE_NAME);
    assertEquals("users collection is the wrong size.", 1, usersCollection.size());
    User userResource = usersCollection.iterator().next();
    assertEquals("username is not correct.", username, userResource.getUsername());
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) ModelMap(org.springframework.ui.ModelMap) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) Test(org.junit.Test)

Example 17 with ResourceCollection

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

the class ProjectUsersControllerTest method testGetUsersForProject.

@Test
public void testGetUsersForProject() throws ProjectWithoutOwnerException {
    String username = "fbristow";
    User u = new User();
    u.setUsername(username);
    u.setId(1L);
    Project p = TestDataFactory.constructProject();
    Join<Project, User> join = new ProjectUserJoin(p, u, ProjectRole.PROJECT_OWNER);
    @SuppressWarnings("unchecked") List<Join<Project, User>> relationships = Lists.newArrayList(join);
    when(userService.getUsersForProject(p)).thenReturn(relationships);
    when(projectService.read(p.getId())).thenReturn(p);
    ModelMap map = controller.getUsersForProject(p.getId());
    verify(projectService, times(1)).read(p.getId());
    verify(userService, times(1)).getUsersForProject(p);
    Object o = map.get(RESTGenericController.RESOURCE_NAME);
    assertNotNull(o);
    assertTrue(o instanceof ResourceCollection);
    @SuppressWarnings("unchecked") ResourceCollection<User> users = (ResourceCollection<User>) o;
    assertEquals(1, users.size());
    User ur = users.iterator().next();
    assertTrue(ur.getLink("self").getHref().endsWith(username));
    Link relationship = ur.getLink(RESTGenericController.REL_RELATIONSHIP);
    assertNotNull(relationship);
    assertEquals("http://localhost/api/projects/" + p.getId() + "/users/" + username, relationship.getHref());
    assertTrue(users.getLink("self").getHref().contains(p.getId().toString()));
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) ModelMap(org.springframework.ui.ModelMap) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) 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 18 with ResourceCollection

use of ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection 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

ResourceCollection (ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection)18 ModelMap (org.springframework.ui.ModelMap)18 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 Project (ca.corefacility.bioinformatics.irida.model.project.Project)10 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)7 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)6 Test (org.junit.Test)6 Link (org.springframework.hateoas.Link)6 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)5 User (ca.corefacility.bioinformatics.irida.model.user.User)5 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)5 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)3 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)2 AnalysisType (ca.corefacility.bioinformatics.irida.model.enums.AnalysisType)2 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)2 ProjectUserJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin)2 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)2 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)2 LabelledRelationshipResource (ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource)2 IridaWorkflowNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.IridaWorkflowNotFoundException)1