View Javadoc

1   package org.kuali.student.enrollment.class2.courseoffering.service.impl;
2   
3   import org.apache.log4j.Logger;
4   import org.junit.After;
5   import org.junit.Before;
6   import org.junit.Test;
7   import org.junit.runner.RunWith;
8   import org.kuali.rice.core.api.criteria.PredicateFactory;
9   import org.kuali.rice.core.api.criteria.QueryByCriteria;
10  import org.kuali.student.common.test.util.AttributeTester;
11  import org.kuali.student.common.test.util.ListOfStringTester;
12  import org.kuali.student.common.test.util.MetaTester;
13  import org.kuali.student.common.test.util.RichTextTester;
14  import org.kuali.student.enrollment.class2.courseoffering.service.helper.CourseOfferingServiceScheduleHelper;
15  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingClusterInfo;
16  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingInfo;
17  import org.kuali.student.enrollment.courseoffering.dto.ActivityOfferingSetInfo;
18  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingCrossListingInfo;
19  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
20  import org.kuali.student.enrollment.courseoffering.dto.FormatOfferingInfo;
21  import org.kuali.student.enrollment.courseoffering.dto.OfferingInstructorInfo;
22  import org.kuali.student.enrollment.courseoffering.dto.RegistrationGroupInfo;
23  import org.kuali.student.enrollment.courseoffering.dto.SeatPoolDefinitionInfo;
24  import org.kuali.student.enrollment.courseoffering.infc.ActivityOffering;
25  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
26  import org.kuali.student.lum.lrc.service.util.MockLrcTestDataLoader;
27  import org.kuali.student.r2.common.dto.AttributeInfo;
28  import org.kuali.student.r2.common.dto.ContextInfo;
29  import org.kuali.student.r2.common.dto.DtoConstants;
30  import org.kuali.student.r2.common.dto.MetaInfo;
31  import org.kuali.student.r2.common.dto.RichTextInfo;
32  import org.kuali.student.r2.common.dto.StatusInfo;
33  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
34  import org.kuali.student.r2.common.util.constants.CourseOfferingServiceConstants;
35  import org.kuali.student.r2.common.util.constants.LuServiceConstants;
36  import org.kuali.student.r2.common.util.constants.LuiServiceConstants;
37  import org.kuali.student.r2.common.util.date.DateFormatters;
38  import org.kuali.student.r2.core.acal.dto.TermInfo;
39  import org.kuali.student.r2.core.class1.state.service.StateService;
40  import org.kuali.student.r2.core.scheduling.service.SchedulingService;
41  import org.kuali.student.r2.lum.course.dto.ActivityInfo;
42  import org.kuali.student.r2.lum.course.dto.CourseInfo;
43  import org.kuali.student.r2.lum.course.dto.FormatInfo;
44  import org.kuali.student.r2.lum.course.service.CourseService;
45  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
46  import org.kuali.student.r2.lum.lrc.service.LRCService;
47  import org.kuali.student.r2.lum.util.constants.LrcServiceConstants;
48  import org.springframework.test.context.ContextConfiguration;
49  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
50  import org.springframework.test.context.transaction.TransactionConfiguration;
51  import org.springframework.transaction.annotation.Transactional;
52  
53  import javax.annotation.Resource;
54  
55  import java.util.ArrayList;
56  import java.util.Arrays;
57  import java.util.Collections;
58  import java.util.Comparator;
59  import java.util.Date;
60  import java.util.List;
61  
62  import static org.junit.Assert.assertEquals;
63  import static org.junit.Assert.assertFalse;
64  import static org.junit.Assert.assertNotNull;
65  import static org.junit.Assert.assertTrue;
66  import static org.junit.Assert.fail;
67  
68  /*
69   * This class was used to test the class1 backed implementation of CourseOfferingService for CourseOffering, FormatOffering and ActivityOffering.
70   * 
71   * For M4 it has been refactored.  Most of the test are now in TestCourseOfferingServiceMockImpl and only db dependent tests go here.
72   * 
73   * See TestLprServiceImpl for an example.
74   *
75   */
76  @RunWith(SpringJUnit4ClassRunner.class)
77  @ContextConfiguration(locations = {"classpath:co-test-context.xml"})
78  @TransactionConfiguration(transactionManager = "JtaTxManager", defaultRollback = true)
79  @Transactional
80  public class TestCourseOfferingServiceImpl {
81      private static final Logger log = Logger
82              .getLogger(TestCourseOfferingServiceImpl.class);
83  
84      @Resource
85      protected CourseOfferingService coService;
86  
87      @Resource
88      protected CourseService courseService;
89  
90      @Resource
91      protected StateService stateService;
92  
93      @Resource
94      protected CourseOfferingServiceTestDataLoader dataLoader;
95  
96      @Resource(name = "LrcService")
97      protected LRCService lrcService;
98  
99      @Resource
100     protected SchedulingService schedulingService;
101     
102     public static String principalId = "123";
103     public ContextInfo callContext = null;
104 
105     private final List<CourseOfferingCrossListingInfo> crossListings = new ArrayList<CourseOfferingCrossListingInfo>();
106 
107     @Before
108     public void setup() throws Exception {
109         callContext = new ContextInfo();
110         callContext.setPrincipalId(principalId);
111         CourseOfferingServiceScheduleHelper.setSchedulingService(schedulingService);
112     }
113 
114     @After
115     public void teardown() throws Exception {
116         dataLoader.afterTest();
117     }
118 
119     private void createCourseCHEM123(TermInfo term, ContextInfo context) throws Exception {
120 
121         CourseInfo canonicalCourse = buildCanonicalCourse("CLU-1", term.getId(), "CHEM", "CHEM123", "Chemistry 123", "description 1");
122 
123         FormatInfo canonicalLectureOnlyFormat = buildCanonicalFormat("CHEM123:LEC-ONLY", canonicalCourse);
124 
125         buildCanonicalActivity(LuServiceConstants.COURSE_ACTIVITY_LECTURE_TYPE_KEY, canonicalLectureOnlyFormat);
126 
127         FormatInfo canonicalLectureAndLabFormat = buildCanonicalFormat("CHEM123:LEC-AND-LAB", canonicalCourse);
128 
129         buildCanonicalActivity(LuServiceConstants.COURSE_ACTIVITY_LECTURE_TYPE_KEY, canonicalLectureAndLabFormat);
130         buildCanonicalActivity(LuServiceConstants.COURSE_ACTIVITY_LAB_TYPE_KEY, canonicalLectureAndLabFormat);
131 
132         courseService.createCourse(canonicalCourse, context);
133     }
134 
135     private CourseInfo buildCanonicalCourse(String id, String startTermId, String subjectArea, String code, String title, String description) {
136         CourseInfo info = new CourseInfo();
137         info.setStartTerm(startTermId);
138         info.setEffectiveDate(calcEffectiveDateForTerm(startTermId, id));
139         info.setId(id);
140         info.setSubjectArea(subjectArea);
141         info.setCode(subjectArea);
142         info.setCourseNumberSuffix(code.substring(subjectArea.length()));
143         info.setCourseTitle(title);
144         RichTextInfo rt = new RichTextInfo();
145         rt.setPlain(description);
146         info.setDescr(rt);
147         info.setTypeKey(LuServiceConstants.CREDIT_COURSE_LU_TYPE_KEY);
148         info.setStateKey(DtoConstants.STATE_ACTIVE);
149         info.setFormats(new ArrayList<FormatInfo>());
150         ResultValuesGroupInfo rvg = new ResultValuesGroupInfo();
151         rvg.setKey(LrcServiceConstants.RESULT_GROUP_KEY_KUALI_CREDITTYPE_CREDIT_2_0);
152         rvg.setTypeKey(LrcServiceConstants.R1_RESULT_COMPONENT_TYPE_KEY_FIXED);
153         info.getCreditOptions().add(rvg);
154         return info;
155     }
156 
157     private ActivityInfo buildCanonicalActivity(String activityTypeKey, FormatInfo format) {
158 
159         ActivityInfo info = new ActivityInfo();
160         info.setId(CourseOfferingServiceTestDataUtils.createCanonicalActivityId(format.getId(), activityTypeKey));
161         info.setTypeKey(activityTypeKey);
162         info.setStateKey(DtoConstants.STATE_ACTIVE);
163 
164         format.getActivities().add(info);
165 
166         return info;
167 
168     }
169 
170     private FormatInfo buildCanonicalFormat(String formatId, CourseInfo course) {
171 
172         FormatInfo info = new FormatInfo();
173         info.setId(formatId);
174         info.setTypeKey(LuServiceConstants.COURSE_FORMAT_TYPE_KEY);
175         info.setStateKey(DtoConstants.STATE_ACTIVE);
176         info.setActivities(new ArrayList<ActivityInfo>());
177 
178         course.getFormats().add(info);
179 
180         return info;
181     }
182 
183     private Date calcEffectiveDateForTerm(String termId, String context) {
184         String year = termId.substring(0, 4);
185         String mmdd = "09-01";
186         if (termId.endsWith("FA")) {
187             mmdd = "09-01";
188         } else if (termId.endsWith("WI")) {
189             mmdd = "01-01";
190         } else if (termId.endsWith("SP")) {
191             mmdd = "03-01";
192         } else if (termId.endsWith("SU")) {
193             mmdd = "06-01";
194         }
195         return str2Date(year + "-" + mmdd + " 00:00:00.0", context);
196     }
197 
198     private Date str2Date(String str, String context) {
199         if (str == null) {
200             return null;
201         }
202         try {
203             Date date = DateFormatters.DEFAULT_YEAR_MONTH_24HOUR_MILLISECONDS_FORMATTER.parse(str);
204             return date;
205         } catch (IllegalArgumentException ex) {
206             throw new IllegalArgumentException("Bad date " + str + " in " + context);
207         }
208     }
209 
210     @Test
211     public void testCRUDSCourseOffering() throws Exception {
212         new MockLrcTestDataLoader(this.lrcService).loadData();
213         dataLoader.createStateTestData();
214         dataLoader.loadTerms();
215         createCourseCHEM123(dataLoader.fall2012, callContext);
216 
217         String coId = testCreateCourseOffering();
218         testUpdateCourseOffering(coId);
219         testSearchForCourseOfferings();
220         testDeleteCourseOffering(coId);
221     }
222 
223     private String testCreateCourseOffering() throws Exception {
224         CourseOfferingInfo created = createCourseOffering();
225         assertNotNull(created);
226         assertEquals("CLU-1", created.getCourseId());
227         assertEquals("2012FA", created.getTermId());
228         assertEquals(LuiServiceConstants.COURSE_OFFERING_LIFECYCLE_STATE_KEYS[0],
229                 created.getStateKey());
230         assertEquals(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY,
231                 created.getTypeKey());
232         assertEquals("CHEM123", created.getCourseOfferingCode());
233         assertEquals("Chemistry 123", created.getCourseOfferingTitle());
234 
235         CourseOfferingInfo retrieved = coService.getCourseOffering(
236                 created.getId(), callContext);
237         assertNotNull(retrieved);
238         assertEquals("CLU-1", retrieved.getCourseId());
239         assertEquals("2012FA", retrieved.getTermId());
240         assertEquals(LuiServiceConstants.COURSE_OFFERING_LIFECYCLE_STATE_KEYS[0],
241                 retrieved.getStateKey());
242         assertEquals(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY,
243                 retrieved.getTypeKey());
244 
245         assertEquals("CHEM123", retrieved.getCourseOfferingCode());
246         assertEquals("Chemistry 123", retrieved.getCourseOfferingTitle());
247 
248         List<CourseOfferingInfo> offerings = coService.getCourseOfferingsByCourse("CLU-1", callContext);
249 
250         assertEquals(1, offerings.size());
251 
252         return created.getId();
253     }
254 
255     private CourseOfferingInfo createCourseOffering() throws Exception {
256         List<String> optionKeys = new ArrayList<String>();
257         CourseInfo canonicalCourse = courseService
258                 .getCourse("CLU-1", callContext);
259         CourseOfferingInfo coInfo = CourseOfferingServiceTestDataUtils
260                 .createCourseOffering(canonicalCourse, "2012FA");
261 
262         buildAndAttachCrossListings(coInfo);
263 
264         // gets around the unique course code constraint
265         // this is ok for testing.
266         coInfo.setCourseCode(coInfo.getCourseOfferingCode() + "TESTING CREATE");
267 
268         CourseOfferingInfo created = coService.createCourseOffering("CLU-1",
269                 "2012FA", LuiServiceConstants.COURSE_OFFERING_TYPE_KEY, coInfo,
270                 optionKeys, callContext);
271 
272 
273         return created;
274     }
275 
276     private void buildAndAttachCrossListings(CourseOfferingInfo coInfo) {
277 
278         CourseOfferingCrossListingInfo info = null;
279         Date createTime = new Date();
280         for( int i = 0, j = 3 ; i < j ; i++ ) {
281             info = new CourseOfferingCrossListingInfo();
282 
283             info.setId("MY-ID_" + i);
284             info.setTypeKey("MY-TYPE_" + i);
285             info.setStateKey("MY-STATE_" + i);
286             info.setCode("MY-CODE_" + i);
287             info.setSubjectArea("MY-SUBJECT_" + i);
288             //info.setDepartmentOrgId(coInfo.getd);     // not sure how this maps
289             info.setCourseNumberSuffix("MY-SUFFIX_" + i);
290 
291             MetaInfo meta = new MetaInfo();
292             meta.setCreateId("META-CREATE-ID_" + i);
293             meta.setCreateTime(createTime);
294             meta.setVersionInd(Integer.toString(i));
295             info.setMeta(meta);
296 
297             List<AttributeInfo> attrs = new ArrayList<AttributeInfo>();
298             AttributeInfo attr = new AttributeInfo();
299             attr.setId("ATTRS-ID_" + i);
300             attr.setKey("ATTRS-KEY_" + i);
301             attr.setValue("ATTRS-VALUE_" + i);
302             attrs.add(attr);
303             info.setAttributes(attrs);
304 
305             crossListings.add(info);
306         }
307 
308         coInfo.setCrossListings(crossListings);
309     }
310 
311 
312     private void testUpdateCourseOffering(String coId) throws Exception {
313         try {
314             CourseOfferingInfo coi = coService.getCourseOffering(coId,
315                     callContext);
316             assertNotNull(coi);
317             coi.setIsHonorsOffering(true);
318             coi.setMaximumEnrollment(40);
319             coi.setMinimumEnrollment(10);
320 
321             //skipping instructors test because we can't config kim personservice at test context
322 
323             // dynamic attributes
324             AttributeTester attributeTester = new AttributeTester();
325             List<AttributeInfo> expectedList = new ArrayList<AttributeInfo>();
326             attributeTester.add2ForCreate(expectedList);
327             coi.getAttributes().addAll(expectedList);
328             coi.setFundingSource("state");
329 
330             CourseOfferingInfo updated = coService.updateCourseOffering(coId,
331                     coi, callContext);
332             assertNotNull(updated);
333 
334             CourseOfferingInfo retrieved = coService.getCourseOffering(coId,
335                     callContext);
336             assertNotNull(retrieved);
337 
338             assertTrue(retrieved.getIsHonorsOffering());
339             assertEquals(coi.getMaximumEnrollment(),
340                     retrieved.getMaximumEnrollment());
341             assertEquals(coi.getMinimumEnrollment(),
342                     retrieved.getMinimumEnrollment());
343             assertEquals("state", coi.getFundingSource());
344             attributeTester.check(expectedList, coi.getAttributes());
345 
346         } catch (Exception ex) {
347             log.error("exception due to", ex);
348             fail("Exception from service call :" + ex.getMessage());
349         }
350     }
351 
352     private void testSearchForCourseOfferings() throws Exception {
353         try {
354             QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
355             qbcBuilder.setPredicates(PredicateFactory.and(
356                     PredicateFactory.like("courseOfferingCode", "CHEM%"),
357                     PredicateFactory.equalIgnoreCase("atpId", "2012FA")));
358             QueryByCriteria criteria = qbcBuilder.build();
359 
360             List<CourseOfferingInfo> coList = coService
361                     .searchForCourseOfferings(criteria, callContext);
362             assertNotNull(coList);
363             assertEquals(1, coList.size());
364             CourseOfferingInfo coInfo = coList.get(0);
365             assertEquals("CHEM123", coInfo.getCourseOfferingCode());
366             assertEquals("2012FA", coInfo.getTermId());
367 
368             validateCrossListings(coInfo.getCrossListings());
369         } catch (Exception ex) {
370             fail("Exception from service call :" + ex.getMessage());
371         }
372     }
373 
374     private static Comparator<CourseOfferingCrossListingInfo> orderByIdComparator = new Comparator<CourseOfferingCrossListingInfo>() {
375 		
376 		@Override
377 		public int compare(CourseOfferingCrossListingInfo o1,
378 				CourseOfferingCrossListingInfo o2) {
379 			return o1.getId().compareTo(o2.getId());
380 		}
381 	};
382 	
383     private void validateCrossListings(List<CourseOfferingCrossListingInfo> crossListings) {
384 
385         assertTrue( this.crossListings.size() == crossListings.size() );
386         
387         Collections.sort(this.crossListings, orderByIdComparator);
388         Collections.sort(crossListings, orderByIdComparator);
389 
390         for( int i = 0, j = this.crossListings.size() ; i < j ; i++ ) {
391             CourseOfferingCrossListingInfo expected = this.crossListings.get(i);
392             CourseOfferingCrossListingInfo actual = crossListings.get(i);
393 
394             // basic props
395             assertEquals( expected.getId(), actual.getId() );
396             assertEquals( expected.getTypeKey(), actual.getTypeKey() );
397             assertEquals( expected.getStateKey(), actual.getStateKey() );
398             assertEquals( expected.getCode() + expected.getCourseNumberSuffix(),    // business-rules append the suffix to the code when object is persisted to DB
399                             actual.getCode() );
400             assertEquals( expected.getSubjectArea(), actual.getSubjectArea() );
401             assertEquals( expected.getSubjectOrgId(), actual.getSubjectOrgId() );
402             assertEquals( expected.getCourseNumberSuffix(), actual.getCourseNumberSuffix() );
403         }
404 
405     }
406 
407     private CourseOfferingCrossListingInfo getCrosslistingWithMatchingIdFromList(CourseOfferingCrossListingInfo target, List<CourseOfferingCrossListingInfo> list) {
408         for( CourseOfferingCrossListingInfo item : list ) {
409             if( item.getId().equals(target.getId())) return target;
410         }
411         return null;
412     }
413 
414     private void testDeleteCourseOffering(String coId) throws Exception {
415         try {
416             // Delete the course offering and check that the status returned was
417             // a success
418             StatusInfo delResult = coService.deleteCourseOffering(coId, callContext);
419             assertTrue(delResult.getIsSuccess());
420 
421             try {
422                 coService.getCourseOffering(coId, callContext);
423             } catch (DoesNotExistException ex) {
424                 //expected
425             }
426 
427         } catch (Exception ex) {
428             log.error("exception due to ", ex);
429             fail("Exception from service call :" + ex.getMessage());
430         }
431 
432     }
433 
434     @Test
435     public void testCRFormatOffering() throws Exception {
436         new MockLrcTestDataLoader(this.lrcService).loadData();
437         dataLoader.createStateTestData();
438         dataLoader.loadTerms();
439         createCourseCHEM123(dataLoader.fall2012, callContext);
440 
441         CourseOfferingInfo co = createCourseOffering();
442         FormatOfferingInfo created = createFormatOffering(co.getId(), co.getTermId());
443 
444         assertNotNull(created);
445         assertEquals(LuiServiceConstants.LUI_FO_STATE_PLANNED_KEY,
446                 created.getStateKey());
447         assertEquals(LuiServiceConstants.FORMAT_OFFERING_TYPE_KEY,
448                 created.getTypeKey());
449         assertEquals("TEST FORMAT OFFERING", created.getDescr().getPlain());
450 
451         FormatOfferingInfo retrieved = coService.getFormatOffering(created.getId(), callContext);
452         assertEquals(retrieved.getStateKey(),
453                 created.getStateKey());
454         assertEquals(retrieved.getTypeKey(),
455                 created.getTypeKey());
456         assertEquals(retrieved.getDescr().getPlain(), created.getDescr().getPlain());
457         assertEquals(retrieved.getTermId(), created.getTermId());
458     }
459 
460     private FormatOfferingInfo createFormatOffering(String coId, String coTermId) throws Exception {
461         FormatOfferingInfo fo = null;
462         try {
463             FormatOfferingInfo newFO = CourseOfferingServiceTestDataUtils
464                     .createFormatOffering(coId, "CHEM123:LEC-ONLY",
465                             coTermId, "TEST FORMAT OFFERING",
466                             LuiServiceConstants.ALL_ACTIVITY_TYPES);
467 
468             fo = coService.createFormatOffering(coId,
469                     "CHEM123:LEC-ONLY", LuiServiceConstants.FORMAT_OFFERING_TYPE_KEY,
470                     newFO, callContext);
471         } catch (Exception ex) {
472             log.error("exception due to ", ex);
473             fail(ex.getMessage());
474         }
475 
476         return fo;
477     }
478 
479     private ActivityOfferingInfo createActivityOffering(CourseOfferingInfo courseOffering, String foId) throws Exception {
480         String activityId = CourseOfferingServiceTestDataUtils
481                 .createCanonicalActivityId("CHEM123:LEC-ONLY",
482                         LuServiceConstants.COURSE_ACTIVITY_LECTURE_TYPE_KEY);
483 
484         List<OfferingInstructorInfo> instructors = new ArrayList<OfferingInstructorInfo>();
485         ActivityOfferingInfo ao = CourseOfferingServiceTestDataUtils
486                 .createActivityOffering("2012FA", courseOffering, foId,
487                         null, activityId, "Lecture", "A",
488                         LuiServiceConstants.LECTURE_ACTIVITY_OFFERING_TYPE_KEY,
489                         instructors);
490 
491         ActivityOfferingInfo created = coService.createActivityOffering(
492                 foId, activityId,
493                 LuiServiceConstants.LECTURE_ACTIVITY_OFFERING_TYPE_KEY, ao,
494                 callContext);
495 
496         return created;
497     }
498 
499     @Test
500     public void testCRActivityOffering() throws Exception {
501         new MockLrcTestDataLoader(this.lrcService).loadData();
502         dataLoader.createStateTestData();
503         dataLoader.loadTerms();
504         createCourseCHEM123(dataLoader.fall2012, callContext);
505 
506         CourseOfferingInfo courseOffering = createCourseOffering();
507         FormatOfferingInfo fo = createFormatOffering(courseOffering.getId(), courseOffering.getTermId());
508 
509         try {
510             ActivityOfferingInfo created = createActivityOffering(courseOffering, fo.getId());
511             assertNotNull(created);
512 
513             ActivityOfferingInfo retrieved = coService.getActivityOffering(
514                     created.getId(), callContext);
515             assertNotNull(retrieved);
516 
517             assertEquals(created.getActivityId(), retrieved.getActivityId());
518             assertEquals(created.getTermId(), retrieved.getTermId());
519             assertEquals(LuiServiceConstants.LUI_AO_STATE_DRAFT_KEY,
520                     retrieved.getStateKey());
521             assertEquals(
522                     LuiServiceConstants.LECTURE_ACTIVITY_OFFERING_TYPE_KEY,
523                     retrieved.getTypeKey());
524 
525             // test getActivityOfferingsByCourseOffering
526             List<ActivityOfferingInfo> activities = coService
527                     .getActivityOfferingsByCourseOffering(courseOffering.getId(), callContext);
528             assertNotNull(activities);
529 
530             assertEquals(1, activities.size());
531 
532             boolean foundActivityId = false;
533 
534             for (ActivityOfferingInfo activityOfferingInfo : activities) {
535 
536                 if (activityOfferingInfo.getActivityId().equals(
537                         created.getActivityId())) {
538                     foundActivityId = true;
539                 }
540             }
541             assertTrue(foundActivityId);
542 
543             ActivityOffering createdAo = coService.getActivityOffering(retrieved.getId(), callContext);
544             assertFalse(createdAo.getIsColocated());
545 
546         } catch (Exception ex) {
547             log.fatal("Exception from serviceCall", ex);
548 
549             fail("Exception from service call :" + ex.getMessage());
550         }
551     }
552 
553     @Test
554     public void testDeleteCourseOfferingCascaded() throws Exception {
555         new MockLrcTestDataLoader(this.lrcService).loadData();
556         dataLoader.createStateTestData();
557         dataLoader.loadTerms();
558         createCourseCHEM123(dataLoader.fall2012, callContext);
559 
560         CourseOfferingInfo courseOffering = createCourseOffering();
561         FormatOfferingInfo fo = createFormatOffering(courseOffering.getId(), courseOffering.getTermId());
562         ActivityOfferingInfo ao = createActivityOffering(courseOffering, fo.getId());
563         List<FormatOfferingInfo> oformats = coService.getFormatOfferingsByCourseOffering(courseOffering.getId(), callContext);
564         assertEquals(1, oformats.size());
565 
566         List<ActivityOfferingInfo> oactivities = coService.getActivityOfferingsByCourseOffering(courseOffering.getId(), callContext);
567         assertEquals(1, oactivities.size());
568 
569         StatusInfo status = coService.deleteCourseOfferingCascaded(courseOffering.getId(), callContext);
570         assertTrue(status.getIsSuccess());
571 
572         List<FormatOfferingInfo> formats = coService.getFormatOfferingsByCourseOffering(courseOffering.getId(), callContext);
573         assertEquals(0, formats.size());
574 
575         List<ActivityOfferingInfo> activities = coService.getActivityOfferingsByCourseOffering(courseOffering.getId(), callContext);
576         assertEquals(0, activities.size());
577 
578         try {
579             coService.getCourseOffering(courseOffering.getId(), callContext);
580         } catch (DoesNotExistException ex) {
581             //expected
582         }
583     }
584 
585     @Test
586     public void testGetActivityOfferingsForSeatPoolDefinition() throws Exception {
587         new MockLrcTestDataLoader(this.lrcService).loadData();
588         dataLoader.createStateTestData();
589         dataLoader.loadTerms();
590         createCourseCHEM123(dataLoader.fall2012, callContext);
591 
592         // Create  Seatpool
593         SeatPoolDefinitionInfo seatPoolDefinitionInfo = new SeatPoolDefinitionInfo();
594         seatPoolDefinitionInfo.setName("TestSeatPoolDefinitionInfo-Id");
595         seatPoolDefinitionInfo.setStateKey("TestSeatPoolDefinitionInfo-StateKey1");
596         seatPoolDefinitionInfo.setTypeKey("TestSeatPoolDefinitionInfo-TypeKey1");
597         seatPoolDefinitionInfo.setExpirationMilestoneTypeKey("TestSeatPoolDefinitionInfo-MilestoneKey1");
598         seatPoolDefinitionInfo.setIsPercentage(false);
599         seatPoolDefinitionInfo.setSeatLimit(50);
600         seatPoolDefinitionInfo.setProcessingPriority(3);
601         seatPoolDefinitionInfo.setId(null);
602         SeatPoolDefinitionInfo seatPoolCreated = coService.createSeatPoolDefinition(seatPoolDefinitionInfo, callContext);
603 
604         // Create AO
605         CourseOfferingInfo courseOffering = createCourseOffering();
606         FormatOfferingInfo fo = createFormatOffering(courseOffering.getId(), courseOffering.getTermId());
607         ActivityOfferingInfo activityOfferingCreated = createActivityOffering(courseOffering, fo.getId());
608 
609         // Add Seatpool to AO
610         coService.addSeatPoolDefinitionToActivityOffering(seatPoolCreated.getId(), activityOfferingCreated.getId(), callContext);
611 
612         //  Actual test
613         List<ActivityOfferingInfo> activityOfferingInfos = coService.getActivityOfferingsForSeatPoolDefinition(seatPoolCreated.getId(), callContext);
614 
615         assertEquals(1, activityOfferingInfos.size());
616         assertEquals(activityOfferingCreated.getId(), activityOfferingInfos.get(0).getId());
617     }
618 
619     @Test
620     public void testActivityOfferingClusters() throws Exception {
621         new MockLrcTestDataLoader(this.lrcService).loadData();
622         dataLoader.createStateTestData();
623         dataLoader.beforeTest(false);
624 
625         // default cluster is 2x3 = 6 reg groups
626 
627         // we want to constrain to not use lec-b
628         // 1x3 = 3 reg groups
629 
630         ActivityOfferingInfo activities[] = new ActivityOfferingInfo[]{
631                 coService.getActivityOffering("CO-1:LEC-AND-LAB:LEC-A",
632                         callContext),
633                 coService.getActivityOffering("CO-1:LEC-AND-LAB:LAB-A",
634                         callContext),
635                 coService.getActivityOffering("CO-1:LEC-AND-LAB:LAB-B",
636                         callContext),
637                 coService.getActivityOffering("CO-1:LEC-AND-LAB:LAB-C",
638                         callContext),};
639 
640         ActivityOfferingClusterInfo expected = CourseOfferingServiceTestDataUtils
641                 .createActivityOfferingCluster("CO-1:LEC-AND-LAB", "Default Cluster",
642                         Arrays.asList(activities));
643 
644         expected.setId("AOC-1");
645         new AttributeTester().add2ForCreate(expected.getAttributes());
646 
647         ActivityOfferingClusterInfo actual = coService.createActivityOfferingCluster("CO-1:LEC-AND-LAB", CourseOfferingServiceConstants.AOC_ROOT_TYPE_KEY, expected, callContext);
648 
649         validateAoc(actual, expected, activities);
650 
651         expected = actual;
652         actual = coService.getActivityOfferingCluster(expected.getId(), callContext);
653 
654         validateAoc(actual, expected, activities);
655 
656         List<String> aocIds = new ArrayList<String>();
657         aocIds.add(actual.getId());
658         List<String> aocIdsTwo = coService.getActivityOfferingClustersIdsByFormatOffering("CO-2:LEC-ONLY", callContext);
659         aocIds.addAll(aocIdsTwo);
660         List<ActivityOfferingClusterInfo> aocList = coService.getActivityOfferingClustersByIds(aocIds, callContext);
661         assertEquals(2, aocList.size());
662         assertTrue(aocList.get(0).getId().equals(aocIds.get(0)) || aocList.get(1).getId().equals(aocIds.get(0)));
663         assertTrue(aocList.get(0).getId().equals(aocIds.get(1)) || aocList.get(1).getId().equals(aocIds.get(1)));
664 
665         List<RegistrationGroupInfo> rgList = coService.getRegistrationGroupsByActivityOfferingCluster(actual.getId(), callContext);
666 
667         assertEquals(0, rgList.size());
668 
669         coService.generateRegistrationGroupsForCluster(actual.getId(), callContext);
670 
671         rgList = coService.getRegistrationGroupsByActivityOfferingCluster(actual.getId(), callContext);
672 
673         assertEquals(3, rgList.size());
674 
675         coService.generateRegistrationGroupsForCluster(actual.getId(), callContext);
676 
677         // verify count stays the same even after calling the method again.
678         rgList = coService.getRegistrationGroupsByActivityOfferingCluster(actual.getId(), callContext);
679 
680         assertEquals(3, rgList.size());
681     }
682 
683     private void validateAoc(ActivityOfferingClusterInfo actual, ActivityOfferingClusterInfo expected, ActivityOfferingInfo... activities) {
684         assertNotNull(actual.getId());
685         assertEquals(expected.getId(), actual.getId());
686         assertEquals(expected.getTypeKey(), actual.getTypeKey());
687         assertEquals(expected.getStateKey(), actual.getStateKey());
688         assertEquals(expected.getFormatOfferingId(), actual.getFormatOfferingId());
689         assertEquals(expected.getPrivateName(), actual.getPrivateName());
690         assertEquals(expected.getName(), actual.getName());
691 
692         new AttributeTester().check(expected.getAttributes(), actual.getAttributes());
693         new MetaTester().checkAfterCreate(actual.getMeta());
694         new RichTextTester().check(expected.getDescr(), actual.getDescr());
695 
696         // check that the union of activity id's matches what we declared
697         assertEquals(expected.getActivityOfferingSets().size(), actual.getActivityOfferingSets().size());
698         List<String> activityIds = new ArrayList<String>();
699         for(ActivityOfferingInfo info : activities) {
700             activityIds.add(info.getId());
701         }
702 
703         new ListOfStringTester().checkExistsAnyOrder(activityIds, extractActivityOfferingIds(actual.getActivityOfferingSets()), true);
704     }
705 
706     private List<String> extractActivityOfferingIds(List<ActivityOfferingSetInfo> aoList) {
707         List<String> idList = new ArrayList<String>();
708 
709         for (ActivityOfferingSetInfo activityOfferingSetInfo : aoList) {
710 
711             idList.addAll(activityOfferingSetInfo.getActivityOfferingIds());
712 
713         }
714         return idList;
715     }
716 }