Manager secm = ManagerFactory.getManager();
BGRightManager rm = BGRightManagerImpl.getInstance();
BGAreaManager am = BGAreaManagerImpl.getInstance();
// 1) context one: learning groups
BGContext c1 = cm.createAndAddBGContextToResource("c1name", course1, BusinessGroup.TYPE_LEARNINGROUP, id1, true);
// create groups without waitinglist
BusinessGroup g1 = bgm.createAndPersistBusinessGroup(BusinessGroup.TYPE_LEARNINGROUP, null, "g1", null, new Integer(0), new Integer(10), false, false, c1);
BusinessGroup g2 = bgm.createAndPersistBusinessGroup(BusinessGroup.TYPE_LEARNINGROUP, null, "g2", null, new Integer(0), new Integer(10), false, false, c1);
// members
secm.addIdentityToSecurityGroup(id1, g2.getOwnerGroup());
secm.addIdentityToSecurityGroup(id1, g1.getPartipiciantGroup());
secm.addIdentityToSecurityGroup(id2, g1.getPartipiciantGroup());
secm.addIdentityToSecurityGroup(id2, g2.getPartipiciantGroup());
secm.addIdentityToSecurityGroup(id3, g1.getOwnerGroup());
// areas
BGArea a1 = am.createAndPersistBGAreaIfNotExists("a1", "desca1",c1);
BGArea a2 = am.createAndPersistBGAreaIfNotExists("a2", null, c1);
BGArea a3 = am.createAndPersistBGAreaIfNotExists("a3", null, c1);
am.addBGToBGArea(g1, a1);
am.addBGToBGArea(g2, a1);
am.addBGToBGArea(g1, a2);
am.addBGToBGArea(g2, a3);
// 2) context two: right groups
BGContext c2 = cm.createAndAddBGContextToResource("c2name", course1, BusinessGroup.TYPE_RIGHTGROUP, id2, true);
// groups
BusinessGroup g3 = bgm.createAndPersistBusinessGroup(BusinessGroup.TYPE_RIGHTGROUP, null, "g3", null, null, null, null/* enableWaitinglist */, null/* enableAutoCloseRanks */, c2);
BusinessGroup g4 = bgm.createAndPersistBusinessGroup(BusinessGroup.TYPE_RIGHTGROUP, null, "g4", null, null, null, null/* enableWaitinglist */, null/* enableAutoCloseRanks */, c2);
// members
secm.addIdentityToSecurityGroup(id1, g3.getPartipiciantGroup());
secm.addIdentityToSecurityGroup(id1, g4.getPartipiciantGroup());
secm.addIdentityToSecurityGroup(id3, g4.getPartipiciantGroup());
// rights
rm.addBGRight(CourseRights.RIGHT_ARCHIVING, g3);
rm.addBGRight(CourseRights.RIGHT_COURSEEDITOR, g3);
rm.addBGRight(CourseRights.RIGHT_ARCHIVING, g4);
rm.addBGRight(CourseRights.RIGHT_GROUPMANAGEMENT, g4);
DBFactory.getInstance().closeSession(); // simulate user clicks
// test groups
CourseGroupManager gm = PersistingCourseGroupManager.getInstance(course1);
assertTrue(gm.isIdentityInLearningGroup(id1, g1.getName()));
assertTrue(gm.isIdentityInLearningGroup(id1, g2.getName()));
assertFalse(gm.isIdentityInLearningGroup(id1, g3.getName())); // not a learning group
assertFalse(gm.isIdentityInLearningGroup(id1, g4.getName())); // not a learning group
assertTrue(gm.isIdentityInLearningGroup(id2, g1.getName()));
assertTrue(gm.isIdentityInLearningGroup(id2, g2.getName()));
assertFalse(gm.isIdentityInLearningGroup(id2, g3.getName())); // not a learning group
assertFalse(gm.isIdentityInLearningGroup(id2, g4.getName())); // not a learning group
DBFactory.getInstance().closeSession();
assertTrue(gm.isIdentityInLearningGroup(id3, g1.getName()));
assertFalse(gm.isIdentityInLearningGroup(id3, g2.getName()));
assertFalse(gm.isIdentityInLearningGroup(id3, g3.getName())); // not a learning group
assertFalse(gm.isIdentityInLearningGroup(id3, g4.getName())); // not a learning group
assertTrue(gm.isIdentityInLearningGroup(id1, g1.getName(), c1.getName()));
assertFalse(gm.isIdentityInLearningGroup(id1, g1.getName(), c2.getName()));
assertTrue(gm.isIdentityInLearningGroup(id3, g1.getName(), c1.getName()));
assertFalse(gm.isIdentityInLearningGroup(id3, g1.getName(), c2.getName()));
// test areas
DBFactory.getInstance().closeSession();
assertTrue(gm.isIdentityInLearningArea(id1, a1.getName()));
assertTrue(gm.isIdentityInLearningArea(id1, a2.getName()));
assertTrue(gm.isIdentityInLearningArea(id1, a3.getName()));
assertTrue(gm.isIdentityInLearningArea(id2, a1.getName()));
assertTrue(gm.isIdentityInLearningArea(id2, a2.getName()));
assertTrue(gm.isIdentityInLearningArea(id2, a3.getName()));
DBFactory.getInstance().closeSession();
assertTrue(gm.isIdentityInLearningArea(id3, a1.getName()));
assertTrue(gm.isIdentityInLearningArea(id3, a2.getName()));
assertFalse(gm.isIdentityInLearningArea(id3, a3.getName()));
DBFactory.getInstance().closeSession();
assertTrue(gm.getLearningAreasOfGroupFromAllContexts(g1.getName()).size() == 2);
assertTrue(gm.getLearningAreasOfGroupFromAllContexts(g2.getName()).size() == 2);
// test rights
DBFactory.getInstance().closeSession();
assertTrue(gm.hasRight(id1, CourseRights.RIGHT_ARCHIVING));
assertTrue(gm.hasRight(id1, CourseRights.RIGHT_COURSEEDITOR));
assertTrue(gm.hasRight(id1, CourseRights.RIGHT_GROUPMANAGEMENT));
assertFalse(gm.hasRight(id1, CourseRights.RIGHT_ASSESSMENT));
assertTrue(gm.hasRight(id1, CourseRights.RIGHT_COURSEEDITOR, c2.getName()));
assertFalse(gm.hasRight(id1, CourseRights.RIGHT_COURSEEDITOR, c1.getName()));
assertFalse(gm.hasRight(id2, CourseRights.RIGHT_COURSEEDITOR));
// test context
DBFactory.getInstance().closeSession();
assertTrue(gm.isIdentityInGroupContext(id1,c1.getName()));
assertTrue(gm.isIdentityInGroupContext(id1,c2.getName()));
assertTrue(gm.isIdentityInGroupContext(id2,c1.getName()));
assertFalse(gm.isIdentityInGroupContext(id2,c2.getName()));
assertTrue(gm.isIdentityInGroupContext(id3,c1.getName()));
assertTrue(gm.isIdentityInGroupContext(id3,c2.getName()));
}