use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class APIUtilTierTest method mockPolicyRetrieval.
private void mockPolicyRetrieval(ApiMgtDAO apiMgtDAO) throws Exception {
QuotaPolicy quotaPolicy = Mockito.mock(QuotaPolicy.class);
ApplicationPolicy applicationPolicy = Mockito.mock(ApplicationPolicy.class);
SubscriptionPolicy subscriptionPolicy = Mockito.mock(SubscriptionPolicy.class);
APIPolicy apiPolicy = Mockito.mock(APIPolicy.class);
Mockito.when(applicationPolicy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
Mockito.when(subscriptionPolicy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
Mockito.when(apiPolicy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
Mockito.when(apiMgtDAO.getApplicationPolicy(anyString(), anyInt())).thenReturn(applicationPolicy);
Mockito.when(apiMgtDAO.getSubscriptionPolicy(anyString(), anyInt())).thenReturn(subscriptionPolicy);
Mockito.when(apiMgtDAO.getAPIPolicy(anyString(), anyInt())).thenReturn(apiPolicy);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
Map<String, List<Notifier>> notifierMap = Mockito.mock(Map.class);
List<Notifier> notifierList = new ArrayList<>();
Mockito.when(notifierMap.get(any())).thenReturn(notifierList);
Mockito.when(serviceReferenceHolder.getNotifiersMap()).thenReturn(notifierMap);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
Map<String, Long> defaultLimits = new HashMap<>();
Mockito.when(throttleProperties.getDefaultThrottleTierLimits()).thenReturn(defaultLimits);
Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
}
use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class APIUtilTierTest method generateAppPolicies.
private ApplicationPolicy[] generateAppPolicies(String[] policyNames) {
List<ApplicationPolicy> policyList = new ArrayList<ApplicationPolicy>();
for (String policyName : policyNames) {
ApplicationPolicy policy = new ApplicationPolicy(policyName);
QuotaPolicy quotaPolicy = new QuotaPolicy();
RequestCountLimit countLimit = new RequestCountLimit();
countLimit.setRequestCount(123);
quotaPolicy.setLimit(countLimit);
policy.setDefaultQuotaPolicy(quotaPolicy);
policy.setDescription(policyName);
policyList.add(policy);
}
ApplicationPolicy[] array = {};
return policyList.toArray(array);
}
use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy 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.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class APIMgtDAOTest method testAddUpdateDeleteBlockCondition.
@Test
public void testAddUpdateDeleteBlockCondition() throws Exception {
Subscriber subscriber = new Subscriber("blockuser1");
subscriber.setTenantId(-1234);
subscriber.setEmail("abc@wso2.com");
subscriber.setSubscribedDate(new Date(System.currentTimeMillis()));
apiMgtDAO.addSubscriber(subscriber, null);
Policy applicationPolicy = getApplicationPolicy("testAddUpdateDeleteBlockCondition");
applicationPolicy.setTenantId(-1234);
apiMgtDAO.addApplicationPolicy((ApplicationPolicy) applicationPolicy);
Application application = new Application("testAddUpdateDeleteBlockCondition", subscriber);
application.setTier("testAddUpdateDeleteBlockCondition");
application.setId(apiMgtDAO.addApplication(application, "blockuser1", "testOrg"));
APIIdentifier apiId = new APIIdentifier("testAddUpdateDeleteBlockCondition", "testAddUpdateDeleteBlockCondition", "1.0.0");
API api = new API(apiId);
api.setContext("/testAddUpdateDeleteBlockCondition");
api.setContextTemplate("/testAddUpdateDeleteBlockCondition/{version}");
api.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
api.setUUID(UUID.randomUUID().toString());
apiMgtDAO.addAPI(api, -1234, "testOrg");
BlockConditionsDTO apiBlockConditionDto = new BlockConditionsDTO();
apiBlockConditionDto.setConditionValue("/testAddUpdateDeleteBlockCondition");
apiBlockConditionDto.setConditionType(APIConstants.BLOCKING_CONDITIONS_API);
apiBlockConditionDto.setUUID(UUID.randomUUID().toString());
apiBlockConditionDto.setTenantDomain("carbon.super");
BlockConditionsDTO apiUUID = apiMgtDAO.addBlockConditions(apiBlockConditionDto);
BlockConditionsDTO applicationBlockCondition = new BlockConditionsDTO();
applicationBlockCondition.setConditionValue("blockuser1:testAddUpdateDeleteBlockCondition");
applicationBlockCondition.setConditionType(APIConstants.BLOCKING_CONDITIONS_APPLICATION);
applicationBlockCondition.setUUID(UUID.randomUUID().toString());
applicationBlockCondition.setTenantDomain("carbon.super");
BlockConditionsDTO applicationUUID = apiMgtDAO.addBlockConditions(applicationBlockCondition);
assertNotNull(applicationUUID);
BlockConditionsDTO ipBlockcondition = new BlockConditionsDTO();
ipBlockcondition.setConditionValue("127.0.0.1");
ipBlockcondition.setConditionType(APIConstants.BLOCKING_CONDITIONS_IP);
ipBlockcondition.setUUID(UUID.randomUUID().toString());
ipBlockcondition.setTenantDomain("carbon.super");
BlockConditionsDTO ipUUID = apiMgtDAO.addBlockConditions(ipBlockcondition);
assertNotNull(ipUUID);
BlockConditionsDTO userBlockcondition = new BlockConditionsDTO();
userBlockcondition.setConditionValue("admin");
userBlockcondition.setConditionType(APIConstants.BLOCKING_CONDITIONS_USER);
userBlockcondition.setUUID(UUID.randomUUID().toString());
userBlockcondition.setTenantDomain("carbon.super");
BlockConditionsDTO userUUID = apiMgtDAO.addBlockConditions(userBlockcondition);
assertNotNull(apiMgtDAO.getBlockConditionByUUID(apiUUID.getUUID()));
assertNotNull(userUUID);
assertNotNull(apiMgtDAO.updateBlockConditionState(apiMgtDAO.getBlockConditionByUUID(userUUID.getUUID()).getConditionId(), "FALSE"));
assertNotNull(apiMgtDAO.updateBlockConditionStateByUUID(userUUID.getUUID(), "FALSE"));
apiMgtDAO.deleteBlockCondition(apiMgtDAO.getBlockConditionByUUID(userUUID.getUUID()).getConditionId());
apiMgtDAO.getBlockCondition(apiMgtDAO.getBlockConditionByUUID(ipUUID.getUUID()).getConditionId());
List<BlockConditionsDTO> blockConditions = apiMgtDAO.getBlockConditions("carbon.super");
for (BlockConditionsDTO blockConditionsDTO : blockConditions) {
apiMgtDAO.deleteBlockConditionByUUID(blockConditionsDTO.getUUID());
}
apiMgtDAO.deleteApplication(application);
apiMgtDAO.removeThrottlePolicy(PolicyConstants.POLICY_LEVEL_APP, applicationPolicy.getPolicyName(), -1234);
apiMgtDAO.deleteAPI(api.getUuid());
deleteSubscriber(subscriber.getId());
}
use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.
the class APIMgtDAOTest method testCreateApplicationRegistrationEntry.
@Test
public void testCreateApplicationRegistrationEntry() throws Exception {
Subscriber subscriber = new Subscriber("testCreateApplicationRegistrationEntry");
subscriber.setTenantId(-1234);
subscriber.setEmail("abc@wso2.com");
subscriber.setSubscribedDate(new Date(System.currentTimeMillis()));
apiMgtDAO.addSubscriber(subscriber, null);
Policy applicationPolicy = getApplicationPolicy("testCreateApplicationRegistrationEntry");
applicationPolicy.setTenantId(-1234);
apiMgtDAO.addApplicationPolicy((ApplicationPolicy) applicationPolicy);
Application application = new Application("testCreateApplicationRegistrationEntry", subscriber);
application.setTier("testCreateApplicationRegistrationEntry");
application.setId(apiMgtDAO.addApplication(application, "testCreateApplicationRegistrationEntry", "testOrg"));
ApplicationRegistrationWorkflowDTO applicationRegistrationWorkflowDTO = new ApplicationRegistrationWorkflowDTO();
applicationRegistrationWorkflowDTO.setApplication(application);
applicationRegistrationWorkflowDTO.setKeyType("PRODUCTION");
applicationRegistrationWorkflowDTO.setDomainList("*");
applicationRegistrationWorkflowDTO.setWorkflowReference(UUID.randomUUID().toString());
applicationRegistrationWorkflowDTO.setValidityTime(100L);
applicationRegistrationWorkflowDTO.setExternalWorkflowReference(UUID.randomUUID().toString());
applicationRegistrationWorkflowDTO.setStatus(WorkflowStatus.CREATED);
applicationRegistrationWorkflowDTO.setKeyManager("Default");
apiMgtDAO.addWorkflowEntry(applicationRegistrationWorkflowDTO);
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
oAuthApplicationInfo.setJsonString("");
oAuthApplicationInfo.addParameter("tokenScope", "deafault");
oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
applicationRegistrationWorkflowDTO.setAppInfoDTO(oAuthAppRequest);
APIIdentifier apiId = new APIIdentifier("testCreateApplicationRegistrationEntry", "testCreateApplicationRegistrationEntry", "1.0.0");
API api = new API(apiId);
api.setContext("/testCreateApplicationRegistrationEntry");
api.setContextTemplate("/testCreateApplicationRegistrationEntry/{version}");
api.setUUID(UUID.randomUUID().toString());
api.setVersionTimestamp(String.valueOf(System.currentTimeMillis()));
int internalAPIID2 = apiMgtDAO.addAPI(api, -1234, "org1");
api.getId().setId(internalAPIID2);
api.setOrganization("org1");
APIIdentifier apiId1 = new APIIdentifier("testCreateApplicationRegistrationEntry1", "testCreateApplicationRegistrationEntry1", "1.0.0");
API api1 = new API(apiId1);
api1.setContext("/testCreateApplicationRegistrationEntry1");
api1.setContextTemplate("/testCreateApplicationRegistrationEntry1/{version}");
api1.setUUID(UUID.randomUUID().toString());
int apiInternalId = apiMgtDAO.addAPI(api1, -1234, "org2");
api1.getId().setId(apiInternalId);
api1.setOrganization("org2");
apiMgtDAO.createApplicationRegistrationEntry(applicationRegistrationWorkflowDTO, false);
ApplicationRegistrationWorkflowDTO retrievedApplicationRegistrationWorkflowDTO = new ApplicationRegistrationWorkflowDTO();
retrievedApplicationRegistrationWorkflowDTO.setExternalWorkflowReference(applicationRegistrationWorkflowDTO.getExternalWorkflowReference());
apiMgtDAO.populateAppRegistrationWorkflowDTO(retrievedApplicationRegistrationWorkflowDTO);
ApiTypeWrapper apiTypeWrapper = new ApiTypeWrapper(api);
ApiTypeWrapper apiTypeWrapper1 = new ApiTypeWrapper(api1);
apiMgtDAO.addSubscription(apiTypeWrapper, application, APIConstants.SubscriptionStatus.ON_HOLD, subscriber.getName());
int subsId = apiMgtDAO.addSubscription(apiTypeWrapper1, application, APIConstants.SubscriptionStatus.ON_HOLD, subscriber.getName());
assertTrue(apiMgtDAO.isContextExist(api.getContext()));
assertTrue(api.getContext().equals(apiMgtDAO.getAPIContext(api.getUuid())));
apiMgtDAO.removeSubscription(apiId, application.getId());
apiMgtDAO.removeSubscriptionById(subsId);
apiMgtDAO.deleteAPI(api.getUuid());
apiMgtDAO.deleteAPI(api1.getUuid());
assertNotNull(apiMgtDAO.getWorkflowReference(application.getName(), subscriber.getName()));
applicationRegistrationWorkflowDTO.setStatus(WorkflowStatus.APPROVED);
apiMgtDAO.updateWorkflowStatus(applicationRegistrationWorkflowDTO);
assertNotNull(apiMgtDAO.retrieveWorkflow(applicationRegistrationWorkflowDTO.getExternalWorkflowReference()));
assertNotNull(apiMgtDAO.retrieveWorkflowFromInternalReference(applicationRegistrationWorkflowDTO.getWorkflowReference(), applicationRegistrationWorkflowDTO.getWorkflowType()));
apiMgtDAO.removeWorkflowEntry(applicationRegistrationWorkflowDTO.getExternalWorkflowReference(), applicationRegistrationWorkflowDTO.getWorkflowType());
apiMgtDAO.deleteApplicationKeyMappingByApplicationIdAndType(application.getId(), "PRODUCTION");
apiMgtDAO.deleteApplicationRegistration(application.getId(), "PRODUCTION", APIConstants.KeyManager.DEFAULT_KEY_MANAGER);
apiMgtDAO.deleteApplication(application);
apiMgtDAO.removeThrottlePolicy(PolicyConstants.POLICY_LEVEL_APP, "testCreateApplicationRegistrationEntry", -1234);
deleteSubscriber(subscriber.getId());
}
Aggregations