use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin 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());
}
use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.
the class ProjectServiceImpl method removeSampleFromProject.
/**
* {@inheritDoc}
*/
@Override
@Transactional
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#project, 'canManageLocalProjectSettings')")
@LaunchesProjectEvent(SampleRemovedProjectEvent.class)
public void removeSampleFromProject(Project project, Sample sample) {
ProjectSampleJoin readSampleForProject = psjRepository.readSampleForProject(project, sample);
psjRepository.delete(readSampleForProject);
// if the sample doesn't refer to any other projects, delete it
if (psjRepository.getProjectForSample(sample).isEmpty()) {
sampleRepository.delete(sample);
}
}
use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.
the class ProjectServiceImpl method shareSamples.
/**
* {@inheritDoc}
*/
@Override
@Transactional
@LaunchesProjectEvent(SampleAddedProjectEvent.class)
@PreAuthorize("hasPermission(#source, 'canManageLocalProjectSettings')" + " and hasPermission(#destination, 'isProjectOwner')" + " and hasPermission(#samples, 'canReadSample')" + " and ((not #giveOwner) or hasPermission(#samples, 'canUpdateSample'))")
public List<ProjectSampleJoin> shareSamples(Project source, Project destination, Collection<Sample> samples, boolean giveOwner) {
List<ProjectSampleJoin> newJoins = new ArrayList<>();
for (Sample sample : samples) {
ProjectSampleJoin newJoin = addSampleToProject(destination, sample, giveOwner);
logger.trace("Shared sample " + sample.getId() + " to project " + destination.getId());
newJoins.add(newJoin);
}
return newJoins;
}
use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.
the class ProjectServiceImpl method moveSampleBetweenProjects.
/**
* {@inheritDoc}
*/
@Override
@Transactional
@LaunchesProjectEvent(SampleAddedProjectEvent.class)
@PreAuthorize("hasRole('ROLE_ADMIN') or ( hasPermission(#source, 'isProjectOwner') and hasPermission(#destination, 'isProjectOwner'))")
public ProjectSampleJoin moveSampleBetweenProjects(Project source, Project destination, Sample sample, boolean owner) {
ProjectSampleJoin join = addSampleToProject(destination, sample, owner);
removeSampleFromProject(source, sample);
return join;
}
use of ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin in project irida by phac-nml.
the class SampleServiceImpl method getSampleForProject.
/**
* {@inheritDoc}
*/
@Override
@Transactional(readOnly = true)
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_SEQUENCER') or (hasPermission(#project, 'canReadProject') and hasPermission(#sampleId, 'canReadSample'))")
public ProjectSampleJoin getSampleForProject(Project project, Long sampleId) {
Sample sample = read(sampleId);
ProjectSampleJoin join = psjRepository.readSampleForProject(project, sample);
if (join == null) {
throw new EntityNotFoundException("Join between the project and this identifier doesn't exist");
}
return join;
}
Aggregations