Search in sources :

Example 6 with CustomPolicy

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();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 7 with CustomPolicy

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 ");
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ArrayList(java.util.ArrayList) APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) File(java.io.File) Test(org.testng.annotations.Test)

Example 8 with CustomPolicy

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());
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ArrayList(java.util.ArrayList) CustomRuleListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO) Test(org.testng.annotations.Test)

Example 9 with CustomPolicy

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);
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO) Test(org.testng.annotations.Test)

Example 10 with CustomPolicy

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);
}
Also used : Response(javax.ws.rs.core.Response) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) PoliciesApiServiceImpl(org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)29 Test (org.testng.annotations.Test)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)10 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)7 ArrayList (java.util.ArrayList)6 CustomRuleDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)6 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)4 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)4 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2