Search in sources :

Example 1 with CacheBuilder

use of javax.cache.CacheBuilder in project carbon-apimgt by wso2.

the class DefaultClaimsRetrieverTestCase method testGetClaimsWhenCacheNonEmpty.

@Test
public void testGetClaimsWhenCacheNonEmpty() throws Exception {
    DefaultClaimsRetriever defaultClaimsRetriever = new DefaultClaimsRetriever();
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.JWT_CLAIM_CACHE_EXPIRY)).thenReturn("3600");
    CacheBuilder cacheBuilder = Mockito.mock(CacheBuilder.class);
    Mockito.when(cacheManager.createCacheBuilder(APIConstants.CLAIMS_APIM_CACHE)).thenReturn(cacheBuilder);
    Cache cache = Mockito.mock(Cache.class);
    Mockito.when(cacheBuilder.setStoreByValue(false)).thenReturn(cacheBuilder);
    Mockito.when(cacheBuilder.setExpiry(Matchers.any(CacheConfiguration.ExpiryType.class), Matchers.any(CacheConfiguration.Duration.class))).thenReturn(cacheBuilder);
    Mockito.when(cacheBuilder.build()).thenReturn(cache);
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.when(APIUtil.getTenantId(USER_NAME)).thenReturn(TENANT_ID);
    SortedMap<String, String> claimValues = new TreeMap<String, String>();
    claimValues.put("claim1", "http://wso2.org/claim1");
    claimValues.put("claim2", "http://wso2.org/claim2");
    UserClaims userClaims = new UserClaims(claimValues);
    Mockito.when(cache.get(Matchers.any(ClaimCacheKey.class))).thenReturn(userClaims);
    SortedMap<String, String> claims = defaultClaimsRetriever.getClaims(USER_NAME);
    Assert.assertNotNull(claims);
    Assert.assertEquals(claimValues, claims);
}
Also used : UserClaims(org.wso2.carbon.apimgt.impl.utils.UserClaims) ClaimCacheKey(org.wso2.carbon.apimgt.impl.utils.ClaimCacheKey) CacheBuilder(javax.cache.CacheBuilder) TreeMap(java.util.TreeMap) Cache(javax.cache.Cache) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

TreeMap (java.util.TreeMap)1 Cache (javax.cache.Cache)1 CacheBuilder (javax.cache.CacheBuilder)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 ClaimCacheKey (org.wso2.carbon.apimgt.impl.utils.ClaimCacheKey)1 UserClaims (org.wso2.carbon.apimgt.impl.utils.UserClaims)1