use of ca.corefacility.bioinformatics.irida.model.project.Project 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.model.project.Project in project irida by phac-nml.
the class ProjectUsersControllerTest method testRemoveUserFromProject.
@Test
public void testRemoveUserFromProject() throws ProjectWithoutOwnerException {
Project p = TestDataFactory.constructProject();
User u = TestDataFactory.constructUser();
when(projectService.read(p.getId())).thenReturn(p);
when(userService.getUserByUsername(u.getUsername())).thenReturn(u);
ModelMap modelMap = controller.removeUserFromProject(p.getId(), u.getUsername());
verify(projectService).read(p.getId());
verify(userService).getUserByUsername(u.getUsername());
verify(projectService).removeUserFromProject(p, u);
Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
assertTrue(o instanceof RootResource);
RootResource r = (RootResource) o;
// confirm that a project link exists
Link projectLink = r.getLink(RESTProjectsController.REL_PROJECT);
assertNotNull(projectLink);
assertEquals("http://localhost/api/projects/" + p.getId(), projectLink.getHref());
// confirm that a project users link exists
Link projectUsersLink = r.getLink(RESTProjectUsersController.REL_PROJECT_USERS);
assertNotNull(projectUsersLink);
assertEquals("http://localhost/api/projects/" + p.getId() + "/users", projectUsersLink.getHref());
}
use of ca.corefacility.bioinformatics.irida.model.project.Project in project irida by phac-nml.
the class RESTProjectSamplesControllerTest method testRemoveSampleFromProject.
@Test
public void testRemoveSampleFromProject() {
Project p = TestDataFactory.constructProject();
Sample s = TestDataFactory.constructSample();
when(projectService.read(p.getId())).thenReturn(p);
when(sampleService.read(s.getId())).thenReturn(s);
ModelMap modelMap = controller.removeSampleFromProject(p.getId(), s.getId());
// verify that we actually tried to remove the sample from the project.
verify(projectService, times(1)).removeSampleFromProject(p, s);
verify(projectService, times(1)).read(p.getId());
verify(sampleService, times(1)).read(s.getId());
// confirm that the response looks right.
Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
assertTrue(o instanceof RootResource);
RootResource resource = (RootResource) o;
List<Link> links = resource.getLinks();
// should be two links in the response, one back to the individual
// project, the other to the samples collection
Set<String> rels = Sets.newHashSet(RESTProjectsController.REL_PROJECT, RESTProjectSamplesController.REL_PROJECT_SAMPLES);
for (Link link : links) {
assertTrue(rels.contains(link.getRel()));
assertNotNull(rels.remove(link.getRel()));
}
assertTrue(rels.isEmpty());
}
use of ca.corefacility.bioinformatics.irida.model.project.Project 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.project.Project in project irida by phac-nml.
the class RESTProjectSamplesControllerTest method testAlreadyCopiedSampleToProject.
@Test(expected = EntityExistsException.class)
public void testAlreadyCopiedSampleToProject() {
final Project p = TestDataFactory.constructProject();
final Sample s = TestDataFactory.constructSample();
when(projectService.read(p.getId())).thenReturn(p);
when(sampleService.read(s.getId())).thenReturn(s);
when(projectService.addSampleToProject(p, s, false)).thenThrow(new EntityExistsException("sample already exists!"));
controller.copySampleToProject(p.getId(), Lists.newArrayList(s.getId()), new MockHttpServletResponse());
}
Aggregations