Search in sources :

Example 6 with AdvancedThrottlePolicyDTO

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());
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Example 7 with AdvancedThrottlePolicyDTO

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());
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) RequestCountLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.RequestCountLimitDTO) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) ThrottleLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleLimitDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Example 8 with AdvancedThrottlePolicyDTO

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;
}
Also used : ConditionalGroupDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ConditionalGroupDTO) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)

Example 9 with AdvancedThrottlePolicyDTO

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;
}
Also used : APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 10 with AdvancedThrottlePolicyDTO

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();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)9 AdvancedThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)8 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)4 Test (org.testng.annotations.Test)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)3 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)3 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)2 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)2 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)2 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 PolicyNotFoundException (org.wso2.carbon.apimgt.api.PolicyNotFoundException)1