use of org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottleLimitDTO in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromBandwidthLimitToDTO.
/**
* Converts a Bandwidth Limit model object into a Bandwidth Limit DTO object
*
* @param bandwidthLimit Bandwidth Limit model object
* @return Bandwidth Limit DTO object derived from model
*/
public static ThrottleLimitDTO fromBandwidthLimitToDTO(BandwidthLimit bandwidthLimit) {
// done
ThrottleLimitDTO dto = new ThrottleLimitDTO();
dto = updateFieldsFromLimitToDTO(bandwidthLimit, dto);
dto.setType(PolicyConstants.BANDWIDTH_LIMIT_TYPE);
dto.setBandwidthLimit(new BandwidthLimitDTO());
dto.getBandwidthLimit().setDataAmount(bandwidthLimit.getDataAmount());
dto.getBandwidthLimit().setDataUnit(bandwidthLimit.getDataUnit());
return dto;
}
use of org.wso2.apimgt.gateway.cli.model.rest.policy.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 - If error occurs
*/
public static ThrottleLimitDTO fromQuotaPolicyToDTO(QuotaPolicy quotaPolicy) throws UnsupportedThrottleLimitTypeException {
Limit limit = quotaPolicy.getLimit();
String throttleLimitType = quotaPolicy.getType();
if (PolicyConstants.REQUEST_COUNT_TYPE.equals(throttleLimitType)) {
if (limit instanceof RequestCountLimit) {
RequestCountLimit requestCountLimit = (RequestCountLimit) limit;
return fromRequestCountLimitToDTO(requestCountLimit);
} else {
String msg = "Throttle limit type " + throttleLimitType + " is not of type RequestCountLimit";
log.error(msg);
throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
}
} else if (PolicyConstants.BANDWIDTH_TYPE.equals(throttleLimitType)) {
if (limit instanceof BandwidthLimit) {
BandwidthLimit bandwidthLimit = (BandwidthLimit) limit;
return fromBandwidthLimitToDTO(bandwidthLimit);
} else {
String msg = "Throttle limit type " + throttleLimitType + " is not of type BandwidthLimit";
log.error(msg);
throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
}
} else {
String msg = "Throttle limit type " + throttleLimitType + " is not supported";
log.error(msg);
throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
}
}
use of org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottleLimitDTO in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtilTestCase method fromRequestCountThrottleLimitDtoToQuotaPolicyTest.
@Test(description = "Convert Request Count Throttle Limit DTO to Quota Policy")
public void fromRequestCountThrottleLimitDtoToQuotaPolicyTest() throws Exception {
ThrottleLimitDTO throttleLimitDTO = new ThrottleLimitDTO();
throttleLimitDTO.setType(PolicyConstants.REQUEST_COUNT_LIMIT_TYPE);
RequestCountLimitDTO requestCountLimitDTO = new RequestCountLimitDTO();
requestCountLimitDTO.setRequestCount(100);
throttleLimitDTO.setRequestCountLimit(requestCountLimitDTO);
throttleLimitDTO.setTimeUnit("sec");
throttleLimitDTO.setUnitTime(1);
QuotaPolicy policy = CommonThrottleMappingUtil.fromDTOToQuotaPolicy(throttleLimitDTO);
Assert.assertNotNull(policy);
RequestCountLimit limit = (RequestCountLimit) policy.getLimit();
Assert.assertNotNull(limit);
assertEquals(limit.getRequestCount(), 100);
assertEquals(limit.getTimeUnit(), "sec");
assertEquals(limit.getUnitTime(), 1);
}
use of org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottleLimitDTO in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtilTestCase method fromBandwidthThrottleLimitDtoToQuotaPolicyTest.
@Test(description = "Convert Bandwidth Throttle Limit DTO to Quota Policy")
public void fromBandwidthThrottleLimitDtoToQuotaPolicyTest() throws Exception {
ThrottleLimitDTO throttleLimitDTO = new ThrottleLimitDTO();
throttleLimitDTO.setType(PolicyConstants.BANDWIDTH_LIMIT_TYPE);
BandwidthLimitDTO bandwidthLimitDTO = new BandwidthLimitDTO();
bandwidthLimitDTO.setDataAmount(10);
bandwidthLimitDTO.setDataUnit(KB);
throttleLimitDTO.setBandwidthLimit(bandwidthLimitDTO);
throttleLimitDTO.setTimeUnit("min");
throttleLimitDTO.setUnitTime(1);
QuotaPolicy policy = CommonThrottleMappingUtil.fromDTOToQuotaPolicy(throttleLimitDTO);
Assert.assertNotNull(policy);
assertEquals(policy.getType(), PolicyConstants.BANDWIDTH_TYPE);
BandwidthLimit bandwidthLimit = (BandwidthLimit) policy.getLimit();
assertEquals(bandwidthLimit.getDataAmount(), 10);
assertEquals(bandwidthLimit.getDataUnit(), KB);
assertEquals(bandwidthLimit.getTimeUnit(), "min");
assertEquals(bandwidthLimit.getUnitTime(), 1);
}
use of org.wso2.apimgt.gateway.cli.model.rest.policy.ThrottleLimitDTO 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());
}
Aggregations