use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit 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"));
}
use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.
the class AdvancedThrottlePolicyMappingUtilTestCase method fromAdvancedPolicyToInfoDTOTest.
@Test(description = "Convert Policy DTO to Policy object")
public void fromAdvancedPolicyToInfoDTOTest() 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 policyDTO = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToInfoDTO(apiPolicy);
Assert.assertNotNull(policyDTO);
Assert.assertEquals(policyDTO.getDisplayName(), displayName);
Assert.assertEquals(policyDTO.getDescription(), description);
Assert.assertEquals(policyDTO.getDefaultLimit().getType(), "RequestCountLimit");
Assert.assertEquals(policyDTO.getDefaultLimit().getTimeUnit(), apiPolicy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
Assert.assertEquals(policyDTO.getDefaultLimit().getUnitTime(), (Integer) apiPolicy.getDefaultQuotaPolicy().getLimit().getUnitTime());
Assert.assertEquals(policyDTO.getDefaultLimit().getRequestCountLimit().getRequestCount(), (Integer) ((RequestCountLimit) apiPolicy.getDefaultQuotaPolicy().getLimit()).getRequestCount());
}
use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.
the class ApplicationThrottlePolicyMappingUtilTest method fromApplicationThrottlePolicyToDTOTest.
@Test(description = "Convert from Policy to DTO")
public void fromApplicationThrottlePolicyToDTOTest() throws Exception {
Policy policy = new ApplicationPolicy(uuid, policyName);
QuotaPolicy quotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit("s", 1000, 10000);
quotaPolicy.setLimit(requestCountLimit);
quotaPolicy.setType(REQUEST_COUNT_TYPE);
policy.setDefaultQuotaPolicy(quotaPolicy);
policy.setDisplayName(displayName);
ApplicationThrottlePolicyDTO dto = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyToDTO(policy);
Assert.assertNotNull(dto);
Assert.assertEquals(dto.getDisplayName(), displayName);
Assert.assertNotNull(dto.getPolicyName(), policyName);
Assert.assertEquals(dto.getId(), uuid);
Assert.assertEquals(dto.getDefaultLimit().getRequestCountLimit().getRequestCount().intValue(), requestCountLimit.getRequestCount());
Assert.assertEquals(dto.getDisplayName(), displayName);
}
use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit 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());
}
use of org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit in project carbon-apimgt by wso2.
the class ApiMgtDAO method setCommonParametersForPolicy.
/**
* Populates common attribute data of the <code>policy</code> to <code>policyStatement</code>
*
* @param policyStatement prepared statement initialized of policy operation
* @param policy <code>Policy</code> object with data
* @throws SQLException
*/
private void setCommonParametersForPolicy(PreparedStatement policyStatement, Policy policy) throws SQLException {
policyStatement.setString(1, policy.getPolicyName());
if (!StringUtils.isEmpty(policy.getDisplayName())) {
policyStatement.setString(2, policy.getDisplayName());
} else {
policyStatement.setString(2, policy.getPolicyName());
}
policyStatement.setInt(3, policy.getTenantId());
policyStatement.setString(4, policy.getDescription());
policyStatement.setString(5, policy.getDefaultQuotaPolicy().getType());
// TODO use requestCount in same format in all places
if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
RequestCountLimit limit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
policyStatement.setLong(6, limit.getRequestCount());
policyStatement.setString(7, null);
} else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
BandwidthLimit limit = (BandwidthLimit) policy.getDefaultQuotaPolicy().getLimit();
policyStatement.setLong(6, limit.getDataAmount());
policyStatement.setString(7, limit.getDataUnit());
} else if (PolicyConstants.EVENT_COUNT_TYPE.equalsIgnoreCase(policy.getDefaultQuotaPolicy().getType())) {
EventCountLimit limit = (EventCountLimit) policy.getDefaultQuotaPolicy().getLimit();
policyStatement.setLong(6, limit.getEventCount());
policyStatement.setString(7, null);
}
policyStatement.setLong(8, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
policyStatement.setString(9, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
// policyStatement.setBoolean(9, APIUtil.isContentAwarePolicy(policy));
policyStatement.setBoolean(10, policy.isDeployed());
if (!StringUtils.isBlank(policy.getUUID())) {
policyStatement.setString(11, policy.getUUID());
} else {
policyStatement.setString(11, UUID.randomUUID().toString());
}
}
Aggregations