Search in sources :

Example 11 with SubscriptionThrottlePolicyDTO

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

the class PoliciesApiServiceImplTest method policiesThrottlingSubscriptionPostTest.

@Test
public void policiesThrottlingSubscriptionPostTest() throws APIManagementException, NotFoundException {
    printTestMethodName();
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    String uuid = UUID.randomUUID().toString();
    SubscriptionThrottlePolicyDTO dto = new SubscriptionThrottlePolicyDTO();
    dto.setRateLimitTimeUnit("m");
    dto.setRateLimitCount(1);
    dto.setStopOnQuotaReach(true);
    SubscriptionPolicy policy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(dto);
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.mockStatic(SubscriptionThrottlePolicyMappingUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    Mockito.doReturn(uuid).doThrow(new IllegalArgumentException()).when(adminService).addSubscriptionPolicy(policy);
    Mockito.doReturn(policy).doThrow(new IllegalArgumentException()).when(adminService).getSubscriptionPolicyByUuid(uuid);
    PowerMockito.when(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(dto)).thenReturn(policy);
    PowerMockito.when(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(policy)).thenReturn(dto);
    Response response = policiesApiService.policiesThrottlingSubscriptionPost(dto, getRequest());
    Assert.assertEquals(201, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) PoliciesApiServiceImpl(org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with SubscriptionThrottlePolicyDTO

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

the class PoliciesApiServiceImplTest method policiesThrottlingSubscriptionPolicyIdPutTest.

@Test
public void policiesThrottlingSubscriptionPolicyIdPutTest() throws APIManagementException, NotFoundException {
    printTestMethodName();
    PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
    String uuid = UUID.randomUUID().toString();
    SubscriptionThrottlePolicyDTO dto = new SubscriptionThrottlePolicyDTO();
    dto.setRateLimitTimeUnit("m");
    dto.setRateLimitCount(1);
    dto.setStopOnQuotaReach(true);
    SubscriptionPolicy policy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(dto);
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.mockStatic(SubscriptionThrottlePolicyMappingUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    Mockito.doNothing().doThrow(new IllegalArgumentException()).when(adminService).updateSubscriptionPolicy(policy);
    Mockito.doReturn(policy).doThrow(new IllegalArgumentException()).when(adminService).getSubscriptionPolicyByUuid(uuid);
    PowerMockito.when(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(dto)).thenReturn(policy);
    PowerMockito.when(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(policy)).thenReturn(dto);
    Response response = policiesApiService.policiesThrottlingSubscriptionIdPut(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) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) PoliciesApiServiceImpl(org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with SubscriptionThrottlePolicyDTO

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

the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionThrottlePolicyDTOToModel.

/**
 * Converts a single Subscription Policy DTO into a model object
 *
 * @param dto Subscription policy DTO object
 * @return Converted Subscription policy model object
 * @throws UnsupportedThrottleLimitTypeException
 */
@SuppressWarnings("unchecked")
public static SubscriptionPolicy fromSubscriptionThrottlePolicyDTOToModel(SubscriptionThrottlePolicyDTO dto) throws UnsupportedThrottleLimitTypeException {
    // update mandatory fields such as tenantDomain etc.
    dto = CommonThrottleMappingUtil.updateDefaultMandatoryFieldsOfThrottleDTO(dto);
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(dto.getPolicyName());
    subscriptionPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, subscriptionPolicy);
    subscriptionPolicy.setBillingPlan(dto.getBillingPlan());
    subscriptionPolicy.setRateLimitTimeUnit(dto.getRateLimitTimeUnit());
    subscriptionPolicy.setRateLimitCount(dto.getRateLimitCount());
    subscriptionPolicy.setSubscriberCount(dto.getSubscriberCount());
    subscriptionPolicy.setStopOnQuotaReach(dto.isStopOnQuotaReach());
    if (dto.getGraphQLMaxComplexity() != null) {
        subscriptionPolicy.setGraphQLMaxComplexity(dto.getGraphQLMaxComplexity());
    }
    if (dto.getGraphQLMaxDepth() != null) {
        subscriptionPolicy.setGraphQLMaxDepth(dto.getGraphQLMaxDepth());
    }
    List<CustomAttributeDTO> customAttributes = dto.getCustomAttributes();
    if (customAttributes != null && customAttributes.size() > 0) {
        JSONArray customAttrJsonArray = new JSONArray();
        for (CustomAttributeDTO customAttributeDTO : customAttributes) {
            JSONObject attrJsonObj = new JSONObject();
            attrJsonObj.put(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_NAME, customAttributeDTO.getName());
            attrJsonObj.put(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_VALUE, customAttributeDTO.getValue());
            customAttrJsonArray.add(attrJsonObj);
        }
        subscriptionPolicy.setCustomAttributes(customAttrJsonArray.toJSONString().getBytes());
    } else {
        // if no custom attributes are set, assign an empty byte array
        subscriptionPolicy.setCustomAttributes(new JSONArray().toJSONString().getBytes());
    }
    if (dto.getMonetization() != null && dto.getMonetization().getMonetizationPlan() != null && StringUtils.isNotBlank(dto.getMonetization().getMonetizationPlan().name())) {
        String tierMonetizationPlan = dto.getMonetization().getMonetizationPlan().toString();
        subscriptionPolicy.setMonetizationPlan(tierMonetizationPlan);
        if (dto.getMonetization().getProperties() != null) {
            Map<String, String> tierMonetizationProperties = new HashMap<>();
            Map<String, String> props = dto.getMonetization().getProperties();
            for (Map.Entry<String, String> entry : props.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                tierMonetizationProperties.put(key, value);
            }
            subscriptionPolicy.setMonetizationPlanProperties(tierMonetizationProperties);
        }
    }
    if (dto.getDefaultLimit() != null) {
        subscriptionPolicy.setDefaultQuotaPolicy(CommonThrottleMappingUtil.fromDTOToQuotaPolicy(dto.getDefaultLimit()));
    }
    return subscriptionPolicy;
}
Also used : JSONObject(org.json.simple.JSONObject) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) HashMap(java.util.HashMap) Map(java.util.Map) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomAttributeDTO)

Example 14 with SubscriptionThrottlePolicyDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyDTO 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 UnsupportedThrottleLimitTypeException
 * @throws ParseException
 */
public static SubscriptionThrottlePolicyListDTO fromSubscriptionPolicyArrayToListDTO(SubscriptionPolicy[] subscriptionPolicies) throws UnsupportedThrottleLimitTypeException, ParseException {
    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.api.model.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) SubscriptionThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyListDTO)

Example 15 with SubscriptionThrottlePolicyDTO

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

the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionThrottlePolicyToDTO.

/**
 * Converts a single Subscription Policy model into REST API DTO
 *
 * @param subscriptionPolicy Subscription Policy model object
 * @return Converted Subscription policy REST API DTO object
 * @throws UnsupportedThrottleLimitTypeException
 * @throws ParseException
 */
public static SubscriptionThrottlePolicyDTO fromSubscriptionThrottlePolicyToDTO(SubscriptionPolicy subscriptionPolicy) throws UnsupportedThrottleLimitTypeException, ParseException {
    SubscriptionThrottlePolicyDTO policyDTO = new SubscriptionThrottlePolicyDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(subscriptionPolicy, policyDTO);
    policyDTO.setBillingPlan(subscriptionPolicy.getBillingPlan());
    policyDTO.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
    policyDTO.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
    policyDTO.setStopOnQuotaReach(subscriptionPolicy.isStopOnQuotaReach());
    policyDTO.setGraphQLMaxComplexity(subscriptionPolicy.getGraphQLMaxComplexity());
    policyDTO.setGraphQLMaxDepth(subscriptionPolicy.getGraphQLMaxDepth());
    policyDTO.setSubscriberCount(subscriptionPolicy.getSubscriberCount());
    byte[] customAttributes = subscriptionPolicy.getCustomAttributes();
    if (customAttributes != null) {
        List<CustomAttributeDTO> customAttributeDTOs = new ArrayList<>();
        JSONParser parser = new JSONParser();
        JSONArray attributeArray = (JSONArray) parser.parse(new String(subscriptionPolicy.getCustomAttributes()));
        for (Object attributeObj : attributeArray) {
            JSONObject attribute = (JSONObject) attributeObj;
            CustomAttributeDTO customAttributeDTO = CommonThrottleMappingUtil.getCustomAttribute(attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_NAME).toString(), attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_VALUE).toString());
            customAttributeDTOs.add(customAttributeDTO);
        }
        policyDTO.setCustomAttributes(customAttributeDTOs);
    }
    if (subscriptionPolicy.getDefaultQuotaPolicy() != null) {
        policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(subscriptionPolicy.getDefaultQuotaPolicy()));
    }
    policyDTO.setType(SUBSCRIPTION_THROTTLE_POLICY_TYPE);
    if (APIConstants.COMMERCIAL_TIER_PLAN.equals(subscriptionPolicy.getBillingPlan())) {
        MonetizationInfoDTO monetizationInfoDTO = new MonetizationInfoDTO();
        if (APIConstants.Monetization.FIXED_RATE.equalsIgnoreCase(subscriptionPolicy.getMonetizationPlan())) {
            monetizationInfoDTO.setMonetizationPlan(MonetizationInfoDTO.MonetizationPlanEnum.FIXEDRATE);
        } else {
            monetizationInfoDTO.setMonetizationPlan(MonetizationInfoDTO.MonetizationPlanEnum.DYNAMICRATE);
        }
        monetizationInfoDTO.setProperties(subscriptionPolicy.getMonetizationPlanProperties());
        policyDTO.setMonetization(monetizationInfoDTO);
    }
    return policyDTO;
}
Also used : MonetizationInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MonetizationInfoDTO) JSONObject(org.json.simple.JSONObject) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomAttributeDTO)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)10 SubscriptionThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO)6 ArrayList (java.util.ArrayList)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)5 JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 ParseException (org.json.simple.parser.ParseException)4 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)3 CustomAttributeDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 Response (javax.ws.rs.core.Response)2 JSONParser (org.json.simple.parser.JSONParser)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 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)2