001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app/src/test/org/sakaiproject/test/section/SectionManagerTest.java $
003: * $Id: SectionManagerTest.java 20230 2007-01-10 01:13:02Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Regents of the University of California and The Regents of the University of Michigan
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.test.section;
021:
022: import java.util.ArrayList;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Set;
027:
028: import junit.framework.Assert;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.sakaiproject.section.api.CourseManager;
033: import org.sakaiproject.section.api.SectionManager;
034: import org.sakaiproject.section.api.coursemanagement.Course;
035: import org.sakaiproject.section.api.coursemanagement.CourseSection;
036: import org.sakaiproject.section.api.coursemanagement.EnrollmentRecord;
037: import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
038: import org.sakaiproject.section.api.coursemanagement.SectionEnrollments;
039: import org.sakaiproject.section.api.coursemanagement.User;
040: import org.sakaiproject.section.api.exception.MembershipException;
041: import org.sakaiproject.section.api.facade.Role;
042: import org.sakaiproject.section.api.facade.manager.Context;
043: import org.sakaiproject.component.section.facade.impl.standalone.AuthnTestImpl;
044: import org.sakaiproject.component.section.support.UserManager;
045:
046: /**
047: * Each test method is isolated in its own transaction, which is rolled back when
048: * the method exits. Since we can not assume that data will exist, we need to use
049: * the SectionManager api to insert data before retrieving it with SectionAwareness.
050: *
051: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
052: *
053: */
054: public class SectionManagerTest extends SectionsTestBase {
055: private static final Log log = LogFactory
056: .getLog(SectionManagerTest.class);
057:
058: private AuthnTestImpl authn;
059: private Context context;
060: private SectionManager secMgr;
061: private CourseManager courseMgr;
062: private UserManager userMgr;
063:
064: protected void onSetUpInTransaction() throws Exception {
065: authn = (AuthnTestImpl) applicationContext
066: .getBean("org.sakaiproject.section.api.facade.manager.Authn");
067: context = (Context) applicationContext
068: .getBean("org.sakaiproject.section.api.facade.manager.Context");
069: secMgr = (SectionManager) applicationContext
070: .getBean("org.sakaiproject.section.api.SectionManager");
071: courseMgr = (CourseManager) applicationContext
072: .getBean("org.sakaiproject.section.api.CourseManager");
073: userMgr = (UserManager) applicationContext
074: .getBean("org.sakaiproject.component.section.support.UserManager");
075: }
076:
077: public void testChangeMembershipOnDeletedSection() throws Exception {
078: // These methods should gracefully handle operations on missing (possibly deleted) sections
079:
080: // Test joining a non-existent section
081: Assert.assertNull(secMgr.joinSection("foo"));
082:
083: // Test switching into a non-existent section
084: secMgr.switchSection("foo");
085:
086: // Test setSectionMemberships on a non-existent section\
087: Set userSet = new HashSet();
088: userSet.add("user1");
089: secMgr.setSectionMemberships(userSet, Role.STUDENT, "foo");
090: }
091:
092: public void testSectionMembership() throws Exception {
093:
094: // FIXME This test has become totally unruly. Split it up, even though it will lead to a lot of duplication
095:
096: String siteContext = context.getContext(null);
097: List categories = secMgr.getSectionCategories(siteContext);
098:
099: // Add a course and a section to work from
100: Course newCourse = courseMgr.createCourse(siteContext,
101: "A course", false, false, false);
102: Course course = secMgr.getCourse(siteContext);
103:
104: // Assert that the correct course was retrieved
105: Assert.assertTrue(newCourse.equals(course));
106:
107: String firstCategory = (String) categories.get(0);
108: String secondCategory = (String) categories.get(1);
109: String thirdCategory = (String) categories.get(2);
110: CourseSection sec1 = secMgr.addSection(course.getUuid(),
111: "A section", firstCategory, new Integer(10), null,
112: null, null, false, false, false, false, false, false,
113: false);
114: CourseSection sec2 = secMgr.addSection(course.getUuid(),
115: "Another section", firstCategory, new Integer(10),
116: null, null, null, false, false, false, false, false,
117: false, false);
118: CourseSection sec3 = secMgr.addSection(course.getUuid(),
119: "A different kind of section", secondCategory,
120: new Integer(10), null, null, null, false, false, false,
121: false, false, false, false);
122: CourseSection sec4 = secMgr.addSection(course.getUuid(),
123: "Barely even a section", thirdCategory,
124: new Integer(10), null, null, null, false, false, false,
125: false, false, false, false);
126:
127: // Load students
128: User student1 = userMgr.createUser("student1", "Joe Student",
129: "Student, Joe", "jstudent");
130: User student2 = userMgr.createUser("student2",
131: "Jane Undergrad", "Undergrad, Jane", "jundergrad");
132:
133: // Load TAs
134: User ta1 = userMgr.createUser("ta1", "Mike Grad", "Grad, Mike",
135: "mgrad");
136: User ta2 = userMgr.createUser("ta2", "Sara Postdoc",
137: "Postdoc, Sara", "spostdoc");
138:
139: // Load instructors
140: User instructor1 = userMgr.createUser("instructor1",
141: "Bill Economist", "Economist, Bill", "beconomist");
142: User instructor2 = userMgr.createUser("instructor2",
143: "Amber Philosopher", "Philosopher, Amber",
144: "aphilosopher");
145:
146: // Load other people
147: User otherPerson = userMgr.createUser("other1", "Other Person",
148: "Person, Other", "operson");
149:
150: // Load enrollments into the course
151: ParticipationRecord siteEnrollment1 = courseMgr.addEnrollment(
152: student1, course);
153: ParticipationRecord siteEnrollment2 = courseMgr.addEnrollment(
154: student2, course);
155: ParticipationRecord siteEnrollment3 = courseMgr.addEnrollment(
156: otherPerson, course);
157:
158: // Load enrollments into sections
159: ParticipationRecord sectionEnrollment1 = secMgr
160: .addSectionMembership("student1", Role.STUDENT, sec1
161: .getUuid());
162: ParticipationRecord sectionEnrollment2 = secMgr
163: .addSectionMembership("student2", Role.STUDENT, sec1
164: .getUuid());
165:
166: // Load TAs into the course
167: ParticipationRecord siteTaRecord1 = courseMgr
168: .addTA(ta1, course);
169: ParticipationRecord siteTaRecord2 = courseMgr
170: .addTA(ta2, course);
171:
172: // Load TAs into the sections
173: ParticipationRecord sectionTaRecord1 = secMgr
174: .addSectionMembership("ta1", Role.TA, sec1.getUuid());
175: ParticipationRecord sectionTaRecord2 = secMgr
176: .addSectionMembership("ta2", Role.TA, sec1.getUuid());
177:
178: // Load instructors into the courses
179: ParticipationRecord siteInstructorRecord1 = courseMgr
180: .addInstructor(instructor1, course);
181:
182: // Assert that an student who joins a section is returned as a member of that section
183: authn.setUserUuid("other1");
184: EnrollmentRecord sectionEnrollment3 = secMgr.joinSection(sec1
185: .getUuid());
186:
187: List enrollments = secMgr.getSectionEnrollments(sec1.getUuid());
188: Assert.assertTrue(enrollments.contains(sectionEnrollment3));
189:
190: // Assert that an enrolled student can not add themselves again
191: authn.setUserUuid("student1");
192: boolean joinSectionErrorThrown = false;
193: try {
194: secMgr.joinSection(sec1.getUuid());
195: } catch (MembershipException me) {
196: joinSectionErrorThrown = true;
197: }
198: Assert.assertTrue(joinSectionErrorThrown);
199:
200: // Assert that a student can switch between sections only within the same category
201: secMgr.switchSection(sec2.getUuid());
202: boolean switchingErrorThrown = false;
203: try {
204: secMgr.switchSection(sec3.getUuid());
205: } catch (MembershipException me) {
206: switchingErrorThrown = true;
207: }
208: Assert.assertTrue(switchingErrorThrown);
209:
210: // Add otherPerson to the section in the third category. This is the only enrollment in this section or category.
211: secMgr.addSectionMembership(otherPerson.getUserUid(),
212: Role.STUDENT, sec4.getUuid());
213:
214: // Assert that the third category's unsectioned students returns the two students
215: List unsectionedEnrollments = secMgr.getUnsectionedEnrollments(
216: course.getUuid(), thirdCategory);
217: List unsectionedStudents = new ArrayList();
218: for (Iterator iter = unsectionedEnrollments.iterator(); iter
219: .hasNext();) {
220: unsectionedStudents.add(((ParticipationRecord) iter.next())
221: .getUser());
222: }
223:
224: Assert.assertTrue(unsectionedStudents.contains(student1));
225: Assert.assertTrue(unsectionedStudents.contains(student2));
226: Assert.assertTrue(!unsectionedStudents.contains(otherPerson));
227:
228: // Assert that an instructor can not be added to a section
229: boolean instructorErrorThrown = false;
230: try {
231: secMgr.addSectionMembership(instructor1.getUserUid(),
232: Role.INSTRUCTOR, sec1.getUuid());
233: } catch (MembershipException me) {
234: instructorErrorThrown = true;
235: }
236: Assert.assertTrue(instructorErrorThrown);
237:
238: // Assert that setting the entire membership of a section is successful
239: Set set = new HashSet();
240: set.add(student1.getUserUid());
241: set.add(student2.getUserUid());
242: secMgr.setSectionMemberships(set, Role.STUDENT, sec4.getUuid());
243: List sectionMemberships = secMgr.getSectionEnrollments(sec4
244: .getUuid());
245: Set sectionMembers = new HashSet();
246: for (Iterator iter = sectionMemberships.iterator(); iter
247: .hasNext();) {
248: sectionMembers.add(((ParticipationRecord) iter.next())
249: .getUser());
250: }
251: Assert.assertTrue(sectionMembers.contains(student1));
252: Assert.assertTrue(sectionMembers.contains(student2));
253: // otherPerson was originally in the section, but wasn't included in the set operation
254: Assert.assertTrue(!sectionMembers.contains(otherPerson));
255:
256: // Drop a student from a section and ensure the enrollments reflect the drop
257: secMgr.dropSectionMembership(student1.getUserUid(), sec2
258: .getUuid());
259: List sec2Members = secMgr.getSectionEnrollments(sec2.getUuid());
260: Assert.assertTrue(!sec2Members.contains(sectionEnrollment1));
261:
262: // Check whether the total enrollments in the course and in the sections is accurate
263: Assert
264: .assertTrue(secMgr
265: .getTotalEnrollments(course.getUuid()) == 3);
266: Assert
267: .assertTrue(secMgr.getTotalEnrollments(sec1.getUuid()) == 2);
268:
269: // Ensure that a section can be updated
270: secMgr.updateSection(sec1.getUuid(), "New title", new Integer(
271: 10), null, null, null, false, false, false, false,
272: false, false, false);
273: CourseSection updatedSec = secMgr.getSection(sec1.getUuid());
274: Assert.assertTrue(updatedSec.getTitle().equals("New title"));
275: sec1 = updatedSec;
276:
277: // Ensure that disbanding a section actually removes it from the course
278: secMgr.disbandSection(sec4.getUuid());
279: Assert.assertTrue(!secMgr.getSections(siteContext).contains(
280: sec4));
281:
282: // Assert that the correct enrollment records are returned for a student in a course
283: ParticipationRecord enrollment1 = secMgr.addSectionMembership(
284: student1.getUserUid(), Role.STUDENT, sec1.getUuid());
285: ParticipationRecord enrollment2 = secMgr.addSectionMembership(
286: student1.getUserUid(), Role.STUDENT, sec3.getUuid());
287: Set myEnrollments = secMgr.getSectionEnrollments(student1
288: .getUserUid(), course.getUuid());
289:
290: Assert.assertTrue(myEnrollments.contains(enrollment1));
291: Assert.assertTrue(myEnrollments.contains(enrollment2));
292: Assert.assertTrue(myEnrollments.size() == 2);
293:
294: // Assert that a valid SectionEnrollments object (just a convenient data structure) can be obtained
295: Set studentUids = new HashSet();
296: studentUids.add("student1");
297: studentUids.add("student2");
298: studentUids.add("other1");
299: SectionEnrollments secEnrollments = secMgr
300: .getSectionEnrollmentsForStudents(siteContext,
301: studentUids);
302:
303: // Student 1 is enrolled in section 1 and 3
304: Assert.assertTrue(secEnrollments.getSection("student1",
305: firstCategory).equals(sec1));
306: Assert.assertTrue(secEnrollments.getSection("student1",
307: secondCategory).equals(sec3));
308:
309: // Student 2 is enrolled in section 1
310: Assert.assertTrue(secEnrollments.getSection("student2",
311: firstCategory).equals(sec1));
312: Assert.assertTrue(secEnrollments.getSection("student2",
313: secondCategory) == null);
314:
315: // Other person is enrolled in section 1
316: Assert.assertTrue(secEnrollments.getSection("other1",
317: firstCategory).equals(sec1));
318: Assert.assertTrue(secEnrollments.getSection("other1",
319: secondCategory) == null);
320:
321: // Remove an enrollment from a category
322: secMgr.dropEnrollmentFromCategory("student1", siteContext,
323: firstCategory);
324:
325: // Assert that the student is no longer in sec1
326: Assert.assertTrue(!secMgr.getSectionEnrollments("student1",
327: course.getUuid()).contains(sec1));
328:
329: // Remove the enrollment from the same category, which should do nothing,
330: // just to ensure that no error is thrown
331: secMgr.dropEnrollmentFromCategory("student1", siteContext,
332: firstCategory);
333:
334: // Change the self reg and self switching flags
335: secMgr.setJoinOptions(course.getUuid(), true, false);
336: Assert.assertTrue(secMgr.isSelfRegistrationAllowed(course
337: .getUuid()));
338: Assert.assertFalse(secMgr.isSelfSwitchingAllowed(course
339: .getUuid()));
340:
341: secMgr.setJoinOptions(course.getUuid(), false, true);
342: Assert.assertFalse(secMgr.isSelfRegistrationAllowed(course
343: .getUuid()));
344: Assert.assertTrue(secMgr.isSelfSwitchingAllowed(course
345: .getUuid()));
346: }
347:
348: }
|