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 policy into a Throttle Limit DTO object.
*
* @param policy policy model object
* @return Throttle Limit DTO
*/
private static ThrottleLimitDTO getThrottleLimitDTO(Policy policy) {
QuotaPolicy quotaPolicy = policy.getQuotaPolicy();
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;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromDTOToLimit.
/**
* Converts a Throttle Limit DTO object into a Limit object
*
* @param dto Throttle Limit DTO object
* @return Limit object derived from DTO
* @throws UnsupportedThrottleLimitTypeException - If error occurs
*/
public static Limit fromDTOToLimit(ThrottleLimitDTO dto) throws UnsupportedThrottleLimitTypeException {
if (PolicyConstants.BANDWIDTH_LIMIT_TYPE.equals(dto.getType())) {
// check if all required params are available
if (dto.getBandwidthLimit() == null || dto.getBandwidthLimit().getDataAmount() == null || dto.getBandwidthLimit().getDataUnit() == null) {
// can't continue, throw
String errorMsg = "One or more required params are missing for the ThrottleLimit type: " + dto.getType();
log.error(errorMsg);
throw new UnsupportedThrottleLimitTypeException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
return fromDTOToBandwidthLimit(dto);
} else if (PolicyConstants.REQUEST_COUNT_LIMIT_TYPE.equals(dto.getType())) {
// check if all required params are available
if (dto.getRequestCountLimit() == null || dto.getRequestCountLimit().getRequestCount() == null) {
// can't continue, throw
String errorMsg = "One or more required params are missing for the ThrottleLimit type: " + dto.getType();
log.error(errorMsg);
throw new UnsupportedThrottleLimitTypeException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
return fromDTOToRequestCountLimit(dto);
} else {
String msg = "Throttle limit type " + dto.getType() + " is not supported";
log.error(msg);
throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
}
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromRequestCountLimitToDTO.
/**
* Converts a Request Count Limit model object into a Request Count Limit DTO object
*
* @param requestCountLimit Request Count Limit model object
* @return Request Count DTO object derived from model
*/
public static ThrottleLimitDTO fromRequestCountLimitToDTO(RequestCountLimit requestCountLimit) {
// done
ThrottleLimitDTO dto = new ThrottleLimitDTO();
dto = updateFieldsFromLimitToDTO(requestCountLimit, dto);
dto.setType("RequestCountLimit");
dto.setRequestCountLimit(new RequestCountLimitDTO());
dto.getRequestCountLimit().setRequestCount(requestCountLimit.getRequestCount());
return dto;
}
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 - If error occurs
*/
public static QuotaPolicy fromDTOToQuotaPolicy(ThrottleLimitDTO dto) throws UnsupportedThrottleLimitTypeException {
QuotaPolicy quotaPolicy = new QuotaPolicy();
quotaPolicy.setLimit(fromDTOToLimit(dto));
quotaPolicy.setType(mapQuotaPolicyTypeFromDTOToModel(dto.getType()));
return quotaPolicy;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ThrottleLimitDTO in project carbon-apimgt by wso2.
the class SubscriptionThrottlePolicyMappingUtilTest method fromSubscriptionThrottlePolicyDTOToModelTest.
@Test(description = "Convert Subscription Throttle Policy DTO to Model")
public void fromSubscriptionThrottlePolicyDTOToModelTest() throws Exception {
SubscriptionThrottlePolicyDTO dto = new SubscriptionThrottlePolicyDTO();
dto.setRateLimitTimeUnit("m");
dto.setRateLimitCount(1);
dto.setStopOnQuotaReach(true);
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);
List<CustomAttributeDTO> customAttributeDTOs = new ArrayList<>();
CustomAttributeDTO customAttributeDTO1 = new CustomAttributeDTO();
customAttributeDTO1.setName("ABC");
customAttributeDTO1.setValue("ABCVALUE");
CustomAttributeDTO customAttributeDTO2 = new CustomAttributeDTO();
customAttributeDTO2.setName("CDE");
customAttributeDTO2.setValue("CDEVALUE");
customAttributeDTOs.add(customAttributeDTO1);
customAttributeDTOs.add(customAttributeDTO2);
dto.setCustomAttributes(customAttributeDTOs);
SubscriptionPolicy policy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(dto);
Assert.assertNotNull(policy);
Assert.assertEquals(policy.getRateLimitCount(), 1);
Assert.assertEquals(policy.getRateLimitTimeUnit(), "m");
Assert.assertEquals(policy.isStopOnQuotaReach(), true);
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());
Assert.assertTrue(new String(policy.getCustomAttributes()).contains("ABC"));
Assert.assertTrue(new String(policy.getCustomAttributes()).contains("ABCVALUE"));
Assert.assertTrue(new String(policy.getCustomAttributes()).contains("CDE"));
Assert.assertTrue(new String(policy.getCustomAttributes()).contains("CDEVALUE"));
}
Aggregations