@Test
public void testKeyVersion() throws Exception {

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