Search in sources :

Example 51 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testAddPolicyWhenPolicyIdNull.

@Test(description = "Add policy when policy id is null")
public void testAddPolicyWhenPolicyIdNull() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    APIPolicy policy = SampleTestObjectCreator.createDefaultAPIPolicy();
    policy.setUuid(null);
    adminService.addApiPolicy(policy);
    Mockito.verify(policyDAO, Mockito.times(1)).addApiPolicy(policy);
}
Also used : APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 52 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testUpdateApiPolicy.

@Test(description = "Test update API policy")
public void testUpdateApiPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    adminService.updateApiPolicy(apiPolicy);
    Mockito.verify(policyDAO, Mockito.times(1)).updateApiPolicy(apiPolicy);
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).updateApiPolicy(apiPolicy);
    try {
        adminService.updateApiPolicy(apiPolicy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't update API policy for uuid: " + apiPolicy.getUuid());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 53 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class ThrottleTemplateBuilderTestCase method testSiddhiQueryForAPIPolicy.

@Test
public void testSiddhiQueryForAPIPolicy() throws APITemplateException {
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    APIThrottlePolicyTemplateBuilder templateBuilder = new APIThrottlePolicyTemplateBuilder(apiPolicy);
    Map<String, String> siddhiQueryMap = templateBuilder.getThrottlePolicyTemplateForPipelines();
    String actualQuery = siddhiQueryMap.get("resource_SampleAPIPolicy_condition_0");
    String expectedQuery = SampleTestObjectCreator.createDefaultSiddhiAppForAPIThrottlePolicy();
    Assert.assertEquals(actualQuery, expectedQuery);
}
Also used : APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Example 54 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class ThrottleTemplateBuilderTestCase method testSiddhiQueryForAPILevelDefaultConditions.

@Test
public void testSiddhiQueryForAPILevelDefaultConditions() throws APITemplateException {
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    APIThrottlePolicyTemplateBuilder templateBuilder = new APIThrottlePolicyTemplateBuilder(apiPolicy);
    String actualQuery = templateBuilder.getThrottlePolicyTemplateForAPILevelDefaultCondition();
    String expectedQuery = SampleTestObjectCreator.createDefaultSiddhiAppForAPILevelDefaultThrottlePolicy();
    Assert.assertEquals(actualQuery, expectedQuery);
}
Also used : APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Example 55 with ApiPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.ApiPolicy in project carbon-apimgt by wso2.

the class PolicyExportManager method getApiPolicySiddhiApps.

/**
 * Get execution plan/ siddhi apps for custom policies.
 *
 * @param apiPolicies APIPolicy object list
 * @return Map<String, String> containing execution plan name and execution plans.
 * @throws APITemplateException If template generating fails
 */
private List<Map<String, String>> getApiPolicySiddhiApps(List<APIPolicy> apiPolicies) throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Get execution plans for API policies.");
    }
    List<Map<String, String>> policies = new ArrayList<>();
    Map<String, String> siddhiApps = new HashMap<>();
    APIThrottlePolicyTemplateBuilder templateBuilder;
    for (APIPolicy apiPolicy : apiPolicies) {
        templateBuilder = new APIThrottlePolicyTemplateBuilder(apiPolicy);
        if (apiPolicy.getPipelines() != null) {
            siddhiApps = templateBuilder.getThrottlePolicyTemplateForPipelines();
        }
        siddhiApps.put(RESOURCE + apiPolicy.getPolicyName() + DEFAULT, templateBuilder.getThrottlePolicyTemplateForAPILevelDefaultCondition());
        policies.add(siddhiApps);
    }
    return policies;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APIThrottlePolicyTemplateBuilder(org.wso2.carbon.apimgt.core.template.APIThrottlePolicyTemplateBuilder) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)87 Test (org.testng.annotations.Test)44 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)40 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)39 ArrayList (java.util.ArrayList)38 API (org.wso2.carbon.apimgt.core.models.API)33 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)30 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)27 HashMap (java.util.HashMap)25 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)22 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 HashSet (java.util.HashSet)19 Test (org.junit.Test)19 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)19 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 PreparedStatement (java.sql.PreparedStatement)15