Search in sources :

Example 21 with CustomPolicy

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());
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 22 with CustomPolicy

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());
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 23 with CustomPolicy

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);
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 24 with CustomPolicy

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;
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) HashMap(java.util.HashMap) CustomThrottlePolicyTemplateBuilder(org.wso2.carbon.apimgt.core.template.CustomThrottlePolicyTemplateBuilder)

Example 25 with CustomPolicy

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;
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)

Aggregations

CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)29 Test (org.testng.annotations.Test)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)10 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)7 ArrayList (java.util.ArrayList)6 CustomRuleDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)6 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)4 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)4 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2