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