use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO in project carbon-apimgt by wso2.
the class AdvancedThrottlePolicyMappingUtil method fromAPIPolicyArrayToListDTO.
/**
* Converts an array of Advanced Policy objects into a List DTO
*
* @param policies Array of Advanced Policies
* @return A List DTO of converted Advanced Policies
* @throws UnsupportedThrottleLimitTypeException - If error occurs
* @throws UnsupportedThrottleConditionTypeException - If error occurs
*/
public static AdvancedThrottlePolicyListDTO fromAPIPolicyArrayToListDTO(List<APIPolicy> policies) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
AdvancedThrottlePolicyListDTO listDTO = new AdvancedThrottlePolicyListDTO();
List<AdvancedThrottlePolicyDTO> advancedPolicyDTOs = new ArrayList<>();
if (policies != null) {
for (APIPolicy policy : policies) {
advancedPolicyDTOs.add(fromAdvancedPolicyToDTO(policy));
}
}
listDTO.setList(advancedPolicyDTOs);
listDTO.setCount(advancedPolicyDTOs.size());
return listDTO;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO in project carbon-apimgt by wso2.
the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyToInfoDTO.
/**
* Converts a single Advanced Policy model into REST API DTO
*
* @param apiPolicy Advanced Policy model object
* @return Converted Advanced policy REST API DTO object
* @throws UnsupportedThrottleLimitTypeException - If error occurs
* @throws UnsupportedThrottleConditionTypeException - If error occurs
*/
public static AdvancedThrottlePolicyDTO fromAdvancedPolicyToInfoDTO(APIPolicy apiPolicy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
AdvancedThrottlePolicyDTO policyDTO = new AdvancedThrottlePolicyDTO();
policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(apiPolicy, policyDTO);
if (apiPolicy.getDefaultQuotaPolicy() != null) {
policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(apiPolicy.getDefaultQuotaPolicy()));
}
return policyDTO;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingAdvancedPost.
/**
* Create Policy
*
* @param body DTO object including the Policy meta information
* @param request msf4j request object
* @return Response object
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingAdvancedPost(AdvancedThrottlePolicyDTO body, Request request) throws NotFoundException {
APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.api;
if (log.isDebugEnabled()) {
log.info("Received Advance Policy POST request " + body + " with tierLevel = " + tierLevel);
}
if (log.isDebugEnabled()) {
log.info("Received Advance Policy PUT request " + body + " with tierLevel = " + tierLevel);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
APIPolicy apiPolicy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(body);
String policyId = apiMgtAdminService.addApiPolicy(apiPolicy);
return Response.status(Response.Status.CREATED).entity(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(apiMgtAdminService.getApiPolicyByUuid(policyId))).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while adding Advanced Throttle Policy, policy name: " + body.getPolicyName();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImplTest method policiesThrottlingAdvancedPolicyIdPutTest.
@Test
public void policiesThrottlingAdvancedPolicyIdPutTest() throws APIManagementException, NotFoundException {
printTestMethodName();
PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
String uuid = UUID.randomUUID().toString();
AdvancedThrottlePolicyDTO dto = new AdvancedThrottlePolicyDTO();
APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
APIPolicy policy1 = new APIPolicy(uuid, "samplePolicy1");
Mockito.doReturn(policy1).doThrow(new IllegalArgumentException()).when(adminService).getApiPolicyByUuid(uuid);
Response response = policiesApiService.policiesThrottlingAdvancedIdPut(uuid, dto, null, null, getRequest());
Assert.assertEquals(201, response.getStatus());
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImplTest method policiesThrottlingAdvancedPostTest.
@Test
public void policiesThrottlingAdvancedPostTest() throws APIManagementException, NotFoundException {
printTestMethodName();
PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
AdvancedThrottlePolicyDTO dto = new AdvancedThrottlePolicyDTO();
String uuid = UUID.randomUUID().toString();
dto.setId(uuid);
dto.setDisplayName("Sample Policy Display Name");
dto.setDescription("Simple Description");
dto.setPolicyName("Simple Policy Name");
dto.setIsDeployed(true);
APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.mockStatic(AdvancedThrottlePolicyMappingUtil.class);
PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
APIPolicy policy1 = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(dto);
Mockito.doReturn(uuid).doThrow(new IllegalArgumentException()).when(adminService).addApiPolicy(policy1);
Mockito.doReturn(policy1).doThrow(new IllegalArgumentException()).when(adminService).getApiPolicyByUuid(uuid);
PowerMockito.when(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(dto)).thenReturn(policy1);
PowerMockito.when(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(policy1)).thenReturn(dto);
Response response = policiesApiService.policiesThrottlingAdvancedPost(dto, getRequest());
Assert.assertEquals(201, response.getStatus());
}
Aggregations