Search in sources :

Example 46 with PolicyDAO

use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testAddCustomRule.

@Test(description = "Test adding a custom rule")
public void testAddCustomRule() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    CustomPolicy customPolicy = SampleTestObjectCreator.createDefaultCustomPolicy();
    Mockito.when(policyDAO.addCustomPolicy(customPolicy)).thenReturn(customPolicy.getUuid());
    String uuid = adminService.addCustomRule(customPolicy);
    Assert.assertEquals(uuid, customPolicy.getUuid());
    // Error path
    Mockito.when(policyDAO.addCustomPolicy(customPolicy)).thenThrow(APIMgtDAOException.class);
    try {
        adminService.addCustomRule(customPolicy);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't add custom policy with policy name: " + customPolicy.getPolicyName());
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 47 with PolicyDAO

use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testGetApplicationPolicyByUuid.

@Test(description = "Test getting Application policy by UUID")
public void testGetApplicationPolicyByUuid() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    Mockito.when(policyDAO.getApplicationPolicyByUuid(applicationPolicy.getUuid())).thenReturn(applicationPolicy);
    adminService.getApplicationPolicyByUuid(applicationPolicy.getUuid());
    Mockito.verify(policyDAO, Mockito.times(1)).getApplicationPolicyByUuid(applicationPolicy.getUuid());
    // Error path
    Mockito.when(policyDAO.getApplicationPolicyByUuid(applicationPolicy.getUuid())).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getApplicationPolicyByUuid(applicationPolicy.getUuid());
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't retrieve Application policy with id: " + applicationPolicy.getUuid());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 48 with PolicyDAO

use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testGetBlockConditions.

@Test(description = "Test getting block conditions")
public void testGetBlockConditions() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    List<BlockConditions> blockConditionsListExpected = new ArrayList<>();
    BlockConditions blockConditions = SampleTestObjectCreator.createDefaultBlockCondition(BLOCK_CONDITION_TYPE);
    blockConditionsListExpected.add(blockConditions);
    Mockito.when(policyDAO.getBlockConditions()).thenReturn(blockConditionsListExpected);
    List<BlockConditions> blockConditionsListReturned = adminService.getBlockConditions();
    Assert.assertEquals(blockConditionsListReturned, blockConditionsListExpected);
    // Error path
    Mockito.when(policyDAO.getBlockConditions()).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getBlockConditions();
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't get list of block conditions.");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 49 with PolicyDAO

use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testAddSubscriptionPolicyWhenPolicyIdNull.

@Test(description = "Test add subscription policy when the policy ID is null")
public void testAddSubscriptionPolicyWhenPolicyIdNull() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIGateway apiGateway = Mockito.mock(APIGateway.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO, apiGateway);
    SubscriptionPolicy policy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    policy.setUuid(null);
    adminService.addSubscriptionPolicy(policy);
    Mockito.verify(policyDAO, Mockito.times(1)).addSubscriptionPolicy(policy);
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 50 with PolicyDAO

use of org.wso2.carbon.apimgt.core.dao.PolicyDAO in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testGetAllPoliciesByLevel.

@Test(description = "Get all policies by level")
public void testGetAllPoliciesByLevel() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    Policy policy = Mockito.mock(Policy.class);
    List<Policy> policyList = new ArrayList<>();
    policyList.add(policy);
    Mockito.when(policyDAO.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.application)).thenReturn(policyList);
    adminService.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.application);
    Mockito.verify(policyDAO, Mockito.times(1)).getPoliciesByLevel(APIMgtAdminService.PolicyLevel.application);
    // Error path
    Mockito.when(policyDAO.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.application)).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.application);
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't retrieve Throttle Policies with level: " + APIMgtAdminService.PolicyLevel.application.name());
    }
}
Also used : ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) 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)

Aggregations

PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)106 Test (org.testng.annotations.Test)102 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)50 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)50 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)45 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)36 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)33 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)30 API (org.wso2.carbon.apimgt.core.models.API)28 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)27 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)24 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)24 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)23 BeforeTest (org.testng.annotations.BeforeTest)20 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)20 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)17 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)16 Application (org.wso2.carbon.apimgt.core.models.Application)15 WorkflowDAO (org.wso2.carbon.apimgt.core.dao.WorkflowDAO)14 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)13