use of ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource in project irida by phac-nml.
the class ProjectUsersControllerTest method testAddUserToProject.
@Test
public void testAddUserToProject() throws ProjectWithoutOwnerException {
Project p = TestDataFactory.constructProject();
User u = TestDataFactory.constructUser();
ProjectRole r = ProjectRole.PROJECT_USER;
ProjectUserJoin j = new ProjectUserJoin(p, u, r);
MockHttpServletResponse response = new MockHttpServletResponse();
when(projectService.read(p.getId())).thenReturn(p);
when(userService.getUserByUsername(u.getUsername())).thenReturn(u);
when(projectService.addUserToProject(p, u, r)).thenReturn(j);
// prepare the "user" for addition to the project, just a map of userId and a username.
Map<String, String> userMap = ImmutableMap.of(RESTProjectUsersController.USER_ID_KEY, u.getUsername());
// add the user to the project
ModelMap map = controller.addUserToProject(p.getId(), userMap, response);
// confirm that the service method was called
verify(projectService, times(1)).addUserToProject(p, u, ProjectRole.PROJECT_USER);
verify(projectService, times(1)).read(p.getId());
verify(userService, times(1)).getUserByUsername(u.getUsername());
// check that the response is as expected:
assertEquals("Response must be CREATED", HttpStatus.CREATED.value(), response.getStatus());
// check for a correct user link
String location = response.getHeader(HttpHeaders.LOCATION);
assertNotNull("location must not be null", location);
assertFalse("location must not be empty", location.isEmpty());
assertEquals("location must be correct", "http://localhost/api/projects/" + p.getId() + "/users/" + u.getUsername(), location);
// check the ModelMap's resource type
Object o = map.get(RESTGenericController.RESOURCE_NAME);
assertNotNull("object must not be null", o);
assertTrue("object must be an instance of LabelledRelationshipResource", o instanceof LabelledRelationshipResource);
@SuppressWarnings("unchecked") LabelledRelationshipResource<Project, User> lrr = (LabelledRelationshipResource<Project, User>) o;
Object o2 = lrr.getResource();
assertNotNull("object must not be null", o2);
assertTrue("object must be an instance of ProjectUserJoin", o2 instanceof ProjectUserJoin);
ProjectUserJoin pj = (ProjectUserJoin) o2;
Object o3 = pj.getObject();
assertNotNull("object must not be null", o3);
assertTrue("object must be an instance of User", o3 instanceof User);
User user = (User) o3;
assertEquals("Username must be correct", user.getUsername(), u.getUsername());
// check for a correct relationship link
assertTrue("relationship link must be correct", lrr.getLink("self").getHref().endsWith(u.getUsername()));
Link relationship = lrr.getLink(RESTGenericController.REL_RELATIONSHIP);
assertNotNull("relationship link must exist", relationship);
assertEquals("relationship link must be correct", "http://localhost/api/projects/" + p.getId() + "/users/" + u.getUsername(), relationship.getHref());
// confirm that a project link exists
Link projectLink = lrr.getLink(RESTProjectsController.REL_PROJECT);
assertNotNull("project link must exist", projectLink);
assertEquals("project link must be correct", "http://localhost/api/projects/" + p.getId(), projectLink.getHref());
// confirm that a project users link exists
Link projectUsersLink = lrr.getLink(RESTProjectUsersController.REL_PROJECT_USERS);
assertNotNull("project users link must exist", projectUsersLink);
assertEquals("project users link must be correct", "http://localhost/api/projects/" + p.getId() + "/users", projectUsersLink.getHref());
}
use of ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource 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;
}
use of ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource in project irida by phac-nml.
the class RESTProjectUsersController method addUserToProject.
/**
* Add a relationship between a {@link Project} and a {@link User}.
*
* @param projectId
* the project ID to add the user to.
* @param representation
* the JSON key-value pair that contains the identifier for the
* project and the identifier for the user.
* @param response
* a reference to the servlet response.
* @return a response indicating that the collection was modified.
* @throws ProjectWithoutOwnerException
* this cannot actually be thrown, it's an artifact of using
* spring HATEOAS {@code linkTo} and {@code methodOn}.
*/
@RequestMapping(value = "/api/projects/{projectId}/users", method = RequestMethod.POST)
public ModelMap addUserToProject(@PathVariable Long projectId, @RequestBody Map<String, String> representation, HttpServletResponse response) throws ProjectWithoutOwnerException {
// first, get the project
Project p = projectService.read(projectId);
String username = representation.get(USER_ID_KEY);
// then, get the user
User u = userService.getUserByUsername(username);
ProjectRole r = ProjectRole.PROJECT_USER;
// then add the user to the project with the specified role.
Join<Project, User> joined = projectService.addUserToProject(p, u, r);
LabelledRelationshipResource<Project, User> lrr = new LabelledRelationshipResource<Project, User>(joined.getLabel(), joined);
// prepare a link to the user
lrr.add(linkTo(RESTUsersController.class).slash(u.getUsername()).withSelfRel());
// prepare a link to the user as added to the project
lrr.add(linkTo(methodOn(RESTProjectUsersController.class).removeUserFromProject(projectId, u.getUsername())).withRel(RESTGenericController.REL_RELATIONSHIP));
// prepare a link back to the user collection of the project
lrr.add(linkTo(methodOn(RESTProjectUsersController.class).getUsersForProject(projectId)).withRel(REL_PROJECT_USERS));
// prepare a link back to the project
lrr.add(linkTo(RESTProjectsController.class).slash(projectId).withRel(RESTProjectsController.REL_PROJECT));
String location = linkTo(RESTProjectsController.class).slash(projectId).slash("users").slash(username).withSelfRel().getHref();
// add a location header and set the response status.
response.addHeader(HttpHeaders.LOCATION, location);
response.setStatus(HttpStatus.CREATED.value());
// prepare the response for the client
ModelMap modelMap = new ModelMap();
modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, lrr);
return modelMap;
}
use of ca.corefacility.bioinformatics.irida.web.assembler.resource.LabelledRelationshipResource 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());
}
Aggregations