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());
}
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());
}
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);
}
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());
}
Aggregations