Search in sources :

Example 16 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtilTestCase method fromAdvancedPolicyToDTOTest.

@Test(description = "Convert Policy to DTO")
public void fromAdvancedPolicyToDTOTest() throws Exception {
    APIPolicy apiPolicy = new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY);
    String uuid = UUID.randomUUID().toString();
    String displayName = "SampleAPIPolicy";
    String description = "Sample Description";
    apiPolicy.setUuid(uuid);
    apiPolicy.setDisplayName(displayName);
    apiPolicy.setDescription(description);
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType("requestCount");
    RequestCountLimit requestCountLimit = new RequestCountLimit("s", 60, 10);
    quotaPolicy.setLimit(requestCountLimit);
    apiPolicy.setDefaultQuotaPolicy(quotaPolicy);
    AdvancedThrottlePolicyDTO dto = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(apiPolicy);
    Assert.assertNotNull(dto);
    Assert.assertEquals(dto.getPolicyName(), APIMgtConstants.DEFAULT_API_POLICY);
    Assert.assertEquals(dto.getDisplayName(), displayName);
    Assert.assertEquals(dto.getDescription(), description);
    Assert.assertEquals(apiPolicy.getDefaultQuotaPolicy().getLimit().getTimeUnit(), dto.getDefaultLimit().getTimeUnit());
    Assert.assertEquals((Integer) apiPolicy.getDefaultQuotaPolicy().getLimit().getUnitTime(), dto.getDefaultLimit().getUnitTime());
    Assert.assertEquals((Integer) ((RequestCountLimit) apiPolicy.getDefaultQuotaPolicy().getLimit()).getRequestCount(), dto.getDefaultLimit().getRequestCountLimit().getRequestCount());
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Example 17 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtilTestCase method fromAdvancedPolicyDTOToPolicyTest.

@Test(description = "Convert Policy DTO to Policy object")
public void fromAdvancedPolicyDTOToPolicyTest() throws Exception {
    AdvancedThrottlePolicyDTO dto = new AdvancedThrottlePolicyDTO();
    dto.setDisplayName(APIMgtConstants.DEFAULT_API_POLICY);
    String uuid = UUID.randomUUID().toString();
    String description = "Sample Description";
    dto.setDescription(description);
    dto.setId(uuid);
    ThrottleLimitDTO throttleLimitDTO = new ThrottleLimitDTO();
    throttleLimitDTO.setType("RequestCountLimit");
    throttleLimitDTO.setTimeUnit("s");
    throttleLimitDTO.setUnitTime(1);
    RequestCountLimitDTO requestCountLimitDTO = new RequestCountLimitDTO();
    requestCountLimitDTO.setRequestCount(2);
    throttleLimitDTO.setRequestCountLimit(requestCountLimitDTO);
    dto.setDefaultLimit(throttleLimitDTO);
    APIPolicy policy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(dto);
    Assert.assertNotNull(policy);
    Assert.assertEquals(policy.getDisplayName(), APIMgtConstants.DEFAULT_API_POLICY);
    Assert.assertEquals(policy.getDescription(), description);
    Assert.assertEquals(policy.getDefaultQuotaPolicy().getType(), "requestCount");
    Assert.assertEquals(policy.getDefaultQuotaPolicy().getLimit().getTimeUnit(), dto.getDefaultLimit().getTimeUnit());
    Assert.assertEquals((Integer) policy.getDefaultQuotaPolicy().getLimit().getUnitTime(), dto.getDefaultLimit().getUnitTime());
    Assert.assertEquals((Integer) ((RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit()).getRequestCount(), dto.getDefaultLimit().getRequestCountLimit().getRequestCount());
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) RequestCountLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.RequestCountLimitDTO) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) ThrottleLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleLimitDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Example 18 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtilTestCase method fromAPIPolicyArrayToListDTOTest.

@Test(description = "Convert Policy list to Policy List DTO object")
public void fromAPIPolicyArrayToListDTOTest() throws Exception {
    APIPolicy policy1 = SampleTestObjectCreator.createAPIPolicyWithRequestLimit("Gold1");
    APIPolicy policy2 = SampleTestObjectCreator.createAPIPolicyWithRequestLimit("Gold2");
    List<APIPolicy> policyList = new ArrayList<>();
    policyList.add(policy1);
    policyList.add(policy2);
    AdvancedThrottlePolicyListDTO listDTO = AdvancedThrottlePolicyMappingUtil.fromAPIPolicyArrayToListDTO(policyList);
    Assert.assertEquals((Integer) policyList.size(), listDTO.getCount());
    Assert.assertEquals(policy1.getDisplayName(), listDTO.getList().get(0).getDisplayName());
    Assert.assertEquals(policy1.getDescription(), listDTO.getList().get(0).getDescription());
    Assert.assertEquals(policy1.getDefaultQuotaPolicy().getType(), "requestCount");
    Assert.assertEquals(policy1.getDefaultQuotaPolicy().getLimit().getTimeUnit(), listDTO.getList().get(0).getDefaultLimit().getTimeUnit());
    Assert.assertEquals((Integer) policy1.getDefaultQuotaPolicy().getLimit().getUnitTime(), listDTO.getList().get(0).getDefaultLimit().getUnitTime());
    Assert.assertEquals((Integer) ((RequestCountLimit) policy1.getDefaultQuotaPolicy().getLimit()).getRequestCount(), listDTO.getList().get(0).getDefaultLimit().getRequestCountLimit().getRequestCount());
    Assert.assertEquals(policy2.getDisplayName(), listDTO.getList().get(1).getDisplayName());
    Assert.assertEquals(policy2.getDescription(), listDTO.getList().get(1).getDescription());
    Assert.assertEquals(policy2.getDefaultQuotaPolicy().getType(), "requestCount");
    Assert.assertEquals(policy2.getDefaultQuotaPolicy().getLimit().getTimeUnit(), listDTO.getList().get(1).getDefaultLimit().getTimeUnit());
    Assert.assertEquals((Integer) policy2.getDefaultQuotaPolicy().getLimit().getUnitTime(), listDTO.getList().get(1).getDefaultLimit().getUnitTime());
    Assert.assertEquals((Integer) ((RequestCountLimit) policy2.getDefaultQuotaPolicy().getLimit()).getRequestCount(), listDTO.getList().get(1).getDefaultLimit().getRequestCountLimit().getRequestCount());
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) ArrayList(java.util.ArrayList) AdvancedThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyListDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Example 19 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class TierMappingUtilTestCase method testFromTierListToDTO.

@Test
public void testFromTierListToDTO() {
    Policy policy1 = SampleTestObjectCreator.createSubscriptionPolicyWithRequestLimit("Gold");
    Policy policy2 = SampleTestObjectCreator.createSubscriptionPolicyWithBndwidthLimit("Silver");
    List<Policy> policyList = new ArrayList<>();
    policyList.add(policy1);
    policyList.add(policy2);
    TierListDTO tierListDTO = TierMappingUtil.fromTierListToDTO(policyList, "subscription", 10, 0);
    assertEquals(tierListDTO.getCount(), (Integer) policyList.size());
    assertEquals(tierListDTO.getList().get(0).getName(), policy1.getPolicyName());
    assertEquals(tierListDTO.getList().get(0).getDescription(), policy1.getDescription());
    assertEquals(tierListDTO.getList().get(0).getTierLevel().name(), "SUBSCRIPTION");
    assertEquals(tierListDTO.getList().get(0).getUnitTime().longValue(), policy1.getDefaultQuotaPolicy().getLimit().getUnitTime());
    assertEquals(tierListDTO.getList().get(0).getRequestCount().longValue(), ((RequestCountLimit) policy1.getDefaultQuotaPolicy().getLimit()).getRequestCount());
    assertEquals(tierListDTO.getList().get(1).getName(), policy2.getPolicyName());
    assertEquals(tierListDTO.getList().get(1).getDescription(), policy2.getDescription());
    assertEquals(tierListDTO.getList().get(1).getTierLevel().name(), "SUBSCRIPTION");
    assertEquals(tierListDTO.getList().get(1).getUnitTime().longValue(), policy2.getDefaultQuotaPolicy().getLimit().getUnitTime());
    assertEquals(tierListDTO.getList().get(1).getRequestCount().longValue(), ((BandwidthLimit) policy2.getDefaultQuotaPolicy().getLimit()).getDataAmount());
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) ArrayList(java.util.ArrayList) TierListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO) Test(org.testng.annotations.Test)

Example 20 with RequestCountLimit

use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.

the class APIUtil method getTiersFromPolicies.

public static Map<String, Tier> getTiersFromPolicies(String policyLevel, int tenantId) throws APIManagementException {
    Map<String, Tier> tierMap = new TreeMap<String, Tier>();
    ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
    Policy[] policies;
    if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
        policies = apiMgtDAO.getSubscriptionPolicies(tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_API.equalsIgnoreCase(policyLevel)) {
        policies = apiMgtDAO.getAPIPolicies(tenantId);
    } else if (PolicyConstants.POLICY_LEVEL_APP.equalsIgnoreCase(policyLevel)) {
        policies = apiMgtDAO.getApplicationPolicies(tenantId);
    } else {
        throw new APIManagementException("No such a policy type : " + policyLevel);
    }
    for (Policy policy : policies) {
        if (!APIConstants.UNLIMITED_TIER.equalsIgnoreCase(policy.getPolicyName())) {
            Tier tier = new Tier(policy.getPolicyName());
            tier.setDescription(policy.getDescription());
            tier.setDisplayName(policy.getDisplayName());
            Limit limit = policy.getDefaultQuotaPolicy().getLimit();
            tier.setTimeUnit(limit.getTimeUnit());
            tier.setUnitTime(limit.getUnitTime());
            tier.setQuotaPolicyType(policy.getDefaultQuotaPolicy().getType());
            // If the policy is a subscription policy
            if (policy instanceof SubscriptionPolicy) {
                SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) policy;
                tier.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
                tier.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
                setBillingPlanAndCustomAttributesToTier(subscriptionPolicy, tier);
                if (StringUtils.equals(subscriptionPolicy.getBillingPlan(), APIConstants.COMMERCIAL_TIER_PLAN)) {
                    tier.setMonetizationAttributes(subscriptionPolicy.getMonetizationPlanProperties());
                }
            }
            if (limit instanceof RequestCountLimit) {
                RequestCountLimit countLimit = (RequestCountLimit) limit;
                tier.setRequestsPerMin(countLimit.getRequestCount());
                tier.setRequestCount(countLimit.getRequestCount());
            } else if (limit instanceof BandwidthLimit) {
                BandwidthLimit bandwidthLimit = (BandwidthLimit) limit;
                tier.setRequestsPerMin(bandwidthLimit.getDataAmount());
                tier.setRequestCount(bandwidthLimit.getDataAmount());
                tier.setBandwidthDataUnit(bandwidthLimit.getDataUnit());
            } else {
                EventCountLimit eventCountLimit = (EventCountLimit) limit;
                tier.setRequestCount(eventCountLimit.getEventCount());
                tier.setRequestsPerMin(eventCountLimit.getEventCount());
            }
            if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
                tier.setTierPlan(((SubscriptionPolicy) policy).getBillingPlan());
            }
            tierMap.put(policy.getPolicyName(), tier);
        } else {
            if (APIUtil.isEnabledUnlimitedTier()) {
                Tier tier = new Tier(policy.getPolicyName());
                tier.setDescription(policy.getDescription());
                tier.setDisplayName(policy.getDisplayName());
                tier.setRequestsPerMin(Integer.MAX_VALUE);
                tier.setRequestCount(Integer.MAX_VALUE);
                if (isUnlimitedTierPaid(getTenantDomainFromTenantId(tenantId))) {
                    tier.setTierPlan(APIConstants.COMMERCIAL_TIER_PLAN);
                } else {
                    tier.setTierPlan(APIConstants.BILLING_PLAN_FREE);
                }
                tierMap.put(policy.getPolicyName(), tier);
            }
        }
    }
    if (PolicyConstants.POLICY_LEVEL_SUB.equalsIgnoreCase(policyLevel)) {
        tierMap.remove(APIConstants.UNAUTHENTICATED_TIER);
    }
    return tierMap;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) Tier(org.wso2.carbon.apimgt.api.model.Tier) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) TreeMap(java.util.TreeMap) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Limit(org.wso2.carbon.apimgt.api.model.policy.Limit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Aggregations

RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)33 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)30 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)25 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)22 ArrayList (java.util.ArrayList)16 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)16 Test (org.testng.annotations.Test)14 EventCountLimit (org.wso2.carbon.apimgt.api.model.policy.EventCountLimit)12 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)12 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)11 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)9 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)9 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)8 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)8 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)7 PreparedStatement (java.sql.PreparedStatement)6 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)6 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)5