use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO 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());
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO 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());
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO in project carbon-apimgt by wso2.
the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyToDTO.
/**
* Converts a single Advanced Policy model into REST API DTO
*
* @param policy Advanced Policy model object
* @return Converted Advanced policy REST API DTO object
* @throws UnsupportedThrottleLimitTypeException - If error occurs
* @throws UnsupportedThrottleConditionTypeException - If error occurs
*/
public static AdvancedThrottlePolicyDTO fromAdvancedPolicyToDTO(APIPolicy policy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
AdvancedThrottlePolicyDTO policyDTO = new AdvancedThrottlePolicyDTO();
policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(policy, policyDTO);
List<ConditionalGroupDTO> groupDTOs = CommonThrottleMappingUtil.fromPipelineListToConditionalGroupDTOList(policy.getPipelines());
policyDTO.setConditionalGroups(groupDTOs);
if (policy.getDefaultQuotaPolicy() != null) {
policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(policy.getDefaultQuotaPolicy()));
}
return policyDTO;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO in project carbon-apimgt by wso2.
the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyDTOToPolicy.
/**
* Converts a single Advanced Policy DTO into a model object
*
* @param dto Advanced policy DTO object
* @return Converted Advanced policy model object
* @throws UnsupportedThrottleLimitTypeException - If error occurs
* @throws UnsupportedThrottleConditionTypeException - If error occurs
*/
public static APIPolicy fromAdvancedPolicyDTOToPolicy(AdvancedThrottlePolicyDTO dto) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
APIPolicy apiPolicy = new APIPolicy(dto.getId(), dto.getPolicyName());
apiPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, apiPolicy);
List<Pipeline> pipelines = CommonThrottleMappingUtil.fromConditionalGroupDTOListToPipelineList(dto.getConditionalGroups());
apiPolicy.setPipelines(pipelines);
if (dto.getDefaultLimit() != null) {
apiPolicy.setDefaultQuotaPolicy(CommonThrottleMappingUtil.fromDTOToQuotaPolicy(dto.getDefaultLimit()));
}
return apiPolicy;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingAdvancedIdPut.
/**
* Update policy by Id
* @param id Uuid of the Advanced policy.
* @param body DTO object including the Policy meta information
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return Response object
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingAdvancedIdPut(String id, AdvancedThrottlePolicyDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.api;
if (log.isDebugEnabled()) {
log.info("Received Advance Policy PUT request " + body + " with tierLevel = " + tierLevel);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
APIPolicy apiPolicy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(body);
apiPolicy.setUuid(id);
apiMgtAdminService.updateApiPolicy(apiPolicy);
return Response.status(Response.Status.CREATED).entity(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(apiMgtAdminService.getApiPolicyByUuid(id))).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while updating Advanced Policy. policy uuid: " + id;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
Aggregations