use of org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator 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();
}
}
use of org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator 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.ApiMgtDAOMockCreator in project carbon-apimgt by wso2.
the class APIUtilTierTest method testAddDefaultTenantAdvancedThrottlePoliciesSubLevel.
@Test
public void testAddDefaultTenantAdvancedThrottlePoliciesSubLevel() 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(false);
Mockito.when(apiMgtDAO.isPolicyDeployed(eq(PolicyConstants.POLICY_LEVEL_SUB), eq(tenantId), eq(policy))).thenReturn(false);
}
APIUtil.addDefaultTenantAdvancedThrottlePolicies(tenantDomain, tenantId);
Mockito.verify(apiMgtDAO, Mockito.times(policies.length)).addSubscriptionPolicy(Mockito.any(SubscriptionPolicy.class));
}
use of org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator in project carbon-apimgt by wso2.
the class ApplicationUtilsTestCase method setup.
@Before
public void setup() throws UserStoreException, RegistryException {
apiMgtDAOMockCreator = new ApiMgtDAOMockCreator(444);
apiMgtDAO = apiMgtDAOMockCreator.getMock();
}
use of org.wso2.carbon.apimgt.impl.ApiMgtDAOMockCreator in project carbon-apimgt by wso2.
the class APIUtilTierTest method testAddDefaultTenantAdvancedThrottlePoliciesApiLevelAlreadyAdded.
@Test
public void testAddDefaultTenantAdvancedThrottlePoliciesApiLevelAlreadyAdded() 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(true);
Mockito.when(apiMgtDAO.isPolicyDeployed(eq(PolicyConstants.POLICY_LEVEL_API), eq(tenantId), eq(policy))).thenReturn(true);
}
APIUtil.addDefaultTenantAdvancedThrottlePolicies(tenantDomain, tenantId);
Mockito.verify(apiMgtDAO, Mockito.never()).addAPIPolicy(Mockito.any(APIPolicy.class));
Mockito.verify(apiMgtDAO, Mockito.never()).setPolicyDeploymentStatus(eq(PolicyConstants.POLICY_LEVEL_API), Mockito.anyString(), eq(tenantId), eq(true));
}
Aggregations