/** Test the bulk expire method */
  public void testExpire() throws Exception {
    initializeData();

    LocalDate date = new LocalDate(new Date());

    testFindExpired();

    flushAndClear();

    screen1NotExpired = genericEntityDao.reloadEntity(screen1NotExpired);
    screen2Expired = genericEntityDao.reloadEntity(screen2Expired);
    screen3Expired = genericEntityDao.reloadEntity(screen3Expired);
    screen4Default = genericEntityDao.reloadEntity(screen4Default);
    screen5RnaiExpired = genericEntityDao.reloadEntity(screen5RnaiExpired);
    screen6ExpiredNoResults = genericEntityDao.reloadEntity(screen6ExpiredNoResults);
    screen7ExpiredDropped = genericEntityDao.reloadEntity(screen7ExpiredDropped);
    screen8ExpiredTransferred = genericEntityDao.reloadEntity(screen8ExpiredTransferred);
    screen9ExpiredMaxDatePassed = genericEntityDao.reloadEntity(screen9ExpiredMaxDatePassed);

    List<Pair<Screen, AdministrativeActivity>> results =
        screenDataSharingLevelUpdater.expireScreenDataSharingLevels(
            date, admin, ScreenType.SMALL_MOLECULE);
    // TODO: Do some checking of the activities as well.
    Set<Screen> screens = Sets.newLinkedHashSet();
    for (Pair<Screen, AdministrativeActivity> result : results) {
      screens.add(result.getFirst());
    }
    log.info("screens: " + screens);
    screen1NotExpired = genericEntityDao.reloadEntity(screen1NotExpired);
    screen2Expired = genericEntityDao.reloadEntity(screen2Expired);
    screen3Expired = genericEntityDao.reloadEntity(screen3Expired);
    screen4Default = genericEntityDao.reloadEntity(screen4Default);
    screen5RnaiExpired = genericEntityDao.reloadEntity(screen5RnaiExpired);

    assertTrue("screen2Expired should be expired", screens.contains(screen2Expired));
    assertTrue(
        "screen2Expired should be shared",
        screen2Expired.getDataSharingLevel() == ScreenDataSharingLevel.MUTUAL_SCREENS);

    assertTrue("screen3Expired should be expired", screens.contains(screen3Expired));
    assertTrue(
        "screen3Expired should be shared",
        screen3Expired.getDataSharingLevel() == ScreenDataSharingLevel.MUTUAL_SCREENS);

    assertFalse("screen4Default should not be expired", screens.contains(screen4Default));
    assertTrue(
        "screen4Default should not be shared",
        screen4Default.getDataSharingLevel() != ScreenDataSharingLevel.MUTUAL_SCREENS);

    assertFalse("screen1NotExpired should be expired", screens.contains(screen1NotExpired));
    assertTrue(
        "screen1NotExpired should not be shared",
        screen1NotExpired.getDataSharingLevel() != ScreenDataSharingLevel.MUTUAL_SCREENS);

    assertFalse(
        "screen5RnaiExpired should NOT be expired (unless setting the screenType=RNAi)",
        screens.contains(screen5RnaiExpired));
    assertTrue(
        "screen5RnaiExpired NOT should be shared",
        screen5RnaiExpired.getDataSharingLevel() != ScreenDataSharingLevel.MUTUAL_SCREENS);

    assertTrue(
        "screen9ExpiredMaxDatePassed should be expired",
        screens.contains(screen9ExpiredMaxDatePassed));
    assertTrue(
        "screen9ExpiredMaxDatePassed should be shared",
        screen9ExpiredMaxDatePassed.getDataSharingLevel() == ScreenDataSharingLevel.MUTUAL_SCREENS);

    // TODO: Do some checking of the activities as well.

    // RNAi
    results =
        screenDataSharingLevelUpdater.expireScreenDataSharingLevels(date, admin, ScreenType.RNAI);
    screen5RnaiExpired = genericEntityDao.reloadEntity(screen5RnaiExpired);
    assertFalse(results.isEmpty());
    assertTrue("should only return the 1 RNAI screen: " + results, results.size() == 1);
    assertTrue(
        "screen5RnaiExpired should be expired (setting the screenType=RNAi)",
        results.get(0).getFirst().equals((screen5RnaiExpired)));
    assertTrue(
        "screen5RnaiExpired should be shared",
        screen5RnaiExpired.getDataSharingLevel() == ScreenDataSharingLevel.MUTUAL_SCREENS);
  }
  public void testAdjustDataPrivacyExpirationByActivities() throws Exception {
    initializeData();

    // as of [#2175] expiration services to become notification services only
    // the DPED will be set separately from the ScreenResultImport (the old way).
    // It will now be set by batch process, SCREEN_ACTIVITY_DATA_PRIVACY_EXPIRATION_AGE_DAYS from
    // the last ScreeningActivity date for a screen:
    // Meaning:
    // DPED will be set in one step (this test)
    // DPED will be used in separate step to find and expire screens (next test)

    // 1. create some screens
    Screen screen1NoActivities = createScreen("SDSL TEST1");
    Screen screen2HasActivity = createScreen("SDSL TEST2");
    Screen screen5TransferredHasActivity = createScreen("SDSL TEST5");
    screen5TransferredHasActivity.createStatusItem(
        new LocalDate(), ScreenStatus.TRANSFERRED_TO_BROAD_INSTITUTE);
    Screen screen3RnaiHasActivity = createScreen("SDSL TEST RNAI", ScreenType.RNAI);
    Screen screen4HasNoLibraryScreeningActivity =
        createScreen("screen4HasNoLibraryScreeningActivity");
    Screen screen6MinMaxSet = createScreen("screen6MinMaxSet");

    // 2. add some activities
    LocalDate newActivityDate = new LocalDate();
    screen2HasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen2HasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen2HasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen2HasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    // create a screening for user provided plates
    LibraryScreening ls =
        screen2HasActivity.createLibraryScreening(
            admin, leadScreener, newActivityDate.plusDays(100));
    ls.setForExternalLibraryPlates(true);
    // create a non-library-screening activity too
    screen2HasActivity.createCherryPickRequest(admin, leadScreener, newActivityDate.plusDays(100));

    screen4HasNoLibraryScreeningActivity.createCherryPickRequest(
        admin, leadScreener, newActivityDate.plusDays(100));

    screen3RnaiHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);

    screen5TransferredHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen5TransferredHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen5TransferredHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen5TransferredHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    // create a screening for user provided plates
    ls =
        screen5TransferredHasActivity.createLibraryScreening(
            admin, leadScreener, newActivityDate.plusDays(100));
    ls.setForExternalLibraryPlates(true);
    // 3. add some results
    screen2HasActivity.createScreenResult();
    screen5TransferredHasActivity.createScreenResult();
    screen4HasNoLibraryScreeningActivity.createScreenResult();

    LocalDate maxAllowedDataPrivacyExpirationDate = newActivityDate.minusDays(10);
    LocalDate minAllowedDataPrivacyExpirationDate = newActivityDate.minusDays(20);
    screen6MinMaxSet.setMinAllowedDataPrivacyExpirationDate(minAllowedDataPrivacyExpirationDate);
    screen6MinMaxSet.setMaxAllowedDataPrivacyExpirationDate(maxAllowedDataPrivacyExpirationDate);
    screen6MinMaxSet.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen6MinMaxSet.createScreenResult();

    genericEntityDao.persistEntity(screen1NoActivities);
    genericEntityDao.persistEntity(screen2HasActivity);
    genericEntityDao.persistEntity(screen3RnaiHasActivity);
    genericEntityDao.persistEntity(screen4HasNoLibraryScreeningActivity);
    genericEntityDao.persistEntity(screen5TransferredHasActivity);
    genericEntityDao.persistEntity(screen6MinMaxSet);
    flushAndClear();

    // 4. find the ones with "old" activities (activity age >
    // SCREEN_ACTIVITY_DATA_PRIVACY_EXPIRATION_AGE_DAYS)
    int ageToExpireFromActivityDateInDays = 760;

    ScreenDataSharingLevelUpdater.DataPrivacyAdjustment adjustment =
        screenDataSharingLevelUpdater.adjustDataPrivacyExpirationByActivities(
            ageToExpireFromActivityDateInDays, admin, ScreenType.SMALL_MOLECULE);

    flushAndClear();

    screen1NoActivities = genericEntityDao.reloadEntity(screen1NoActivities);
    screen2HasActivity = genericEntityDao.reloadEntity(screen2HasActivity);
    screen3RnaiHasActivity = genericEntityDao.reloadEntity(screen3RnaiHasActivity);
    screen4HasNoLibraryScreeningActivity =
        genericEntityDao.reloadEntity(screen4HasNoLibraryScreeningActivity);
    screen5TransferredHasActivity = genericEntityDao.reloadEntity(screen5TransferredHasActivity);
    screen6MinMaxSet = genericEntityDao.reloadEntity(screen6MinMaxSet);

    boolean containsFirst = false,
        containsSecond = false,
        containsThird = false,
        containsFourth = false,
        containsFifth = false,
        containsSixth = false;
    for (Pair<Screen, AdministrativeActivity> result : adjustment.screensAdjusted) {
      log.info("entry: " + result.getSecond());
      if (result.getFirst().equals(screen1NoActivities)) containsFirst = true;
      if (result.getFirst().equals(screen2HasActivity)) containsSecond = true;
      if (result.getFirst().equals(screen3RnaiHasActivity)) containsThird = true;
      if (result.getFirst().equals(screen4HasNoLibraryScreeningActivity)) containsFourth = true;
      if (result.getFirst().equals(screen5TransferredHasActivity)) containsFifth = true;
      if (result.getFirst().equals(screen6MinMaxSet)) containsSixth = true;
    }

    // TODO: update test for this
    assertTrue(
        "no screens should not be allowed", adjustment.screenPrivacyAdjustmentNotAllowed.isEmpty());
    assertFalse(
        "the sixth screen should adjusted to allowed",
        adjustment.screensAdjustedToAllowed.isEmpty());

    assertFalse("screen1NoActivities should not have been adjusted", containsFirst);
    assertTrue("screen2HasActivity should have been adjusted", containsSecond);
    assertFalse("screen3RnaiHasActivity should not have been adjusted", containsThird);
    assertFalse(
        "screen4HasNoLibraryScreeningActivity should not have been adjusted", containsFourth);
    assertFalse("screen5TransferredHasActivity should not have been adjusted", containsFifth);
    assertFalse("screen6MinMaxSet should not have been adjusted", containsSixth);
    assertEquals(
        "new screen2HasActivity dataPrivacyExpirationDate() wrong: ",
        newActivityDate.plusDays(ageToExpireFromActivityDateInDays),
        screen2HasActivity.getDataPrivacyExpirationDate());
  }