001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sections/tags/sakai_2-4-1/sections-app/src/test/org/sakaiproject/test/section/SectionAwarenessTest.java $
003: * $Id: SectionAwarenessTest.java 18134 2006-11-14 18:59:25Z 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.List;
023: import java.util.Locale;
024:
025: import junit.framework.Assert;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.section.api.CourseManager;
030: import org.sakaiproject.section.api.SectionAwareness;
031: import org.sakaiproject.section.api.SectionManager;
032: import org.sakaiproject.section.api.coursemanagement.Course;
033: import org.sakaiproject.section.api.coursemanagement.CourseSection;
034: import org.sakaiproject.section.api.coursemanagement.ParticipationRecord;
035: import org.sakaiproject.section.api.coursemanagement.User;
036: import org.sakaiproject.section.api.facade.Role;
037: import org.sakaiproject.section.api.facade.manager.Context;
038: import org.sakaiproject.component.section.support.UserManager;
039:
040: /**
041: * Each test method is isolated in its own transaction, which is rolled back when
042: * the method exits. Since we can not assume that data will exist, we need to use
043: * the SectionManager api to insert data before retrieving it with SectionAwareness.
044: *
045: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
046: *
047: */
048: public class SectionAwarenessTest extends SectionsTestBase {
049: private static final Log log = LogFactory
050: .getLog(SectionAwarenessTest.class);
051:
052: private Context context;
053: private SectionManager secMgr;
054: private SectionAwareness secAware;
055: private CourseManager courseMgr;
056: private UserManager userMgr;
057:
058: protected void onSetUpInTransaction() throws Exception {
059: context = (Context) applicationContext
060: .getBean("org.sakaiproject.section.api.facade.manager.Context");
061: secMgr = (SectionManager) applicationContext
062: .getBean("org.sakaiproject.section.api.SectionManager");
063: secAware = (SectionAwareness) applicationContext
064: .getBean("org.sakaiproject.section.api.SectionAwareness");
065: courseMgr = (CourseManager) applicationContext
066: .getBean("org.sakaiproject.section.api.CourseManager");
067: userMgr = (UserManager) applicationContext
068: .getBean("org.sakaiproject.component.section.support.UserManager");
069: }
070:
071: public void testSectionsAndCategories() throws Exception {
072: String siteContext = context.getContext(null);
073: List categories = secAware.getSectionCategories(siteContext);
074:
075: // Add a course and a section to work from
076: courseMgr.createCourse(siteContext, "A course", false, false,
077: false);
078: Course course = secMgr.getCourse(siteContext);
079:
080: String firstCategory = (String) categories.get(0);
081: CourseSection sec = secMgr.addSection(course.getUuid(),
082: "A section", firstCategory, new Integer(10), null,
083: null, null, false, false, false, false, false, false,
084: false);
085:
086: // Assert that the course exists at this context
087: Assert.assertTrue(secMgr.getCourse(siteContext).getUuid()
088: .equals(course.getUuid()));
089:
090: // Assert that section awareness can retrieve the new section
091: List sections = secAware.getSections(siteContext);
092: Assert.assertTrue(sections.size() == 1);
093: Assert.assertTrue(sections.contains(sec));
094:
095: // Assert that section awareness can retrieve the section by its uuid
096: CourseSection secByUuid = secAware.getSection(sec.getUuid());
097: Assert.assertTrue(secByUuid.equals(sec));
098:
099: // Assert that section awareness can retrieve the section by its category
100: List sectionsInCategory = secAware.getSectionsInCategory(
101: siteContext, firstCategory);
102: Assert.assertTrue(sectionsInCategory.contains(sec));
103:
104: // Assert that section awareness can retrieve the category name
105: Assert.assertTrue(secAware.getCategoryName(firstCategory,
106: Locale.US) != null);
107: }
108:
109: public void testSectionSorting() throws Exception {
110: String siteContext = context.getContext(null);
111: List categories = secAware.getSectionCategories(siteContext);
112:
113: // Add a course and a section to work from
114: courseMgr.createCourse(siteContext, "A course", false, false,
115: false);
116: Course course = secMgr.getCourse(siteContext);
117:
118: String firstCategory = (String) categories.get(0);
119: String secondCategory = (String) categories.get(1);
120:
121: CourseSection sec1 = secMgr.addSection(course.getUuid(),
122: "aSection", firstCategory, new Integer(10), null, null,
123: null, false, false, false, false, false, false, false);
124: CourseSection sec2 = secMgr.addSection(course.getUuid(),
125: "bSection", firstCategory, new Integer(10), null, null,
126: null, false, false, false, false, false, false, false);
127:
128: CourseSection sec3 = secMgr.addSection(course.getUuid(),
129: "aaSection", secondCategory, new Integer(10), null,
130: null, null, false, false, false, false, false, false,
131: false);
132: CourseSection sec4 = secMgr.addSection(course.getUuid(),
133: "bbSection", secondCategory, new Integer(10), null,
134: null, null, false, false, false, false, false, false,
135: false);
136:
137: CourseSection sec5 = secMgr.addSection(course.getUuid(),
138: "aaaSection", null, new Integer(10), null, null, null,
139: false, false, false, false, false, false, false);
140: CourseSection sec6 = secMgr.addSection(course.getUuid(),
141: "bbbSection", null, new Integer(10), null, null, null,
142: false, false, false, false, false, false, false);
143:
144: List sections = secAware.getSections(siteContext);
145:
146: // Ensure that sections in the same category sort by title
147: Assert.assertTrue(sections.indexOf(sec1) < sections
148: .indexOf(sec2));
149:
150: // Ensure that secions in different categories sort by category
151: Assert.assertTrue(sections.indexOf(sec1) < sections
152: .indexOf(sec3));
153:
154: // Ensure that sections with null categories sort by title
155: Assert.assertTrue(sections.indexOf(sec5) < sections
156: .indexOf(sec6));
157:
158: // Ensure that sections with null categories sort after a section with a category
159: // This isn't possible in standalone... so do we need to test for it?
160: // Since the sorting is in the DB, we can't be sure where null categories will be sorted (first or last)
161: Assert.assertTrue(sections.indexOf(sec1) < sections
162: .indexOf(sec5));
163: Assert.assertTrue(sections.indexOf(sec1) < sections
164: .indexOf(sec6));
165: }
166:
167: public void testSectionMembership() throws Exception {
168: String siteContext = context.getContext(null);
169: List categories = secAware.getSectionCategories(siteContext);
170:
171: // Add a course and a section to work from
172: courseMgr.createCourse(siteContext, "A course", false, false,
173: false);
174: Course course = secMgr.getCourse(siteContext);
175: String firstCategory = (String) categories.get(0);
176: CourseSection sec = secMgr.addSection(course.getUuid(),
177: "A section", firstCategory, new Integer(10), null,
178: null, null, false, false, false, false, false, false,
179: false);
180:
181: // Load students
182: User student1 = userMgr.createUser("student1", "Joe Student",
183: "Student, Joe", "jstudent");
184: User student2 = userMgr.createUser("student2",
185: "Jane Undergrad", "Undergrad, Jane", "jundergrad");
186:
187: // Load TAs
188: User ta1 = userMgr.createUser("ta1", "Mike Grad", "Grad, Mike",
189: "mgrad");
190: User ta2 = userMgr.createUser("ta2", "Sara Postdoc",
191: "Postdoc, Sara", "spostdoc");
192:
193: // Load instructors
194: User instructor1 = userMgr.createUser("instructor1",
195: "Bill Economist", "Economist, Bill", "beconomist");
196: User instructor2 = userMgr.createUser("instructor2",
197: "Amber Philosopher", "Philosopher, Amber",
198: "aphilosopher");
199:
200: // Load other people
201: User otherPerson = userMgr.createUser("other1", "Other Person",
202: "Person, Other", "operson");
203:
204: // Load enrollments into the course
205: ParticipationRecord siteEnrollment1 = courseMgr.addEnrollment(
206: student1, course);
207: ParticipationRecord siteEnrollment2 = courseMgr.addEnrollment(
208: student2, course);
209:
210: // Load enrollments into sections
211: ParticipationRecord sectionEnrollment1 = secMgr
212: .addSectionMembership("student1", Role.STUDENT, sec
213: .getUuid());
214: ParticipationRecord sectionEnrollment2 = secMgr
215: .addSectionMembership("student2", Role.STUDENT, sec
216: .getUuid());
217:
218: // Load TAs into the course
219: ParticipationRecord siteTaRecord1 = courseMgr
220: .addTA(ta1, course);
221: ParticipationRecord siteTaRecord2 = courseMgr
222: .addTA(ta2, course);
223:
224: // Load TAs into the sections
225: ParticipationRecord sectionTaRecord1 = secMgr
226: .addSectionMembership("ta1", Role.TA, sec.getUuid());
227: ParticipationRecord sectionTaRecord2 = secMgr
228: .addSectionMembership("ta2", Role.TA, sec.getUuid());
229:
230: // Load instructors into the courses
231: ParticipationRecord siteInstructorRecord1 = courseMgr
232: .addInstructor(instructor1, course);
233:
234: // Assert that section awareness can find site members for each role
235: List siteInstructors = secAware.getSiteMembersInRole(
236: siteContext, Role.INSTRUCTOR);
237: Assert.assertTrue(siteInstructors
238: .contains(siteInstructorRecord1));
239:
240: List siteEnrollments = secAware.getSiteMembersInRole(
241: siteContext, Role.STUDENT);
242: Assert.assertTrue(siteEnrollments.contains(siteEnrollment1));
243: Assert.assertTrue(siteEnrollments.contains(siteEnrollment2));
244:
245: List siteTAs = secAware.getSiteMembersInRole(siteContext,
246: Role.TA);
247: Assert.assertTrue(siteTAs.contains(siteTaRecord1));
248: Assert.assertTrue(siteTAs.contains(siteTaRecord2));
249:
250: // Assert that section awareness can find site members matching a string pattern in a role
251: List membersMatchingSearch = secAware.findSiteMembersInRole(
252: siteContext, Role.STUDENT, "student");
253: Assert.assertTrue(membersMatchingSearch
254: .contains(siteEnrollment1));
255: Assert.assertTrue(!membersMatchingSearch
256: .contains(siteEnrollment2));
257:
258: // Assert that section awareness can find section members
259: List allSectionMembers = secAware.getSectionMembers(sec
260: .getUuid());
261: Assert.assertTrue(allSectionMembers
262: .contains(sectionEnrollment1));
263: Assert.assertTrue(allSectionMembers
264: .contains(sectionEnrollment2));
265: Assert.assertTrue(allSectionMembers.contains(sectionTaRecord1));
266: Assert.assertTrue(allSectionMembers.contains(sectionTaRecord2));
267:
268: // Make sure the site records are not returned
269: Assert.assertTrue(!allSectionMembers.contains(siteEnrollment1));
270: Assert.assertTrue(!allSectionMembers.contains(siteEnrollment2));
271: Assert.assertTrue(!allSectionMembers.contains(siteTaRecord1));
272: Assert.assertTrue(!allSectionMembers.contains(siteTaRecord2));
273: Assert.assertTrue(!allSectionMembers
274: .contains(siteInstructorRecord1));
275:
276: // Assert that section awareness can find section members for each role
277: List studentSectionRecords = secAware.getSectionMembersInRole(
278: sec.getUuid(), Role.STUDENT);
279: Assert.assertTrue(studentSectionRecords
280: .contains(sectionEnrollment1));
281: Assert.assertTrue(studentSectionRecords
282: .contains(sectionEnrollment2));
283:
284: List taSectionRecords = secAware.getSectionMembersInRole(sec
285: .getUuid(), Role.TA);
286: Assert.assertTrue(taSectionRecords.contains(sectionTaRecord1));
287: Assert.assertTrue(taSectionRecords.contains(sectionTaRecord2));
288: }
289:
290: public void testGetUnassignedMembers() throws Exception {
291: String siteContext = context.getContext(null);
292: List categories = secAware.getSectionCategories(siteContext);
293:
294: // Add a course and a section to work from
295: Course course = courseMgr.createCourse(siteContext, "A course",
296: false, false, false);
297:
298: String firstCategory = (String) categories.get(0);
299: CourseSection sec = secMgr.addSection(course.getUuid(),
300: "A section", firstCategory, new Integer(10), null,
301: null, null, false, false, false, false, false, false,
302: false);
303:
304: // Load students
305: User student1 = userMgr.createUser("student1", "Joe Student",
306: "Student, Joe", "jstudent");
307: User student2 = userMgr.createUser("student2",
308: "Jane Undergrad", "Undergrad, Jane", "jundergrad");
309: User student3 = userMgr.createUser("student3", "Foo Bar",
310: "Bar, Foo", "fbar");
311:
312: // Load TAs
313: User ta1 = userMgr.createUser("ta1", "Mike Grad", "Grad, Mike",
314: "mgrad");
315: User ta2 = userMgr.createUser("ta2", "Sara Postdoc",
316: "Postdoc, Sara", "spostdoc");
317:
318: // Load enrollments into the course
319: ParticipationRecord siteEnrollment1 = courseMgr.addEnrollment(
320: student1, course);
321: ParticipationRecord siteEnrollment2 = courseMgr.addEnrollment(
322: student2, course);
323: ParticipationRecord siteEnrollment3 = courseMgr.addEnrollment(
324: student3, course);
325:
326: // Load enrollments into sections
327: ParticipationRecord sectionEnrollment1 = secMgr
328: .addSectionMembership("student1", Role.STUDENT, sec
329: .getUuid());
330:
331: // Load TAs into the course
332: ParticipationRecord siteTaRecord1 = courseMgr
333: .addTA(ta1, course);
334: ParticipationRecord siteTaRecord2 = courseMgr
335: .addTA(ta2, course);
336:
337: // Load TAs into the sections
338: ParticipationRecord sectionTaRecord1 = secMgr
339: .addSectionMembership("ta1", Role.TA, sec.getUuid());
340:
341: // Students 2 and 3 should be unsectioned
342: List unassignedStudents = secAware.getUnassignedMembersInRole(
343: siteContext, Role.STUDENT);
344: Assert.assertEquals(unassignedStudents.size(), 2);
345: Assert.assertTrue(!unassignedStudents
346: .contains(sectionEnrollment1));
347:
348: // TA2 should be unsectioned
349: List unassignedTas = secAware.getUnassignedMembersInRole(
350: siteContext, Role.TA);
351: Assert.assertEquals(unassignedTas.size(), 1);
352: Assert.assertTrue(!unassignedTas.contains(sectionTaRecord1));
353: }
354: }
|