/**
   * If this test is run by itself, it leaves the database in a state that is convenient for testing
   * from the command line.
   */
  @Rollback(value = false)
  public void testThatSetsUpForCommandLineTest() throws Exception {
    initializeData();

    int daysToNofify = 60;
    int daysToExpire =
        790; // 2 years, 2months  (technically, a month is variable, using 30 days for all
    // calculations)
    LocalDate today = new LocalDate();
    LocalDate activityDateToExpireToday = today.minusDays(daysToExpire);
    LocalDate activityDateToNotifyToday = today.minusDays(daysToExpire).plusDays(daysToNofify);

    Screen screen1ToExpireTodayAfterAdjust =
        createScreen("is testing for expiration (760 days after lab activity) today");
    screen1ToExpireTodayAfterAdjust.createScreenResult();
    screen1ToExpireTodayAfterAdjust.createLibraryScreening(
        admin, leadScreener, activityDateToExpireToday);

    Screen screen2ToNotifyTodayAfterAdjust =
        createScreen("is testing for notification (60 days before expiration) today");
    screen2ToNotifyTodayAfterAdjust.createScreenResult();
    screen2ToNotifyTodayAfterAdjust.createLibraryScreening(
        admin, leadScreener, activityDateToNotifyToday);

    Screen screen3AjustementOverridden =
        createScreen("is testing for max/min DPED override - Adjustment to the allowed value");
    screen3AjustementOverridden.createScreenResult();
    screen3AjustementOverridden.createLibraryScreening(
        admin, leadScreener, activityDateToExpireToday);
    screen3AjustementOverridden.setDataPrivacyExpirationDate(today.plusDays(2));
    screen3AjustementOverridden.setMinAllowedDataPrivacyExpirationDate(today.plusDays(1));

    Screen screen3aAjustementOverridden =
        createScreen("is testing for max/min DPED override - adjustment not allowed - overridden");
    screen3aAjustementOverridden.createScreenResult();
    screen3aAjustementOverridden.createLibraryScreening(
        admin, leadScreener, activityDateToExpireToday);
    screen3aAjustementOverridden.setMinAllowedDataPrivacyExpirationDate(today.plusDays(1));
    screen3aAjustementOverridden.setDataPrivacyExpirationDate(today.plusDays(1));

    Screen screen4Published = createScreen("is testing a published Screen");
    screen4Published.createScreenResult();
    screen4Published.createLibraryScreening(admin, leadScreener, activityDateToNotifyToday);
    screen4Published.addPublication(new Publication());

    genericEntityDao.persistEntity(screen1ToExpireTodayAfterAdjust);
    genericEntityDao.persistEntity(screen2ToNotifyTodayAfterAdjust);
    genericEntityDao.persistEntity(screen3AjustementOverridden);
    genericEntityDao.persistEntity(screen3aAjustementOverridden);
    genericEntityDao.persistEntity(screen4Published);
    genericEntityDao.flush();
  }
  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());
  }