Search in sources :

Example 1 with IKeyResolver

use of com.microsoft.azure.keyvault.core.IKeyResolver in project azure-sdk-for-java by Azure.

the class CachingKeyResolverTest method KeyVault_CachingKeyResolverThrows.

/* 
     * Tests the behavior of CachingKeyResolver when resolving key throws 
     * and validate that the failed entity is not added to the cache. 
     */
@Test
public void KeyVault_CachingKeyResolverThrows() {
    IKeyResolver mockedKeyResolver = mock(IKeyResolver.class);
    CachingKeyResolver resolver = new CachingKeyResolver(10, mockedKeyResolver);
    // First throw exception and for the second call return a value
    when(mockedKeyResolver.resolveKeyAsync(keyId)).thenThrow(new RuntimeException("test")).thenReturn(ikeyAsync);
    try {
        resolver.resolveKeyAsync(keyId);
        assertFalse("Should have thrown an exception.", true);
    } catch (UncheckedExecutionException e) {
        assertTrue("RuntimeException is expected.", e.getCause() instanceof RuntimeException);
    }
    resolver.resolveKeyAsync(keyId);
    resolver.resolveKeyAsync(keyId);
    verify(mockedKeyResolver, times(2)).resolveKeyAsync(keyId);
}
Also used : IKeyResolver(com.microsoft.azure.keyvault.core.IKeyResolver) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) CachingKeyResolver(com.microsoft.azure.keyvault.extensions.CachingKeyResolver) Test(org.junit.Test)

Example 2 with IKeyResolver

use of com.microsoft.azure.keyvault.core.IKeyResolver in project azure-sdk-for-java by Azure.

the class CachingKeyResolverTest method KeyVault_CapacityLimitOfCachingKeyResolver.

/* 
     * Tests the capacity limit of CachingKeyResolver by adding more keys
     * than the cache limit and verifying that least recently used entity is evicted.
     */
@Test
public void KeyVault_CapacityLimitOfCachingKeyResolver() {
    IKeyResolver mockedKeyResolver = mock(IKeyResolver.class);
    CachingKeyResolver resolver = new CachingKeyResolver(2, mockedKeyResolver);
    when(mockedKeyResolver.resolveKeyAsync(keyId)).thenReturn(ikeyAsync);
    when(mockedKeyResolver.resolveKeyAsync(keyId2)).thenReturn(ikeyAsync);
    when(mockedKeyResolver.resolveKeyAsync(keyId3)).thenReturn(ikeyAsync);
    resolver.resolveKeyAsync(keyId);
    resolver.resolveKeyAsync(keyId2);
    resolver.resolveKeyAsync(keyId3);
    resolver.resolveKeyAsync(keyId2);
    resolver.resolveKeyAsync(keyId3);
    resolver.resolveKeyAsync(keyId);
    resolver.resolveKeyAsync(keyId3);
    verify(mockedKeyResolver, times(1)).resolveKeyAsync(keyId2);
    verify(mockedKeyResolver, times(1)).resolveKeyAsync(keyId3);
    verify(mockedKeyResolver, times(2)).resolveKeyAsync(keyId);
}
Also used : IKeyResolver(com.microsoft.azure.keyvault.core.IKeyResolver) CachingKeyResolver(com.microsoft.azure.keyvault.extensions.CachingKeyResolver) Test(org.junit.Test)

Aggregations

IKeyResolver (com.microsoft.azure.keyvault.core.IKeyResolver)2 CachingKeyResolver (com.microsoft.azure.keyvault.extensions.CachingKeyResolver)2 Test (org.junit.Test)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1