use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testDeleteCustomRule.
@Test(description = "Test deleting a custom rule")
public void testDeleteCustomRule() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
CustomPolicy customPolicy = SampleTestObjectCreator.createDefaultCustomPolicy();
adminService.deleteCustomRule(customPolicy.getUuid());
// Error path
Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).deleteCustomPolicy(customPolicy.getUuid());
try {
adminService.deleteCustomRule(customPolicy.getUuid());
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't delete custom policy with UUID: " + customPolicy.getUuid());
}
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class APIMgtAdminServiceImplTestCase method testGetCustomRuleByUUID.
@Test(description = "Test getting custom rule by uuid")
public void testGetCustomRuleByUUID() throws APIManagementException {
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
CustomPolicy customPolicy = SampleTestObjectCreator.createDefaultCustomPolicy();
Mockito.when(policyDAO.getCustomPolicyByUuid(customPolicy.getUuid())).thenReturn(customPolicy);
CustomPolicy customPolicyReturned = adminService.getCustomRuleByUUID(customPolicy.getUuid());
Assert.assertEquals(customPolicyReturned, customPolicy);
// Error path
Mockito.when(policyDAO.getCustomPolicyByUuid(customPolicy.getUuid())).thenThrow(APIMgtDAOException.class);
try {
adminService.getCustomRuleByUUID(customPolicy.getUuid());
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Couldn't get custom policy by UUID: " + customPolicy.getUuid());
}
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class PolicyExportManager method createArchiveFromExecutionPlans.
public String createArchiveFromExecutionPlans(String exportedPoliciesDirName, String archiveDir, String archiveName) throws APIManagementException {
try {
// retrieve all policies under each policy level
List<APIPolicy> apiPolicies = apiMgtAdminService.getApiPolicies();
List<ApplicationPolicy> applicationPolicies = apiMgtAdminService.getApplicationPolicies();
List<SubscriptionPolicy> subscriptionPolicies = apiMgtAdminService.getSubscriptionPolicies();
List<CustomPolicy> customPolicies = apiMgtAdminService.getCustomRules();
// write all execution Plans/Siddhi Apps to exportPoliciesDirName directory
String dirLocation = exportedPoliciesDirName + File.separator + EXPORT_POLICIES;
APIFileUtils.createDirectory(dirLocation);
if (!apiPolicies.isEmpty()) {
for (Map<String, String> map : getApiPolicySiddhiApps(apiPolicies)) {
prepareFile(dirLocation, map);
}
}
if (!applicationPolicies.isEmpty()) {
prepareFile(dirLocation, getAppPolicySiddhiApps(applicationPolicies));
}
if (!subscriptionPolicies.isEmpty()) {
prepareFile(dirLocation, getSubscriptionPolicySiddhiApps(subscriptionPolicies));
}
if (!customPolicies.isEmpty()) {
prepareFile(dirLocation, getCustomPolicySiddhiApps(customPolicies));
}
// create archive and get the archive location
String zippedFilePath = createArchiveFromPolicies(exportedPoliciesDirName, archiveDir, archiveName);
APIFileUtils.deleteDirectory(exportedPoliciesDirName);
return zippedFilePath;
} catch (APIManagementException e) {
String errorMessage = "Error while exporting policies";
log.error(errorMessage, e);
throw new APIManagementException(errorMessage, e);
}
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class PolicyExportManager method getCustomPolicySiddhiApps.
/**
* Get execution plan/ siddhi apps for custom policies.
*
* @param customPolicies custom policy object list
* @return Map<String, String> containing execution plan name and execution plans.
* @throws APITemplateException If template generating fails
*/
private Map<String, String> getCustomPolicySiddhiApps(List<CustomPolicy> customPolicies) throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Get execution plans for custom policies.");
}
Map<String, String> siddhiApps = new HashMap<>();
String name;
String executionPlan;
CustomThrottlePolicyTemplateBuilder templateBuilder;
for (CustomPolicy policy : customPolicies) {
templateBuilder = new CustomThrottlePolicyTemplateBuilder(policy);
name = CUSTOM + policy.getPolicyName();
executionPlan = templateBuilder.getThrottlePolicyTemplateForCustomPolicy();
siddhiApps.put(name, executionPlan);
}
return siddhiApps;
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultCustomPolicy.
public static CustomPolicy createDefaultCustomPolicy() {
CustomPolicy customPolicy = new CustomPolicy(SAMPLE_CUSTOM_RULE);
customPolicy.setKeyTemplate("$userId");
String siddhiQuery = "FROM RequestStream SELECT userId, ( userId == 'admin@carbon.super' ) AS isEligible , " + "str:concat('admin@carbon.super','') as throttleKey INSERT INTO EligibilityStream;" + "FROM EligibilityStream[isEligible==true]#throttler:timeBatch(1 min) SELECT throttleKey, " + "(count(userId) >= 5 as isThrottled, expiryTimeStamp group by throttleKey INSERT ALL EVENTS into " + "ResultStream;";
customPolicy.setSiddhiQuery(siddhiQuery);
customPolicy.setDescription("Sample custom policy");
return customPolicy;
}
Aggregations