Search in sources :

Example 1 with DEFAULT_DIALECT_URI

use of org.wso2.carbon.apimgt.impl.token.ClaimsRetriever.DEFAULT_DIALECT_URI in project carbon-apimgt by wso2.

the class APIProviderImplTest method testGetSubscriberClaims.

@Test
public void testGetSubscriberClaims() throws APIManagementException, UserStoreException, XMLStreamException {
    String configuredClaims = "http://wso2.org/claim1,http://wso2.org/claim2";
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    // Mock retrieving the tenant domain
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.mockStatic(APIUtil.class);
    Mockito.when(MultitenantUtils.getTenantDomain("admin")).thenReturn("carbon.super");
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
    UserStoreManager userStoreManager = Mockito.mock(UserStoreManager.class);
    UserRealm userRealm = Mockito.mock(UserRealm.class);
    PowerMockito.when(realmService.getTenantUserRealm(-1234)).thenReturn(userRealm);
    PowerMockito.when(userRealm.getUserStoreManager()).thenReturn(userStoreManager);
    PowerMockito.when(userStoreManager.isExistingUser("admin")).thenReturn(true);
    SortedMap<String, String> claimValues = new TreeMap<String, String>();
    claimValues.put("claim1", "http://wso2.org/claim1");
    claimValues.put("claim2", "http://wso2.org/claim2");
    claimValues.put("claim3", "http://wso2.org/claim3");
    PowerMockito.when(APIUtil.getClaims("admin", -1234, DEFAULT_DIALECT_URI)).thenReturn(claimValues);
    APIManagerConfiguration configuration = Mockito.mock(APIManagerConfiguration.class);
    APIManagerConfigurationService configurationService = Mockito.mock(APIManagerConfigurationService.class);
    PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(configurationService);
    PowerMockito.when(configurationService.getAPIManagerConfiguration()).thenReturn(configuration);
    Mockito.when(configuration.getFirstProperty(APIConstants.API_PUBLISHER_SUBSCRIBER_CLAIMS)).thenReturn(configuredClaims);
    Map subscriberClaims = apiProvider.getSubscriberClaims("admin");
    assertNotNull(subscriberClaims);
    assertEquals(configuredClaims.split(",").length, subscriberClaims.size());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) UserRealm(org.wso2.carbon.user.api.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreManager(org.wso2.carbon.user.core.UserStoreManager) TreeMap(java.util.TreeMap) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with DEFAULT_DIALECT_URI

use of org.wso2.carbon.apimgt.impl.token.ClaimsRetriever.DEFAULT_DIALECT_URI in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testAddSubscriber.

@Test
public void testAddSubscriber() throws APIManagementException, org.wso2.carbon.user.api.UserStoreException {
    int tenantId = -1234;
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(null, null, null, tenantManager, apiMgtDAO);
    Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(tenantId);
    PowerMockito.mockStatic(APIUtil.class);
    SortedMap<String, String> claimValues = new TreeMap<String, String>();
    claimValues.put("admin@wso2.om", APIConstants.EMAIL_CLAIM);
    PowerMockito.when(APIUtil.getClaims(API_PROVIDER, tenantId, DEFAULT_DIALECT_URI)).thenReturn(claimValues);
    try {
        abstractAPIManager.addSubscriber(API_PROVIDER, SAMPLE_RESOURCE_ID);
        Assert.fail("User store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while adding the subscriber"));
    }
    Mockito.doThrow(APIManagementException.class).doNothing().when(apiMgtDAO).addSubscriber((Subscriber) Mockito.any(), Mockito.anyString());
    try {
        abstractAPIManager.addSubscriber(API_PROVIDER, SAMPLE_RESOURCE_ID);
        Assert.fail("APIM exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while adding the subscriber"));
    }
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.when(APIUtil.isEnabledUnlimitedTier()).thenReturn(true, false);
    Mockito.doNothing().when(apiMgtDAO).addSubscriber((Subscriber) Mockito.any(), Mockito.anyString());
    abstractAPIManager.addSubscriber(API_PROVIDER, SAMPLE_RESOURCE_ID);
    List<Tier> tierValues = new ArrayList<Tier>();
    tierValues.add(new Tier("Gold"));
    tierValues.add(new Tier("Silver"));
    Map<String, Tier> tierMap = new HashMap<String, Tier>();
    tierMap.put("Gold", new Tier("Gold"));
    tierMap.put("Silver", new Tier("Silver"));
    PowerMockito.when(APIUtil.getTiers(Mockito.anyInt(), Mockito.anyString())).thenReturn(tierMap);
    PowerMockito.when(APIUtil.sortTiers(Mockito.anySet())).thenReturn(tierValues);
    abstractAPIManager.addSubscriber(API_PROVIDER, SAMPLE_RESOURCE_ID);
    Mockito.verify(apiMgtDAO, Mockito.times(3)).addSubscriber((Subscriber) Mockito.any(), Mockito.anyString());
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with DEFAULT_DIALECT_URI

use of org.wso2.carbon.apimgt.impl.token.ClaimsRetriever.DEFAULT_DIALECT_URI in project carbon-apimgt by wso2.

the class DefaultClaimsRetrieverTestCase method testGetClaimsWhenCacheEmpty.

@Test
public void testGetClaimsWhenCacheEmpty() throws Exception {
    DefaultClaimsRetriever defaultClaimsRetriever = new DefaultClaimsRetriever();
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.JWT_CLAIM_CACHE_EXPIRY)).thenReturn(null);
    Cache cache = Mockito.mock(Cache.class);
    Mockito.when(cacheManager.getCache(APIConstants.CLAIMS_APIM_CACHE)).thenReturn(cache);
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.when(APIUtil.getTenantId(USER_NAME)).thenReturn(TENANT_ID);
    Claim claim1 = new Claim();
    claim1.setClaimUri("http://wso2.org/claim1");
    Claim claim2 = new Claim();
    claim2.setClaimUri("http://wso2.com/claim2");
    SortedMap<String, String> claimValues = new TreeMap<String, String>();
    claimValues.put("claim1", "http://wso2.org/claim1");
    claimValues.put("claim2", "http://wso2.org/claim2");
    PowerMockito.when(APIUtil.getClaims(USER_NAME, TENANT_ID, DEFAULT_DIALECT_URI)).thenReturn(claimValues);
    SortedMap<String, String> claims = defaultClaimsRetriever.getClaims(USER_NAME);
    Assert.assertNotNull(claims);
    Assert.assertEquals(claimValues, claims);
}
Also used : TreeMap(java.util.TreeMap) Claim(org.wso2.carbon.user.api.Claim) Cache(javax.cache.Cache) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with DEFAULT_DIALECT_URI

use of org.wso2.carbon.apimgt.impl.token.ClaimsRetriever.DEFAULT_DIALECT_URI in project carbon-apimgt by wso2.

the class APIRealmUtilTest method testLoggedInUserClaims.

@Test
public void testLoggedInUserClaims() throws APIManagementException, UserStoreException, XMLStreamException {
    String configuredClaims = "http://wso2.org/claim1,http://wso2.org/claim2,http://wso2.org/claim3";
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.mockStatic(APIUtil.class);
    Mockito.when(MultitenantUtils.getTenantDomain("admin")).thenReturn("carbon.super");
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
    SortedMap<String, String> claimValues = new TreeMap<String, String>();
    claimValues.put("claim1", "http://wso2.org/claim1");
    claimValues.put("claim2", "http://wso2.org/claim2");
    claimValues.put("claim3", "http://wso2.org/claim3");
    PowerMockito.when(APIUtil.getTenantId("carbon.super")).thenReturn(-1234);
    PowerMockito.when(APIUtil.getClaims("admin", -1234, DEFAULT_DIALECT_URI)).thenReturn(claimValues);
    Map loggedInUserClaims = APIRealmUtils.getUserClaims("admin");
    assertNotNull(loggedInUserClaims);
    assertEquals(configuredClaims.split(",").length, loggedInUserClaims.size());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) RealmService(org.wso2.carbon.user.core.service.RealmService) TreeMap(java.util.TreeMap) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

TreeMap (java.util.TreeMap)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SortedMap (java.util.SortedMap)2 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)2 RealmService (org.wso2.carbon.user.core.service.RealmService)2 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)2 ArrayList (java.util.ArrayList)1 Cache (javax.cache.Cache)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 Tier (org.wso2.carbon.apimgt.api.model.Tier)1 Claim (org.wso2.carbon.user.api.Claim)1 UserRealm (org.wso2.carbon.user.api.UserRealm)1 UserStoreException (org.wso2.carbon.user.core.UserStoreException)1 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)1