Search in sources :

Example 1 with CustomRuleDTO

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

the class CustomPolicyMappingUtil method fromCustomPolicyToDTO.

/**
 * Converts a single Custom Policy model object into DTO object.
 *
 * @param globalPolicy Custom Policy model object
 * @return DTO object derived from the Policy model object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static CustomRuleDTO fromCustomPolicyToDTO(CustomPolicy globalPolicy) throws UnsupportedThrottleLimitTypeException {
    CustomRuleDTO policyDTO = new CustomRuleDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(globalPolicy, policyDTO);
    policyDTO.setKeyTemplate(globalPolicy.getKeyTemplate());
    policyDTO.setSiddhiQuery(globalPolicy.getSiddhiQuery());
    return policyDTO;
}
Also used : CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)

Example 2 with CustomRuleDTO

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

the class CustomPolicyMappingUtil method fromCustomPolicyArrayToListDTO.

/**
 * Converts an array of Custom Policy model objects into REST API DTO objects.
 *
 * @param customPolicies An array of custom policy model objects
 * @return A List DTO of Custom Policy DTOs derived from the array of model objects
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static CustomRuleListDTO fromCustomPolicyArrayToListDTO(List<CustomPolicy> customPolicies) throws UnsupportedThrottleLimitTypeException {
    CustomRuleListDTO listDTO = new CustomRuleListDTO();
    List<CustomRuleDTO> customPolicyDTOList = new ArrayList<>();
    if (customPolicies != null) {
        for (CustomPolicy policy : customPolicies) {
            CustomRuleDTO dto = fromCustomPolicyToDTO(policy);
            customPolicyDTOList.add(dto);
        }
    }
    listDTO.setCount(customPolicyDTOList.size());
    listDTO.setList(customPolicyDTOList);
    return listDTO;
}
Also used : CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ArrayList(java.util.ArrayList) CustomRuleListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO)

Example 3 with CustomRuleDTO

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

the class PoliciesApiServiceImpl method policiesThrottlingCustomPost.

/**
 * Add a Custom Policy.
 *
 * @param body        DTO of new policy to be created
 * @param request     msf4j request object
 * @return Created policy along with the location of it with Location header
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingCustomPost(CustomRuleDTO body, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Custom Policy POST request " + body);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        CustomPolicy customPolicy = CustomPolicyMappingUtil.fromCustomPolicyDTOToModel(body);
        String uuid = apiMgtAdminService.addCustomRule(customPolicy);
        return Response.status(Response.Status.CREATED).entity(CustomPolicyMappingUtil.fromCustomPolicyToDTO(apiMgtAdminService.getCustomRuleByUUID(uuid))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding custom 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) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 4 with CustomRuleDTO

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

the class CustomPolicyMappingUtilTest method fromCustomPolicyToDTOTest.

@Test(description = "Convert custom Policy to DTO object")
public void fromCustomPolicyToDTOTest() throws Exception {
    CustomPolicy policy = new CustomPolicy(name);
    policy.setUuid(uuid);
    CustomRuleDTO dto = CustomPolicyMappingUtil.fromCustomPolicyToDTO(policy);
    Assert.assertNotNull(dto);
    Assert.assertEquals(dto.getPolicyName(), name);
    Assert.assertEquals(dto.getId(), uuid);
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO) Test(org.testng.annotations.Test)

Example 5 with CustomRuleDTO

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

the class PoliciesApiServiceImplTest method policiesThrottlingCustomRuleIdPutTest.

@Test
public void policiesThrottlingCustomRuleIdPutTest() throws APIManagementException, NotFoundException {
    printTestMethodName();
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    String uuid = UUID.randomUUID().toString();
    CustomPolicy policy = new CustomPolicy(uuid, "Policy");
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    Mockito.doNothing().doThrow(new IllegalArgumentException()).when(adminService).updateCustomRule(policy);
    Mockito.doReturn(policy).doThrow(new IllegalArgumentException()).when(adminService).getCustomRuleByUUID(uuid);
    CustomRuleDTO dto = CustomPolicyMappingUtil.fromCustomPolicyToDTO(policy);
    Response response = policiesApiService.policiesThrottlingCustomRuleIdPut(uuid, dto, null, null, getRequest());
    Assert.assertEquals(response.getStatus(), 200);
}
Also used : Response(javax.ws.rs.core.Response) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO) PoliciesApiServiceImpl(org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

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