Search in sources :

Example 6 with SubscriptionThrottlePolicyDTO

use of org.wso2.carbon.apimgt.rest.api.admin.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 - If error occurs
 */
@SuppressWarnings("unchecked")
public static SubscriptionPolicy fromSubscriptionThrottlePolicyDTOToModel(SubscriptionThrottlePolicyDTO dto) throws APIManagementException {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(dto.getPolicyName());
    subscriptionPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, subscriptionPolicy);
    subscriptionPolicy.setBillingPlan(dto.getBillingPlan());
    subscriptionPolicy.setRateLimitTimeUnit(dto.getRateLimitTimeUnit());
    subscriptionPolicy.setRateLimitCount(dto.getRateLimitCount());
    subscriptionPolicy.setStopOnQuotaReach(dto.getStopOnQuotaReach());
    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);
        }
        try {
            subscriptionPolicy.setCustomAttributes(customAttrJsonArray.toJSONString().getBytes(System.getProperty(ENCODING_SYSTEM_PROPERTY, ENCODING_UTF_8)));
        } catch (UnsupportedEncodingException e) {
            String errorMsg = "Error while setting custom attributes for Subscription Policy: " + dto.getPolicyName();
            log.error(errorMsg, e);
            throw new APIManagementException(errorMsg, e, ExceptionCodes.INTERNAL_ERROR);
        }
    }
    if (dto.getDefaultLimit() != null) {
        subscriptionPolicy.setDefaultQuotaPolicy(CommonThrottleMappingUtil.fromDTOToQuotaPolicy(dto.getDefaultLimit()));
    }
    return subscriptionPolicy;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) JSONArray(org.json.simple.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO)

Example 7 with SubscriptionThrottlePolicyDTO

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

the class SubscriptionThrottlePolicyMappingUtilTest method fromSubscriptionThrottlePolicyToDTOTest.

@Test(description = "Convert Subscription Throttle Policy to DTO")
public void fromSubscriptionThrottlePolicyToDTOTest() throws Exception {
    SubscriptionPolicy policy = new SubscriptionPolicy(uuid, name);
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    RequestCountLimit requestCountLimit = new RequestCountLimit("s", 1000, 10000);
    quotaPolicy.setLimit(requestCountLimit);
    quotaPolicy.setType(REQUEST_COUNT_TYPE);
    policy.setDefaultQuotaPolicy(quotaPolicy);
    policy.setCustomAttributes("[{\"name\":\"dwd\",\"value\":\"wdw\"},{\"name\":\"dwdw\",\"value\":\"dwdw\"}]".getBytes());
    SubscriptionThrottlePolicyDTO dto = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(policy);
    Assert.assertNotNull(dto);
    Assert.assertEquals(dto.getPolicyName(), name);
    Assert.assertEquals(dto.getId(), uuid);
    Assert.assertEquals(dto.getDefaultLimit().getRequestCountLimit().getRequestCount().intValue(), requestCountLimit.getRequestCount());
    Assert.assertEquals(dto.getCustomAttributes().get(0).getName(), "dwd");
    Assert.assertEquals(dto.getCustomAttributes().get(1).getName(), "dwdw");
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) Test(org.testng.annotations.Test)

Example 8 with SubscriptionThrottlePolicyDTO

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

the class SubscriptionThrottlePolicyMappingUtilTest method fromSubscriptionThrottlePolicyDTOToModelTest.

@Test(description = "Convert Subscription Throttle Policy DTO to Model")
public void fromSubscriptionThrottlePolicyDTOToModelTest() throws Exception {
    SubscriptionThrottlePolicyDTO dto = new SubscriptionThrottlePolicyDTO();
    dto.setRateLimitTimeUnit("m");
    dto.setRateLimitCount(1);
    dto.setStopOnQuotaReach(true);
    ThrottleLimitDTO throttleLimitDTO = new ThrottleLimitDTO();
    throttleLimitDTO.setType("RequestCountLimit");
    throttleLimitDTO.setTimeUnit("s");
    throttleLimitDTO.setUnitTime(1);
    RequestCountLimitDTO requestCountLimitDTO = new RequestCountLimitDTO();
    requestCountLimitDTO.setRequestCount(2);
    throttleLimitDTO.setRequestCountLimit(requestCountLimitDTO);
    dto.setDefaultLimit(throttleLimitDTO);
    List<CustomAttributeDTO> customAttributeDTOs = new ArrayList<>();
    CustomAttributeDTO customAttributeDTO1 = new CustomAttributeDTO();
    customAttributeDTO1.setName("ABC");
    customAttributeDTO1.setValue("ABCVALUE");
    CustomAttributeDTO customAttributeDTO2 = new CustomAttributeDTO();
    customAttributeDTO2.setName("CDE");
    customAttributeDTO2.setValue("CDEVALUE");
    customAttributeDTOs.add(customAttributeDTO1);
    customAttributeDTOs.add(customAttributeDTO2);
    dto.setCustomAttributes(customAttributeDTOs);
    SubscriptionPolicy policy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(dto);
    Assert.assertNotNull(policy);
    Assert.assertEquals(policy.getRateLimitCount(), 1);
    Assert.assertEquals(policy.getRateLimitTimeUnit(), "m");
    Assert.assertEquals(policy.isStopOnQuotaReach(), true);
    Assert.assertEquals(policy.getDefaultQuotaPolicy().getType(), "requestCount");
    Assert.assertEquals(policy.getDefaultQuotaPolicy().getLimit().getTimeUnit(), dto.getDefaultLimit().getTimeUnit());
    Assert.assertEquals((Integer) policy.getDefaultQuotaPolicy().getLimit().getUnitTime(), dto.getDefaultLimit().getUnitTime());
    Assert.assertEquals((Integer) ((RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit()).getRequestCount(), dto.getDefaultLimit().getRequestCountLimit().getRequestCount());
    Assert.assertTrue(new String(policy.getCustomAttributes()).contains("ABC"));
    Assert.assertTrue(new String(policy.getCustomAttributes()).contains("ABCVALUE"));
    Assert.assertTrue(new String(policy.getCustomAttributes()).contains("CDE"));
    Assert.assertTrue(new String(policy.getCustomAttributes()).contains("CDEVALUE"));
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) RequestCountLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.RequestCountLimitDTO) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) ThrottleLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleLimitDTO) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO) Test(org.testng.annotations.Test)

Example 9 with SubscriptionThrottlePolicyDTO

use of org.wso2.carbon.apimgt.rest.api.admin.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 10 with SubscriptionThrottlePolicyDTO

use of org.wso2.carbon.apimgt.rest.api.admin.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)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)10 SubscriptionThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO)6 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 ArrayList (java.util.ArrayList)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 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)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 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)1