Search in sources :

Example 76 with ApplicationPolicy

use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingApplicationIdGet.

/**
 * Returns a matching Application policy for the given policy id @{policyId}
 *
 * @param id          Uuid of the Application policy
 * @param ifNoneMatch       If-None-Match header value
 * @param ifModifiedSince   If-Modified-Since header value
 * @param request           msf4j request object
 * @return Response object  Response with matching {@link ApplicationThrottlePolicyDTO} object
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingApplicationIdGet(String id, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.info("Received Application Policy Get request. Policy uuid: " + id);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        Policy applicationPolicy = apiMgtAdminService.getApplicationPolicyByUuid(id);
        return Response.status(Response.Status.OK).entity(ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyToDTO(applicationPolicy)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while getting Application 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 : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) 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)

Example 77 with ApplicationPolicy

use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingApplicationGet.

/**
 * Returns all Application Policies deployed in the system
 *
 * @param ifNoneMatch       If-None-Match header value
 * @param ifModifiedSince   If-Modified-Since header value
 * @param request           msf4j request object
 * @return Response object  Response containing the Application Policy list {@link ApplicationThrottlePolicyListDTO}
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingApplicationGet(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<ApplicationPolicy> policies = apiMgtAdminService.getApplicationPolicies();
        ApplicationThrottlePolicyListDTO applicationThrottlePolicyListDTO = ApplicationThrottlePolicyMappingUtil.fromApplicationPolicyArrayToListDTO(policies);
        return Response.ok().entity(applicationThrottlePolicyListDTO).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) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 78 with ApplicationPolicy

use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingApplicationPost.

/**
 * Adds a new Application throttle policy to the system
 *
 * @param body              DTO object including the Policy meta information
 * @param request           msf4j request object
 * @return Response object  response object with the created application throttle policy resource
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingApplicationPost(ApplicationThrottlePolicyDTO body, Request request) throws NotFoundException {
    APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.application;
    if (log.isDebugEnabled()) {
        log.info("Received Application Policy PUT request " + body + " with tierLevel = " + tierLevel);
    }
    String policyName = null;
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        ApplicationPolicy applicationPolicy = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyDTOToModel(body);
        policyName = applicationPolicy.getPolicyName();
        String applicationPolicyUuid = apiMgtAdminService.addApplicationPolicy(applicationPolicy);
        return Response.status(Response.Status.CREATED).entity(ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyToDTO(apiMgtAdminService.getApplicationPolicyByUuid(applicationPolicyUuid))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding Application Policy. policy name: " + policyName;
        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) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 79 with ApplicationPolicy

use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImplTest method policiesThrottlingApplicationPostTest.

@Test
public void policiesThrottlingApplicationPostTest() throws APIManagementException, NotFoundException {
    printTestMethodName();
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    ApplicationThrottlePolicyDTO dto = new ApplicationThrottlePolicyDTO();
    String uuid = UUID.randomUUID().toString();
    dto.setId(uuid);
    dto.setPolicyName("SamplePolicy");
    dto.setDisplayName("DisplayName");
    dto.setDescription("Policy Description");
    dto.setIsDeployed(true);
    ApplicationPolicy policy = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyDTOToModel(dto);
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.mockStatic(ApplicationThrottlePolicyMappingUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    Mockito.doReturn(uuid).doThrow(new IllegalArgumentException()).when(adminService).addApplicationPolicy(policy);
    Mockito.doReturn(policy).doThrow(new IllegalArgumentException()).when(adminService).getApplicationPolicyByUuid(uuid);
    PowerMockito.when(ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyToDTO(policy)).thenReturn(dto);
    PowerMockito.when(ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyDTOToModel(dto)).thenReturn(policy);
    Response response = policiesApiService.policiesThrottlingApplicationPost(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) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ApplicationThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ApplicationThrottlePolicyDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 80 with ApplicationPolicy

use of org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultApplicationPolicy.

/**
 * Create default application policy.
 *
 * @return
 */
protected static ApplicationPolicy createDefaultApplicationPolicy() {
    ApplicationPolicy applicationPolicy = new ApplicationPolicy(SAMPLE_APP_POLICY);
    applicationPolicy.setUuid(UUID.randomUUID().toString());
    applicationPolicy.setDisplayName(SAMPLE_APP_POLICY);
    applicationPolicy.setDescription(SAMPLE_APP_POLICY_DESCRIPTION);
    QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
    defaultQuotaPolicy.setType(REQUEST_COUNT_TYPE);
    RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 10000, 1000);
    defaultQuotaPolicy.setLimit(requestCountLimit);
    applicationPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
    return applicationPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)

Aggregations

ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)56 Test (org.testng.annotations.Test)30 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)29 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)24 ArrayList (java.util.ArrayList)23 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)18 Application (org.wso2.carbon.apimgt.core.models.Application)17 Connection (java.sql.Connection)16 PreparedStatement (java.sql.PreparedStatement)16 SQLException (java.sql.SQLException)16 ResultSet (java.sql.ResultSet)13 BeforeTest (org.testng.annotations.BeforeTest)13 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)13 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)13 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)13 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)12 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)12