Search in sources :

Example 81 with ApiPolicy

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

the class APIMgtDAOTest method testUpdateAPIPolicy.

@Test
public void testUpdateAPIPolicy() throws APIManagementException {
    String policyName = "TestUpdateApiPolicy";
    APIPolicy policy = (APIPolicy) getPolicyAPILevelPerUser(policyName);
    apiMgtDAO.addAPIPolicy(policy);
    policy = apiMgtDAO.getAPIPolicy(policyName, -1234);
    policy.setDescription("New Description");
    ArrayList<Pipeline> pipelines = new ArrayList<Pipeline>();
    Pipeline p = new Pipeline();
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType("requestCount");
    RequestCountLimit requestCountLimit = new RequestCountLimit();
    requestCountLimit.setTimeUnit("min");
    requestCountLimit.setUnitTime(50);
    requestCountLimit.setRequestCount(1000);
    quotaPolicy.setLimit(requestCountLimit);
    ArrayList<Condition> conditions = new ArrayList<Condition>();
    DateCondition dateCondition = new DateCondition();
    dateCondition.setSpecificDate("2016-03-03");
    conditions.add(dateCondition);
    HeaderCondition headerCondition1 = new HeaderCondition();
    headerCondition1.setHeader("User-Agent");
    headerCondition1.setValue("Chrome");
    conditions.add(headerCondition1);
    HeaderCondition headerCondition2 = new HeaderCondition();
    headerCondition2.setHeader("Accept-Ranges");
    headerCondition2.setValue("bytes");
    conditions.add(headerCondition2);
    QueryParameterCondition queryParameterCondition1 = new QueryParameterCondition();
    queryParameterCondition1.setParameter("test1");
    queryParameterCondition1.setValue("testValue1");
    conditions.add(queryParameterCondition1);
    QueryParameterCondition queryParameterCondition2 = new QueryParameterCondition();
    queryParameterCondition2.setParameter("x");
    queryParameterCondition2.setValue("abc");
    conditions.add(queryParameterCondition2);
    JWTClaimsCondition jwtClaimsCondition1 = new JWTClaimsCondition();
    jwtClaimsCondition1.setClaimUrl("test_url");
    jwtClaimsCondition1.setAttribute("test_attribute");
    conditions.add(jwtClaimsCondition1);
    p.setQuotaPolicy(quotaPolicy);
    p.setConditions(conditions);
    pipelines.add(p);
    policy.setPipelines(pipelines);
    apiMgtDAO.updateAPIPolicy(policy);
    APIPolicy apiPolicy = apiMgtDAO.getAPIPolicy(policyName, -1234);
    assertNotNull(apiPolicy);
    List<Pipeline> pipelineList = apiPolicy.getPipelines();
    assertNotNull(pipelineList);
    assertEquals(pipelineList.size(), pipelines.size());
}
Also used : DateRangeCondition(org.wso2.carbon.apimgt.api.model.policy.DateRangeCondition) IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) DateCondition(org.wso2.carbon.apimgt.api.model.policy.DateCondition) HTTPVerbCondition(org.wso2.carbon.apimgt.api.model.policy.HTTPVerbCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) Condition(org.wso2.carbon.apimgt.api.model.policy.Condition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) ArrayList(java.util.ArrayList) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) DateCondition(org.wso2.carbon.apimgt.api.model.policy.DateCondition) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 82 with ApiPolicy

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

the class APIMgtDAOTest method getPolicyAPILevelPerUser.

private Policy getPolicyAPILevelPerUser(String policyName) {
    APIPolicy policy = new APIPolicy(policyName);
    policy.setUserLevel(PolicyConstants.PER_USER);
    policy.setDescription("Description");
    policy.setTenantId(-1234);
    BandwidthLimit defaultLimit = new BandwidthLimit();
    defaultLimit.setTimeUnit("min");
    defaultLimit.setUnitTime(5);
    defaultLimit.setDataAmount(400);
    defaultLimit.setDataUnit("MB");
    QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
    defaultQuotaPolicy.setLimit(defaultLimit);
    defaultQuotaPolicy.setType(PolicyConstants.BANDWIDTH_TYPE);
    policy.setDefaultQuotaPolicy(defaultQuotaPolicy);
    List<Pipeline> pipelines;
    QuotaPolicy quotaPolicy;
    List<Condition> condition;
    BandwidthLimit bandwidthLimit;
    RequestCountLimit requestCountLimit;
    pipelines = new ArrayList<Pipeline>();
    // /////////pipeline item 1 start//////
    Pipeline p1 = new Pipeline();
    quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType(PolicyConstants.BANDWIDTH_TYPE);
    bandwidthLimit = new BandwidthLimit();
    bandwidthLimit.setTimeUnit("min");
    bandwidthLimit.setUnitTime(5);
    bandwidthLimit.setDataAmount(100);
    bandwidthLimit.setDataUnit("GB");
    quotaPolicy.setLimit(bandwidthLimit);
    condition = new ArrayList<Condition>();
    HTTPVerbCondition verbCond = new HTTPVerbCondition();
    verbCond.setHttpVerb("POST");
    condition.add(verbCond);
    IPCondition ipCondition = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
    ipCondition.setSpecificIP("127.0.0.1");
    condition.add(ipCondition);
    DateRangeCondition dateRangeCondition = new DateRangeCondition();
    dateRangeCondition.setStartingDate("2016-01-03");
    dateRangeCondition.setEndingDate("2016-01-31");
    condition.add(dateRangeCondition);
    p1.setQuotaPolicy(quotaPolicy);
    p1.setConditions(condition);
    pipelines.add(p1);
    // /////////pipeline item 1 end//////
    // /////////pipeline item 2 start//////
    Pipeline p2 = new Pipeline();
    quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType("requestCount");
    requestCountLimit = new RequestCountLimit();
    requestCountLimit.setTimeUnit("min");
    requestCountLimit.setUnitTime(50);
    requestCountLimit.setRequestCount(1000);
    quotaPolicy.setLimit(requestCountLimit);
    List<Condition> condition2 = new ArrayList<Condition>();
    DateCondition dateCondition = new DateCondition();
    dateCondition.setSpecificDate("2016-01-02");
    condition2.add(dateCondition);
    HeaderCondition headerCondition1 = new HeaderCondition();
    headerCondition1.setHeader("User-Agent");
    headerCondition1.setValue("Firefox");
    condition2.add(headerCondition1);
    HeaderCondition headerCondition2 = new HeaderCondition();
    headerCondition2.setHeader("Accept-Ranges");
    headerCondition2.setValue("bytes");
    condition2.add(headerCondition2);
    QueryParameterCondition queryParameterCondition1 = new QueryParameterCondition();
    queryParameterCondition1.setParameter("test1");
    queryParameterCondition1.setValue("testValue1");
    condition2.add(queryParameterCondition1);
    QueryParameterCondition queryParameterCondition2 = new QueryParameterCondition();
    queryParameterCondition2.setParameter("test2");
    queryParameterCondition2.setValue("testValue2");
    condition2.add(queryParameterCondition2);
    JWTClaimsCondition jwtClaimsCondition1 = new JWTClaimsCondition();
    jwtClaimsCondition1.setClaimUrl("test_url");
    jwtClaimsCondition1.setAttribute("test_attribute");
    condition2.add(jwtClaimsCondition1);
    JWTClaimsCondition jwtClaimsCondition2 = new JWTClaimsCondition();
    jwtClaimsCondition2.setClaimUrl("test_url");
    jwtClaimsCondition2.setAttribute("test_attribute");
    condition2.add(jwtClaimsCondition2);
    IPCondition ipRangeCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
    ipCondition.setStartingIP("127.0.0.1");
    ipCondition.setEndingIP("127.0.0.12");
    condition2.add(ipRangeCondition);
    p2.setQuotaPolicy(quotaPolicy);
    p2.setConditions(condition2);
    pipelines.add(p2);
    // /////////pipeline item 2 end//////
    policy.setPipelines(pipelines);
    return policy;
}
Also used : DateRangeCondition(org.wso2.carbon.apimgt.api.model.policy.DateRangeCondition) IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) DateCondition(org.wso2.carbon.apimgt.api.model.policy.DateCondition) HTTPVerbCondition(org.wso2.carbon.apimgt.api.model.policy.HTTPVerbCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) Condition(org.wso2.carbon.apimgt.api.model.policy.Condition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) DateRangeCondition(org.wso2.carbon.apimgt.api.model.policy.DateRangeCondition) ArrayList(java.util.ArrayList) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) HTTPVerbCondition(org.wso2.carbon.apimgt.api.model.policy.HTTPVerbCondition) IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit) DateCondition(org.wso2.carbon.apimgt.api.model.policy.DateCondition)

Example 83 with ApiPolicy

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

the class PolicyUtilTest method testDeployAllPolicies.

@Test
public void testDeployAllPolicies() throws ExecutionPlanConfigurationException, ThrottlePolicyDeployerException, ExecutionPlanDependencyValidationException {
    ExecutionPlanConfigurationException executionPlanConfigurationException = Mockito.mock(ExecutionPlanConfigurationException.class);
    Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenThrow(executionPlanConfigurationException);
    Map<String, ExecutionPlanConfiguration> executionPlanConfigurationMap = new HashMap<>();
    ExecutionPlanConfiguration executionPlanConfiguration = Mockito.mock(ExecutionPlanConfiguration.class);
    executionPlanConfigurationMap.put("sample_policy", executionPlanConfiguration);
    Mockito.when(eventProcessorService.getAllActiveExecutionConfigurations()).thenReturn(executionPlanConfigurationMap);
    SubscriptionPolicyList subscriptionPolicyList = new SubscriptionPolicyList();
    SubscriptionPolicy subscriptionPolicy = TestUtil.getPolicySubLevel();
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    subscriptionPolicies.add(subscriptionPolicy);
    subscriptionPolicyList.setList(subscriptionPolicies);
    Mockito.when(policyRetriever.getAllSubscriptionPolicies()).thenReturn(subscriptionPolicyList);
    ApplicationPolicyList applicationPolicyList = new ApplicationPolicyList();
    ApplicationPolicy applicationPolicy = TestUtil.getPolicyAppLevel();
    List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
    applicationPolicies.add(applicationPolicy);
    applicationPolicyList.setList(applicationPolicies);
    Mockito.when(policyRetriever.getAllApplicationPolicies()).thenReturn(applicationPolicyList);
    ApiPolicyList apiPolicyList = new ApiPolicyList();
    ApiPolicy apiPolicy = TestUtil.getPolicyAPILevel();
    List<ApiPolicy> apiPolicies = new ArrayList<>();
    apiPolicies.add(apiPolicy);
    apiPolicyList.setList(apiPolicies);
    Mockito.when(policyRetriever.getAllApiPolicies()).thenReturn(apiPolicyList);
    GlobalPolicyList globalPolicyList = new GlobalPolicyList();
    GlobalPolicy globalPolicy = TestUtil.getPolicyGlobalLevel();
    List<GlobalPolicy> globalPolicies = new ArrayList<>();
    globalPolicies.add(globalPolicy);
    globalPolicyList.setList(globalPolicies);
    Mockito.when(policyRetriever.getAllGlobalPolicies()).thenReturn(globalPolicyList);
    PolicyUtil.deployAllPolicies();
    Mockito.verify(eventProcessorService, Mockito.times(5)).deployExecutionPlan(Mockito.anyString());
}
Also used : ApplicationPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicyList) HashMap(java.util.HashMap) GlobalPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy) ArrayList(java.util.ArrayList) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ExecutionPlanConfigurationException(org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException) SubscriptionPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy) ExecutionPlanConfiguration(org.wso2.carbon.event.processor.core.ExecutionPlanConfiguration) ApplicationPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApplicationPolicy) SubscriptionPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicyList) ApiPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicyList) GlobalPolicyList(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicyList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 84 with ApiPolicy

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

the class PolicyUtilTest method testDeletePolicy_APIType.

@Test
public void testDeletePolicy_APIType() throws ExecutionPlanConfigurationException {
    ApiPolicy policy = TestUtil.getPolicyAPILevel();
    List<Integer> deletedConditionGroupIds = new ArrayList<>();
    deletedConditionGroupIds.add(5);
    deletedConditionGroupIds.add(6);
    Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_condition_5")).thenReturn("EXECUTION_PLAN");
    Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_condition_6")).thenReturn("EXECUTION_PLAN");
    Mockito.when(eventProcessorService.getActiveExecutionPlan(policy.getTenantDomain() + "_" + PolicyConstants.POLICY_LEVEL_RESOURCE + "_" + policy.getName() + "_default")).thenReturn("EXECUTION_PLAN");
    APIPolicyEvent policyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_DELETE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType(), null, deletedConditionGroupIds);
    PolicyUtil.undeployPolicy(policyEvent);
    Mockito.verify(eventProcessorService, Mockito.times(3)).undeployActiveExecutionPlan(Mockito.anyString());
}
Also used : APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 85 with ApiPolicy

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

the class PolicyUtilTest method testAddPolicy_APIType.

@Test
public void testAddPolicy_APIType() throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
    ExecutionPlanConfigurationException executionPlanConfigurationException = Mockito.mock(ExecutionPlanConfigurationException.class);
    Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenThrow(executionPlanConfigurationException);
    ApiPolicy policy = TestUtil.getPolicyAPILevel();
    APIPolicyEvent policyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType(), null, null);
    PolicyUtil.deployPolicy(policy, policyEvent);
    Mockito.verify(eventProcessorService, Mockito.times(2)).deployExecutionPlan(Mockito.anyString());
}
Also used : APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) ApiPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.ApiPolicy) ExecutionPlanConfigurationException(org.wso2.carbon.event.processor.core.exception.ExecutionPlanConfigurationException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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