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): Make PeerGroup with remaining students #53

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 @@ -25,6 +25,7 @@

import java.util.List;
import org.wise.portal.dao.SimpleDao;
import org.wise.portal.domain.group.Group;
import org.wise.portal.domain.peergroup.PeerGroup;
import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
import org.wise.portal.domain.run.Run;
Expand All @@ -43,5 +44,5 @@ public interface PeerGroupDao<T extends PeerGroup> extends SimpleDao<T> {

List<PeerGroup> getListByWorkgroup(Workgroup workgroup);

List<Workgroup> getWorkgroupsInPeerGroup(PeerGroupActivity activity);
List<Workgroup> getWorkgroupsInPeerGroup(PeerGroupActivity activity, Group period);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.springframework.stereotype.Repository;
import org.wise.portal.dao.impl.AbstractHibernateDao;
import org.wise.portal.dao.peergroup.PeerGroupDao;
import org.wise.portal.domain.group.Group;
import org.wise.portal.domain.peergroup.PeerGroup;
import org.wise.portal.domain.peergroup.impl.PeerGroupImpl;
import org.wise.portal.domain.peergroupactivity.PeerGroupActivity;
Expand Down Expand Up @@ -130,13 +131,14 @@ public List<PeerGroup> getListByWorkgroup(Workgroup workgroup) {

@Override
@SuppressWarnings("unchecked")
public List<Workgroup> getWorkgroupsInPeerGroup(PeerGroupActivity activity) {
public List<Workgroup> getWorkgroupsInPeerGroup(PeerGroupActivity activity, Group period) {
CriteriaBuilder cb = getCriteriaBuilder();
CriteriaQuery<WorkgroupImpl> cq = cb.createQuery(WorkgroupImpl.class);
Root<PeerGroupImpl> peerGroupImplRoot = cq.from(PeerGroupImpl.class);
Root<WorkgroupImpl> workgroupImplRoot = cq.from(WorkgroupImpl.class);
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.equal(peerGroupImplRoot.get("peerGroupActivity"), activity.getId()));
predicates.add(cb.equal(workgroupImplRoot.get("period"), period.getId()));
predicates.add(cb.isMember(workgroupImplRoot.get("id"),
peerGroupImplRoot.<Set<Workgroup>>get("members")));
cq.select(workgroupImplRoot).where(predicates.toArray(new Predicate[predicates.size()]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
import org.springframework.stereotype.Service;
import org.wise.portal.dao.ObjectNotFoundException;
import org.wise.portal.dao.peergroup.PeerGroupDao;
import org.wise.portal.dao.run.RunDao;
import org.wise.portal.dao.work.StudentWorkDao;
import org.wise.portal.domain.group.Group;
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.run.Run;
import org.wise.portal.domain.workgroup.Workgroup;
import org.wise.portal.service.peergroup.PeerGroupCreationException;
import org.wise.portal.service.peergroup.PeerGroupActivityThresholdNotSatisfiedException;
Expand All @@ -61,6 +63,9 @@ public class PeerGroupServiceImpl implements PeerGroupService {
@Autowired
private StudentWorkDao<StudentWork> studentWorkDao;

@Autowired
private RunDao<Run> runDao;

public PeerGroup getById(Long id) throws ObjectNotFoundException {
return peerGroupDao.getById(id);
}
Expand Down Expand Up @@ -102,30 +107,61 @@ private boolean hasWorkForPeerGroupLogicActivity(Workgroup workgroup,

private Set<Workgroup> getPeerGroupMembers(Workgroup workgroup, PeerGroupActivity activity)
throws PeerGroupCreationException {
Set<Workgroup> members = new HashSet<Workgroup>();
members.add(workgroup);
try {
List<StudentWork> logicComponentStudentWorkByWorkgroupNotInPeerGroup =
getLogicComponentStudentWorkByWorkgroupNotInPeerGroup(workgroup, activity);
for (StudentWork work : logicComponentStudentWorkByWorkgroupNotInPeerGroup) {
members.add(work.getWorkgroup());
if (members.size() == activity.getMaxMembershipCount()) {
break;
}
List<Workgroup> workgroupsInPeerGroup = peerGroupDao.getWorkgroupsInPeerGroup(activity,
workgroup.getPeriod());
Set<Workgroup> workgroupsNotInPeerGroupAndCompletedLogicActivity =
getWorkgroupsNotInPeerGroupAndCompletedLogicActivity(activity, workgroup.getPeriod(),
workgroupsInPeerGroup);
if (isLastOnesLeftToPair(workgroup.getRun(), workgroup.getPeriod(), workgroupsInPeerGroup,
workgroupsNotInPeerGroupAndCompletedLogicActivity, activity.getMaxMembershipCount())) {
return workgroupsNotInPeerGroupAndCompletedLogicActivity;
} else {
return getWorkgroupsInPeerGroupUpToMaxMembership(workgroup, activity,
workgroupsNotInPeerGroupAndCompletedLogicActivity);
}
} catch (JSONException e) {
throw new PeerGroupCreationException();
}
}

private boolean isLastOnesLeftToPair(Run run, Group period, List<Workgroup> workgroupsInPeerGroup,
Set<Workgroup> workgroupsNotInPeerGroupAndCompletedLogicActivity, int maxMembershipCount) {
List<Workgroup> workgroupsInPeriod = runDao.getWorkgroupsForRunAndPeriod(run.getId(),
period.getId());
workgroupsInPeriod.removeIf(workgroup -> workgroup.getMembers().size() == 0);
int numWorkgroupsNotInPeerGroup = workgroupsInPeriod.size() - workgroupsInPeerGroup.size();
int numWorkgroupsNotInPeerGroupAndCompletedLogicActivity =
workgroupsNotInPeerGroupAndCompletedLogicActivity.size();
return numWorkgroupsNotInPeerGroup == numWorkgroupsNotInPeerGroupAndCompletedLogicActivity &&
(numWorkgroupsNotInPeerGroupAndCompletedLogicActivity - maxMembershipCount) <= 1;
}

private Set<Workgroup> getWorkgroupsInPeerGroupUpToMaxMembership(Workgroup workgroup,
PeerGroupActivity activity, Set<Workgroup> possibleMembers) {
Set<Workgroup> members = new HashSet<Workgroup>();
members.add(workgroup);
for (Workgroup possibleMember : possibleMembers) {
members.add(possibleMember);
if (members.size() == activity.getMaxMembershipCount()) {
break;
}
}
return members;
}

private List<StudentWork> getLogicComponentStudentWorkByWorkgroupNotInPeerGroup(
Workgroup workgroup, PeerGroupActivity activity) throws JSONException {
List<Workgroup> workgroupsInPeerGroup = peerGroupDao.getWorkgroupsInPeerGroup(activity);
private Set<Workgroup> getWorkgroupsNotInPeerGroupAndCompletedLogicActivity(
PeerGroupActivity activity, Group period, List<Workgroup> workgroupsInPeerGroup)
throws JSONException {
List<StudentWork> logicComponentStudentWork = getLogicComponentStudentWorkForPeriod(activity,
workgroup.getPeriod());
logicComponentStudentWork.removeIf(work -> workgroupsInPeerGroup.contains(work.getWorkgroup()));
return logicComponentStudentWork;
period);
Set<Workgroup> workgroupsNotInPeerGroup = new HashSet<Workgroup>();
for (StudentWork work : logicComponentStudentWork) {
if (!workgroupsInPeerGroup.contains(work.getWorkgroup())) {
workgroupsNotInPeerGroup.add(work.getWorkgroup());
}
}
return workgroupsNotInPeerGroup;
}

private List<StudentWork> getLogicComponentStudentWorkForPeriod(PeerGroupActivity activity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void getListByWorkgroup_WorkgroupInAndNotInPeerGroups_ReturnListByWorkgro

@Test
public void getWorkgroupsInPeerGroup_ActivityWithPeerGroups_ReturnWorkgroupList() {
assertEquals(1, peerGroupDao.getWorkgroupsInPeerGroup(activity1).size());
assertEquals(2, peerGroupDao.getWorkgroupsInPeerGroup(activity2).size());
assertEquals(1, peerGroupDao.getWorkgroupsInPeerGroup(activity1, run1Period1).size());
assertEquals(2, peerGroupDao.getWorkgroupsInPeerGroup(activity2, run1Period1).size());
}
}
22 changes: 13 additions & 9 deletions src/test/java/org/wise/portal/service/WISEServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,33 @@ public class WISEServiceTest {

protected User student1, student2, student3, student4, student5;

protected StudentWork componentWorkSubmit1, componentWorkSubmit2, componentWorkNonSubmit1;
protected StudentWork componentWorkSubmit1, componentWorkSubmit2, componentWorkSubmit3,
componentWorkSubmit4, componentWorkNonSubmit1;

@Before
public void setUp() throws Exception {
run1Period1 = new PersistentGroup();
run1Period1.setId(run1Id);
run1Workgroup1 = createWorkgroupInPeriod(1L, run1Period1, student1);
run1Workgroup2 = createWorkgroupInPeriod(2L, run1Period1, student2);
run1Workgroup3 = createWorkgroupInPeriod(3L, run1Period1, student3);
run1Workgroup4 = createWorkgroupInPeriod(4L, run1Period1, student4);
run1Workgroup5 = createWorkgroupInPeriod(5L, run1Period1, student5);
run1 = new RunImpl();
run1.setId(run1Id);
run1Period1 = new PersistentGroup();
run1Period1.setId(run1Id);
run1Workgroup1 = createWorkgroupInPeriod(run1, 1L, run1Period1, student1);
run1Workgroup2 = createWorkgroupInPeriod(run1, 2L, run1Period1, student2);
run1Workgroup3 = createWorkgroupInPeriod(run1, 3L, run1Period1, student3);
run1Workgroup4 = createWorkgroupInPeriod(run1, 4L, run1Period1, student4);
run1Workgroup5 = createWorkgroupInPeriod(run1, 5L, run1Period1, student5);
run1Component1 = new Component(run1, run1Node1Id, run1Component1Id);
run1Component2 = new Component(run1, run1Node2Id, run1Component2Id);
componentWorkSubmit1 = createComponentWork(run1Workgroup1, true);
componentWorkSubmit2 = createComponentWork(run1Workgroup2, true);
componentWorkSubmit3 = createComponentWork(run1Workgroup3, true);
componentWorkSubmit4 = createComponentWork(run1Workgroup4, true);
componentWorkNonSubmit1 = createComponentWork(run1Workgroup1, false);
}

private Workgroup createWorkgroupInPeriod(Long id, Group period, User member) {
private Workgroup createWorkgroupInPeriod(Run run, Long id, Group period, User member) {
Workgroup workgroup = new WorkgroupImpl();
workgroup.setId(id);
workgroup.setRun(run);
workgroup.setPeriod(period);
Set<User> members = new HashSet<User>();
members.add(member);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
import org.junit.runner.RunWith;
import org.wise.portal.dao.peergroup.PeerGroupDao;
import org.wise.portal.dao.peergroupactivity.PeerGroupActivityDao;
import org.wise.portal.dao.run.RunDao;
import org.wise.portal.dao.work.StudentWorkDao;
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.run.impl.RunImpl;
import org.wise.portal.service.WISEServiceTest;
import org.wise.portal.service.peergroup.PeerGroupActivityThresholdNotSatisfiedException;
import org.wise.portal.service.peergroup.PeerGroupCreationException;
Expand All @@ -68,11 +70,14 @@ public class PeerGroupServiceImplTest extends WISEServiceTest {
private PeerGroupActivityDao<PeerGroupActivity> peerGroupActivityDao;

@Mock
private StudentWorkDao<StudentWork> studentWorkDao;
private RunDao<RunImpl> runDao;

@Mock
private RunService runService;

@Mock
private StudentWorkDao<StudentWork> studentWorkDao;

PeerGroupActivity activity;

PeerGroup peerGroup;
Expand All @@ -94,7 +99,7 @@ public void getPeerGroup_PeerGroupInDB_ReturnPeerGroup() throws Exception {
}

@Test
public void getPeerGroup_PeerGroupNotInDBAndNoWorkForLogicComponent_ThrowException()
public void getPeerGroup_NoWorkForLogicComponent_ThrowException()
throws Exception {
expectPeerGroupFromDB(null);
expectWorkForComponentByWorkgroup(Arrays.asList());
Expand All @@ -108,7 +113,7 @@ public void getPeerGroup_PeerGroupNotInDBAndNoWorkForLogicComponent_ThrowExcepti
}

@Test
public void getPeerGroup_PeerGroupNotInDBAndCompletionThresholdNotSatisfied_ThrowException()
public void getPeerGroup_CompletionThresholdNotSatisfied_ThrowException()
throws Exception {
expectPeerGroupFromDB(null);
expectWorkForComponentByWorkgroup(Arrays.asList(componentWorkSubmit1));
Expand All @@ -123,7 +128,7 @@ public void getPeerGroup_PeerGroupNotInDBAndCompletionThresholdNotSatisfied_Thro
}

@Test
public void getPeerGroup_PeerGroupNotInDBAndWorkgroupCountThresholdNotSatisfied_ThrowException()
public void getPeerGroup_WorkgroupCountThresholdNotSatisfied_ThrowException()
throws Exception {
expectPeerGroupFromDB(null);
expectWorkForComponentByWorkgroup(Arrays.asList(componentWorkSubmit1));
Expand All @@ -139,19 +144,32 @@ public void getPeerGroup_PeerGroupNotInDBAndWorkgroupCountThresholdNotSatisfied_
}

@Test(timeout = 250)
public void getPeerGroup_PeerGroupNotInDBAndAllThresholdsSatisfied_CreateAndReturnPeerGroup()
public void getPeerGroup_AllThresholdsSatisfied3WorkgroupsLeft_Create3WorkgroupPeerGroup()
throws Exception {
expectPeerGroupFromDB(null);
expectWorkForComponentByWorkgroup(Arrays.asList(componentWorkSubmit1));
expectCompletionThresholdSatisfied(true);
expectWorkgroupCountThresholdSatisfied(true);
expectAllThresholdsSatisfied();
expectWorkgroupsInPeerGroup(Arrays.asList());
expectWorkForLogicComponent(createStudentWorkList(componentWorkSubmit1, componentWorkSubmit2,
componentWorkNonSubmit1));
componentWorkSubmit3, componentWorkNonSubmit1));
expectIsLastOnesLeftToPair();
peerGroupDao.save(isA(PeerGroupImpl.class));
expectLastCall();
replayAll();
assertNotNull(service.getPeerGroup(run1Workgroup1, activity));
assertEquals(3, service.getPeerGroup(run1Workgroup1, activity).getMembers().size());
verifyAll();
}

@Test(timeout = 250)
public void getPeerGroup_AllThresholdsSatisfiedMoreThan3WorkgroupsLeft_Create2WorkgroupPeerGroup()
throws Exception {
expectAllThresholdsSatisfied();
expectWorkgroupsInPeerGroup(Arrays.asList());
expectWorkForLogicComponent(createStudentWorkList(componentWorkSubmit1, componentWorkSubmit2,
componentWorkSubmit3, componentWorkSubmit4, componentWorkNonSubmit1));
expectIsMultiplePairingsLeft();
peerGroupDao.save(isA(PeerGroupImpl.class));
expectLastCall();
replayAll();
assertEquals(2, service.getPeerGroup(run1Workgroup1, activity).getMembers().size());
verifyAll();
}

Expand All @@ -163,6 +181,13 @@ public void getStudentWork_PeerGroupExist_ReturnStudentWorkList() {
verifyAll();
}

private void expectAllThresholdsSatisfied() throws JSONException {
expectPeerGroupFromDB(null);
expectWorkForComponentByWorkgroup(Arrays.asList(componentWorkSubmit1));
expectCompletionThresholdSatisfied(true);
expectWorkgroupCountThresholdSatisfied(true);
}

private void expectGetWorkForComponentByWorkgroups() {
expect(studentWorkDao.getWorkForComponentByWorkgroups(peerGroup.getMembers(),
peerGroup.getPeerGroupActivity().getNodeId(),
Expand All @@ -172,7 +197,7 @@ private void expectGetWorkForComponentByWorkgroups() {
}

private void expectWorkgroupsInPeerGroup(List<Object> asList) {
expect(peerGroupDao.getWorkgroupsInPeerGroup(activity)).andReturn(Arrays.asList());
expect(peerGroupDao.getWorkgroupsInPeerGroup(activity, run1Period1)).andReturn(Arrays.asList());
}

private void expectWorkgroupCountThresholdSatisfied(boolean isSatisfied) {
Expand Down Expand Up @@ -200,11 +225,21 @@ private void expectWorkForComponentByWorkgroup(List<StudentWork> expectedWork)
activity.getLogicComponentId())).andReturn(expectedWork);
}

private void expectIsLastOnesLeftToPair() {
expect(runDao.getWorkgroupsForRunAndPeriod(run1Id, run1Period1.getId())).andReturn(
Arrays.asList(run1Workgroup1, run1Workgroup2, run1Workgroup3));
}

private void expectIsMultiplePairingsLeft() {
expect(runDao.getWorkgroupsForRunAndPeriod(run1Id, run1Period1.getId())).andReturn(
Arrays.asList(run1Workgroup1, run1Workgroup2, run1Workgroup3, run1Workgroup4));
}

private void verifyAll() {
verify(peerGroupDao, peerGroupThresholdService, runService, studentWorkDao);
verify(peerGroupDao, peerGroupThresholdService, runDao, runService, studentWorkDao);
}

private void replayAll() {
replay(peerGroupDao, peerGroupThresholdService, runService, studentWorkDao);
replay(peerGroupDao, peerGroupThresholdService, runDao, runService, studentWorkDao);
}
}