Search in sources :

Example 1 with SubscriptionThrottlePolicyListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO in project carbon-apimgt by wso2.

the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionPolicyArrayToListDTO.

/**
 * Converts an array of Subscription Policy objects into a List DTO
 *
 * @param subscriptionPolicies Array of Subscription Policies
 * @return A List DTO of converted Subscription Policies
 * @throws SubscriptionThrottlePolicyException - If error occurs
 */
public static SubscriptionThrottlePolicyListDTO fromSubscriptionPolicyArrayToListDTO(List<SubscriptionPolicy> subscriptionPolicies) throws SubscriptionThrottlePolicyException {
    SubscriptionThrottlePolicyListDTO listDTO = new SubscriptionThrottlePolicyListDTO();
    List<SubscriptionThrottlePolicyDTO> subscriptionPolicyDTOList = new ArrayList<>();
    if (subscriptionPolicies != null) {
        for (SubscriptionPolicy policy : subscriptionPolicies) {
            SubscriptionThrottlePolicyDTO dto = fromSubscriptionThrottlePolicyToDTO(policy);
            subscriptionPolicyDTOList.add(dto);
        }
    }
    listDTO.setCount(subscriptionPolicyDTOList.size());
    listDTO.setList(subscriptionPolicyDTOList);
    return listDTO;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) SubscriptionThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO)

Example 2 with SubscriptionThrottlePolicyListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO in project carbon-apimgt by wso2.

the class SubscriptionThrottlePolicyMappingUtilTest method fromSubscriptionPolicyArrayToListDTOTest.

@Test(description = "Convert Subscription Throttle Policy List to List DTO")
public void fromSubscriptionPolicyArrayToListDTOTest() throws Exception {
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    SubscriptionPolicy policy1 = SampleTestObjectCreator.createSubscriptionPolicyWithRequestLimit("Gold1");
    SubscriptionPolicy policy2 = SampleTestObjectCreator.createSubscriptionPolicyWithRequestLimit("Gold2");
    subscriptionPolicies.add(policy1);
    subscriptionPolicies.add(policy2);
    SubscriptionThrottlePolicyListDTO listDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionPolicyArrayToListDTO(subscriptionPolicies);
    assertEquals(listDTO.getCount(), (Integer) subscriptionPolicies.size());
    assertEquals(listDTO.getList().get(0).getPolicyName(), policy1.getPolicyName());
    assertEquals(listDTO.getList().get(0).getDescription(), policy1.getDescription());
    assertEquals(listDTO.getList().get(0).getDefaultLimit().getUnitTime(), (Integer) policy1.getDefaultQuotaPolicy().getLimit().getUnitTime());
    assertEquals(listDTO.getList().get(0).getDefaultLimit().getRequestCountLimit().getRequestCount(), (Integer) ((RequestCountLimit) policy1.getDefaultQuotaPolicy().getLimit()).getRequestCount());
    assertEquals(listDTO.getList().get(1).getPolicyName(), policy2.getPolicyName());
    assertEquals(listDTO.getList().get(1).getDescription(), policy2.getDescription());
    assertEquals(listDTO.getList().get(1).getDefaultLimit().getUnitTime(), (Integer) policy2.getDefaultQuotaPolicy().getLimit().getUnitTime());
    assertEquals(listDTO.getList().get(1).getDefaultLimit().getRequestCountLimit().getRequestCount(), (Integer) ((RequestCountLimit) policy2.getDefaultQuotaPolicy().getLimit()).getRequestCount());
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ArrayList(java.util.ArrayList) SubscriptionThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO) Test(org.testng.annotations.Test)

Example 3 with SubscriptionThrottlePolicyListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionGet.

/**
 * Get subscription level policies
 *
 * @param ifNoneMatch       If-None-Match header value
 * @param ifModifiedSince   If-Modified-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 policiesThrottlingSubscriptionGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Application Throttle Policy GET request");
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        List<SubscriptionPolicy> policies = apiMgtAdminService.getSubscriptionPolicies();
        SubscriptionThrottlePolicyListDTO subscriptionThrottlePolicyListDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionPolicyArrayToListDTO(policies);
        return Response.ok().entity(subscriptionThrottlePolicyListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving Application Policies";
        org.wso2.carbon.apimgt.rest.api.common.dto.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) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)3 ArrayList (java.util.ArrayList)2 SubscriptionThrottlePolicyListDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO)2 Test (org.testng.annotations.Test)1 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)1 SubscriptionThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1