@Test
public void testCurrentKey() throws Exception {

  KeyProvider.KeyVersion mockKey = Mockito.mock(KeyProvider.KeyVersion.class);
  KeyProvider mockProv = Mockito.mock(KeyProvider.class);
  Mockito.when(mockProv.getCurrentKey(Mockito.eq("k1"))).thenReturn(mockKey);
  Mockito.when(mockProv.getCurrentKey(Mockito.eq("k2"))).thenReturn(null);
  KeyProvider cache = new KMSCacheKeyProvider(mockProv, 100);
  // asserting caching
  Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));
  Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1"));
  Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));
  Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1"));
  Thread.sleep(1200);
  Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));
  Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k1"));
  // asserting no caching when key is not known
  cache = new KMSCacheKeyProvider(mockProv, 100);
  Assert.assertEquals(null, cache.getCurrentKey("k2"));
  Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k2"));
  Assert.assertEquals(null, cache.getCurrentKey("k2"));
  Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k2"));
}