use of ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection in project irida by phac-nml.
the class UserControllerTest method testGetAllUsers.
@Test
public void testGetAllUsers() {
String username = "fbristow";
User u = new User();
u.setUsername(username);
List<User> users = Lists.newArrayList(u);
when(userService.findAll()).thenReturn(users);
when(userService.count()).thenReturn(1L);
ModelMap output = controller.listAllResources();
@SuppressWarnings("unchecked") ResourceCollection<User> usersCollection = (ResourceCollection<User>) output.get(RESTGenericController.RESOURCE_NAME);
assertEquals("users collection is the wrong size.", 1, usersCollection.size());
User userResource = usersCollection.iterator().next();
assertEquals("username is not correct.", username, userResource.getUsername());
}
use of ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection in project irida by phac-nml.
the class ProjectUsersControllerTest method testGetUsersForProject.
@Test
public void testGetUsersForProject() throws ProjectWithoutOwnerException {
String username = "fbristow";
User u = new User();
u.setUsername(username);
u.setId(1L);
Project p = TestDataFactory.constructProject();
Join<Project, User> join = new ProjectUserJoin(p, u, ProjectRole.PROJECT_OWNER);
@SuppressWarnings("unchecked") List<Join<Project, User>> relationships = Lists.newArrayList(join);
when(userService.getUsersForProject(p)).thenReturn(relationships);
when(projectService.read(p.getId())).thenReturn(p);
ModelMap map = controller.getUsersForProject(p.getId());
verify(projectService, times(1)).read(p.getId());
verify(userService, times(1)).getUsersForProject(p);
Object o = map.get(RESTGenericController.RESOURCE_NAME);
assertNotNull(o);
assertTrue(o instanceof ResourceCollection);
@SuppressWarnings("unchecked") ResourceCollection<User> users = (ResourceCollection<User>) o;
assertEquals(1, users.size());
User ur = users.iterator().next();
assertTrue(ur.getLink("self").getHref().endsWith(username));
Link relationship = ur.getLink(RESTGenericController.REL_RELATIONSHIP);
assertNotNull(relationship);
assertEquals("http://localhost/api/projects/" + p.getId() + "/users/" + username, relationship.getHref());
assertTrue(users.getLink("self").getHref().contains(p.getId().toString()));
}
use of ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection 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