use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtil method fromCustomPolicyToDTO.
/**
* Converts a single Custom Policy model object into DTO object.
*
* @param globalPolicy Custom Policy model object
* @return DTO object derived from the Policy model object
* @throws UnsupportedThrottleLimitTypeException - If error occurs
*/
public static CustomRuleDTO fromCustomPolicyToDTO(CustomPolicy globalPolicy) throws UnsupportedThrottleLimitTypeException {
CustomRuleDTO policyDTO = new CustomRuleDTO();
policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(globalPolicy, policyDTO);
policyDTO.setKeyTemplate(globalPolicy.getKeyTemplate());
policyDTO.setSiddhiQuery(globalPolicy.getSiddhiQuery());
return policyDTO;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtil method fromCustomPolicyArrayToListDTO.
/**
* Converts an array of Custom Policy model objects into REST API DTO objects.
*
* @param customPolicies An array of custom policy model objects
* @return A List DTO of Custom Policy DTOs derived from the array of model objects
* @throws UnsupportedThrottleLimitTypeException - If error occurs
*/
public static CustomRuleListDTO fromCustomPolicyArrayToListDTO(List<CustomPolicy> customPolicies) throws UnsupportedThrottleLimitTypeException {
CustomRuleListDTO listDTO = new CustomRuleListDTO();
List<CustomRuleDTO> customPolicyDTOList = new ArrayList<>();
if (customPolicies != null) {
for (CustomPolicy policy : customPolicies) {
CustomRuleDTO dto = fromCustomPolicyToDTO(policy);
customPolicyDTOList.add(dto);
}
}
listDTO.setCount(customPolicyDTOList.size());
listDTO.setList(customPolicyDTOList);
return listDTO;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingCustomPost.
/**
* Add a Custom Policy.
*
* @param body DTO of new policy to be created
* @param request msf4j request object
* @return Created policy along with the location of it with Location header
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingCustomPost(CustomRuleDTO body, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.debug("Received Custom Policy POST request " + body);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
CustomPolicy customPolicy = CustomPolicyMappingUtil.fromCustomPolicyDTOToModel(body);
String uuid = apiMgtAdminService.addCustomRule(customPolicy);
return Response.status(Response.Status.CREATED).entity(CustomPolicyMappingUtil.fromCustomPolicyToDTO(apiMgtAdminService.getCustomRuleByUUID(uuid))).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while adding custom 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.CustomRuleDTO in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtilTest method fromCustomPolicyToDTOTest.
@Test(description = "Convert custom Policy to DTO object")
public void fromCustomPolicyToDTOTest() throws Exception {
CustomPolicy policy = new CustomPolicy(name);
policy.setUuid(uuid);
CustomRuleDTO dto = CustomPolicyMappingUtil.fromCustomPolicyToDTO(policy);
Assert.assertNotNull(dto);
Assert.assertEquals(dto.getPolicyName(), name);
Assert.assertEquals(dto.getId(), uuid);
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImplTest method policiesThrottlingCustomRuleIdPutTest.
@Test
public void policiesThrottlingCustomRuleIdPutTest() throws APIManagementException, NotFoundException {
printTestMethodName();
PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
String uuid = UUID.randomUUID().toString();
CustomPolicy policy = new CustomPolicy(uuid, "Policy");
APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
Mockito.doNothing().doThrow(new IllegalArgumentException()).when(adminService).updateCustomRule(policy);
Mockito.doReturn(policy).doThrow(new IllegalArgumentException()).when(adminService).getCustomRuleByUUID(uuid);
CustomRuleDTO dto = CustomPolicyMappingUtil.fromCustomPolicyToDTO(policy);
Response response = policiesApiService.policiesThrottlingCustomRuleIdPut(uuid, dto, null, null, getRequest());
Assert.assertEquals(response.getStatus(), 200);
}
Aggregations