public ActivityOfferingInfo testCRUDActivityOffering(FormatOfferingInfo fo)
      throws DoesNotExistException, InvalidParameterException, MissingParameterException,
          OperationFailedException, PermissionDeniedException, DataValidationErrorException,
          ReadOnlyException, VersionMismatchException {

    ActivityOfferingInfo orig = new ActivityOfferingInfo();
    orig.setFormatOfferingId(fo.getId());
    orig.setActivityId(fo.getId() + "." + LuServiceConstants.COURSE_ACTIVITY_LECTURE_TYPE_KEY);
    orig.setTypeKey(LuiServiceConstants.LECTURE_ACTIVITY_OFFERING_TYPE_KEY);
    orig.setStateKey(LuiServiceConstants.LUI_AO_STATE_DRAFT_KEY);
    orig.setMinimumEnrollment(100);
    orig.setMaximumEnrollment(150);
    orig.setIsEvaluated(true);
    orig.setIsMaxEnrollmentEstimate(false);
    orig.setIsHonorsOffering(true);
    ActivityOfferingInfo info =
        courseOfferingService.createActivityOffering(
            orig.getFormatOfferingId(), orig.getActivityId(), orig.getTypeKey(), orig, callContext);
    assertNotNull(info);
    assertNotNull(info.getId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());
    assertEquals(orig.getFormatOfferingId(), info.getFormatOfferingId());
    assertEquals(orig.getActivityId(), info.getActivityId());
    assertEquals(orig.getMinimumEnrollment(), info.getMinimumEnrollment());
    assertEquals(orig.getMaximumEnrollment(), info.getMaximumEnrollment());
    assertEquals(orig.getIsEvaluated(), info.getIsEvaluated());
    assertEquals(orig.getIsMaxEnrollmentEstimate(), info.getIsMaxEnrollmentEstimate());
    assertEquals(orig.getIsHonorsOffering(), info.getIsHonorsOffering());

    orig = info;
    info = courseOfferingService.getActivityOffering(orig.getId(), callContext);
    assertNotNull(info);
    assertEquals(orig.getId(), info.getId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());
    assertEquals(orig.getFormatOfferingId(), info.getFormatOfferingId());
    assertEquals(orig.getActivityId(), info.getActivityId());
    assertEquals(orig.getMinimumEnrollment(), info.getMinimumEnrollment());
    assertEquals(orig.getMaximumEnrollment(), info.getMaximumEnrollment());

    orig = info;
    orig.setMinimumEnrollment(100);
    info = courseOfferingService.updateActivityOffering(orig.getId(), orig, callContext);
    assertNotNull(info);
    assertEquals(orig.getId(), info.getId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());
    assertEquals(orig.getFormatOfferingId(), info.getFormatOfferingId());
    assertEquals(orig.getActivityId(), info.getActivityId());
    assertEquals(orig.getMinimumEnrollment(), info.getMinimumEnrollment());
    assertEquals(orig.getMaximumEnrollment(), info.getMaximumEnrollment());
    return info;
  }
  private void testDeletes(CourseOfferingInfo co, FormatOfferingInfo fo, ActivityOfferingInfo ao)
      throws InvalidParameterException, MissingParameterException, OperationFailedException,
          PermissionDeniedException, DoesNotExistException, DependentObjectsExistException {

    // delete activity offering
    StatusInfo status = this.courseOfferingService.deleteActivityOffering(ao.getId(), callContext);
    assertNotNull(status);
    assertEquals(Boolean.TRUE, status.getIsSuccess());

    try {
      courseOfferingService.getActivityOffering(ao.getId(), callContext);
      fail("should have thrown DoesNotExistException");
    } catch (DoesNotExistException ex) {
      assertNotNull(ex.getMessage());
      assertEquals(ao.getId(), ex.getMessage());
    }

    // delete fo
    status = this.courseOfferingService.deleteFormatOffering(fo.getId(), callContext);
    assertNotNull(status);
    assertEquals(Boolean.TRUE, status.getIsSuccess());

    try {
      courseOfferingService.getFormatOffering(fo.getId(), callContext);
      fail("should have thrown DoesNotExistException");
    } catch (DoesNotExistException ex) {
      assertNotNull(ex.getMessage());
      assertEquals(fo.getId(), ex.getMessage());
    }

    // delete co
    status = this.courseOfferingService.deleteCourseOffering(co.getId(), callContext);
    assertNotNull(status);
    assertEquals(Boolean.TRUE, status.getIsSuccess());

    try {
      courseOfferingService.getCourseOffering(co.getId(), callContext);
      fail("should have thrown DoesNotExistException");
    } catch (DoesNotExistException ex) {
      assertNotNull(ex.getMessage());
      assertEquals(co.getId(), ex.getMessage());
    }
  }
 /**
  * Find all RGs that contain any of the aoIds listed
  *
  * @param aoIds List of AO ids
  * @param context Context info
  * @return Map of reg group to AO ids
  * @throws PermissionDeniedException
  * @throws MissingParameterException
  * @throws InvalidParameterException
  * @throws OperationFailedException
  * @throws DoesNotExistException
  */
 private Map<String, Set<String>> findRegGroupIdToAoIds(List<String> aoIds, ContextInfo context)
     throws PermissionDeniedException, MissingParameterException, InvalidParameterException,
         OperationFailedException, DoesNotExistException {
   Map<String, Set<String>> regGroupIdToAoIds = new HashMap<>();
   for (String aoId : aoIds) {
     List<RegistrationGroupInfo> rgInfos =
         coService.getRegistrationGroupsByActivityOffering(aoId, context);
     for (RegistrationGroupInfo rgInfo : rgInfos) {
       Set<String> aoIdsInRg = regGroupIdToAoIds.get(rgInfo.getId());
       if (aoIdsInRg == null) {
         Set<String> aoIdSet = new HashSet<>(rgInfo.getActivityOfferingIds());
         regGroupIdToAoIds.put(rgInfo.getId(), aoIdSet);
       }
     }
   }
   return regGroupIdToAoIds;
 }
  private CourseOfferingInfo createCourseOffering(
      boolean shouldThrowPermissionDenied, CourseInfo course, String principalId)
      throws DoesNotExistException, DataValidationErrorException, InvalidParameterException,
          MissingParameterException, OperationFailedException, PermissionDeniedException,
          ReadOnlyException, DependentObjectsExistException {
    ContextInfo context = this.getContext(principalId);
    List<String> optionKeys = new ArrayList<String>();
    CourseOfferingInfo sourceCo =
        CourseOfferingServiceTestDataUtils.createCourseOffering(
            course, FA2011_TERM.getId(), LuiServiceConstants.LUI_CO_STATE_OFFERED_KEY);

    try {
      sourceCo =
          courseOfferingService.createCourseOffering(
              sourceCo.getCourseId(),
              sourceCo.getTermId(),
              sourceCo.getTypeKey(),
              sourceCo,
              optionKeys,
              context);
    } catch (PermissionDeniedException ex) {
      if (!shouldThrowPermissionDenied) {
        fail(
            "Create should not have thrown permission denied but did "
                + course.getCode()
                + " "
                + principalId);
        return null;
      }
      return null;
    }
    if (shouldThrowPermissionDenied) {
      fail(
          "Create should have thrown permission denied but did not "
              + course.getCode()
              + " "
              + principalId);
      return null;
    }
    return sourceCo;
  }
  public FormatOfferingInfo testCRUDFormatOffering(CourseOfferingInfo co)
      throws DoesNotExistException, InvalidParameterException, MissingParameterException,
          OperationFailedException, PermissionDeniedException, DataValidationErrorException,
          VersionMismatchException, ReadOnlyException, DependentObjectsExistException {
    FormatOfferingInfo orig = new FormatOfferingInfo();
    orig.setCourseOfferingId(co.getId());
    orig.setFormatId("COURSE1-FORMAT1");
    orig.setActivityOfferingTypeKeys(
        Arrays.asList(LuiServiceConstants.LECTURE_ACTIVITY_OFFERING_TYPE_KEY));
    orig.setTypeKey(LuiServiceConstants.FORMAT_OFFERING_TYPE_KEY);
    orig.setStateKey(LuiServiceConstants.LUI_FO_STATE_DRAFT_KEY);
    FormatOfferingInfo info =
        courseOfferingService.createFormatOffering(
            orig.getCourseOfferingId(), orig.getFormatId(), orig.getTypeKey(), orig, callContext);
    assertNotNull(info);
    assertNotNull(info.getId());
    assertEquals(orig.getCourseOfferingId(), info.getCourseOfferingId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());
    assertEquals(orig.getFormatId(), info.getFormatId());
    // TODO: turn these tests back on once we get the corresponding JPA entities working
    //        assertEquals(orig.getActivityOfferingTypeKeys().size(),
    // info.getActivityOfferingTypeKeys().size());
    //        assertEquals(orig.getActivityOfferingTypeKeys().get(0),
    // info.getActivityOfferingTypeKeys().get(0));

    List<FormatOfferingInfo> formats =
        courseOfferingService.getFormatOfferingsByCourseOffering(co.getId(), callContext);
    info = formats.get(0);
    assertNotNull(info);
    assertNotNull(info.getId());
    assertEquals(orig.getCourseOfferingId(), info.getCourseOfferingId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());
    assertEquals(orig.getFormatId(), info.getFormatId());

    orig = info;
    info = courseOfferingService.getFormatOffering(orig.getId(), callContext);
    assertNotNull(info);
    assertEquals(orig.getId(), info.getId());
    assertEquals(orig.getCourseOfferingId(), info.getCourseOfferingId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());
    assertEquals(orig.getFormatId(), info.getFormatId());
    //        assertEquals(orig.getActivityOfferingTypeKeys().size(),
    // info.getActivityOfferingTypeKeys().size());
    //        assertEquals(orig.getActivityOfferingTypeKeys().get(0),
    // info.getActivityOfferingTypeKeys().get(0));

    orig = info;
    orig.setActivityOfferingTypeKeys(
        Arrays.asList(LuiServiceConstants.LECTURE_ACTIVITY_OFFERING_TYPE_KEY));
    info = courseOfferingService.updateFormatOffering(orig.getId(), orig, callContext);
    assertNotNull(info);
    assertEquals(orig.getId(), info.getId());
    assertEquals(orig.getCourseOfferingId(), info.getCourseOfferingId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());
    assertEquals(orig.getFormatId(), info.getFormatId());
    //        assertEquals(orig.getActivityOfferingTypeKeys().size(),
    // info.getActivityOfferingTypeKeys().size());
    //        assertEquals(orig.getActivityOfferingTypeKeys().get(0),
    // info.getActivityOfferingTypeKeys().get(0));
    return info;
  }
  public CourseOfferingInfo testCRUDCourseOffering()
      throws DoesNotExistException, DataValidationErrorException, InvalidParameterException,
          MissingParameterException, OperationFailedException, PermissionDeniedException,
          ReadOnlyException, VersionMismatchException {
    // get course
    CourseInfo course;
    course = courseService.getCourse("COURSE1", ContextUtils.getContextInfo());
    // create co from course
    List<String> optionKeys = new ArrayList<String>();
    CourseOfferingInfo orig = new CourseOfferingInfo();
    orig.setCourseId(course.getId());
    orig.setTermId("testAtpId1");
    orig.setTypeKey(LuiServiceConstants.COURSE_OFFERING_TYPE_KEY);
    orig.setStateKey(LuiServiceConstants.LUI_CO_STATE_DRAFT_KEY);
    orig.setCourseOfferingTitle("my name");
    orig.setWaitlistLevelTypeKey("waitlist key");
    orig.setHasWaitlist(true);
    orig.setFinalExamType(FinalExam.STANDARD.toString());
    orig.setIsEvaluated(true);
    orig.setIsFeeAtActivityOffering(false);
    orig.setFundingSource("funding source");
    orig.setCourseOfferingCode("CODE");
    orig.setCourseNumberSuffix("");
    orig.setCourseOfferingTitle("Title");
    orig.getStudentRegistrationGradingOptions()
        .add(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_AUDIT);
    orig.getStudentRegistrationGradingOptions()
        .add(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_PASSFAIL);
    orig.setGradingOptionId(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_LETTER);

    CourseOfferingInfo info =
        courseOfferingService.createCourseOffering(
            orig.getCourseId(), orig.getTermId(), orig.getTypeKey(), orig, optionKeys, callContext);
    assertNotNull(info);
    assertNotNull(info.getId());
    assertEquals(orig.getCourseId(), info.getCourseId());
    assertEquals(orig.getTermId(), info.getTermId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());
    assertEquals(orig.getWaitlistLevelTypeKey(), info.getWaitlistLevelTypeKey());
    assertEquals(orig.getHasWaitlist(), info.getHasWaitlist());
    assertEquals(orig.getFinalExamType(), info.getFinalExamType());
    assertEquals(orig.getIsFeeAtActivityOffering(), info.getIsFeeAtActivityOffering());
    assertEquals(orig.getFundingSource(), info.getFundingSource());
    assertEquals(course.getCode() + info.getCourseNumberSuffix(), info.getCourseOfferingCode());
    assertEquals(orig.getCourseNumberSuffix(), info.getCourseNumberSuffix());
    assertEquals(course.getSubjectArea(), info.getSubjectArea());
    if (course.getDescr() != null) {
      assertEquals(course.getDescr().getPlain(), info.getDescr().getPlain());
      assertEquals(course.getDescr().getFormatted(), info.getDescr().getFormatted());
    }
    //        assertEquals(2,info.getStudentRegistrationOptionIds().size());
    //
    // assertTrue(info.getStudentRegistrationOptionIds().contains(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_AUDIT));
    //
    // assertTrue(info.getStudentRegistrationOptionIds().contains(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_PASSFAIL));
    //
    //        assertEquals(2,info.getGradingOptionIds().size());
    //
    // assertTrue(info.getGradingOptionIds().contains(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_LETTER));
    //
    // assertTrue(info.getGradingOptionIds().contains(LrcServiceConstants.RESULT_GROUP_KEY_GRADE_PERCENTAGE));

    // TODO: test for these things
    //        assertEquals(course.getUnitsContentOwnerOrgIds(), info.getUnitsContentOwnerOrgIds());
    //        assertEquals(course.getUnitsDeploymentOrgIds(), info.getUnitsDeploymentOrgIds());
    //        assertEquals(course.getGradingOptions(), info.getGradingOptionIds());
    //        assertEquals(course.getCreditOptionIds(), info.getCreditOptionIds());

    // refetch co
    orig = info;
    info = courseOfferingService.getCourseOffering(orig.getId(), callContext);
    assertNotNull(info);
    assertEquals(orig.getId(), info.getId());
    assertEquals(orig.getCourseId(), info.getCourseId());
    assertEquals(orig.getTermId(), info.getTermId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());

    // update co
    orig = info;
    orig.setIsHonorsOffering(true);
    orig.setMaximumEnrollment(40);
    orig.setMinimumEnrollment(10);
    List<OfferingInstructorInfo> instructors = new ArrayList<OfferingInstructorInfo>();
    //        OfferingInstructorInfo instructor = new OfferingInstructorInfo();
    //        instructor.setPersonId("Pers-1");
    //        instructor.setPercentageEffort(Float.valueOf("60"));
    //        instructor.setTypeKey(LprServiceConstants.INSTRUCTOR_MAIN_TYPE_KEY);
    //        instructor.setStateKey(LprServiceConstants.ASSIGNED_STATE_KEY);
    // TODO: add this back in and test for it
    //        instructors.add(instructor);
    orig.setInstructors(instructors);
    info = courseOfferingService.updateCourseOffering(orig.getId(), orig, callContext);
    assertNotNull(info);
    assertEquals(orig.getId(), info.getId());
    assertEquals(orig.getCourseId(), info.getCourseId());
    assertEquals(orig.getTermId(), info.getTermId());
    assertEquals(orig.getStateKey(), info.getStateKey());
    assertEquals(orig.getTypeKey(), info.getTypeKey());
    assertEquals(orig.getIsHonorsOffering(), info.getIsHonorsOffering());
    assertEquals(orig.getMaximumEnrollment(), info.getMaximumEnrollment());
    assertEquals(orig.getMinimumEnrollment(), info.getMinimumEnrollment());
    assertEquals(orig.getInstructors().size(), info.getInstructors().size());
    //        OfferingInstructorInfo origInst1 = orig.getInstructors().get(0);
    //        OfferingInstructorInfo infoInst1 = info.getInstructors().get(0);
    //        assertEquals(origInst1.getPersonId(), infoInst1.getPersonId());
    //        assertEquals(origInst1.getPercentageEffort(), infoInst1.getPercentageEffort());
    //        assertEquals(origInst1.getTypeKey(), infoInst1.getTypeKey());
    //        assertEquals(origInst1.getStateKey(), infoInst1.getStateKey());
    return info;
  }