Search in sources :

Example 1 with ResourceCollection

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

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

the class SampleSequenceFilesControllerTest method testGetSampleSequenceFiles.

@Test
public void testGetSampleSequenceFiles() throws IOException {
    Sample s = TestDataFactory.constructSample();
    SingleEndSequenceFile so = TestDataFactory.constructSingleEndSequenceFile();
    SampleSequencingObjectJoin r = new SampleSequencingObjectJoin(s, so);
    List<SampleSequencingObjectJoin> relationships = Lists.newArrayList(r);
    // mock out the service calls
    when(sampleService.read(s.getId())).thenReturn(s);
    when(sequencingObjectService.getSequencingObjectsForSample(s)).thenReturn(relationships);
    ModelMap modelMap = controller.getSampleSequenceFiles(s.getId());
    // verify that the service calls were used.
    verify(sampleService).read(s.getId());
    verify(sequencingObjectService).getSequencingObjectsForSample(s);
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertTrue(o instanceof ResourceCollection);
    @SuppressWarnings("unchecked") ResourceCollection<SequenceFile> resources = (ResourceCollection<SequenceFile>) o;
    assertNotNull(resources);
    assertEquals(1, resources.size());
    Link selfCollection = resources.getLink(Link.REL_SELF);
    Link sample = resources.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);
    String sampleLocation = "http://localhost/api/samples/" + s.getId();
    String sequenceFileLocation = sampleLocation + "/unpaired/" + so.getIdentifier() + "/files/" + so.getSequenceFile().getId();
    assertEquals(sampleLocation + "/sequenceFiles", selfCollection.getHref());
    assertEquals(sampleLocation, sample.getHref());
    // confirm that the self rel for an individual sequence file exists
    SequenceFile sfr = resources.iterator().next();
    Link self = sfr.getLink(Link.REL_SELF);
    assertEquals(sequenceFileLocation, self.getHref());
    assertEquals(so.getSequenceFile().getFile(), sfr.getFile());
}
Also used : SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) Link(org.springframework.hateoas.Link) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) Test(org.junit.Test)

Example 3 with ResourceCollection

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

the class RESTUsersController method getUserProjects.

/**
 * Get the collection of projects for a specific user.
 *
 * @param username
 *            the username for the desired user.
 * @return a model containing the collection of projects for that user.
 */
@RequestMapping(value = "/api/{username}/projects", method = RequestMethod.GET)
public ModelMap getUserProjects(@PathVariable String username) {
    logger.debug("Loading projects for user [" + username + "]");
    ModelMap mav = new ModelMap();
    // get the appropriate user from the database
    User u = userService.getUserByUsername(username);
    // get all of the projects that this user belongs to
    ResourceCollection<Project> resources = new ResourceCollection<>();
    List<Join<Project, User>> projects = projectService.getProjectsForUser(u);
    ControllerLinkBuilder linkBuilder = linkTo(RESTProjectsController.class);
    // add the project and a self-rel link to the project representation
    for (Join<Project, User> join : projects) {
        Project project = join.getSubject();
        project.add(linkBuilder.slash(project.getId()).withSelfRel());
        resources.add(project);
    }
    // add the resources to the response
    mav.addAttribute("projectResources", resources);
    // respond to the user
    return mav;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) ModelMap(org.springframework.ui.ModelMap) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) ControllerLinkBuilder(org.springframework.hateoas.mvc.ControllerLinkBuilder) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ResourceCollection

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

the class RESTProjectSamplesController method copySampleToProject.

/**
 * Copy an existing sample to a project.
 *
 * @param projectId
 *            the project to copy the sample to.
 * @param sampleIds
 *            the collection of sample IDs to copy.
 * @param response
 *            a reference to the servlet response.
 * @return the response indicating that the sample was joined to the
 *         project.
 */
@RequestMapping(value = "/api/projects/{projectId}/samples", method = RequestMethod.POST, consumes = "application/idcollection+json")
public ModelMap copySampleToProject(@PathVariable final Long projectId, @RequestBody final List<Long> sampleIds, HttpServletResponse response) {
    ModelMap modelMap = new ModelMap();
    Project p = projectService.read(projectId);
    ResourceCollection<LabelledRelationshipResource<Project, Sample>> labeledProjectSampleResources = new ResourceCollection<>(sampleIds.size());
    for (final long sampleId : sampleIds) {
        Sample sample = sampleService.read(sampleId);
        Join<Project, Sample> r = projectService.addSampleToProject(p, sample, false);
        LabelledRelationshipResource<Project, Sample> resource = new LabelledRelationshipResource<Project, Sample>(r.getLabel(), r);
        // add a labeled relationship resource to the resource collection
        // that will fill the body of the response.
        resource.add(linkTo(methodOn(RESTProjectSamplesController.class).getSample(sample.getId())).withSelfRel());
        resource.add(linkTo(methodOn(RESTProjectSamplesController.class).getProjectSample(projectId, sample.getId())).withRel(REL_PROJECT_SAMPLE));
        resource.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).getSampleSequenceFiles(sample.getId())).withRel(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES));
        resource.add(linkTo(RESTProjectsController.class).slash(projectId).withRel(REL_PROJECT));
        labeledProjectSampleResources.add(resource);
        final String location = linkTo(methodOn(RESTProjectSamplesController.class).getProjectSample(projectId, sampleId)).withSelfRel().getHref();
        response.addHeader(HttpHeaders.LOCATION, location);
    }
    // add a link to the project that was copied to.
    labeledProjectSampleResources.add(linkTo(methodOn(RESTProjectSamplesController.class).getProjectSamples(projectId)).withSelfRel());
    modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, labeledProjectSampleResources);
    response.setStatus(HttpStatus.CREATED.value());
    return modelMap;
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) LabelledRelationshipResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ResourceCollection

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

the class RESTSampleSequenceFilesController method listSequencingObjectsOfTypeForSample.

/**
 * List all {@link SequencingObject}s of a given type for a {@link Sample}
 *
 * @param sampleId
 *            ID of the {@link Sample} to read from
 * @param objectType
 *            {@link SequencingObject} type
 * @return The {@link SequencingObject}s of the given type for the
 *         {@link Sample}
 */
@RequestMapping(value = "/api/samples/{sampleId}/{objectType}", method = RequestMethod.GET)
public ModelMap listSequencingObjectsOfTypeForSample(@PathVariable Long sampleId, @PathVariable String objectType) {
    ModelMap modelMap = new ModelMap();
    logger.debug("Reading seq file  for sample " + sampleId);
    Sample sample = sampleService.read(sampleId);
    Class<? extends SequencingObject> type = objectLabels.inverse().get(objectType);
    Collection<SampleSequencingObjectJoin> unpairedSequenceFilesForSample = sequencingObjectService.getSequencesForSampleOfType(sample, type);
    ResourceCollection<SequencingObject> resources = new ResourceCollection<>(unpairedSequenceFilesForSample.size());
    for (SampleSequencingObjectJoin join : unpairedSequenceFilesForSample) {
        SequencingObject sequencingObject = join.getObject();
        sequencingObject = addSequencingObjectLinks(sequencingObject, sampleId);
        resources.add(sequencingObject);
    }
    // add a link to this collection
    resources.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).listSequencingObjectsOfTypeForSample(sampleId, objectType)).withSelfRel());
    // add a link back to the sample
    resources.add(linkTo(methodOn(RESTProjectSamplesController.class).getSample(sampleId)).withRel(RESTSampleSequenceFilesController.REL_SAMPLE));
    modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, resources);
    return modelMap;
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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