Search in sources :

Example 11 with ThrottleLimitDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO in project carbon-apimgt by wso2.

the class ApplicationThrottlePolicyMappingUtilTest method fromApplicationThrottlePolicyDTOToModelTest.

@Test(description = "Convert from DTO to Policy")
public void fromApplicationThrottlePolicyDTOToModelTest() throws Exception {
    ApplicationThrottlePolicyDTO dto = new ApplicationThrottlePolicyDTO();
    dto.setDisplayName(displayName);
    dto.setPolicyName(policyName);
    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);
    ApplicationPolicy policy = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyDTOToModel(dto);
    Assert.assertNotNull(policy);
    Assert.assertEquals(policy.getDisplayName(), displayName);
    Assert.assertEquals(policy.getPolicyName(), policyName);
    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) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ThrottleLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleLimitDTO) ApplicationThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ApplicationThrottlePolicyDTO) Test(org.testng.annotations.Test)

Example 12 with ThrottleLimitDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO in project carbon-apimgt by wso2.

the class ThrottlingPolicyMappingUtil method fromSubscriptionToDTO.

public static SubscriptionPolicyDTO fromSubscriptionToDTO(SubscriptionPolicy subscriptionPolicy, int position) {
    SubscriptionPolicyDTO dto = new SubscriptionPolicyDTO();
    dto.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
    dto.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
    dto.setStopOnQuotaReach(subscriptionPolicy.isStopOnQuotaReach());
    ThrottleLimitDTO limitDTO = new ThrottleLimitDTO();
    limitDTO.setType(ThrottleLimitDTO.TypeEnum.EVENTCOUNTLIMIT);
    EventCountLimit eventCountLimit = (EventCountLimit) subscriptionPolicy.getDefaultQuotaPolicy().getLimit();
    EventCountLimitDTO eventCountLimitDTO = new EventCountLimitDTO();
    eventCountLimitDTO.setEventCount(eventCountLimit.getEventCount());
    eventCountLimitDTO.setTimeUnit(eventCountLimit.getTimeUnit());
    eventCountLimitDTO.setUnitTime(eventCountLimit.getUnitTime());
    limitDTO.setEventCount(eventCountLimitDTO);
    dto.setDefaultLimit(limitDTO);
    dto.setSubscriberCount(subscriptionPolicy.getSubscriberCount());
    dto.setDisplayName(subscriptionPolicy.getDisplayName());
    dto.setDescription(subscriptionPolicy.getDescription());
    dto.setIsDeployed(subscriptionPolicy.isDeployed());
    dto.setPolicyName(subscriptionPolicy.getPolicyName());
    dto.setBillingPlan(subscriptionPolicy.getBillingPlan());
    dto.setPolicyId(position);
    dto.setUuid(subscriptionPolicy.getUUID());
    dto.setIsDeployed(subscriptionPolicy.isDeployed());
    dto.setTenantDomain(subscriptionPolicy.getTenantDomain());
    dto.setTenantId(subscriptionPolicy.getTenantId());
    return dto;
}
Also used : EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) EventCountLimitDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.EventCountLimitDTO) ThrottleLimitDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO) SubscriptionPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionPolicyDTO)

Example 13 with ThrottleLimitDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO in project carbon-apimgt by wso2.

the class SubscriptionValidationDataUtil method getThrottleLimitDTO.

/**
 * Converts a quota policy object of a condition group into a Throttle Limit DTO object.
 *
 * @param apiPolicyConditionGroup condition group model object
 * @return Throttle Limit DTO
 */
private static ThrottleLimitDTO getThrottleLimitDTO(APIPolicyConditionGroup apiPolicyConditionGroup) {
    QuotaPolicy quotaPolicy = apiPolicyConditionGroup.getQuotaPolicy();
    if (quotaPolicy != null) {
        ThrottleLimitDTO defaultLimit = new ThrottleLimitDTO();
        defaultLimit.setQuotaType(quotaPolicy.getType());
        if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
            RequestCountLimit requestCountLimit = (RequestCountLimit) quotaPolicy.getLimit();
            defaultLimit.setRequestCount(fromRequestCountLimitToDTO(requestCountLimit));
        } else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
            BandwidthLimit bandwidthLimit = (BandwidthLimit) quotaPolicy.getLimit();
            defaultLimit.setBandwidth(fromBandwidthLimitToDTO(bandwidthLimit));
        } else if (PolicyConstants.EVENT_COUNT_TYPE.equals(quotaPolicy.getType())) {
            EventCountLimit eventCountLimit = (EventCountLimit) quotaPolicy.getLimit();
            defaultLimit.setEventCount(fromEventCountLimitToDTO(eventCountLimit));
        }
        return defaultLimit;
    }
    return null;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) ThrottleLimitDTO(org.wso2.carbon.apimgt.internal.service.dto.ThrottleLimitDTO) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 14 with ThrottleLimitDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromQuotaPolicyToDTO.

/**
 * Converts a Quota Policy object into a Throttle Limit DTO object
 *
 * @param quotaPolicy Quota Policy object
 * @return Throttle Limit DTO object derived from the Quota Policy object
 * @throws UnsupportedThrottleLimitTypeException
 */
public static ThrottleLimitDTO fromQuotaPolicyToDTO(QuotaPolicy quotaPolicy) throws UnsupportedThrottleLimitTypeException {
    ThrottleLimitDTO defaultLimitType = new ThrottleLimitDTO();
    if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
        RequestCountLimit requestCountLimit = (RequestCountLimit) quotaPolicy.getLimit();
        defaultLimitType.setType(ThrottleLimitDTO.TypeEnum.REQUESTCOUNTLIMIT);
        defaultLimitType.setRequestCount(fromRequestCountLimitToDTO(requestCountLimit));
    } else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
        BandwidthLimit bandwidthLimit = (BandwidthLimit) quotaPolicy.getLimit();
        defaultLimitType.setType(ThrottleLimitDTO.TypeEnum.BANDWIDTHLIMIT);
        defaultLimitType.setBandwidth(fromBandwidthLimitToDTO(bandwidthLimit));
    } else if (PolicyConstants.EVENT_COUNT_TYPE.equals(quotaPolicy.getType())) {
        EventCountLimit eventCountLimit = (EventCountLimit) quotaPolicy.getLimit();
        defaultLimitType.setType(ThrottleLimitDTO.TypeEnum.EVENTCOUNTLIMIT);
        defaultLimitType.setEventCount(fromEventCountLimitToDTO(eventCountLimit));
    } else {
        String msg = "Throttle limit type " + quotaPolicy.getType() + " is not supported";
        throw new UnsupportedThrottleLimitTypeException(msg);
    }
    return defaultLimitType;
}
Also used : UnsupportedThrottleLimitTypeException(org.wso2.carbon.apimgt.api.UnsupportedThrottleLimitTypeException) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) ThrottleLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ThrottleLimitDTO) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 15 with ThrottleLimitDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromDTOToQuotaPolicy.

/**
 * Converts a Throttle Limit DTO object into a Quota Policy object
 *
 * @param dto Throttle limit DTO object
 * @return Derived Quota policy object from DTO
 * @throws UnsupportedThrottleLimitTypeException
 */
public static QuotaPolicy fromDTOToQuotaPolicy(ThrottleLimitDTO dto) throws UnsupportedThrottleLimitTypeException {
    String errorMessage;
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    ThrottleLimitDTO.TypeEnum limitType = dto.getType();
    if (limitType != null) {
        switch(dto.getType()) {
            case REQUESTCOUNTLIMIT:
                {
                    if (dto.getRequestCount() != null) {
                        quotaPolicy.setLimit(fromDTOToRequestCountLimit(dto.getRequestCount()));
                    } else {
                        errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleLimitDTO.TypeEnum.REQUESTCOUNTLIMIT) + dto.toString();
                        throw new UnsupportedThrottleLimitTypeException(errorMessage);
                    }
                    break;
                }
            case BANDWIDTHLIMIT:
                {
                    if (dto.getBandwidth() != null) {
                        quotaPolicy.setLimit(fromDTOToBandwidthLimit(dto.getBandwidth()));
                    } else {
                        errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleLimitDTO.TypeEnum.BANDWIDTHLIMIT) + dto.toString();
                        throw new UnsupportedThrottleLimitTypeException(errorMessage);
                    }
                    break;
                }
            case EVENTCOUNTLIMIT:
                {
                    if (dto.getEventCount() != null) {
                        quotaPolicy.setLimit(fromDTOToEventCountLimit(dto.getEventCount()));
                    } else {
                        errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleLimitDTO.TypeEnum.EVENTCOUNTLIMIT) + dto.toString();
                        throw new UnsupportedThrottleLimitTypeException(errorMessage);
                    }
                    break;
                }
        }
        quotaPolicy.setType(mapQuotaPolicyTypeFromDTOToModel(dto.getType()));
    } else {
        errorMessage = "defaultLimit 'type' property has not been specified\n" + dto.toString();
        throw new UnsupportedThrottleLimitTypeException(errorMessage);
    }
    return quotaPolicy;
}
Also used : UnsupportedThrottleLimitTypeException(org.wso2.carbon.apimgt.api.UnsupportedThrottleLimitTypeException) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) ThrottleLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ThrottleLimitDTO)

Aggregations

Test (org.testng.annotations.Test)5 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)5 ThrottleLimitDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleLimitDTO)5 EventCountLimit (org.wso2.carbon.apimgt.api.model.policy.EventCountLimit)4 RequestCountLimitDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.RequestCountLimitDTO)4 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)3 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)3 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)3 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)3 UnsupportedThrottleLimitTypeException (org.wso2.carbon.apimgt.api.UnsupportedThrottleLimitTypeException)2 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)2 ThrottleLimitDTO (org.wso2.carbon.apimgt.internal.service.dto.ThrottleLimitDTO)2 UnsupportedThrottleLimitTypeException (org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException)2 ThrottleLimitDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ThrottleLimitDTO)2 ArrayList (java.util.ArrayList)1 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)1 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)1 Limit (org.wso2.carbon.apimgt.core.models.policy.Limit)1 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)1 AdvancedThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)1