/**
   * 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 testFindExpired() throws Exception {
    initializeData();

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

    screen1NotExpired = createScreen("SDSL TEST1");
    screen1NotExpired.setDataPrivacyExpirationDate(date.plusYears(2));
    genericEntityDao.persistEntity(screen1NotExpired);

    screen2Expired = createScreen("SDSL TEST2");
    screen2Expired.createScreenResult();
    screen2Expired.setDataPrivacyExpirationDate(date);
    genericEntityDao.persistEntity(screen2Expired);

    screen3Expired = createScreen("SDSL TEST3");
    screen3Expired.createScreenResult();
    screen3Expired.setDataPrivacyExpirationDate(date.minusYears(1));
    genericEntityDao.persistEntity(screen3Expired);

    screen9ExpiredMaxDatePassed = createScreen("screen9ExpiredMaxDatePassed");
    screen9ExpiredMaxDatePassed.createScreenResult();
    screen9ExpiredMaxDatePassed.setMinAllowedDataPrivacyExpirationDate(date.minusYears(2));
    screen9ExpiredMaxDatePassed.setMaxAllowedDataPrivacyExpirationDate(date.minusYears(2));
    screen9ExpiredMaxDatePassed.setDataPrivacyExpirationDate(date.minusYears(2));
    genericEntityDao.persistEntity(screen9ExpiredMaxDatePassed);

    screen4Default = createScreen("SDSL TEST4");
    genericEntityDao.persistEntity(screen4Default);

    screen5RnaiExpired = createScreen("Test RNAI expired", ScreenType.RNAI);
    screen5RnaiExpired.setDataPrivacyExpirationDate(date.minusYears(1));
    screen5RnaiExpired.createScreenResult();
    genericEntityDao.persistEntity(screen5RnaiExpired);

    screen6ExpiredNoResults = createScreen("test expired no results", ScreenType.RNAI);
    screen6ExpiredNoResults.setDataPrivacyExpirationDate(date.minusYears(1));
    genericEntityDao.persistEntity(screen6ExpiredNoResults);

    screen7ExpiredDropped = createScreen("test expired dropped technical");
    screen7ExpiredDropped.setDataPrivacyExpirationDate(date.minusYears(1));
    screen7ExpiredDropped.createScreenResult();
    screen7ExpiredDropped.createStatusItem(new LocalDate(), ScreenStatus.DROPPED_TECHNICAL);
    genericEntityDao.persistEntity(screen7ExpiredDropped);

    screen8ExpiredTransferred = createScreen("test expired transferred");
    screen8ExpiredTransferred.setDataPrivacyExpirationDate(date.minusYears(1));
    screen8ExpiredTransferred.createScreenResult();
    screen8ExpiredTransferred.createStatusItem(
        new LocalDate(), ScreenStatus.TRANSFERRED_TO_BROAD_INSTITUTE);
    genericEntityDao.persistEntity(screen8ExpiredTransferred);

    flushAndClear();

    //    startNewTransaction();

    List<Screen> allScreens = genericEntityDao.findAllEntitiesOfType(Screen.class);
    for (Screen screen : allScreens) {
      log.info(
          "allScreens: "
              + screen.getTitle()
              + " , expires: "
              + screen.getDataPrivacyExpirationDate());
    }

    List<Screen> screens =
        screenDataSharingLevelUpdater.findNewExpiredNotNotified(date, ScreenType.SMALL_MOLECULE);
    for (Screen screen : screens) {
      log.info(
          "expiredScreens: "
              + screen.getTitle()
              + " , expires: "
              + screen.getDataPrivacyExpirationDate());
    }
    assertFalse("no expired screens", screens.isEmpty());

    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);

    assertTrue("screen2Expired should be expired: ", screens.contains(screen2Expired));
    assertTrue("screen3Expired should be expired: ", screens.contains(screen3Expired));
    assertTrue(
        "screen9ExpiredMaxDatePassed should be expired: ",
        screens.contains(screen9ExpiredMaxDatePassed));

    assertFalse("screen4Default should not be expired", screens.contains(screen4Default));
    assertFalse("screen1NotExpired should not be expired", screens.contains(screen1NotExpired));
    assertFalse(
        "screen5RnaiExpired should NOT be expired (unless setting screenType=RNAi)",
        screens.contains(screen5RnaiExpired));
    assertFalse(
        "screen6ExpiredNoResults should not be expired", screens.contains(screen6ExpiredNoResults));
    assertFalse(
        "screen7ExpiredDropped should not be expired", screens.contains(screen7ExpiredDropped));
    assertFalse(
        "screen8ExpiredTransferred should not be expired",
        screens.contains(screen8ExpiredTransferred));

    // just a little test here to see if we get a null or an empty list, find none
    screens =
        screenDataSharingLevelUpdater.findNewExpiredNotNotified(
            date.minusYears(20), ScreenType.SMALL_MOLECULE);
    log.info("empty result: " + screens);
    assertNotNull(screens);
    assertTrue(screens.isEmpty());

    screens = screenDataSharingLevelUpdater.findNewExpiredNotNotified(date, ScreenType.RNAI);
    assertFalse("no expired screens", screens.isEmpty());
    assertTrue(
        "only the RNAi screen should be returned (no Small Molecule): " + screens,
        screens.size() == 1);
    assertTrue(
        "screen5RnaiExpired should NOT be expired (unless setting screenType=RNAi)",
        screens.contains(screen5RnaiExpired));
  }