001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/course-management/tags/sakai_2-4-1/cm-impl/integration-test/src/test/org/sakaiproject/coursemanagement/impl/CmIntegrationTest.java $
003: * $Id: CmIntegrationTest.java 20528 2007-01-22 18:22:33Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006 The Sakai Foundation.
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.coursemanagement.impl;
021:
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: import junit.extensions.TestSetup;
026: import junit.framework.Assert;
027: import junit.framework.Test;
028: import junit.framework.TestSuite;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.sakaiproject.authz.api.AuthzGroup;
033: import org.sakaiproject.authz.api.AuthzGroupService;
034: import org.sakaiproject.authz.api.Member;
035: import org.sakaiproject.coursemanagement.api.CourseManagementAdministration;
036: import org.sakaiproject.site.api.Site;
037: import org.sakaiproject.site.api.SiteService;
038: import org.sakaiproject.test.SakaiTestBase;
039: import org.sakaiproject.tool.api.Session;
040: import org.sakaiproject.tool.api.SessionManager;
041: import org.sakaiproject.user.api.UserDirectoryService;
042:
043: /**
044: * Integration tests for the CM services. The CmGroupProvider must be registered
045: * with the component manager for these tests to pass.
046: *
047: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
048: *
049: */
050: public class CmIntegrationTest extends SakaiTestBase {
051: private static final Log log = LogFactory
052: .getLog(CmIntegrationTest.class);
053:
054: private AuthzGroupService authzGroupService;
055: private SiteService siteService;
056: private CourseManagementAdministration cmAdmin;
057: private UserDirectoryService uds;
058:
059: private Site site;
060:
061: public static Test suite() {
062: TestSetup setup = new TestSetup(new TestSuite(
063: CmIntegrationTest.class)) {
064: protected void setUp() throws Exception {
065: log.debug("starting setup");
066: oneTimeSetup();
067: log.debug("finished setup");
068: }
069:
070: protected void tearDown() throws Exception {
071: oneTimeTearDown();
072: }
073: };
074: return setup;
075: }
076:
077: public void setUp() throws Exception {
078: log.debug("Setting up a CmIntegrationTest test");
079:
080: // Connect to the required services
081: authzGroupService = (AuthzGroupService) getService(AuthzGroupService.class
082: .getName());
083: siteService = (SiteService) getService(SiteService.class
084: .getName());
085: cmAdmin = (CourseManagementAdministration) getService(CourseManagementAdministration.class
086: .getName());
087: uds = (UserDirectoryService) getService(UserDirectoryService.class
088: .getName());
089:
090: // Log in
091: SessionManager sessionManager = (SessionManager) getService(SessionManager.class
092: .getName());
093: Session session = sessionManager.getCurrentSession();
094: session.setUserId("admin");
095:
096: // Create some Sakai users (who will be added to the site by the CMGroupProvider)
097: uds.addUser("cm-student1", "cm-student1", "cm-student",
098: "CM-one", "cm-student1@foo.bar", "cm-student1", null,
099: null);
100: uds.addUser("cm-student2", "cm-student2", "cm-student",
101: "CM-two", "cm-student2@foo.bar", "cm-student2", null,
102: null);
103: uds.addUser("cm-instructor1", "cm-instructor1",
104: "cm-instructor", "CM-one", "cm-instructor1@foo.bar",
105: "cm-instructor1", null, null);
106: uds.addUser("cm-instructor2", "cm-instructor2",
107: "cm-instructor", "CM-two", "cm-instructor2@foo.bar",
108: "cm-instructor2", null, null);
109:
110: // Create CM objects
111: cmAdmin.createAcademicSession("CM-AS1", "CM-AS1", "CM-AS1",
112: null, null);
113: cmAdmin.createCanonicalCourse("CM-CC1", "CM-CC1", "CM-CC1");
114: cmAdmin.createCourseOffering("CM-CO1", "CM-CO1", "CM-CO1",
115: "open", "CM-AS1", "CM-CC1", null, null);
116: Set instructors = new HashSet();
117: instructors.add("cm-instructor1");
118: cmAdmin.createEnrollmentSet("CM-ES1", "CM-ES1", "CM-ES1",
119: "lecture", "3", "CM-CO1", instructors);
120: cmAdmin.createSection("CM-SEC1", "CM-SEC1", "CM-SEC1",
121: "lecture", null, "CM-CO1", "CM-ES1");
122:
123: // Create some CM memberships, etc
124: cmAdmin.addOrUpdateCourseOfferingMembership("cm-instructor2",
125: "Just helping out with guest lectures", "CM-CO1",
126: "active");
127: cmAdmin.addOrUpdateEnrollment("cm-student1", "CM-ES1",
128: "waitlisted", "4", "letter grade");
129:
130: // Create a site
131: site = siteService.addSite("CM1", "course");
132: site.setTitle("CM Integration Test Site");
133: site.setProviderGroupId("CM-SEC1");
134: siteService.save(site);
135:
136: // This (or something similar) is needed to trigger the authz provider update, I think
137: String siteRef = site.getReference();
138: AuthzGroup azg = authzGroupService.getAuthzGroup(siteRef);
139: // Ensure that the azg has the correct provider id
140: Assert.assertEquals("CM-SEC1", azg.getProviderGroupId());
141: azg.addMember("cm-student2", "Student", true, false);
142: authzGroupService.save(azg);
143:
144: // Now we have all of our users as members of the site: student1 is enrolled,
145: // student2 was manually added, instructor1 is an official instructor, and
146: // instructor2 has a membership in the course offering.
147: }
148:
149: public void tearDown() throws Exception {
150: log.debug("Tearing down an AuthzIntegrationTest test");
151:
152: // Remove the objects created for testing
153: uds.removeUser(uds.editUser("cm-student1"));
154: uds.removeUser(uds.editUser("cm-student2"));
155: uds.removeUser(uds.editUser("cm-instructor1"));
156: uds.removeUser(uds.editUser("cm-instructor2"));
157:
158: cmAdmin.removeSection("CM-SEC1");
159: cmAdmin.removeEnrollmentSet("CM-ES1");
160: cmAdmin.removeCourseOffering("CM-CO1");
161: cmAdmin.removeCanonicalCourse("CM-CC1");
162: cmAdmin.removeAcademicSession("CM-AS1");
163:
164: siteService.removeSite(site);
165: }
166:
167: public void testCmGroupProvider() throws Exception {
168: // Our official instructors, enrollments and CM memberships should be expressed as sakai members
169: Site site = siteService.getSite("CM1");
170:
171: // Of course, our manually added member should be here
172: Member manualMember = site.getMember("cm-student2");
173: Assert.assertNotNull(manualMember);
174: Assert.assertEquals("Student", manualMember.getRole().getId());
175:
176: // Our CM-defined student should be here, too
177: Member student = site.getMember("cm-student1");
178: Assert.assertNotNull(student);
179: Assert.assertEquals("Student", student.getRole().getId());
180:
181: }
182: }
|