Search in sources :

Example 6 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.

the class ProjectServiceImplTest method testGetProjectsForSample.

@Test
public void testGetProjectsForSample() {
    Sample sample = new Sample("my sample");
    @SuppressWarnings("unchecked") List<Join<Project, Sample>> projects = Lists.newArrayList(new ProjectSampleJoin(new Project("p1"), sample, true), new ProjectSampleJoin(new Project("p2"), sample, true));
    when(psjRepository.getProjectForSample(sample)).thenReturn(projects);
    List<Join<Project, Sample>> projectsForSample = projectService.getProjectsForSample(sample);
    assertEquals(2, projectsForSample.size());
    verify(psjRepository).getProjectForSample(sample);
}
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) ProjectReferenceFileJoin(ca.corefacility.bioinformatics.irida.model.project.ProjectReferenceFileJoin) RelatedProjectJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) UserGroupProjectJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin) Test(org.junit.Test)

Example 7 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.

the class SampleServiceImplTest method testMergeSamples.

@Test
public void testMergeSamples() {
    // For every sample in toMerge, the service should:
    // 1. call SequenceFileRepository to get the sequence files in that
    // sample,
    // 2. call SequenceFileRepository to add the sequence files to
    // mergeInto,
    // 3. call SampleRepository to persist the sample as deleted.
    final int SIZE = 3;
    Sample s = s(1L);
    Project project = p(1L);
    Sample[] toMerge = new Sample[SIZE];
    SequenceFile[] toMerge_sf = new SequenceFile[SIZE];
    SequencingObject[] toMerge_so = new SequencingObject[SIZE];
    SampleSequencingObjectJoin[] s_so_joins = new SampleSequencingObjectJoin[SIZE];
    SampleSequencingObjectJoin[] s_so_original = new SampleSequencingObjectJoin[SIZE];
    ProjectSampleJoin[] p_s_joins = new ProjectSampleJoin[SIZE];
    List<Sample> mergeSamples = new ArrayList<>();
    for (long i = 0; i < SIZE; i++) {
        int p = (int) i;
        toMerge[p] = s(i + 2);
        mergeSamples.add(toMerge[p]);
        toMerge_sf[p] = sf(i + 2);
        toMerge_so[p] = so(i + 2);
        s_so_joins[p] = new SampleSequencingObjectJoin(s, toMerge_so[p]);
        p_s_joins[p] = new ProjectSampleJoin(project, toMerge[p], true);
        List<Join<Project, Sample>> projectSampleJoins = new ArrayList<>();
        projectSampleJoins.add(p_s_joins[p]);
        List<SampleSequencingObjectJoin> sampleSeqObjectJoins = new ArrayList<>();
        SampleSequencingObjectJoin join = new SampleSequencingObjectJoin(toMerge[p], toMerge_so[p]);
        sampleSeqObjectJoins.add(join);
        s_so_original[p] = join;
        when(ssoRepository.getSequencesForSample(toMerge[p])).thenReturn(null);
        when(ssoRepository.getSequencesForSample(toMerge[p])).thenReturn(sampleSeqObjectJoins);
        when(ssoRepository.save(s_so_joins[p])).thenReturn(s_so_joins[p]);
        when(ssoRepository.readObjectForSample(toMerge[p], toMerge_so[p].getId())).thenReturn(join);
        when(psjRepository.getProjectForSample(toMerge[p])).thenReturn(projectSampleJoins);
        // for deletion
        when(psjRepository.readSampleForProject(project, toMerge[p])).thenReturn(p_s_joins[p]);
    }
    List<Join<Project, Sample>> joins = new ArrayList<>();
    joins.add(new ProjectSampleJoin(project, s, true));
    when(psjRepository.getProjectForSample(s)).thenReturn(joins);
    Sample saved = sampleService.mergeSamples(project, s, mergeSamples);
    verify(psjRepository).getProjectForSample(s);
    for (int i = 0; i < SIZE; i++) {
        verify(ssoRepository).getSequencesForSample(toMerge[i]);
        verify(ssoRepository).save(s_so_joins[i]);
        verify(ssoRepository).delete(s_so_original[i]);
        verify(sampleRepository).delete(toMerge[i].getId());
        verify(psjRepository).getProjectForSample(toMerge[i]);
        verify(psjRepository).delete(p_s_joins[i]);
    }
    assertEquals("The saved sample should be the same as the sample to merge into.", s, saved);
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ArrayList(java.util.ArrayList) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) Test(org.junit.Test)

Example 8 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin 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 9 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin 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)

Example 10 with ProjectSampleJoin

use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.

the class RESTProjectSamplesControllerTest method testGetProjectSample.

@Test
public void testGetProjectSample() throws IOException {
    Project p = TestDataFactory.constructProject();
    Sample s = TestDataFactory.constructSample();
    // mock out the service calls
    when(projectService.read(p.getId())).thenReturn(p);
    when(sampleService.read(s.getId())).thenReturn(s);
    when(sampleService.getSampleForProject(p, s.getId())).thenReturn(new ProjectSampleJoin(p, s, true));
    ModelMap modelMap = controller.getProjectSample(p.getId(), s.getId());
    verify(sampleService).getSampleForProject(p, s.getId());
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertTrue(o instanceof Sample);
    Sample sr = (Sample) o;
    Link selfLink = sr.getLink(Link.REL_SELF);
    Link sequenceFilesLink = sr.getLink(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES);
    Link projectLink = sr.getLink(RESTProjectSamplesController.REL_PROJECT);
    String projectLocation = "http://localhost/api/projects/" + p.getId();
    String sampleLocation = "http://localhost/api/samples/" + s.getId();
    assertNotNull(selfLink);
    assertEquals(sampleLocation, selfLink.getHref());
    assertNotNull(sequenceFilesLink);
    assertEquals(sampleLocation + "/sequenceFiles", sequenceFilesLink.getHref());
    assertNotNull(projectLink);
    assertEquals(projectLocation, projectLink.getHref());
}
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) Link(org.springframework.hateoas.Link) Test(org.junit.Test)

Aggregations

ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)59 Project (ca.corefacility.bioinformatics.irida.model.project.Project)51 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)51 Test (org.junit.Test)43 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)15 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)15 ArrayList (java.util.ArrayList)11 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)8 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)8 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 User (ca.corefacility.bioinformatics.irida.model.user.User)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 Authentication (org.springframework.security.core.Authentication)7 Transactional (org.springframework.transaction.annotation.Transactional)7 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)6 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)6 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)6 LaunchesProjectEvent (ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent)5 EntityNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException)5 RelatedProjectJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.RelatedProjectJoin)5