@Test
  public void shouldNotSetTimerIfNoVersionLifeIsMentioned() {
    systemEnv.put(TLB_VERSION_LIFE_IN_DAYS.key, "-1");

    final EntryRepoFactory repoFactory = mock(EntryRepoFactory.class);
    final Timer timer =
        new Timer() {
          @Override
          public void schedule(TimerTask task, long delay, long period) {
            if (task instanceof TlbServerInitializer.Purge) {
              fail("Should not have scheduled anything!");
            }
          }
        };

    new TlbServerInitializer(new SystemEnvironment(systemEnv), timer) {
      @Override
      EntryRepoFactory repoFactory() {
        return repoFactory;
      }
    }.init();

    verify(repoFactory).registerExitHook();
    verifyNoMoreInteractions(repoFactory);
  }
 @Test
 public void shouldHonorDiskStorageRootOverride() throws IOException, ClassNotFoundException {
   File tmpDir = TestUtil.createTmpDir();
   try {
     String tmpDirPath = tmpDir.getAbsolutePath();
     systemEnv.put(TlbConstants.Server.TLB_DATA_DIR.key, tmpDirPath);
     initializer = new TlbServerInitializer(new SystemEnvironment(systemEnv));
     EntryRepoFactory factory = initializer.repoFactory();
     File file =
         new File(
             tmpDirPath,
             new EntryRepoFactory.VersionedNamespace(LATEST_VERSION, SUBSET_SIZE)
                 .getIdUnder("quux"));
     file.deleteOnExit();
     writeEntriedTo(file);
     SubsetSizeRepo repo = factory.createSubsetRepo("quux", LATEST_VERSION);
     assertThat(
         (List<SubsetSizeEntry>) repo.list(),
         is(
             Arrays.asList(
                 new SubsetSizeEntry(1), new SubsetSizeEntry(2), new SubsetSizeEntry(3))));
   } finally {
     FileUtils.deleteQuietly(tmpDir);
   }
 }
  @Test
  public void shouldSetTimerToPurgeOldVersions() {
    final TimerTask[] tasks = new TimerTask[1];
    final EntryRepoFactory repoFactory = mock(EntryRepoFactory.class);
    final Timer timer =
        new Timer() {
          @Override
          public void schedule(TimerTask task, long delay, long period) {
            if (task instanceof TlbServerInitializer.Purge) {
              tasks[0] = task;
              assertThat(delay, is(0l));
              assertThat(period, is(TlbServerInitializer.ONCE_A_DAY));
            }
          }
        };

    new TlbServerInitializer(new SystemEnvironment(systemEnv), timer) {
      @Override
      EntryRepoFactory repoFactory() {
        return repoFactory;
      }
    }.init();

    verify(repoFactory).registerExitHook();
    verifyNoMoreInteractions(repoFactory);

    tasks[0].run();
    verify(repoFactory).purgeVersionsOlderThan(7);
    verifyNoMoreInteractions(repoFactory);

    final EntryRepoFactory anotherRepoFactory = mock(EntryRepoFactory.class);

    systemEnv.put(TLB_VERSION_LIFE_IN_DAYS.key, "3");

    new TlbServerInitializer(new SystemEnvironment(systemEnv), timer) {
      @Override
      EntryRepoFactory repoFactory() {
        return anotherRepoFactory;
      }
    }.init();

    verify(repoFactory).registerExitHook();
    verifyNoMoreInteractions(repoFactory);

    tasks[0].run();
    verify(anotherRepoFactory).purgeVersionsOlderThan(3);
    verifyNoMoreInteractions(repoFactory);
  }
 @Test
 public void shouldInitializeTlbToRunOnConfiguredPort() {
   systemEnv.put(TlbConstants.Server.TLB_SERVER_PORT.key, "1234");
   assertThat(new TlbServerInitializer(new SystemEnvironment(systemEnv)).appPort(), is(1234));
 }
 private SystemEnvironment env() {
   final HashMap<String, String> env = new HashMap<String, String>();
   env.put(TlbConstants.Server.TLB_DATA_DIR.key, baseDir.getAbsolutePath());
   return new SystemEnvironment(env);
 }