Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Peer Group): Add teacher endpoint to get work for peer group #50

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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<StudentWork> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<StudentWork> 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<StudentWork> 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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<StudentWork> peerGroup1StudentWork = new ArrayList<StudentWork>();

@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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Loading