Search in sources :

Example 26 with APIMgtAdminServiceImpl

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

Example 27 with APIMgtAdminServiceImpl

use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testGetThreatProtectionPolicyList.

@Test(description = "Test getting list of all threat protection policies")
public void testGetThreatProtectionPolicyList() throws APIManagementException {
    ThreatProtectionDAO threatProtectionDAO = Mockito.mock(ThreatProtectionDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(threatProtectionDAO);
    List<ThreatProtectionPolicy> policyList = new ArrayList<>();
    policyList.add(SampleTestObjectCreator.createUniqueThreatProtectionPolicy());
    policyList.add(SampleTestObjectCreator.createUniqueThreatProtectionPolicy());
    Mockito.when(threatProtectionDAO.getPolicies()).thenReturn(policyList);
    List<ThreatProtectionPolicy> policyListReturned = adminService.getThreatProtectionPolicyList();
    Assert.assertEquals(policyListReturned.size(), policyList.size());
    // Error path
    Mockito.when(threatProtectionDAO.getPolicies()).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getThreatProtectionPolicyList();
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error while retrieving threat protection policy list");
    }
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ArrayList(java.util.ArrayList) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) Test(org.testng.annotations.Test)

Example 28 with APIMgtAdminServiceImpl

use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testGetCustomRules.

@Test(description = "Test getting custom rules")
public void testGetCustomRules() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    List<CustomPolicy> customPolicyListExpected = new ArrayList<>();
    CustomPolicy customPolicy = SampleTestObjectCreator.createDefaultCustomPolicy();
    customPolicyListExpected.add(customPolicy);
    Mockito.when(policyDAO.getCustomPolicies()).thenReturn(customPolicyListExpected);
    List<CustomPolicy> customPolicyListReturned = adminService.getCustomRules();
    Assert.assertEquals(customPolicyListReturned, customPolicyListExpected);
    // Error path
    Mockito.when(policyDAO.getCustomPolicies()).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getCustomRules();
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't get list of custom policy.");
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ArrayList(java.util.ArrayList) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 29 with APIMgtAdminServiceImpl

use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testDeleteThreatProtectionPolicy.

@Test(description = "Test delete threat protection policy")
public void testDeleteThreatProtectionPolicy() throws APIManagementException {
    ThreatProtectionDAO threatProtectionDAO = Mockito.mock(ThreatProtectionDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(threatProtectionDAO);
    ThreatProtectionPolicy policy = SampleTestObjectCreator.createUniqueThreatProtectionPolicy();
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(threatProtectionDAO).deletePolicy(policy.getUuid());
    try {
        adminService.deleteThreatProtectionPolicy(policy.getUuid());
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error deleting threat protection policy");
    }
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ThreatProtectionDAO(org.wso2.carbon.apimgt.core.dao.ThreatProtectionDAO) Test(org.testng.annotations.Test)

Example 30 with APIMgtAdminServiceImpl

use of org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testDeletePolicyByUuid.

@Test(description = "Test delete policy by UUID")
public void testDeletePolicyByUuid() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    adminService.deletePolicyByUuid(apiPolicy.getUuid(), APIMgtAdminService.PolicyLevel.api);
    // Error path
    Mockito.doThrow(APIMgtDAOException.class).when(policyDAO).deletePolicyByUuid(APIMgtAdminService.PolicyLevel.api, apiPolicy.getUuid());
    try {
        adminService.deletePolicyByUuid(apiPolicy.getUuid(), APIMgtAdminService.PolicyLevel.api);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't delete policy with id: " + apiPolicy.getUuid() + ", level: " + APIMgtAdminService.PolicyLevel.api);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Aggregations

APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)67 Response (javax.ws.rs.core.Response)65 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)65 Test (org.junit.Test)59 Test (org.testng.annotations.Test)58 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)46 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)33 ArrayList (java.util.ArrayList)28 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)20 APIManagerFactory (org.wso2.carbon.apimgt.core.impl.APIManagerFactory)17 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)14 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)13 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)11 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)11 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)11 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)10 Label (org.wso2.carbon.apimgt.core.models.Label)10 ThreatProtectionPolicy (org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy)9 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)6 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)6