Search in sources :

Example 31 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-apimgt by wso2.

the class APIUtilTierTest method testGetTiersFromApiPolicies.

@Test
public void testGetTiersFromApiPolicies() throws Exception {
    System.setProperty("carbon.home", APIUtilRolesTest.class.getResource("/").getFile());
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("abc.com");
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(1);
        String policyLevel = PolicyConstants.POLICY_LEVEL_API;
        int tenantId = 1;
        ApiMgtDAOMockCreator daoMockHolder = new ApiMgtDAOMockCreator(tenantId);
        ApiMgtDAO apiMgtDAO = daoMockHolder.getMock();
        PowerMockito.mockStatic(ServiceReferenceHolder.class);
        ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
        RealmService realmService = Mockito.mock(RealmService.class);
        TenantManager tenantManager = Mockito.mock(TenantManager.class);
        Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
        Mockito.when(tenantManager.getDomain(1)).thenReturn("abc.com");
        Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
        Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
        APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
        Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
        Mockito.when(apimConfigService.getTenantConfig("abc.com")).thenReturn(IOUtils.toString(tenantConf));
        APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
        APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
        Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
        Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
        ThrottleProperties throttleProperties = new ThrottleProperties();
        throttleProperties.setEnableUnlimitedTier(true);
        Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
        APIPolicy[] policies = generateApiPolicies(tiersReturned);
        Mockito.when(apiMgtDAO.getAPIPolicies(tenantId)).thenReturn(policies);
        Map<String, Tier> tiersFromPolicies = APIUtil.getTiersFromPolicies(policyLevel, tenantId);
        Mockito.verify(apiMgtDAO, Mockito.only()).getAPIPolicies(tenantId);
        for (APIPolicy policy : policies) {
            Tier tier = tiersFromPolicies.get(policy.getPolicyName());
            Assert.assertNotNull(tier);
            Assert.assertEquals(policy.getPolicyName(), tier.getName());
            Assert.assertEquals(policy.getDescription(), tier.getDescription());
        }
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) Tier(org.wso2.carbon.apimgt.api.model.Tier) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) Matchers.anyString(org.mockito.Matchers.anyString) RealmService(org.wso2.carbon.user.core.service.RealmService) ApiMgtDAOMockCreator(org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) APIMConfigService(org.wso2.carbon.apimgt.impl.config.APIMConfigService) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 32 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-apimgt by wso2.

the class APIUtilTierTest method testGetTiersFromAppPolicies.

@Test
public void testGetTiersFromAppPolicies() throws Exception {
    System.setProperty("carbon.home", APIUtilTierTest.class.getResource("/").getFile());
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("abc.com");
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(1);
        String policyLevel = PolicyConstants.POLICY_LEVEL_APP;
        int tenantId = 1;
        ApiMgtDAOMockCreator daoMockHolder = new ApiMgtDAOMockCreator(tenantId);
        ApiMgtDAO apiMgtDAO = daoMockHolder.getMock();
        PowerMockito.mockStatic(ServiceReferenceHolder.class);
        ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
        Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
        APIMConfigService apimConfigService = Mockito.mock(APIMConfigService.class);
        Mockito.when(serviceReferenceHolder.getApimConfigService()).thenReturn(apimConfigService);
        Mockito.when(apimConfigService.getTenantConfig("abc.com")).thenReturn(IOUtils.toString(tenantConf));
        APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
        APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
        Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
        Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
        RealmService realmService = Mockito.mock(RealmService.class);
        TenantManager tenantManager = Mockito.mock(TenantManager.class);
        Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
        Mockito.when(tenantManager.getDomain(1)).thenReturn("abc.com");
        Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
        ThrottleProperties throttleProperties = new ThrottleProperties();
        throttleProperties.setEnableUnlimitedTier(true);
        Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
        ApplicationPolicy[] policies = generateAppPolicies(tiersReturned);
        Mockito.when(apiMgtDAO.getApplicationPolicies(tenantId)).thenReturn(policies);
        Map<String, Tier> tiersFromPolicies = APIUtil.getTiersFromPolicies(policyLevel, tenantId);
        Mockito.verify(apiMgtDAO, Mockito.only()).getApplicationPolicies(tenantId);
        for (ApplicationPolicy policy : policies) {
            Tier tier = tiersFromPolicies.get(policy.getPolicyName());
            Assert.assertNotNull(tier);
            Assert.assertEquals(policy.getPolicyName(), tier.getName());
            Assert.assertEquals(policy.getDescription(), tier.getDescription());
        }
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) Tier(org.wso2.carbon.apimgt.api.model.Tier) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) Matchers.anyString(org.mockito.Matchers.anyString) RealmService(org.wso2.carbon.user.core.service.RealmService) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) ApiMgtDAOMockCreator(org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator) APIMConfigService(org.wso2.carbon.apimgt.impl.config.APIMConfigService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 33 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-apimgt by wso2.

the class RegistryCacheInvalidationServiceTestCase method setup.

@Before
public void setup() throws Exception {
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(6543);
    Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
    Mockito.when(registryService.getGovernanceSystemRegistry(6543)).thenReturn(userRegistry);
    PowerMockito.mockStatic(RegistryUtils.class);
    Mockito.when(userRegistry.getRegistryContext()).thenReturn(registryContext);
    System.setProperty(CARBON_HOME, "");
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
}
Also used : PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) Before(org.junit.Before)

Example 34 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-apimgt by wso2.

the class WSO2APIPublisherTestCase method init.

@Before
public void init() throws Exception {
    store = new APIStore();
    store.setDisplayName(storeName);
    store.setUsername(storeUserName);
    store.setPassword(storePassword);
    store.setEndpoint(storeEndpoint);
    identifier = new APIIdentifier(apiIdentifier);
    api = new API(identifier);
    defaultHttpClient = Mockito.mock(CloseableHttpClient.class);
    wso2APIPublisher = new WSO2APIPublisherWrapper(defaultHttpClient, username, Mockito.mock(APIProvider.class));
    CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class);
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    RealmService realmService = Mockito.mock(RealmService.class);
    tenantManager = Mockito.mock(TenantManager.class);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    HttpEntity entity = Mockito.mock(HttpEntity.class);
    statusLine = Mockito.mock(StatusLine.class);
    Mockito.doReturn(statusLine).when(httpResponse).getStatusLine();
    Mockito.doReturn(entity).when(httpResponse).getEntity();
    PowerMockito.mockStatic(EntityUtils.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.EXTERNAL_API_STORES + "." + APIConstants.EXTERNAL_API_STORES_STORE_URL)).thenReturn(storeRedirectURL);
    HttpGet httpGet = Mockito.mock(HttpGet.class);
    HttpPost httpPost = Mockito.mock(HttpPost.class);
    HttpDelete httpDelete = Mockito.mock(HttpDelete.class);
    PowerMockito.whenNew(HttpGet.class).withAnyArguments().thenReturn(httpGet);
    PowerMockito.whenNew(HttpPost.class).withAnyArguments().thenReturn(httpPost);
    PowerMockito.whenNew(HttpDelete.class).withAnyArguments().thenReturn(httpDelete);
    Mockito.doReturn(httpResponse).when(defaultHttpClient).execute(httpPost);
    Mockito.doReturn(httpResponse).when(defaultHttpClient).execute(httpGet);
    Mockito.doReturn(httpResponse).when(defaultHttpClient).execute(httpDelete);
    MultipartEntityBuilder multipartEntityBuilder = Mockito.mock(MultipartEntityBuilder.class);
    PowerMockito.mockStatic(MultipartEntityBuilder.class);
    Mockito.when(MultipartEntityBuilder.create()).thenReturn(multipartEntityBuilder);
    Mockito.when(multipartEntityBuilder.build()).thenReturn(Mockito.mock(HttpEntity.class));
    Mockito.doNothing().when(httpPost).setEntity(Matchers.any());
    importExportAPI = Mockito.mock(ImportExportAPI.class);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) HttpPost(org.apache.http.client.methods.HttpPost) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) StatusLine(org.apache.http.StatusLine) RealmService(org.wso2.carbon.user.core.service.RealmService) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) API(org.wso2.carbon.apimgt.api.model.API) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) APIStore(org.wso2.carbon.apimgt.api.model.APIStore) Before(org.junit.Before)

Example 35 with TenantManager

use of org.wso2.carbon.user.core.tenant.TenantManager in project carbon-apimgt by wso2.

the class AbstractAPIManagerTestCase method testGetGraphqlSchemaDefinition.

// 
// @Test
// public void testGetSwagger20Definition() throws Exception {
// int tenantId = -1234;
// Organization org = Mockito.mock(Organization.class);
// PowerMockito.whenNew(Organization.class).withArguments(SAMPLE_TENANT_DOMAIN, null).thenReturn(org);
// 
// AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(apiPersistenceInstance);
// APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
// identifier.setUuid(SAMPLE_RESOURCE_ID);
// PowerMockito.mockStatic(OASParserUtil.class);
// String swaggerContent = "sample swagger";
// PowerMockito.when(apiPersistenceInstance.getOASDefinition(org ,
// SAMPLE_RESOURCE_ID)).thenReturn(swaggerContent);
// 
// 
// Assert.assertEquals(abstractAPIManager.getOpenAPIDefinition(SAMPLE_RESOURCE_ID, SAMPLE_TENANT_DOMAIN), swaggerContent);
// abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN;
// Assert.assertEquals(abstractAPIManager.getOpenAPIDefinition(identifier), swaggerContent);
// }
@Test
public void testGetGraphqlSchemaDefinition() throws Exception {
    int tenantId = -1234;
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(null, registryService, tenantManager);
    Mockito.when(tenantManager.getTenantId(SAMPLE_TENANT_DOMAIN)).thenThrow(UserStoreException.class).thenReturn(tenantId);
    APIIdentifier identifier = getAPIIdentifier(SAMPLE_API_NAME, API_PROVIDER, SAMPLE_API_VERSION);
    try {
        abstractAPIManager.getGraphqlSchemaDefinition(identifier);
        Assert.fail("Use store exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get graphql schema definition of Graphql API"));
    }
    String schemaContent = "sample graphql schema";
    setFinalStatic(AbstractAPIManager.class.getDeclaredField("schemaDef"), graphQLSchemaDefinition);
    Mockito.when(graphQLSchemaDefinition.getGraphqlSchemaDefinition(identifier, null)).thenReturn(schemaContent);
    Assert.assertEquals(abstractAPIManager.getGraphqlSchemaDefinition(identifier), schemaContent);
    abstractAPIManager.tenantDomain = SAMPLE_TENANT_DOMAIN;
    Assert.assertEquals(abstractAPIManager.getGraphqlSchemaDefinition(identifier), schemaContent);
    Mockito.when(registryService.getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId)).thenThrow(RegistryException.class);
    abstractAPIManager.tenantDomain = null;
    try {
        abstractAPIManager.getGraphqlSchemaDefinition(identifier);
        Assert.fail("Registry exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Failed to get graphql schema definition of Graphql API"));
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

RealmService (org.wso2.carbon.user.core.service.RealmService)50 TenantManager (org.wso2.carbon.user.core.tenant.TenantManager)49 Test (org.junit.Test)46 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)42 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)32 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)26 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)23 Resource (org.wso2.carbon.registry.core.Resource)21 API (org.wso2.carbon.apimgt.api.model.API)20 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)17 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)15 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)15 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)15 QName (javax.xml.namespace.QName)14 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)14 Registry (org.wso2.carbon.registry.core.Registry)13 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)12 Before (org.junit.Before)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10