Search in sources :

Example 6 with CustomRuleDTO

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

the class PoliciesApiServiceImplTest method policiesThrottlingCustomPostTest.

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

Example 7 with CustomRuleDTO

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

the class PoliciesApiServiceImpl method policiesThrottlingCustomRuleIdPut.

/**
 * Updates a given Global level policy/custom rule specified by uuid.
 *
 * @param ruleId            uuid of the policy
 * @param body              DTO of policy to be updated
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Updated policy
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingCustomRuleIdPut(String ruleId, CustomRuleDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Custom Policy PUT request " + body + " with rule ID = " + ruleId);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        CustomPolicy customPolicy = CustomPolicyMappingUtil.fromCustomPolicyDTOToModel(body);
        customPolicy.setUuid(ruleId);
        apiMgtAdminService.updateCustomRule(customPolicy);
        return Response.status(Response.Status.OK).entity(CustomPolicyMappingUtil.fromCustomPolicyToDTO(apiMgtAdminService.getCustomRuleByUUID(ruleId))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while updating Custom Policy. policy ID: " + ruleId;
        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 8 with CustomRuleDTO

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

the class PoliciesApiServiceImpl method policiesThrottlingCustomRuleIdGet.

/**
 * Get a specific custom rule by its id.
 *
 * @param ruleId          uuid of the policy
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return Matched Global Throttle Policy by the given name
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingCustomRuleIdGet(String ruleId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Custom Policy GET request with rule ID = " + ruleId);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        CustomPolicy customPolicy = apiMgtAdminService.getCustomRuleByUUID(ruleId);
        CustomRuleDTO dto = CustomPolicyMappingUtil.fromCustomPolicyToDTO(customPolicy);
        return Response.status(Response.Status.OK).entity(dto).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while getting custom policy. policy uuid: " + ruleId;
        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 9 with CustomRuleDTO

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

the class CustomPolicyMappingUtil method fromCustomPolicyDTOToModel.

/**
 * Converts a single Custom Policy DTO object into model object.
 *
 * @param dto Custom Policy DTO object
 * @return Model object derived from DTO
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static CustomPolicy fromCustomPolicyDTOToModel(CustomRuleDTO dto) throws UnsupportedThrottleLimitTypeException {
    CustomPolicy customPolicy = new CustomPolicy(dto.getPolicyName());
    customPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, customPolicy);
    customPolicy.setKeyTemplate(dto.getKeyTemplate());
    customPolicy.setSiddhiQuery(dto.getSiddhiQuery());
    return customPolicy;
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)

Example 10 with CustomRuleDTO

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

the class CustomPolicyMappingUtilTest method fromCustomPolicyDTOToModelTest.

@Test(description = "Convert DTO to Model")
public void fromCustomPolicyDTOToModelTest() throws Exception {
    CustomRuleDTO dto = new CustomRuleDTO();
    dto.setPolicyName(name);
    CustomPolicy policy = CustomPolicyMappingUtil.fromCustomPolicyDTOToModel(dto);
    Assert.assertNotNull(policy);
    Assert.assertEquals(policy.getPolicyName(), name);
}
Also used : CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) Test(org.testng.annotations.Test)

Aggregations

CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)9 CustomRuleDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)6 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 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 ArrayList (java.util.ArrayList)1 CustomRuleListDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO)1