TimeToLive implements
    Computable { @Override public long compute() { final long daysDiff = daysBetweenDateBeforeTodayAndNow( dateBeforeToday.toInstant() ); final long daysMultiplier = daysDiff > 0 ? daysDiff : DEFAULT_MULTIPLIER; return ttlFactor * daysMultiplier; } TimeToLive( final Date dateBeforeToday, final int ttlFactor ); @Override long compute();  }
@Test( expected = IllegalArgumentException.class ) public void testComputeForCurrentDayWhenCacheFactorIsNegative() { final Integer aNegativeCachingFactor = -1; final Date endingDate = new Date(); new TimeToLive( endingDate, aNegativeCachingFactor ).compute(); fail( "IllegalArgumentException was expected." ); }
@Test public void testComputeForZeroDayDiffWhenCacheFactorIsPositive() { final Integer aPositiveCachingFactor = 3; final Date endingDate = new Date(); final long expectedTtl = DEFAULT_MULTIPLIER * aPositiveCachingFactor; when( systemSettingManager.getSystemSetting(ANALYTICS_CACHE_PROGRESSIVE_TTL_FACTOR) ) .thenReturn( aPositiveCachingFactor ); final long actualTtl = new TimeToLive( endingDate, aPositiveCachingFactor ).compute(); assertThat( actualTtl, is( equalTo( expectedTtl ) ) ); }
@Test public void testComputeForOneDayBeforeWhenCacheFactorIsPositive() { final int oneDayDiff = 1; final Integer aPositiveCachingFactor = 2; final Date endingDate = calculateDateFrom( new Date(), minus( oneDayDiff ), DATE ); final long expectedTtl = aPositiveCachingFactor * oneDayDiff; final long actualTtl = new TimeToLive( endingDate, aPositiveCachingFactor ).compute(); assertThat( actualTtl, is( equalTo( expectedTtl ) ) ); }
@Test public void testComputeEndingDateIsAheadOfNowAndCacheFactorIsPositive() { final int tenDaysAhead = 10; final Integer aPositiveCachingFactor = 1; final Date beginningDate = new Date(); final Date endingDate = calculateDateFrom( beginningDate, plus( tenDaysAhead ), DATE ); final long expectedTtl = DEFAULT_MULTIPLIER * aPositiveCachingFactor; when( systemSettingManager.getSystemSetting(ANALYTICS_CACHE_PROGRESSIVE_TTL_FACTOR) ) .thenReturn( aPositiveCachingFactor ); final long actualTtl = new TimeToLive( endingDate, aPositiveCachingFactor ).compute(); assertThat( actualTtl, is( equalTo( expectedTtl ) ) ); }
@Test public void testComputeEndingDateIsTenDaysBeforeNowAndCacheFactorIsPositive() { final int tenDays = 10; final Integer aPositiveCachingFactor = 2; final Date now = new Date(); final Date endingDate = calculateDateFrom( now, minus( tenDays ), DATE ); final long expectedTtl = aPositiveCachingFactor * tenDays; when( systemSettingManager.getSystemSetting(ANALYTICS_CACHE_PROGRESSIVE_TTL_FACTOR) ) .thenReturn( aPositiveCachingFactor ); final long actualTtl = new TimeToLive( endingDate, aPositiveCachingFactor ).compute(); assertThat( actualTtl, is( equalTo( expectedTtl ) ) ); }