diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupAPIController.java b/src/main/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupAPIController.java index 9b1103273..d3df9a1cc 100644 --- a/src/main/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupAPIController.java +++ b/src/main/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupAPIController.java @@ -23,8 +23,6 @@ */ package org.wise.portal.presentation.web.controllers.peergroup; -import java.util.List; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.annotation.Secured; @@ -47,7 +45,6 @@ import org.wise.portal.service.run.RunService; import org.wise.portal.service.user.UserService; import org.wise.portal.service.workgroup.WorkgroupService; -import org.wise.vle.domain.work.StudentWork; @RestController @Secured("ROLE_USER") @@ -90,20 +87,4 @@ private PeerGroup getPeerGroup(Run run, String nodeId, String componentId, Workg PeerGroupActivity activity = peerGroupActivityService.getByComponent(run, nodeId, componentId); return peerGroupService.getPeerGroup(workgroup, activity); } - - @GetMapping("/{peerGroupId}/student-work") - List getPeerGroupWork(@PathVariable Long peerGroupId, Authentication auth) - throws ObjectNotFoundException { - PeerGroup peerGroup = peerGroupService.getById(peerGroupId); - if (isUserInPeerGroup(peerGroup, auth)) { - return peerGroupService.getStudentWork(peerGroup); - } else { - throw new AccessDeniedException("Not permitted"); - } - } - - private boolean isUserInPeerGroup(PeerGroup peerGroup, Authentication auth) { - User user = userService.retrieveUserByUsername(auth.getName()); - return peerGroup.isMember(user); - } } diff --git a/src/main/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupWorkAPIController.java b/src/main/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupWorkAPIController.java new file mode 100644 index 000000000..7db72afcf --- /dev/null +++ b/src/main/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupWorkAPIController.java @@ -0,0 +1,83 @@ +package org.wise.portal.presentation.web.controllers.peergroup; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.access.annotation.Secured; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.wise.portal.dao.ObjectNotFoundException; +import org.wise.portal.domain.peergroup.PeerGroup; +import org.wise.portal.domain.peergroupactivity.PeerGroupActivity; +import org.wise.portal.domain.run.Run; +import org.wise.portal.domain.user.User; +import org.wise.portal.domain.workgroup.Workgroup; +import org.wise.portal.service.peergroup.PeerGroupActivityThresholdNotSatisfiedException; +import org.wise.portal.service.peergroup.PeerGroupCreationException; +import org.wise.portal.service.peergroup.PeerGroupService; +import org.wise.portal.service.peergroupactivity.PeerGroupActivityNotFoundException; +import org.wise.portal.service.peergroupactivity.PeerGroupActivityService; +import org.wise.portal.service.run.RunService; +import org.wise.portal.service.user.UserService; +import org.wise.portal.service.workgroup.WorkgroupService; +import org.wise.vle.domain.work.StudentWork; + +@RestController +@Secured("ROLE_USER") +@RequestMapping("/api/peer-group") +public class PeerGroupWorkAPIController { + + @Autowired + private PeerGroupActivityService peerGroupActivityService; + + @Autowired + private PeerGroupService peerGroupService; + + @Autowired + private RunService runService; + + @Autowired + private UserService userService; + + @Autowired + private WorkgroupService workgroupService; + + @GetMapping("/{peerGroupId}/student-work") + List getPeerGroupWork(@PathVariable Long peerGroupId, Authentication auth) + throws ObjectNotFoundException { + PeerGroup peerGroup = peerGroupService.getById(peerGroupId); + if (isUserInPeerGroup(peerGroup, auth)) { + return peerGroupService.getStudentWork(peerGroup); + } else { + throw new AccessDeniedException("Not permitted"); + } + } + + private boolean isUserInPeerGroup(PeerGroup peerGroup, Authentication auth) { + User user = userService.retrieveUserByUsername(auth.getName()); + return peerGroup.isMember(user); + } + + @Secured("ROLE_TEACHER") + @GetMapping("/{runId}/{workgroupId}/{nodeId}/{componentId}/student-work") + List getPeerGroupWork(@PathVariable Long runId, @PathVariable Long workgroupId, + @PathVariable String nodeId, @PathVariable String componentId, Authentication auth) + throws ObjectNotFoundException, PeerGroupActivityNotFoundException, + PeerGroupActivityThresholdNotSatisfiedException, PeerGroupCreationException { + Run run = runService.retrieveById(runId); + User user = userService.retrieveUserByUsername(auth.getName()); + if (runService.isAllowedToViewStudentWork(run, user)) { + PeerGroupActivity activity = peerGroupActivityService.getByComponent(run, nodeId, + componentId); + Workgroup workgroup = workgroupService.retrieveById(workgroupId); + PeerGroup peerGroup = peerGroupService.getPeerGroup(workgroup, activity); + return peerGroupService.getStudentWork(peerGroup); + } else { + throw new AccessDeniedException("Not permitted"); + } + } +} diff --git a/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/AbstractPeerGroupAPIControllerTest.java b/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/AbstractPeerGroupAPIControllerTest.java new file mode 100644 index 000000000..8d67a514e --- /dev/null +++ b/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/AbstractPeerGroupAPIControllerTest.java @@ -0,0 +1,65 @@ +package org.wise.portal.presentation.web.controllers.peergroup; + +import static org.easymock.EasyMock.*; + +import org.easymock.Mock; +import org.junit.Before; +import org.wise.portal.domain.peergroup.PeerGroup; +import org.wise.portal.domain.peergroup.impl.PeerGroupImpl; +import org.wise.portal.domain.peergroupactivity.PeerGroupActivity; +import org.wise.portal.domain.peergroupactivity.impl.PeerGroupActivityImpl; +import org.wise.portal.presentation.web.controllers.APIControllerTest; +import org.wise.portal.service.peergroup.PeerGroupCreationException; +import org.wise.portal.service.peergroup.PeerGroupService; +import org.wise.portal.service.peergroupactivity.PeerGroupActivityNotFoundException; +import org.wise.portal.service.peergroupactivity.PeerGroupActivityService; + +public abstract class AbstractPeerGroupAPIControllerTest extends APIControllerTest { + + @Mock + protected PeerGroupService peerGroupService; + + @Mock + protected PeerGroupActivityService peerGroupActivityService; + + protected String run1Node1Id = "run1Node1"; + + protected String run1Component1Id = "run1Component1"; + + protected PeerGroupActivity peerGroupActivity; + + protected PeerGroup peerGroup1; + + protected Long peerGroup1Id = 1L; + + @Before + public void setUp() { + super.setUp(); + peerGroupActivity = new PeerGroupActivityImpl(); + peerGroup1 = new PeerGroupImpl(); + peerGroup1.addMember(workgroup1); + } + + protected void expectPeerGroupActivityFound() throws PeerGroupActivityNotFoundException { + expect(peerGroupActivityService.getByComponent(run1, run1Node1Id, run1Component1Id)) + .andReturn(peerGroupActivity); + } + + protected void expectPeerGroupActivityNotFound() throws PeerGroupActivityNotFoundException { + expect(peerGroupActivityService.getByComponent(run1, run1Node1Id, run1Component1Id)) + .andThrow(new PeerGroupActivityNotFoundException()); + } + + protected void expectPeerGroupCreationException() throws Exception { + expect(peerGroupService.getPeerGroup(workgroup1, peerGroupActivity)) + .andThrow(new PeerGroupCreationException()); + } + + protected void verifyAll() { + verify(peerGroupActivityService, peerGroupService, runService, userService, workgroupService); + } + + protected void replayAll() { + replay(peerGroupActivityService, peerGroupService, runService, userService, workgroupService); + } +} diff --git a/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupAPIControllerTest.java b/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupAPIControllerTest.java index 69b185150..2a6a14b6b 100644 --- a/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupAPIControllerTest.java +++ b/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupAPIControllerTest.java @@ -3,61 +3,21 @@ import static org.easymock.EasyMock.*; import static org.junit.Assert.*; -import java.util.ArrayList; -import java.util.List; - import org.easymock.EasyMockRunner; -import org.easymock.Mock; import org.easymock.TestSubject; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.security.access.AccessDeniedException; -import org.wise.portal.dao.ObjectNotFoundException; -import org.wise.portal.domain.peergroup.PeerGroup; -import org.wise.portal.domain.peergroup.impl.PeerGroupImpl; -import org.wise.portal.domain.peergroupactivity.PeerGroupActivity; -import org.wise.portal.domain.peergroupactivity.impl.PeerGroupActivityImpl; -import org.wise.portal.presentation.web.controllers.APIControllerTest; import org.wise.portal.service.peergroup.PeerGroupActivityThresholdNotSatisfiedException; import org.wise.portal.service.peergroup.PeerGroupCreationException; -import org.wise.portal.service.peergroup.PeerGroupService; import org.wise.portal.service.peergroupactivity.PeerGroupActivityNotFoundException; -import org.wise.portal.service.peergroupactivity.PeerGroupActivityService; -import org.wise.vle.domain.work.StudentWork; @RunWith(EasyMockRunner.class) -public class PeerGroupAPIControllerTest extends APIControllerTest { +public class PeerGroupAPIControllerTest extends AbstractPeerGroupAPIControllerTest { @TestSubject private PeerGroupAPIController controller = new PeerGroupAPIController(); - @Mock - private PeerGroupService peerGroupService; - - @Mock - private PeerGroupActivityService peerGroupActivityService; - - private String run1Node1Id = "run1Node1"; - - private String run1Component1Id = "run1Component1"; - - private PeerGroupActivity peerGroupActivity; - - private PeerGroup peerGroup1; - - private Long peerGroup1Id = 1L; - - private List peerGroup1StudentWork = new ArrayList(); - - @Before - public void setUp() { - super.setUp(); - peerGroupActivity = new PeerGroupActivityImpl(); - peerGroup1 = new PeerGroupImpl(); - peerGroup1.addMember(workgroup1); - } - @Test public void getPeerGroup_WorkgroupNotAssociatedWithRun_AccessDenied() throws Exception { expectWorkgroupAssociatedWithRun(false); @@ -73,7 +33,7 @@ public void getPeerGroup_WorkgroupNotAssociatedWithRun_AccessDenied() throws Exc @Test public void getPeerGroup_PeerGroupActivityNotFound_ThrowException() throws Exception { expectWorkgroupAssociatedWithRun(true); - expectPeerGroupActivityFound(false); + expectPeerGroupActivityNotFound(); replayAll(); try { controller.getPeerGroup(runId1, workgroup1Id, run1Node1Id, run1Component1Id, studentAuth); @@ -119,70 +79,10 @@ public void getPeerGroup_FoundExistingGroupOrGroupCreated_ReturnGroup() throws E verifyAll(); } - @Test - public void getPeerGroupWork_NonExistingPeerGroupId_ThrowObjectNotFound() { - expectPeerGroupIdNotExist(); - replayAll(); - try { - controller.getPeerGroupWork(peerGroup1Id, studentAuth); - fail("Expected ObjectNotFoundException, but was not thrown"); - } catch (ObjectNotFoundException e) { - } - verifyAll(); - } - - @Test - public void getPeerGroupWork_UserNotInPeerGroup_ThrowAccessDenied() - throws ObjectNotFoundException { - expectPeerGroup(); - expectUserNotInPeerGroup(); - replayAll(); - try { - controller.getPeerGroupWork(peerGroup1Id, studentAuth); - fail("Expected AccessDeniedException, but was not thrown"); - } catch (AccessDeniedException e) { - } - verifyAll(); - } - - @Test - public void getPeerGroupWork_UserInPeerGroup_ReturnStudentWork() throws ObjectNotFoundException { - expectPeerGroup(); - expectUserInPeerGroup(); - expectGetStudentWork(); - replayAll(); - assertNotNull(controller.getPeerGroupWork(peerGroup1Id, studentAuth)); - verifyAll(); - } - - private void expectGetStudentWork() { - expect(peerGroupService.getStudentWork(peerGroup1)).andReturn(peerGroup1StudentWork); - } - - private void expectUserNotInPeerGroup() { - expect(userService.retrieveUserByUsername(studentAuth.getName())).andReturn(student2); - } - - private void expectUserInPeerGroup() { - expect(userService.retrieveUserByUsername(studentAuth.getName())).andReturn(student1); - } - - private void expectPeerGroup() throws ObjectNotFoundException { - expect(peerGroupService.getById(peerGroup1Id)).andReturn(peerGroup1); - } - - private void expectPeerGroupIdNotExist() { - try { - expect(peerGroupService.getById(peerGroup1Id)).andThrow(new ObjectNotFoundException( - peerGroup1Id, PeerGroup.class)); - } catch (ObjectNotFoundException e) { - } - } - private void expectWorkgroupAssociatedWithRunAndActivityFound() throws Exception, PeerGroupActivityNotFoundException { expectWorkgroupAssociatedWithRun(true); - expectPeerGroupActivityFound(true); + expectPeerGroupActivityFound(); } private void expectWorkgroupAssociatedWithRun(boolean isAssociated) throws Exception { @@ -193,36 +93,12 @@ private void expectWorkgroupAssociatedWithRun(boolean isAssociated) throws Excep .andReturn(isAssociated); } - private void expectPeerGroupActivityFound(boolean isFound) - throws PeerGroupActivityNotFoundException { - if (isFound) { - expect(peerGroupActivityService.getByComponent(run1, run1Node1Id, run1Component1Id)) - .andReturn(peerGroupActivity); - } else { - expect(peerGroupActivityService.getByComponent(run1, run1Node1Id, run1Component1Id)) - .andThrow(new PeerGroupActivityNotFoundException()); - } - } - private void expectPeerGroupThresholdNotSatisifed() throws Exception { expect(peerGroupService.getPeerGroup(workgroup1, peerGroupActivity)) .andThrow(new PeerGroupActivityThresholdNotSatisfiedException()); } - private void expectPeerGroupCreationException() throws Exception { - expect(peerGroupService.getPeerGroup(workgroup1, peerGroupActivity)) - .andThrow(new PeerGroupCreationException()); - } - private void expectPeerGroupCreated() throws Exception { expect(peerGroupService.getPeerGroup(workgroup1, peerGroupActivity)).andReturn(peerGroup1); } - - private void verifyAll() { - verify(peerGroupActivityService, peerGroupService, runService, userService, workgroupService); - } - - private void replayAll() { - replay(peerGroupActivityService, peerGroupService, runService, userService, workgroupService); - } } diff --git a/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupWorkAPIControllerTest.java b/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupWorkAPIControllerTest.java new file mode 100644 index 000000000..eed4ddac8 --- /dev/null +++ b/src/test/java/org/wise/portal/presentation/web/controllers/peergroup/PeerGroupWorkAPIControllerTest.java @@ -0,0 +1,213 @@ +package org.wise.portal.presentation.web.controllers.peergroup; + +import static org.easymock.EasyMock.expect; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.easymock.EasyMockRunner; +import org.easymock.TestSubject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.security.access.AccessDeniedException; +import org.wise.portal.dao.ObjectNotFoundException; +import org.wise.portal.domain.peergroup.PeerGroup; +import org.wise.portal.domain.run.impl.RunImpl; +import org.wise.portal.domain.workgroup.impl.WorkgroupImpl; +import org.wise.portal.service.peergroup.PeerGroupCreationException; +import org.wise.portal.service.peergroupactivity.PeerGroupActivityNotFoundException; +import org.wise.vle.domain.work.StudentWork; + +@RunWith(EasyMockRunner.class) +public class PeerGroupWorkAPIControllerTest extends AbstractPeerGroupAPIControllerTest { + + @TestSubject + private PeerGroupWorkAPIController controller = new PeerGroupWorkAPIController(); + + private List peerGroup1StudentWork = new ArrayList(); + + @Test + public void getPeerGroupWork_NonExistingPeerGroupIdSpecifiedByPeerGroupId_ThrowObjectNotFound() + throws ObjectNotFoundException { + expectPeerGroupIdNotExist(); + replayAll(); + try { + controller.getPeerGroupWork(peerGroup1Id, studentAuth); + fail("Expected ObjectNotFoundException, but was not thrown"); + } catch (ObjectNotFoundException e) { + } + verifyAll(); + } + + @Test + public void getPeerGroupWork_UserNotInPeerGroupSpecifiedByPeerGroupId_ThrowAccessDenied() + throws ObjectNotFoundException { + expectPeerGroup(); + expectUserNotInPeerGroup(); + replayAll(); + try { + controller.getPeerGroupWork(peerGroup1Id, studentAuth); + fail("Expected AccessDeniedException, but was not thrown"); + } catch (AccessDeniedException e) { + } + verifyAll(); + } + + @Test + public void getPeerGroupWork_UserInPeerGroupSpecifiedByPeerGroupId_ReturnStudentWork() + throws ObjectNotFoundException { + expectPeerGroup(); + expectUserInPeerGroup(); + expectGetStudentWork(); + replayAll(); + assertNotNull(controller.getPeerGroupWork(peerGroup1Id, studentAuth)); + verifyAll(); + } + + @Test + public void getPeerGroupWork_NonExistingRunIdSpecifiedByComponent_ThrowObjectNotFound() + throws Exception { + expectRunNotExists(); + replayAll(); + try { + controller.getPeerGroupWork(runId1, workgroup1Id, run1Node1Id, run1Component1Id, teacherAuth); + fail("Expected ObjectNotFoundException, but was not thrown"); + } catch (ObjectNotFoundException e) { + } + verifyAll(); + } + + @Test + public void getPeerGroupWork_TeacherNotAllowedToViewRunTeacherSpecifiedByComponent_ThrowAccessDenied() + throws Exception { + expectRunExists(); + expectRetrieveTeacherUser(); + expectUserNotAllowedToViewStudentWorkForRun(); + replayAll(); + try { + controller.getPeerGroupWork(runId1, workgroup1Id, run1Node1Id, run1Component1Id, teacherAuth); + fail("Expected AccessDeniedException, but was not thrown"); + } catch (AccessDeniedException e) { + } + verifyAll(); + } + + @Test + public void getPeerGroupWork_PeerGroupActivityNotFoundSpecifiedByComponent_ThrowException() + throws Exception { + expectUserAllowedToViewStudentWorkForRun(); + expectPeerGroupActivityNotFound(); + replayAll(); + try { + controller.getPeerGroupWork(runId1, workgroup1Id, run1Node1Id, run1Component1Id, + teacherAuth); + fail("PeerGroupActivityNotFoundException expected, but was not thrown"); + } catch (PeerGroupActivityNotFoundException e) {} + verifyAll(); + } + + @Test + public void getPeerGroupWork_WorkgroupNotFoundSpecifiedByComponent_ThrowObjectNotFound() + throws Exception { + expectUserAllowedToViewStudentWorkForRun(); + expectPeerGroupActivityFound(); + expectWorkgroupNotFound(); + replayAll(); + try { + controller.getPeerGroupWork(runId1, workgroup1Id, run1Node1Id, run1Component1Id, + teacherAuth); + fail("ObjectNotFoundException expected, but was not thrown"); + } catch (ObjectNotFoundException e) {} + verifyAll(); + } + + @Test + public void getPeerGroupWork_PeerGroupNotFoundSpecifiedByComponent_ThrowException() + throws Exception { + expectUserAllowedToViewStudentWorkForRun(); + expectPeerGroupActivityFound(); + expectWorkgroupFound(); + expectPeerGroupCreationException(); + replayAll(); + try { + controller.getPeerGroupWork(runId1, workgroup1Id, run1Node1Id, run1Component1Id, + teacherAuth); + fail("PeerGroupCreationException expected, but was not thrown"); + } catch (PeerGroupCreationException e) {} + verifyAll(); + } + + @Test + public void getPeerGroupWork_PeerGroupFoundSpecifiedByComponent_ReturnStudentWork() + throws Exception { + expectUserAllowedToViewStudentWorkForRun(); + expectPeerGroupActivityFound(); + expectWorkgroupFound(); + expectPeerGroupFound(); + expectGetStudentWork(); + replayAll(); + assertNotNull(controller.getPeerGroupWork(runId1, workgroup1Id, run1Node1Id, run1Component1Id, + teacherAuth)); + verifyAll(); + } + + private void expectRetrieveTeacherUser() { + expect(userService.retrieveUserByUsername(teacher1UserDetails.getUsername())).andReturn( + teacher1); + } + + private void expectRunNotExists() throws ObjectNotFoundException { + expect(runService.retrieveById(runId1)).andThrow(new ObjectNotFoundException(runId1, + RunImpl.class)); + } + + private void expectRunExists() throws ObjectNotFoundException { + expect(runService.retrieveById(runId1)).andReturn(run1); + } + + private void expectPeerGroupFound() throws Exception { + expect(peerGroupService.getPeerGroup(workgroup1, peerGroupActivity)).andReturn(peerGroup1); + } + + private void expectWorkgroupFound() throws ObjectNotFoundException { + expect(workgroupService.retrieveById(workgroup1Id)).andReturn(workgroup1); + } + + private void expectWorkgroupNotFound() throws ObjectNotFoundException { + expect(workgroupService.retrieveById(workgroup1Id)).andThrow( + new ObjectNotFoundException(workgroup1Id, WorkgroupImpl.class)); + } + + private void expectUserAllowedToViewStudentWorkForRun() throws ObjectNotFoundException { + expectRunExists(); + expectRetrieveTeacherUser(); + expect(runService.isAllowedToViewStudentWork(run1, teacher1)).andReturn(true); + } + + private void expectUserNotAllowedToViewStudentWorkForRun() { + expect(runService.isAllowedToViewStudentWork(run1, teacher1)).andReturn(false); + } + + private void expectGetStudentWork() { + expect(peerGroupService.getStudentWork(peerGroup1)).andReturn(peerGroup1StudentWork); + } + + private void expectUserNotInPeerGroup() { + expect(userService.retrieveUserByUsername(studentAuth.getName())).andReturn(student2); + } + + private void expectUserInPeerGroup() { + expect(userService.retrieveUserByUsername(studentAuth.getName())).andReturn(student1); + } + + private void expectPeerGroup() throws ObjectNotFoundException { + expect(peerGroupService.getById(peerGroup1Id)).andReturn(peerGroup1); + } + + private void expectPeerGroupIdNotExist() throws ObjectNotFoundException { + expect(peerGroupService.getById(peerGroup1Id)).andThrow(new ObjectNotFoundException( + peerGroup1Id, PeerGroup.class)); + } +}