use of org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator in project carbon-apimgt by wso2.
the class APIUtilTierTest method testAddDefaultSuperTenantAdvancedThrottlePoliciesApiLevel.
@Test
public void testAddDefaultSuperTenantAdvancedThrottlePoliciesApiLevel() throws Exception {
ApiMgtDAOMockCreator daoMockHolder = new ApiMgtDAOMockCreator(1);
ApiMgtDAO apiMgtDAO = daoMockHolder.getMock();
String[] apiPolicies = new String[] { APIConstants.DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_UNLIMITED };
for (String policy : apiPolicies) {
Mockito.when(apiMgtDAO.isPolicyExist(eq(PolicyConstants.POLICY_LEVEL_API), eq(MultitenantConstants.SUPER_TENANT_ID), eq(policy))).thenReturn(false);
}
try {
APIUtil.addDefaultSuperTenantAdvancedThrottlePolicies();
Mockito.verify(apiMgtDAO, Mockito.times(apiPolicies.length)).addAPIPolicy(Mockito.any(APIPolicy.class));
} catch (APIManagementException e) {
Assert.assertTrue("Exception thrown", false);
}
}
use of org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator in project carbon-apimgt by wso2.
the class APIUtilTierTest method testAddDefaultTenantAdvancedThrottlePoliciesSubLevelAlreadyAdded.
@Test
public void testAddDefaultTenantAdvancedThrottlePoliciesSubLevelAlreadyAdded() throws Exception {
int tenantId = 1;
String tenantDomain = "test.com";
ApiMgtDAOMockCreator daoMockHolder = new ApiMgtDAOMockCreator(tenantId);
ApiMgtDAO apiMgtDAO = daoMockHolder.getMock();
mockPolicyRetrieval(apiMgtDAO);
String[] policies = new String[] { APIConstants.DEFAULT_SUB_POLICY_GOLD, APIConstants.DEFAULT_SUB_POLICY_SILVER, APIConstants.DEFAULT_SUB_POLICY_BRONZE, APIConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED, APIConstants.DEFAULT_SUB_POLICY_UNLIMITED, APIConstants.DEFAULT_SUB_POLICY_ASYNC_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED };
for (String policy : policies) {
Mockito.when(apiMgtDAO.isPolicyExist(eq(PolicyConstants.POLICY_LEVEL_SUB), eq(tenantId), eq(policy))).thenReturn(true);
Mockito.when(apiMgtDAO.isPolicyDeployed(eq(PolicyConstants.POLICY_LEVEL_SUB), eq(tenantId), eq(policy))).thenReturn(true);
}
APIUtil.addDefaultTenantAdvancedThrottlePolicies(tenantDomain, tenantId);
Mockito.verify(apiMgtDAO, Mockito.never()).addSubscriptionPolicy(Mockito.any(SubscriptionPolicy.class));
Mockito.verify(apiMgtDAO, Mockito.never()).setPolicyDeploymentStatus(eq(PolicyConstants.POLICY_LEVEL_SUB), Mockito.anyString(), eq(tenantId), eq(true));
}
use of org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator in project carbon-apimgt by wso2.
the class APIUtilTierTest method testGetTiersFromApiPoliciesBandwidth.
@Test
public void testGetTiersFromApiPoliciesBandwidth() throws Exception {
String policyLevel = PolicyConstants.POLICY_LEVEL_API;
int tenantId = 1;
System.setProperty("carbon.home", APIUtilTierTest.class.getResource("/").getFile());
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("abc.com");
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(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));
APIPolicy[] policies = generateApiPoliciesBandwidth(tiersReturned);
Mockito.when(apiMgtDAO.getAPIPolicies(tenantId)).thenReturn(policies);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.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);
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);
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();
}
}
use of org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator in project carbon-apimgt by wso2.
the class APIUtilTierTest method testAddDefaultSuperTenantAdvancedThrottlePoliciesSubLevel.
@Test
public void testAddDefaultSuperTenantAdvancedThrottlePoliciesSubLevel() throws Exception {
ApiMgtDAOMockCreator daoMockHolder = new ApiMgtDAOMockCreator(1);
ApiMgtDAO apiMgtDAO = daoMockHolder.getMock();
String[] subPolicies = new String[] { APIConstants.DEFAULT_SUB_POLICY_GOLD, APIConstants.DEFAULT_SUB_POLICY_SILVER, APIConstants.DEFAULT_SUB_POLICY_BRONZE, APIConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED, APIConstants.DEFAULT_SUB_POLICY_UNLIMITED, APIConstants.DEFAULT_SUB_POLICY_ASYNC_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED };
for (String policy : subPolicies) {
Mockito.when(apiMgtDAO.isPolicyExist(eq(PolicyConstants.POLICY_LEVEL_SUB), eq(MultitenantConstants.SUPER_TENANT_ID), eq(policy))).thenReturn(false);
}
try {
APIUtil.addDefaultSuperTenantAdvancedThrottlePolicies();
Mockito.verify(apiMgtDAO, Mockito.times(subPolicies.length)).addSubscriptionPolicy(Mockito.any(SubscriptionPolicy.class));
} catch (APIManagementException e) {
Assert.assertTrue("Exception thrown", false);
}
}
use of org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator in project carbon-apimgt by wso2.
the class APIUtilTierTest method testAddDefaultTenantAdvancedThrottlePoliciesApiLevel.
@Test
public void testAddDefaultTenantAdvancedThrottlePoliciesApiLevel() throws Exception {
int tenantId = 1;
String tenantDomain = "test.com";
ApiMgtDAOMockCreator daoMockHolder = new ApiMgtDAOMockCreator(tenantId);
ApiMgtDAO apiMgtDAO = daoMockHolder.getMock();
mockPolicyRetrieval(apiMgtDAO);
String[] policies = new String[] { APIConstants.DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN, APIConstants.DEFAULT_API_POLICY_UNLIMITED };
for (String policy : policies) {
Mockito.when(apiMgtDAO.isPolicyExist(eq(PolicyConstants.POLICY_LEVEL_API), eq(tenantId), eq(policy))).thenReturn(false);
Mockito.when(apiMgtDAO.isPolicyDeployed(eq(PolicyConstants.POLICY_LEVEL_API), eq(tenantId), eq(policy))).thenReturn(false);
}
APIUtil.addDefaultTenantAdvancedThrottlePolicies(tenantDomain, tenantId);
Mockito.verify(apiMgtDAO, Mockito.times(policies.length)).addAPIPolicy(Mockito.any(APIPolicy.class));
}
Aggregations