### Question:
EphemeralKeyManager { static boolean shouldRefreshKey( @Nullable EphemeralKey key, long bufferInSeconds, @Nullable Calendar proxyCalendar) { if (key == null) { return true; } Calendar now = proxyCalendar == null ? Calendar.getInstance() : proxyCalendar; long nowInSeconds = TimeUnit.MILLISECONDS.toSeconds(now.getTimeInMillis()); long nowPlusBuffer = nowInSeconds + bufferInSeconds; return key.getExpires() < nowPlusBuffer; } EphemeralKeyManager(
            @NonNull EphemeralKeyProvider ephemeralKeyProvider,
            @NonNull KeyManagerListener keyManagerListener,
            long timeBufferInSeconds,
            @Nullable Calendar overrideCalendar);   }### Answer:
@Test public void shouldRefreshKey_whenKeyIsNullAndTimeIsInFuture_returnsTrue() { Calendar futureCalendar = Calendar.getInstance(); futureCalendar.add(Calendar.YEAR, 1); futureCalendar.getTimeInMillis(); assertTrue(EphemeralKeyManager.shouldRefreshKey(null, TEST_SECONDS_BUFFER, futureCalendar)); }
@Test public void shouldRefreshKey_whenKeyIsNullAndTimeIsInPast_returnsTrue() { Calendar pastCalendar = Calendar.getInstance(); pastCalendar.add(Calendar.YEAR, -1); pastCalendar.getTimeInMillis(); assertTrue(EphemeralKeyManager.shouldRefreshKey(null, TEST_SECONDS_BUFFER, pastCalendar)); }
@Test public void shouldRefreshKey_whenKeyExpiryIsAfterBufferFromPresent_returnsFalse() { Calendar fixedCalendar = Calendar.getInstance(); EphemeralKey key = EphemeralKey.fromString(FIRST_SAMPLE_KEY_RAW); assertNotNull(key); long expiryTimeInSeconds = key.getExpires(); long sufficientBufferTime = 2*TEST_SECONDS_BUFFER; key.setExpires(expiryTimeInSeconds + sufficientBufferTime); long expiryTimeInMilliseconds = TimeUnit.SECONDS.toMillis(expiryTimeInSeconds); fixedCalendar.setTimeInMillis(expiryTimeInMilliseconds); assertEquals(expiryTimeInMilliseconds, fixedCalendar.getTimeInMillis()); assertFalse(EphemeralKeyManager.shouldRefreshKey(key, TEST_SECONDS_BUFFER, fixedCalendar)); }
@Test public void shouldRefreshKey_whenKeyExpiryIsInThePast_returnsTrue() { Calendar fixedCalendar = Calendar.getInstance(); EphemeralKey key = EphemeralKey.fromString(FIRST_SAMPLE_KEY_RAW); assertNotNull(key); long currentTimeInMillis = fixedCalendar.getTimeInMillis(); long timeAgoInMillis = currentTimeInMillis - 100L; key.setExpires(TimeUnit.MILLISECONDS.toSeconds(timeAgoInMillis)); assertTrue(EphemeralKeyManager.shouldRefreshKey(key, TEST_SECONDS_BUFFER, fixedCalendar)); }
@Test public void shouldRefreshKey_whenKeyExpiryIsInFutureButWithinBuffer_returnsTrue() { Calendar fixedCalendar = Calendar.getInstance(); EphemeralKey key = EphemeralKey.fromString(FIRST_SAMPLE_KEY_RAW); assertNotNull(key); long parsedExpiryTimeInMillis = TimeUnit.SECONDS.toMillis(key.getExpires()); long bufferTimeInMillis = TimeUnit.SECONDS.toMillis(TEST_SECONDS_BUFFER); long notFarEnoughInTheFuture = parsedExpiryTimeInMillis + bufferTimeInMillis / 2; fixedCalendar.setTimeInMillis(notFarEnoughInTheFuture); assertEquals(notFarEnoughInTheFuture, fixedCalendar.getTimeInMillis()); assertTrue(EphemeralKeyManager.shouldRefreshKey(key, TEST_SECONDS_BUFFER, fixedCalendar)); }