use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService 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();
}
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class APIUtilTierTest method testGetAllTiers.
@Test
public void testGetAllTiers() throws APIManagementException, RegistryException {
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ApiMgtDAO.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(amConfigService);
Mockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
Mockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
Mockito.when(throttleProperties.isEnableUnlimitedTier()).thenReturn(true);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getGovernanceSystemRegistry()).thenReturn(userRegistry);
SubscriptionPolicy[] policies = new SubscriptionPolicy[3];
policies[0] = TestUtils.getUniqueSubscriptionPolicyWithBandwidthLimit();
policies[1] = TestUtils.getUniqueSubscriptionPolicyWithRequestCountLimit();
policies[2] = TestUtils.getUniqueSubscriptionPolicyWithBandwidthLimit();
Mockito.when(apiMgtDAO.getSubscriptionPolicies(Mockito.anyInt())).thenReturn(policies);
// IsEnabled true scenario
Assert.assertEquals(3, APIUtil.getAllTiers().size());
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class APIManagerComponentTest method testShouldActivateWhenAllPrerequisitesMet.
@Test
public void testShouldActivateWhenAllPrerequisitesMet() throws Exception {
PowerMockito.mockStatic(APIMgtDBUtil.class);
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.mockStatic(AuthorizationUtils.class);
PowerMockito.mockStatic(RegistryUtils.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.mockStatic(SQLConstantManagerFactory.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
ComponentContext componentContext = Mockito.mock(ComponentContext.class);
BundleContext bundleContext = Mockito.mock(BundleContext.class);
APIManagerConfiguration configuration = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService configurationService = Mockito.mock(APIManagerConfigurationService.class);
AuthorizationManager authManager = Mockito.mock(AuthorizationManager.class);
Registry registry = Mockito.mock(Registry.class);
RealmService realmService = Mockito.mock(RealmService.class);
UserRealm userRealm = Mockito.mock(UserRealm.class);
OutputEventAdapterService adapterService = Mockito.mock(OutputEventAdapterService.class);
ThrottleProperties throttleProperties = new ThrottleProperties();
Mockito.doNothing().when(configuration).load(Mockito.anyString());
Mockito.doNothing().when(authManager).authorizeRole(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
Mockito.doNothing().when(adapterService).create(null);
Mockito.when(componentContext.getBundleContext()).thenReturn(bundleContext);
Mockito.when(registry.resourceExists(Mockito.anyString())).thenReturn(true);
Mockito.when(configuration.getFirstProperty(Mockito.anyString())).thenReturn("").thenReturn(null);
Mockito.when(bundleContext.registerService("", CommonConfigDeployer.class, null)).thenReturn(null);
Mockito.when(authManager.isRoleAuthorized(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(true);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(configurationService);
Mockito.when(serviceReferenceHolder.getOutputEventAdapterService()).thenReturn(adapterService);
Mockito.when(configurationService.getAPIManagerConfiguration()).thenReturn(configuration);
Mockito.when(realmService.getTenantUserRealm(Mockito.anyInt())).thenReturn(userRealm);
Mockito.when(userRealm.getAuthorizationManager()).thenReturn(authManager);
Mockito.when(configuration.getThrottleProperties()).thenReturn(throttleProperties);
PowerMockito.doNothing().when(APIMgtDBUtil.class, "initialize");
PowerMockito.doNothing().when(APIUtil.class, "loadTenantExternalStoreConfig", Mockito.anyString());
PowerMockito.doNothing().when(AuthorizationUtils.class, "addAuthorizeRoleListener", Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
PowerMockito.doNothing().when(SQLConstantManagerFactory.class, "initializeSQLConstantManager");
PowerMockito.when(APIUtil.getMountedPath(null, "")).thenReturn("");
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
PowerMockito.when(RegistryUtils.getAbsolutePath(null, null)).thenReturn("");
PowerMockito.whenNew(APIManagerConfiguration.class).withAnyArguments().thenReturn(configuration);
PowerMockito.mockStatic(ApiMgtDAO.class);
ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
APIManagerComponent apiManagerComponent = new APIManagerComponentWrapper(registry);
GatewayArtifactSynchronizerProperties synchronizerProperties = new GatewayArtifactSynchronizerProperties();
Mockito.when(config.getGatewayArtifactSynchronizerProperties()).thenReturn(synchronizerProperties);
EventHubConfigurationDto eventHubConfigurationDto = new EventHubConfigurationDto();
eventHubConfigurationDto.setEnabled(true);
eventHubConfigurationDto.setInitDelay(0);
eventHubConfigurationDto.setUsername("a");
eventHubConfigurationDto.setPassword("sss".toCharArray());
eventHubConfigurationDto.setServiceUrl("https://localhost");
EventHubConfigurationDto.EventHubPublisherConfiguration eventHubPublisherConfiguration = new EventHubConfigurationDto.EventHubPublisherConfiguration();
eventHubConfigurationDto.setEventHubPublisherConfiguration(eventHubPublisherConfiguration);
Mockito.when(config.getEventHubConfigurationDto()).thenReturn(eventHubConfigurationDto);
try {
apiManagerComponent.activate(componentContext);
} catch (FileNotFoundException f) {
// Exception thrown here means that method was continued without the configuration file
Assert.fail("Should not throw an exception");
}
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class CommonConfigDeployerTestCase method testCreatedConfigurationContext.
@Test
public void testCreatedConfigurationContext() throws APIManagementException {
PowerMockito.mockStatic(CommonUtil.class);
System.setProperty(CARBON_HOME, "");
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);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
ThrottleProperties throttleProperties = new ThrottleProperties();
RegistryService registryService = Mockito.mock(RegistryService.class);
Mockito.when(ServiceReferenceHolder.getInstance().getRegistryService()).thenReturn(registryService);
Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
ThrottleProperties.PolicyDeployer policyDeployer = Mockito.mock(ThrottleProperties.PolicyDeployer.class);
throttleProperties.setPolicyDeployer(policyDeployer);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
PowerMockito.mockStatic(APIUtil.class);
CommonConfigDeployer commonConfigDeployer = new CommonConfigDeployer();
commonConfigDeployer.createdConfigurationContext(configurationContext);
PowerMockito.verifyStatic(APIUtil.class);
APIUtil.createDefaultRoles(TENANT_ID);
PowerMockito.verifyStatic(APIUtil.class);
APIUtil.loadAndSyncTenantConf(TENANT_DOMAIN);
// PowerMockito.verifyStatic(APIUtil.class);
// APIUtil.addDefaultTenantAdvancedThrottlePolicies(TENANT_DOMAIN, TENANT_ID);
}
use of org.wso2.carbon.apimgt.impl.APIManagerConfigurationService in project carbon-apimgt by wso2.
the class CommonConfigDeployerTestCase method testExceptions.
@Test
public void testExceptions() throws Exception {
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.mockStatic(CommonUtil.class);
System.setProperty(CARBON_HOME, "");
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);
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);
RegistryService registryService = Mockito.mock(RegistryService.class);
Mockito.when(ServiceReferenceHolder.getInstance().getRegistryService()).thenReturn(registryService);
ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.doThrow(new APIManagementException("error")).when(APIUtil.class);
APIUtil.addDefaultTenantAdvancedThrottlePolicies(TENANT_DOMAIN, TENANT_ID);
PowerMockito.doThrow(new APIManagementException("error")).when(APIUtil.class);
APIUtil.writeDefinedSequencesToTenantRegistry(TENANT_ID);
PowerMockito.doThrow(new APIManagementException("error")).when(APIUtil.class);
APIUtil.loadTenantExternalStoreConfig(TENANT_DOMAIN);
PowerMockito.doThrow(new APIManagementException("error")).when(APIUtil.class);
APIUtil.loadTenantGAConfig(TENANT_DOMAIN);
PowerMockito.doThrow(new APIManagementException("error")).when(APIUtil.class);
APIUtil.loadTenantWorkFlowExtensions(TENANT_DOMAIN);
PowerMockito.doThrow(new APIManagementException("error")).when(APIUtil.class);
APIUtil.loadTenantSelfSignUpConfigurations(TENANT_DOMAIN);
PowerMockito.doThrow(new APIManagementException("error")).when(APIUtil.class);
APIUtil.loadAndSyncTenantConf(TENANT_DOMAIN);
PowerMockito.doThrow(new APIManagementException("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);
}
Aggregations