Search in sources :

Example 1 with AdvancedThrottlePolicyDTO

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

the class AdvancedThrottlePolicyMappingUtil method fromAPIPolicyArrayToListDTO.

/**
 * Converts an array of Advanced Policy objects into a List DTO
 *
 * @param policies Array of Advanced Policies
 * @return A List DTO of converted Advanced Policies
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static AdvancedThrottlePolicyListDTO fromAPIPolicyArrayToListDTO(List<APIPolicy> policies) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyListDTO listDTO = new AdvancedThrottlePolicyListDTO();
    List<AdvancedThrottlePolicyDTO> advancedPolicyDTOs = new ArrayList<>();
    if (policies != null) {
        for (APIPolicy policy : policies) {
            advancedPolicyDTOs.add(fromAdvancedPolicyToDTO(policy));
        }
    }
    listDTO.setList(advancedPolicyDTOs);
    listDTO.setCount(advancedPolicyDTOs.size());
    return listDTO;
}
Also used : AdvancedThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyListDTO) ArrayList(java.util.ArrayList) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 2 with AdvancedThrottlePolicyDTO

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

the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyToInfoDTO.

/**
 * Converts a single Advanced Policy model into REST API DTO
 *
 * @param apiPolicy 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 fromAdvancedPolicyToInfoDTO(APIPolicy apiPolicy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyDTO policyDTO = new AdvancedThrottlePolicyDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(apiPolicy, policyDTO);
    if (apiPolicy.getDefaultQuotaPolicy() != null) {
        policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(apiPolicy.getDefaultQuotaPolicy()));
    }
    return policyDTO;
}
Also used : AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)

Example 3 with AdvancedThrottlePolicyDTO

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

the class PoliciesApiServiceImpl method policiesThrottlingAdvancedPost.

/**
 * Create Policy
 *
 * @param body              DTO object including the Policy meta information
 * @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 policiesThrottlingAdvancedPost(AdvancedThrottlePolicyDTO body, Request request) throws NotFoundException {
    APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.api;
    if (log.isDebugEnabled()) {
        log.info("Received Advance Policy POST request " + body + " with tierLevel = " + tierLevel);
    }
    if (log.isDebugEnabled()) {
        log.info("Received Advance Policy PUT request " + body + " with tierLevel = " + tierLevel);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        APIPolicy apiPolicy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(body);
        String policyId = apiMgtAdminService.addApiPolicy(apiPolicy);
        return Response.status(Response.Status.CREATED).entity(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(apiMgtAdminService.getApiPolicyByUuid(policyId))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding Advanced Throttle Policy, policy name: " + body.getPolicyName();
        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)

Example 4 with AdvancedThrottlePolicyDTO

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

the class PoliciesApiServiceImplTest method policiesThrottlingAdvancedPolicyIdPutTest.

@Test
public void policiesThrottlingAdvancedPolicyIdPutTest() throws APIManagementException, NotFoundException {
    printTestMethodName();
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    String uuid = UUID.randomUUID().toString();
    AdvancedThrottlePolicyDTO dto = new AdvancedThrottlePolicyDTO();
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    APIPolicy policy1 = new APIPolicy(uuid, "samplePolicy1");
    Mockito.doReturn(policy1).doThrow(new IllegalArgumentException()).when(adminService).getApiPolicyByUuid(uuid);
    Response response = policiesApiService.policiesThrottlingAdvancedIdPut(uuid, dto, null, null, getRequest());
    Assert.assertEquals(201, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) PoliciesApiServiceImpl(org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with AdvancedThrottlePolicyDTO

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

the class PoliciesApiServiceImplTest method policiesThrottlingAdvancedPostTest.

@Test
public void policiesThrottlingAdvancedPostTest() throws APIManagementException, NotFoundException {
    printTestMethodName();
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    AdvancedThrottlePolicyDTO dto = new AdvancedThrottlePolicyDTO();
    String uuid = UUID.randomUUID().toString();
    dto.setId(uuid);
    dto.setDisplayName("Sample Policy Display Name");
    dto.setDescription("Simple Description");
    dto.setPolicyName("Simple Policy Name");
    dto.setIsDeployed(true);
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.mockStatic(AdvancedThrottlePolicyMappingUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    APIPolicy policy1 = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(dto);
    Mockito.doReturn(uuid).doThrow(new IllegalArgumentException()).when(adminService).addApiPolicy(policy1);
    Mockito.doReturn(policy1).doThrow(new IllegalArgumentException()).when(adminService).getApiPolicyByUuid(uuid);
    PowerMockito.when(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(dto)).thenReturn(policy1);
    PowerMockito.when(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(policy1)).thenReturn(dto);
    Response response = policiesApiService.policiesThrottlingAdvancedPost(dto, getRequest());
    Assert.assertEquals(201, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) PoliciesApiServiceImpl(org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)9 AdvancedThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)8 Test (org.testng.annotations.Test)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 ArrayList (java.util.ArrayList)1 Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)1 AdvancedThrottlePolicyListDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyListDTO)1 ConditionalGroupDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ConditionalGroupDTO)1 RequestCountLimitDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.RequestCountLimitDTO)1 ThrottleLimitDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleLimitDTO)1