use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy 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.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class PolicyExportManagerTest method testCreateArchiveFromExecutionPlans.
@Test(description = "testing creating archive from execution plans")
public void testCreateArchiveFromExecutionPlans() throws APIManagementException {
APIMgtAdminService apiMgtAdminService = Mockito.mock(APIMgtAdminService.class);
List<APIPolicy> apiPolicies = new ArrayList<>();
List<ApplicationPolicy> applicationPolicies = new ArrayList<>();
List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
List<CustomPolicy> customPolicies = new ArrayList<>();
apiPolicies.add(SampleTestObjectCreator.createDefaultAPIPolicy());
applicationPolicies.add(SampleTestObjectCreator.createDefaultApplicationPolicy());
subscriptionPolicies.add(SampleTestObjectCreator.createDefaultSubscriptionPolicy());
customPolicies.add(SampleTestObjectCreator.createDefaultCustomPolicy());
Mockito.when(apiMgtAdminService.getApiPolicies()).thenReturn(apiPolicies);
Mockito.when(apiMgtAdminService.getApplicationPolicies()).thenReturn(applicationPolicies);
Mockito.when(apiMgtAdminService.getCustomRules()).thenReturn(customPolicies);
Mockito.when(apiMgtAdminService.getSubscriptionPolicies()).thenReturn(subscriptionPolicies);
PolicyExportManager policyExportManager = new PolicyExportManager(apiMgtAdminService);
String path = policyExportManager.createArchiveFromExecutionPlans("exportDir", exportRootDirectory, "policies");
String unzippedPath = exportRootDirectory + File.separator + "export-policies";
APIFileUtils.extractArchive(path, unzippedPath);
Assert.assertEquals(new File(APIFileUtils.getDirectoryList(unzippedPath).iterator().next()).listFiles().length == 6, true, "Exported policy count is not equal to ");
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtilTest method fromCustomPolicyArrayToListDTOTest.
@Test(description = "Convert List<CustomPolicy> to CustomRuleListDTO")
public void fromCustomPolicyArrayToListDTOTest() throws Exception {
CustomPolicy customPolicy1 = new CustomPolicy("namw1");
customPolicy1.setDescription("custom policy desc1");
customPolicy1.setKeyTemplate("keytemplate1");
customPolicy1.setSiddhiQuery("sample query1");
CustomPolicy customPolicy2 = new CustomPolicy("namw2");
customPolicy2.setDescription("custom policy desc2");
customPolicy2.setKeyTemplate("keytemplate2");
customPolicy2.setSiddhiQuery("sample query2");
List<CustomPolicy> customPolicyList = new ArrayList<>();
customPolicyList.add(customPolicy1);
customPolicyList.add(customPolicy2);
CustomRuleListDTO listDTO = CustomPolicyMappingUtil.fromCustomPolicyArrayToListDTO(customPolicyList);
Assert.assertEquals((Integer) customPolicyList.size(), listDTO.getCount());
Assert.assertEquals(customPolicy1.getPolicyName(), listDTO.getList().get(0).getPolicyName());
Assert.assertEquals(customPolicy1.getDisplayName(), listDTO.getList().get(0).getDisplayName());
Assert.assertEquals(customPolicy1.getKeyTemplate(), listDTO.getList().get(0).getKeyTemplate());
Assert.assertEquals(customPolicy1.getSiddhiQuery(), listDTO.getList().get(0).getSiddhiQuery());
Assert.assertEquals(customPolicy1.getDescription(), listDTO.getList().get(0).getDescription());
Assert.assertEquals(customPolicy2.getPolicyName(), listDTO.getList().get(1).getPolicyName());
Assert.assertEquals(customPolicy2.getDisplayName(), listDTO.getList().get(1).getDisplayName());
Assert.assertEquals(customPolicy2.getKeyTemplate(), listDTO.getList().get(1).getKeyTemplate());
Assert.assertEquals(customPolicy2.getSiddhiQuery(), listDTO.getList().get(1).getSiddhiQuery());
Assert.assertEquals(customPolicy2.getDescription(), listDTO.getList().get(1).getDescription());
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy 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.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class PoliciesApiServiceImplTest method policiesThrottlingCustomRuleIdGetTest.
@Test
public void policiesThrottlingCustomRuleIdGetTest() 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.doReturn(policy).doThrow(new IllegalArgumentException()).when(adminService).getCustomRuleByUUID(uuid);
Response response = policiesApiService.policiesThrottlingCustomRuleIdGet(uuid, null, null, getRequest());
Assert.assertEquals(response.getStatus(), 200);
}
Aggregations