Search in sources :

Example 26 with Sample

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

the class RESTProjectSamplesControllerTest method testUpdateSample.

@Test
public void testUpdateSample() {
    Sample s = TestDataFactory.constructSample();
    Map<String, Object> updatedFields = ImmutableMap.of("sampleName", (Object) "some new name");
    when(sampleService.updateFields(s.getId(), updatedFields)).thenReturn(s);
    ModelMap modelMap = controller.updateSample(s.getId(), updatedFields);
    verify(sampleService).updateFields(s.getId(), updatedFields);
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertNotNull("There should be *something* in the response!", o);
    assertTrue("Should be a sample in the response.", o instanceof Sample);
    Sample resource = (Sample) o;
    Map<String, String> links = linksToMap(resource.getLinks());
    String self = links.get(Link.REL_SELF);
    assertEquals("http://localhost/api/samples/" + s.getId(), self);
    String sequenceFiles = links.get(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES);
    assertEquals("http://localhost/api/samples/" + s.getId() + "/sequenceFiles", sequenceFiles);
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) Test(org.junit.Test)

Example 27 with Sample

use of ca.corefacility.bioinformatics.irida.model.sample.Sample 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 28 with Sample

use of ca.corefacility.bioinformatics.irida.model.sample.Sample 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 29 with Sample

use of ca.corefacility.bioinformatics.irida.model.sample.Sample 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)

Example 30 with Sample

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

the class RESTProjectSamplesControllerTest method testAddSampleToProject.

@Test
public void testAddSampleToProject() {
    MockHttpServletResponse response = new MockHttpServletResponse();
    Sample s = TestDataFactory.constructSample();
    Project p = TestDataFactory.constructProject();
    Join<Project, Sample> r = new ProjectSampleJoin(p, s, true);
    when(projectService.read(p.getId())).thenReturn(p);
    when(projectService.addSampleToProject(p, s, true)).thenReturn(r);
    ModelMap modelMap = controller.addSampleToProject(p.getId(), s, response);
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertTrue("ModelMap should contan a SampleResource", o instanceof Sample);
    verify(projectService, times(1)).read(p.getId());
    verify(projectService, times(1)).addSampleToProject(p, s, true);
    Link selfLink = s.getLink(Link.REL_SELF);
    Link sequenceFilesLink = s.getLink(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES);
    Link projectLink = s.getLink(RESTProjectSamplesController.REL_PROJECT);
    String projectLocation = "http://localhost/api/projects/" + p.getId();
    String sampleLocation = "http://localhost/api/samples/" + s.getId();
    assertNotNull("Sample resource's self link should not be null", selfLink);
    assertEquals("Sample resource's sample location should equal [" + sampleLocation + "]", sampleLocation, selfLink.getHref());
    assertNotNull("Sequence files link must not be null", sequenceFilesLink);
    assertEquals("Sequence files link must be well formed", sampleLocation + "/sequenceFiles", sequenceFilesLink.getHref());
    assertNotNull("Project link must not be null", projectLink);
    assertEquals("Project link must be well formed", projectLocation, projectLink.getHref());
    assertEquals("response should have CREATED status", HttpStatus.CREATED.value(), response.getStatus());
}
Also used : ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Link(org.springframework.hateoas.Link) Test(org.junit.Test)

Aggregations

Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)243 Test (org.junit.Test)162 Project (ca.corefacility.bioinformatics.irida.model.project.Project)114 WithMockUser (org.springframework.security.test.context.support.WithMockUser)71 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)62 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)53 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)53 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)41 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)33 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)33 Path (java.nio.file.Path)28 ModelMap (org.springframework.ui.ModelMap)28 SequenceFilePair (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair)24 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)24 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)23 ArrayList (java.util.ArrayList)22 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)17 User (ca.corefacility.bioinformatics.irida.model.user.User)14 HashMap (java.util.HashMap)14 RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)13