001: /**********************************************************************************
002: *
003: * $Id: GradebookServiceTest.java 20348 2007-01-16 23:03:28Z ray@media.berkeley.edu $
004: *
005: ***********************************************************************************
006: *
007: * Copyright (c) 2005 The Regents of the University of California, The MIT Corporation
008: *
009: * Licensed under the Educational Community License, Version 1.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.opensource.org/licenses/ecl1.php
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: *
021: **********************************************************************************/package org.sakaiproject.tool.gradebook.test;
022:
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.Date;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: import junit.framework.Assert;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.sakaiproject.service.gradebook.shared.AssignmentHasIllegalPointsException;
036: import org.sakaiproject.service.gradebook.shared.ConflictingAssignmentNameException;
037: import org.sakaiproject.service.gradebook.shared.ConflictingExternalIdException;
038: import org.sakaiproject.tool.gradebook.Assignment;
039: import org.sakaiproject.tool.gradebook.AssignmentGradeRecord;
040: import org.sakaiproject.tool.gradebook.CourseGradeRecord;
041: import org.sakaiproject.tool.gradebook.Gradebook;
042:
043: /**
044: * Uses spring's mock-objects to test the gradebook service without modifying the database
045: *
046: * @author <a href="mailto:jholtzman@berkeley.edu">Josh Holtzman</a>
047: */
048: public class GradebookServiceTest extends GradebookTestBase {
049:
050: private static final Log log = LogFactory
051: .getLog(GradebookServiceTest.class);
052:
053: private static final String GRADEBOOK_UID = "gradebookServiceTest";
054: private static final String ASN_1 = "Assignment #1";
055: private static final String EXT_ID_1 = "External #1";
056: private static final String EXT_TITLE_1 = "External Title #1";
057:
058: private Long asn_1Id;
059:
060: /**
061: * @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpInTransaction()
062: */
063: protected void onSetUpInTransaction() throws Exception {
064: super .onSetUpInTransaction();
065:
066: gradebookFrameworkService.addGradebook(GRADEBOOK_UID,
067: GRADEBOOK_UID);
068: Gradebook gradebook = gradebookManager
069: .getGradebook(GRADEBOOK_UID);
070:
071: // Set up a holder for enrollments, teaching assignments, and sections.
072: integrationSupport.createCourse(GRADEBOOK_UID, GRADEBOOK_UID,
073: false, false, false);
074:
075: List studentUidsList = Arrays.asList(new String[] { "student1",
076: "student2", "student3", });
077: addUsersEnrollments(gradebook, studentUidsList);
078:
079: // Add an internal assignment
080: Long gbId = gradebook.getId();
081: asn_1Id = gradebookManager.createAssignment(gbId, ASN_1,
082: new Double(10), null, Boolean.FALSE, Boolean.FALSE);
083:
084: // Add a score for the internal assignment
085: List assignments = gradebookManager.getAssignments(gbId);
086: Assignment asn = null;
087: for (Iterator iter = assignments.iterator(); iter.hasNext();) {
088: Assignment tmp = (Assignment) iter.next();
089: if (tmp.getName().equals(ASN_1)) {
090: asn = tmp;
091: break;
092: }
093: }
094: List gradeRecords = new ArrayList();
095: gradeRecords.add(new AssignmentGradeRecord(asn, "student1",
096: new Double(10)));
097: gradebookManager
098: .updateAssignmentGradeRecords(asn, gradeRecords);
099: }
100:
101: /**
102: * Tests the gradebook service.
103: *
104: * @throws Exception
105: */
106: public void testCreateExternalAssessment() throws Exception {
107: Assert.assertTrue(gradebookFrameworkService
108: .isGradebookDefined(GRADEBOOK_UID));
109:
110: // Make sure the service knows that the external id has not been defined
111: Assert.assertFalse(gradebookExternalAssessmentService
112: .isExternalAssignmentDefined(GRADEBOOK_UID, EXT_ID_1));
113:
114: gradebookExternalAssessmentService.addExternalAssessment(
115: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 10,
116: new Date(), "Samigo");
117:
118: // Make sure the service knows that the external id has been defined
119: Assert.assertTrue(gradebookExternalAssessmentService
120: .isExternalAssignmentDefined(GRADEBOOK_UID, EXT_ID_1));
121:
122: // Make sure that internal name conflicts are detected
123: try {
124: gradebookExternalAssessmentService.addExternalAssessment(
125: GRADEBOOK_UID, "A unique external id", null, ASN_1,
126: 10, new Date(), "Samigo");
127: fail();
128: } catch (ConflictingAssignmentNameException e) {
129: }
130:
131: // Make sure that external name conflicts are detected
132: try {
133: gradebookExternalAssessmentService.addExternalAssessment(
134: GRADEBOOK_UID, "Another unique external id", null,
135: EXT_TITLE_1, 10, new Date(), "Samigo");
136: fail();
137: } catch (ConflictingAssignmentNameException e) {
138: }
139:
140: // Make sure that external id conflicts are detected
141: try {
142: gradebookExternalAssessmentService.addExternalAssessment(
143: GRADEBOOK_UID, EXT_ID_1, null, "A unique title",
144: 10, new Date(), "Samigo");
145: fail();
146: } catch (ConflictingExternalIdException e) {
147: }
148:
149: // Test a floating value.
150: double floatingPoints = 10.66666;
151: String floatingExtId = "Just another external ID";
152: gradebookExternalAssessmentService.addExternalAssessment(
153: GRADEBOOK_UID, floatingExtId, null,
154: "AFractionalAssessment", floatingPoints, new Date(),
155: "Samigo");
156:
157: // Find the assessment and ensure that it has been updated
158: Long gbId = gradebookManager.getGradebook(GRADEBOOK_UID)
159: .getId();
160: Assignment asn = null;
161: List assignments = gradebookManager.getAssignments(gbId);
162: for (Iterator iter = assignments.iterator(); iter.hasNext();) {
163: Assignment tmp = (Assignment) iter.next();
164: if (tmp.getExternalId() != null
165: && tmp.getExternalId().equals(floatingExtId)) {
166: asn = tmp;
167: break;
168: }
169: }
170: Assert.assertEquals(asn.getPointsPossible(), new Double(
171: floatingPoints));
172: }
173:
174: public void testModifyExternalAssessment() throws Exception {
175: Assert.assertTrue(gradebookFrameworkService
176: .isGradebookDefined(GRADEBOOK_UID));
177: gradebookExternalAssessmentService.addExternalAssessment(
178: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 10,
179: new Date(), "Samigo");
180: gradebookExternalAssessmentService.updateExternalAssessment(
181: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 20, null);
182:
183: // Find the assessment and ensure that it has been updated
184: Long gbId = gradebookManager.getGradebook(GRADEBOOK_UID)
185: .getId();
186: Assignment asn = null;
187: List assignments = gradebookManager.getAssignments(gbId);
188: for (Iterator iter = assignments.iterator(); iter.hasNext();) {
189: Assignment tmp = (Assignment) iter.next();
190: if (tmp.getExternalId() != null
191: && tmp.getExternalId().equals(EXT_ID_1)) {
192: asn = tmp;
193: break;
194: }
195: }
196: Assert.assertEquals(asn.getPointsPossible(), new Double(20));
197:
198: // Ensure that the total points possible in the gradebook reflects the updated assessment's points
199: Assert.assertTrue(gradebookManager.getTotalPoints(gbId) == 30);
200: }
201:
202: public void testCreateExternalGradeRecords() throws Exception {
203:
204: // Add an external assessment
205: gradebookExternalAssessmentService.addExternalAssessment(
206: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 10,
207: new Date(), "Samigo");
208:
209: // Add the external assessment score
210: Gradebook gb = gradebookManager.getGradebook(GRADEBOOK_UID);
211: gradebookExternalAssessmentService
212: .updateExternalAssessmentScore(gb.getUid(), EXT_ID_1,
213: "student1", new Double(5));
214:
215: // Ensure that the course grade record for student1 has been updated
216: CourseGradeRecord cgr = gradebookManager
217: .getStudentCourseGradeRecord(gb, "student1");
218: Assert.assertTrue(cgr.getPointsEarned().equals(new Double(15))); // 10 points on internal, 5 points on external
219: }
220:
221: public void testModifyExternalGradeRecords() throws Exception {
222: // Add an external assessment
223: gradebookExternalAssessmentService.addExternalAssessment(
224: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 10,
225: new Date(), "Samigo");
226:
227: // Add the external assessment score
228: Gradebook gb = gradebookManager.getGradebook(GRADEBOOK_UID);
229: gradebookExternalAssessmentService
230: .updateExternalAssessmentScore(gb.getUid(), EXT_ID_1,
231: "student1", new Double(2));
232:
233: // Ensure that the course grade record for student1 has been updated
234: CourseGradeRecord cgr = gradebookManager
235: .getStudentCourseGradeRecord(gb, "student1");
236: Assert.assertTrue(cgr.getPointsEarned().equals(new Double(12))); // 10 points on internal, 2 points on external
237:
238: // Update the score with null points
239: gradebookExternalAssessmentService
240: .updateExternalAssessmentScore(gb.getUid(), EXT_ID_1,
241: "student1", null);
242:
243: // Ensure that the course grade record for student1 has been updated
244: cgr = gradebookManager.getStudentCourseGradeRecord(gb,
245: "student1");
246: Assert.assertEquals(new Double(10), cgr.getPointsEarned()); // 10 points on internal, 0 points on external
247: }
248:
249: public void testUpdateMultipleScores() throws Exception {
250: // Add an external assessment
251: gradebookExternalAssessmentService.addExternalAssessment(
252: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 10,
253: new Date(), "Samigo");
254:
255: // Add the external assessment score
256: Gradebook gb = gradebookManager.getGradebook(GRADEBOOK_UID);
257: gradebookExternalAssessmentService
258: .updateExternalAssessmentScore(gb.getUid(), EXT_ID_1,
259: "student1", new Double(2));
260:
261: // Ensure that the course grade record for student1 has been updated
262: CourseGradeRecord cgr = gradebookManager
263: .getStudentCourseGradeRecord(gb, "student1");
264: Assert.assertTrue(cgr.getPointsEarned().equals(new Double(12))); // 10 points on internal, 2 points on external
265:
266: // Update multiple scores at once.
267: Map studentUidsToScores = new HashMap();
268: studentUidsToScores.put("student1", null);
269: studentUidsToScores.put("student2", new Double(4));
270: studentUidsToScores.put("student3", new Double(5));
271: gradebookExternalAssessmentService
272: .updateExternalAssessmentScores(gb.getUid(), EXT_ID_1,
273: studentUidsToScores);
274: cgr = gradebookManager.getStudentCourseGradeRecord(gb,
275: "student1");
276: Assert.assertTrue(cgr.getPointsEarned().equals(new Double(10)));
277: cgr = gradebookManager.getStudentCourseGradeRecord(gb,
278: "student2");
279: Assert.assertTrue(cgr.getPointsEarned().equals(new Double(4)));
280:
281: // Do a bogus update of a null collection of scores, a la Assignments.
282: gradebookExternalAssessmentService
283: .updateExternalAssessmentScores(gb.getUid(), EXT_ID_1,
284: new HashMap());
285: cgr = gradebookManager.getStudentCourseGradeRecord(gb,
286: "student2");
287: Assert.assertTrue(cgr.getPointsEarned().equals(new Double(4)));
288: }
289:
290: public void testRemoveExternalAssignment() throws Exception {
291: Gradebook gb = gradebookManager.getGradebook(GRADEBOOK_UID);
292:
293: // Grade a second student on the internal assignment to test
294: // course grade calculations later.
295: Assignment asn = gradebookManager
296: .getAssignmentWithStats(asn_1Id);
297: List gradeRecords = new ArrayList();
298: gradeRecords.add(new AssignmentGradeRecord(asn, "student2",
299: new Double(10)));
300: gradebookManager
301: .updateAssignmentGradeRecords(asn, gradeRecords);
302:
303: // Add an external assessment
304: gradebookExternalAssessmentService.addExternalAssessment(
305: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 10,
306: new Date(), "Samigo");
307:
308: // Add the external assessment score
309: gradebookExternalAssessmentService
310: .updateExternalAssessmentScore(GRADEBOOK_UID, EXT_ID_1,
311: "student1", new Double(5));
312: CourseGradeRecord cgr = gradebookManager
313: .getStudentCourseGradeRecord(gb, "student1");
314: Assert.assertTrue(cgr.getPointsEarned().equals(new Double(15)));// 10 points on internal, 10 points on external
315:
316: // Check the display grade (which is what the students would see).
317: if (log.isDebugEnabled())
318: log.debug("student1 cgr displayGrade="
319: + cgr.getDisplayGrade());
320: Assert.assertTrue(cgr.getDisplayGrade().equals("C"));
321: cgr = gradebookManager.getStudentCourseGradeRecord(gb,
322: "student2");
323: if (log.isDebugEnabled())
324: log.debug("student2 cgr displayGrade="
325: + cgr.getDisplayGrade());
326: Assert.assertTrue(cgr.getDisplayGrade().equals("F"));
327:
328: // Remove the external assessment
329: gradebookExternalAssessmentService.removeExternalAssessment(
330: GRADEBOOK_UID, EXT_ID_1);
331:
332: // Ensure that the course grade record for student1 has been updated
333: cgr = gradebookManager.getStudentCourseGradeRecord(gb,
334: "student1");
335: Assert.assertTrue(cgr.getPointsEarned().equals(new Double(10)));// 10 points on internal, 0 points on external
336:
337: // Check the display grade (which is what the students would see).
338: if (log.isDebugEnabled())
339: log.debug("student1 cgr displayGrade="
340: + cgr.getDisplayGrade());
341: Assert.assertTrue(cgr.getDisplayGrade().equals("A+"));
342: cgr = gradebookManager.getStudentCourseGradeRecord(gb,
343: "student2");
344: if (log.isDebugEnabled())
345: log.debug("student2 cgr displayGrade="
346: + cgr.getDisplayGrade());
347: Assert.assertTrue(cgr.getDisplayGrade().equals("A+"));
348:
349: // Try to add another external assessment with the same external ID as the recently deleted external assessment
350: gradebookExternalAssessmentService.addExternalAssessment(
351: GRADEBOOK_UID, EXT_ID_1, null,
352: "some other unique title", 10, new Date(), "Samigo");
353: }
354:
355: public void testDeleteGradebook() throws Exception {
356: gradebookFrameworkService.deleteGradebook(GRADEBOOK_UID);
357: Assert.assertFalse(gradebookFrameworkService
358: .isGradebookDefined(GRADEBOOK_UID));
359: }
360:
361: public void testIsAssignmentDefined() throws Exception {
362: String assignmentTitle = "Is Assignment Defined Quiz";
363: Assert.assertFalse(gradebookExternalAssessmentService
364: .isAssignmentDefined(GRADEBOOK_UID, assignmentTitle));
365: gradebookExternalAssessmentService.addExternalAssessment(
366: GRADEBOOK_UID, "Is Assignment Defined ID", null,
367: assignmentTitle, 10, new Date(), "Assignments");
368: Assert.assertTrue(gradebookExternalAssessmentService
369: .isAssignmentDefined(GRADEBOOK_UID, assignmentTitle));
370:
371: // Now test conflicts with an internally defined assignment.
372: Assert.assertTrue(gradebookExternalAssessmentService
373: .isAssignmentDefined(GRADEBOOK_UID, ASN_1));
374: gradebookManager.removeAssignment(asn_1Id);
375: Assert.assertFalse(gradebookExternalAssessmentService
376: .isAssignmentDefined(GRADEBOOK_UID, ASN_1));
377: }
378:
379: public void testExternalAssignmentWithZeroPoints() throws Exception {
380: //add assignment to grade book
381: try {
382: gradebookExternalAssessmentService.addExternalAssessment(
383: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 0,
384: new Date(), "Samigo");
385: fail();
386: } catch (AssignmentHasIllegalPointsException e) {
387: }
388:
389: gradebookExternalAssessmentService.addExternalAssessment(
390: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 10,
391: new Date(), "Samigo");
392: try {
393: gradebookExternalAssessmentService
394: .updateExternalAssessment(GRADEBOOK_UID, EXT_ID_1,
395: null, EXT_TITLE_1, 0, null);
396: fail();
397: } catch (AssignmentHasIllegalPointsException e) {
398: }
399: }
400:
401: public void testDuplicateExternalIds() throws Exception {
402: // Add an external assessment
403: gradebookExternalAssessmentService.addExternalAssessment(
404: GRADEBOOK_UID, EXT_ID_1, null, EXT_TITLE_1, 10,
405: new Date(), "Samigo");
406:
407: // Try to add another external assessment with a duplicate external ID
408: try {
409: gradebookExternalAssessmentService.addExternalAssessment(
410: GRADEBOOK_UID, EXT_ID_1, null, "some unique title",
411: 10, new Date(), "Samigo");
412: fail();
413: } catch (ConflictingExternalIdException e) {
414: }
415:
416: }
417: }
|