Search in sources :

Example 16 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class CommonConfigDeployerTestCase method testCreatedConfigurationContextRuntimeException.

@Test
public void testCreatedConfigurationContextRuntimeException() throws APIManagementException {
    System.setProperty(CARBON_HOME, "");
    PowerMockito.mockStatic(CommonUtil.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);
    Mockito.when(privilegedCarbonContext.getTenantId()).thenReturn(TENANT_ID);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    RegistryService registryService = Mockito.mock(RegistryService.class);
    Mockito.when(ServiceReferenceHolder.getInstance().getRegistryService()).thenReturn(registryService);
    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.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
    ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
    PowerMockito.mockStatic(APIUtil.class);
    // Mockito.doThrow(new RuntimeException("error")).when(APIUtil.loadTenantConf(TENANT_ID));
    PowerMockito.doThrow(new RuntimeException("error")).when(APIUtil.class);
    APIUtil.createDefaultRoles(TENANT_ID);
    CommonConfigDeployer commonConfigDeployer = new CommonConfigDeployer();
    commonConfigDeployer.createdConfigurationContext(configurationContext);
    PowerMockito.verifyStatic(APIUtil.class, Mockito.times(1));
    APIUtil.createDefaultRoles(TENANT_ID);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 17 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class CertificateManagerImplTest method init.

@BeforeClass
public static void init() throws XMLStreamException {
    PowerMockito.mockStatic(CertificateMgtDAO.class);
    certificateMgtDAO = Mockito.mock(CertificateMgtDAO.class);
    PowerMockito.when(CertificateMgtDAO.getInstance()).thenReturn(certificateMgtDAO);
    System.setProperty("javax.net.ssl.trustStore", CertificateManagerImplTest.class.getClassLoader().getResource("security/client-truststore.jks").getPath());
    System.setProperty(JAVAX_NET_SSL_TRUST_STORE_PASSWORD_PROPERTY, JAVAX_SSL_TRUST_STORE_PASSWORD);
    TestUtils.initConfigurationContextService(true);
    certificateManager = CertificateManagerImpl.getInstance();
    System.setProperty(CARBON_HOME, "");
    PowerMockito.mockStatic(CarbonContext.class);
    CarbonContext carbonContext = Mockito.mock(CarbonContext.class);
    PowerMockito.when(CarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
    Mockito.when(carbonContext.getTenantDomain()).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    Mockito.when(carbonContext.getTenantId()).thenReturn(MultitenantConstants.SUPER_TENANT_ID);
}
Also used : CertificateMgtDAO(org.wso2.carbon.apimgt.impl.dao.CertificateMgtDAO) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) CarbonContext(org.wso2.carbon.context.CarbonContext) BeforeClass(org.junit.BeforeClass)

Example 18 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class APIUtilTest method testGetTiersWhenTierTypeIsAPI.

@Test
public void testGetTiersWhenTierTypeIsAPI() throws Exception {
    System.setProperty(CARBON_HOME, "");
    int tierType = APIConstants.TIER_API_TYPE;
    int tenantID = 1;
    Tier tier1 = Mockito.mock(Tier.class);
    Map<String, Tier> tierMap = new TreeMap<String, Tier>();
    tierMap.put("UNLIMITED", tier1);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.doNothing().when(PrivilegedCarbonContext.class, "startTenantFlow");
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    Mockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    doNothing().when(privilegedCarbonContext).setTenantDomain(tenantDomain, true);
    Mockito.when(privilegedCarbonContext.getTenantId()).thenReturn(tenantID);
    PowerMockito.spy(APIUtil.class);
    PowerMockito.doReturn(tenantID).when(APIUtil.class, "getInternalOrganizationId", tenantDomain);
    PowerMockito.doReturn(tierMap).when(APIUtil.class, "getTiersFromPolicies", PolicyConstants.POLICY_LEVEL_SUB, tenantID);
    Map<String, Tier> appTierMap = APIUtil.getTiers(tierType, tenantDomain);
    Assert.assertEquals(tierMap, appTierMap);
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) TreeMap(java.util.TreeMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class APIUtilTest method testHasPermission.

@Test
public void testHasPermission() throws Exception {
    int tenantId = 2;
    String userNameWithoutChange = "Drake";
    String permission = APIConstants.Permissions.API_PUBLISH;
    System.setProperty(CARBON_HOME, "");
    PowerMockito.spy(APIUtil.class);
    PowerMockito.doReturn(false).when(APIUtil.class, "isPermissionCheckDisabled");
    PowerMockito.doReturn(1).when(APIUtil.class, "getValueFromCache", APIConstants.API_PUBLISHER_ADMIN_PERMISSION_CACHE, userNameWithoutChange);
    PowerMockito.mockStatic(MultitenantUtils.class);
    Mockito.when(MultitenantUtils.getTenantDomain(userNameWithoutChange)).thenReturn(tenantDomain);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.mockStatic(CarbonContext.class);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    Mockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    RealmService realmService = Mockito.mock(RealmService.class);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantId);
    UserRealm userRealm = Mockito.mock(UserRealm.class);
    Mockito.when(realmService.getTenantUserRealm(tenantId)).thenReturn(userRealm);
    org.wso2.carbon.user.api.AuthorizationManager authorizationManager = Mockito.mock(org.wso2.carbon.user.api.AuthorizationManager.class);
    Mockito.when(userRealm.getAuthorizationManager()).thenReturn(authorizationManager);
    Mockito.when(authorizationManager.isUserAuthorized(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(true);
    org.wso2.carbon.user.core.UserRealm userRealm2 = Mockito.mock(org.wso2.carbon.user.core.UserRealm.class);
    Mockito.when(ServiceReferenceHolder.getUserRealm()).thenReturn((userRealm2));
    PowerMockito.mockStatic(AuthorizationManager.class);
    AuthorizationManager authorizationManager1 = Mockito.mock(AuthorizationManager.class);
    Mockito.when(AuthorizationManager.getInstance()).thenReturn(authorizationManager1);
    Mockito.when(authorizationManager1.isUserAuthorized(Mockito.anyString(), Mockito.anyString())).thenReturn(true);
    Log logMock = Mockito.mock(Log.class);
    PowerMockito.mockStatic(LogFactory.class);
    Mockito.when(LogFactory.getLog(any(Class.class))).thenReturn(logMock);
    boolean expectedResult = APIUtil.hasPermission(userNameWithoutChange, permission);
    Assert.assertEquals(true, expectedResult);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) Log(org.apache.commons.logging.Log) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) UserRealm(org.wso2.carbon.user.api.UserRealm) RealmService(org.wso2.carbon.user.core.service.RealmService) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with CARBON_HOME

use of org.wso2.carbon.utils.ServerConstants.CARBON_HOME in project carbon-apimgt by wso2.

the class APIUtilTest method testGetTiersWhenTierTypeIsResource.

@Test
public void testGetTiersWhenTierTypeIsResource() throws Exception {
    System.setProperty(CARBON_HOME, "");
    int tierType = APIConstants.TIER_RESOURCE_TYPE;
    int tenantID = 1;
    Tier tier1 = Mockito.mock(Tier.class);
    Map<String, Tier> tierMap = new TreeMap<String, Tier>();
    tierMap.put("UNLIMITED", tier1);
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    PowerMockito.doNothing().when(PrivilegedCarbonContext.class, "startTenantFlow");
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    Mockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    doNothing().when(privilegedCarbonContext).setTenantDomain(tenantDomain, true);
    Mockito.when(privilegedCarbonContext.getTenantId()).thenReturn(tenantID);
    PowerMockito.spy(APIUtil.class);
    PowerMockito.doReturn(tenantID).when(APIUtil.class, "getInternalOrganizationId", tenantDomain);
    PowerMockito.doReturn(tierMap).when(APIUtil.class, "getTiersFromPolicies", PolicyConstants.POLICY_LEVEL_API, tenantID);
    Map<String, Tier> appTierMap = APIUtil.getTiers(tierType, tenantDomain);
    Assert.assertEquals(tierMap, appTierMap);
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) TreeMap(java.util.TreeMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)16 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)13 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)5 Before (org.junit.Before)4 API (org.wso2.carbon.apimgt.api.model.API)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)4 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)4 APIManagerConfigurationService (org.wso2.carbon.apimgt.impl.APIManagerConfigurationService)4 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)4 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)4 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)4 Registry (org.wso2.carbon.registry.core.Registry)4 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)4 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)4 TreeMap (java.util.TreeMap)3 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Tier (org.wso2.carbon.apimgt.api.model.Tier)3