Search in sources :

Example 1 with IdentityCacheConfigKey

use of org.wso2.carbon.identity.core.model.IdentityCacheConfigKey in project carbon-identity-framework by wso2.

the class IdentityUtilTest method testPopulateProperties.

@Test
public void testPopulateProperties() throws Exception {
    Map<String, Object> mockConfig = new HashMap<>();
    mockConfig.put("dummy", new Object());
    Map<IdentityEventListenerConfigKey, IdentityEventListenerConfig> mockedEventListenerConfig = new HashMap<>();
    IdentityEventListenerConfigKey configKey = new IdentityEventListenerConfigKey("type", "name");
    mockedEventListenerConfig.put(configKey, new IdentityEventListenerConfig("false", 0, configKey, null));
    Map<IdentityCacheConfigKey, IdentityCacheConfig> mockedCacheConfig = new HashMap<>();
    IdentityCacheConfigKey cacheConfigKey = new IdentityCacheConfigKey("manager", "key");
    mockedCacheConfig.put(cacheConfigKey, new IdentityCacheConfig(cacheConfigKey));
    Map<String, IdentityCookieConfig> mockedCookieConfig = new HashMap<>();
    mockedCookieConfig.put("cookie", new IdentityCookieConfig("cookieName"));
    when(mockConfigParser.getConfiguration()).thenReturn(mockConfig);
    when(IdentityConfigParser.getEventListenerConfiguration()).thenReturn(mockedEventListenerConfig);
    when(IdentityConfigParser.getIdentityCacheConfigurationHolder()).thenReturn(mockedCacheConfig);
    when(IdentityConfigParser.getIdentityCookieConfigurationHolder()).thenReturn(mockedCookieConfig);
    when(IdentityConfigParser.getInstance()).thenReturn(mockConfigParser);
    IdentityUtil.populateProperties();
    assertEquals(Whitebox.getField(IdentityUtil.class, "configuration").get(IdentityUtil.class), mockConfig, "Configuration is not set properly during config population");
    assertEquals(Whitebox.getField(IdentityUtil.class, "eventListenerConfiguration").get(IdentityUtil.class), mockedEventListenerConfig, "eventListenerConfiguration is not set properly during config population");
    assertEquals(IdentityUtil.getIdentityCookiesConfigurationHolder(), mockedCookieConfig, "cookieConfiguration is not set properly during config population");
    assertEquals(Whitebox.getField(IdentityUtil.class, "identityCacheConfigurationHolder").get(IdentityUtil.class), mockedCacheConfig, "identityCacheConfigurationHolder is not set properly during config population");
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IdentityEventListenerConfigKey(org.wso2.carbon.identity.core.model.IdentityEventListenerConfigKey) Matchers.anyString(org.mockito.Matchers.anyString) IdentityCacheConfig(org.wso2.carbon.identity.core.model.IdentityCacheConfig) IdentityCacheConfigKey(org.wso2.carbon.identity.core.model.IdentityCacheConfigKey) IdentityCookieConfig(org.wso2.carbon.identity.core.model.IdentityCookieConfig) IdentityEventListenerConfig(org.wso2.carbon.identity.core.model.IdentityEventListenerConfig) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with IdentityCacheConfigKey

use of org.wso2.carbon.identity.core.model.IdentityCacheConfigKey in project carbon-identity-framework by wso2.

the class IdentityUtilTest method getIdentityCacheConfigTestData.

@DataProvider
public Object[][] getIdentityCacheConfigTestData() {
    Map<IdentityCacheConfigKey, IdentityCacheConfig> mockedCacheConfig = new HashMap<>();
    IdentityCacheConfigKey cacheConfigKey1 = new IdentityCacheConfigKey("manager1", "key1");
    IdentityCacheConfig cacheConfig1 = new IdentityCacheConfig(cacheConfigKey1);
    mockedCacheConfig.put(cacheConfigKey1, cacheConfig1);
    IdentityCacheConfigKey cacheConfigKey2 = new IdentityCacheConfigKey("manager2", "");
    IdentityCacheConfig cacheConfig2 = new IdentityCacheConfig(cacheConfigKey1);
    mockedCacheConfig.put(cacheConfigKey2, cacheConfig2);
    Whitebox.setInternalState(IdentityUtil.class, "identityCacheConfigurationHolder", mockedCacheConfig);
    return new Object[][] { { "manager1", "key1", cacheConfig1 }, { "manager1", "", null }, { "manager2", "", cacheConfig2 }, { "manager1", "$__local__$.key1", cacheConfig1 } };
}
Also used : IdentityCacheConfig(org.wso2.carbon.identity.core.model.IdentityCacheConfig) IdentityCacheConfigKey(org.wso2.carbon.identity.core.model.IdentityCacheConfigKey) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataProvider(org.testng.annotations.DataProvider)

Example 3 with IdentityCacheConfigKey

use of org.wso2.carbon.identity.core.model.IdentityCacheConfigKey in project carbon-identity-framework by wso2.

the class IdentityUtil method getIdentityCacheConfig.

/**
 * This reads the &lt;CacheConfig&gt; configuration in identity.xml.
 * Since the name of the cache is different between the distributed mode and local mode,
 * that is specially handled.
 * <p>
 * When calling this method, only pass the cacheManagerName and cacheName parameters considering
 * how the names are set in a clustered environment i.e. without the CachingConstants.LOCAL_CACHE_PREFIX.
 */
public static IdentityCacheConfig getIdentityCacheConfig(String cacheManagerName, String cacheName) {
    IdentityCacheConfigKey configKey = new IdentityCacheConfigKey(cacheManagerName, cacheName);
    IdentityCacheConfig identityCacheConfig = identityCacheConfigurationHolder.get(configKey);
    if (identityCacheConfig == null && cacheName.startsWith(CachingConstants.LOCAL_CACHE_PREFIX)) {
        configKey = new IdentityCacheConfigKey(cacheManagerName, cacheName.replace(CachingConstants.LOCAL_CACHE_PREFIX, ""));
        identityCacheConfig = identityCacheConfigurationHolder.get(configKey);
    }
    return identityCacheConfig;
}
Also used : IdentityCacheConfig(org.wso2.carbon.identity.core.model.IdentityCacheConfig) IdentityCacheConfigKey(org.wso2.carbon.identity.core.model.IdentityCacheConfigKey)

Example 4 with IdentityCacheConfigKey

use of org.wso2.carbon.identity.core.model.IdentityCacheConfigKey in project carbon-identity-framework by wso2.

the class IdentityConfigParser method buildCacheConfig.

private void buildCacheConfig() {
    OMElement cacheConfig = this.getConfigElement(IdentityConstants.CACHE_CONFIG);
    if (cacheConfig != null) {
        Iterator<OMElement> cacheManagers = cacheConfig.getChildrenWithName(new QName(IdentityCoreConstants.IDENTITY_DEFAULT_NAMESPACE, IdentityConstants.CACHE_MANAGER));
        if (cacheManagers != null) {
            while (cacheManagers.hasNext()) {
                OMElement cacheManager = cacheManagers.next();
                String cacheManagerName = cacheManager.getAttributeValue(new QName(IdentityConstants.CACHE_MANAGER_NAME));
                if (StringUtils.isBlank(cacheManagerName)) {
                    throw IdentityRuntimeException.error("CacheManager name not defined correctly");
                }
                Iterator<OMElement> caches = cacheManager.getChildrenWithName(new QName(IdentityCoreConstants.IDENTITY_DEFAULT_NAMESPACE, IdentityConstants.CACHE));
                if (caches != null) {
                    while (caches.hasNext()) {
                        OMElement cache = caches.next();
                        String cacheName = cache.getAttributeValue(new QName(IdentityConstants.CACHE_NAME));
                        if (StringUtils.isBlank(cacheName)) {
                            throw IdentityRuntimeException.error("Cache name not defined correctly");
                        }
                        IdentityCacheConfigKey identityCacheConfigKey = new IdentityCacheConfigKey(cacheManagerName, cacheName);
                        IdentityCacheConfig identityCacheConfig = new IdentityCacheConfig(identityCacheConfigKey);
                        String enable = cache.getAttributeValue(new QName(IdentityConstants.CACHE_ENABLE));
                        if (StringUtils.isNotBlank(enable)) {
                            identityCacheConfig.setEnabled(Boolean.parseBoolean(enable));
                        }
                        String timeout = cache.getAttributeValue(new QName(IdentityConstants.CACHE_TIMEOUT));
                        if (StringUtils.isNotBlank(timeout)) {
                            identityCacheConfig.setTimeout(Integer.parseInt(timeout));
                        }
                        String capacity = cache.getAttributeValue(new QName(IdentityConstants.CACHE_CAPACITY));
                        if (StringUtils.isNotBlank(capacity)) {
                            identityCacheConfig.setCapacity(Integer.parseInt(capacity));
                        }
                        String isDistributedCache = cache.getAttributeValue(new QName(IS_DISTRIBUTED_CACHE));
                        if (StringUtils.isNotBlank(isDistributedCache)) {
                            identityCacheConfig.setDistributed(Boolean.parseBoolean(isDistributedCache));
                        }
                        String isTemporaryCache = cache.getAttributeValue(new QName(IS_TEMPORARY));
                        if (StringUtils.isNotBlank(isTemporaryCache)) {
                            identityCacheConfig.setTemporary(Boolean.parseBoolean(isTemporaryCache));
                        }
                        // Add the config to container
                        identityCacheConfigurationHolder.put(identityCacheConfigKey, identityCacheConfig);
                    }
                }
                IdentityCacheConfigKey spCacheKey = new IdentityCacheConfigKey(cacheManagerName, SERVICE_PROVIDER_CACHE);
                IdentityCacheConfig identityCacheConfig = identityCacheConfigurationHolder.get(spCacheKey);
                IdentityCacheConfigKey key = new IdentityCacheConfigKey(cacheManagerName, SERVICE_PROVIDER_AUTH_KEY_CACHE);
                identityCacheConfigurationHolder.putIfAbsent(key, identityCacheConfig);
                key = new IdentityCacheConfigKey(cacheManagerName, SERVICE_PROVIDER_ID_CACHE);
                identityCacheConfigurationHolder.putIfAbsent(key, identityCacheConfig);
            }
        }
    }
}
Also used : IdentityCacheConfig(org.wso2.carbon.identity.core.model.IdentityCacheConfig) IdentityCacheConfigKey(org.wso2.carbon.identity.core.model.IdentityCacheConfigKey) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement)

Aggregations

IdentityCacheConfig (org.wso2.carbon.identity.core.model.IdentityCacheConfig)4 IdentityCacheConfigKey (org.wso2.carbon.identity.core.model.IdentityCacheConfigKey)4 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 QName (javax.xml.namespace.QName)1 OMElement (org.apache.axiom.om.OMElement)1 Matchers.anyString (org.mockito.Matchers.anyString)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 DataProvider (org.testng.annotations.DataProvider)1 Test (org.testng.annotations.Test)1 IdentityCookieConfig (org.wso2.carbon.identity.core.model.IdentityCookieConfig)1 IdentityEventListenerConfig (org.wso2.carbon.identity.core.model.IdentityEventListenerConfig)1 IdentityEventListenerConfigKey (org.wso2.carbon.identity.core.model.IdentityEventListenerConfigKey)1