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);
}
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);
}
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());
}
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());
}
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());
}
Aggregations