Search in sources :

Example 76 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testGetPoliciesByLevel.

@Test
public void testGetPoliciesByLevel() throws Exception {
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    int policySize;
    // api policy by level
    policySize = policyDAO.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.api).size();
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    policyDAO.addApiPolicy(apiPolicy);
    Assert.assertTrue(policyDAO.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.api).size() == policySize + 1);
    Assert.assertEquals(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName()).getUuid(), apiPolicy.getUuid());
    Assert.assertEquals(policyDAO.getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName()).getUuid(), apiPolicy.getUuid());
    // application policy by level
    policySize = policyDAO.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.application).size();
    ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    policyDAO.addApplicationPolicy(applicationPolicy);
    Assert.assertTrue(policyDAO.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.application).size() == policySize + 1);
    Assert.assertEquals(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getPolicyName()).getUuid(), applicationPolicy.getUuid());
    Assert.assertEquals(policyDAO.getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getPolicyName()).getUuid(), applicationPolicy.getUuid());
    // subscription policy by level
    policySize = policyDAO.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.subscription).size();
    SubscriptionPolicy subscriptionPolicy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    policyDAO.addSubscriptionPolicy(subscriptionPolicy);
    Assert.assertTrue(policyDAO.getPoliciesByLevel(APIMgtAdminService.PolicyLevel.subscription).size() == policySize + 1);
    Assert.assertEquals(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, subscriptionPolicy.getPolicyName()).getUuid(), subscriptionPolicy.getUuid());
    Assert.assertEquals(policyDAO.getPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, subscriptionPolicy.getPolicyName()).getUuid(), subscriptionPolicy.getUuid());
    // When policy is not in the DB
    try {
        policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, "test");
    } catch (APIMgtResourceNotFoundException ex) {
        Assert.assertEquals(ex.getMessage(), "Policy " + APIMgtAdminService.PolicyLevel.api + "Couldn't found " + "test");
    }
}
Also used : 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) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 77 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testAddGetDeleteSubscriptionPolicies.

@Test(description = "Add,Get and Delete Subscription Policies")
public void testAddGetDeleteSubscriptionPolicies() throws Exception {
    SubscriptionPolicy policy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    policy.setUuid("3d253272-25b3-11e7-93ae-92361f002671");
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    // add policy
    policyDAO.addSubscriptionPolicy(policy);
    // get added policy
    Policy addedPolicy = policyDAO.getSubscriptionPolicy(policy.getPolicyName());
    Assert.assertNotNull(addedPolicy);
    Assert.assertEquals(addedPolicy.getPolicyName(), policy.getPolicyName());
    // delete policy
    policyDAO.deletePolicy(APIMgtAdminService.PolicyLevel.subscription, policy.getPolicyName());
    // get policy after deletion
    try {
        policyDAO.getSubscriptionPolicy(policy.getPolicyName());
        Assert.fail("Exception expected, but not thrown.");
    } catch (APIMgtDAOException ex) {
        Assert.assertEquals(ex.getMessage(), "Subscription Policy not found for name: " + addedPolicy.getPolicyName());
    }
    // test for exception: retrieving not available policy
    try {
        policyDAO.getSubscriptionPolicyByUuid(policy.getUuid());
        Assert.fail("Exception expected, but not thrown.");
    } catch (APIMgtDAOException ex) {
        Assert.assertEquals(ex.getMessage(), "Subscription Policy not found for id: " + addedPolicy.getUuid());
    }
}
Also used : 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) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 78 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testGetPolicyByLevelAndUUUID.

@Test
public void testGetPolicyByLevelAndUUUID() throws Exception {
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    SubscriptionPolicy subscriptionPolicy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    policyDAO.addApiPolicy(apiPolicy);
    policyDAO.addApplicationPolicy(applicationPolicy);
    policyDAO.addSubscriptionPolicy(subscriptionPolicy);
    Assert.assertEquals(policyDAO.getPolicyByLevelAndUUID(APIMgtAdminService.PolicyLevel.api, apiPolicy.getUuid()).getPolicyName(), apiPolicy.getPolicyName());
    Assert.assertEquals(policyDAO.getPolicyByLevelAndUUID(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getUuid()).getPolicyName(), applicationPolicy.getPolicyName());
    Assert.assertEquals(policyDAO.getPolicyByLevelAndUUID(APIMgtAdminService.PolicyLevel.subscription, subscriptionPolicy.getUuid()).getPolicyName(), subscriptionPolicy.getPolicyName());
}
Also used : 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) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 79 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testPolicyExists.

@Test(description = "policy exists test")
public void testPolicyExists() throws Exception {
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    policyDAO.addApplicationPolicy(applicationPolicy);
    Assert.assertTrue(policyDAO.policyExists(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getPolicyName()), "Application policy with name: " + applicationPolicy.getPolicyName() + " does not exist");
    policyDAO.deletePolicyByUuid(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getUuid());
    Assert.assertFalse(policyDAO.policyExists(APIMgtAdminService.PolicyLevel.application, applicationPolicy.getPolicyName()), "Deleted Application policy with name: " + applicationPolicy.getPolicyName() + " still exists");
    SubscriptionPolicy subscriptionPolicy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    policyDAO.addSubscriptionPolicy(subscriptionPolicy);
    Assert.assertTrue(policyDAO.policyExists(APIMgtAdminService.PolicyLevel.subscription, subscriptionPolicy.getPolicyName()), "Subscription policy with name: " + subscriptionPolicy.getPolicyName() + " does not exist");
    policyDAO.deletePolicyByUuid(APIMgtAdminService.PolicyLevel.subscription, subscriptionPolicy.getUuid());
    Assert.assertFalse(policyDAO.policyExists(APIMgtAdminService.PolicyLevel.subscription, subscriptionPolicy.getPolicyName()), "Deleted Subscription policy with name: " + subscriptionPolicy.getPolicyName() + " still exists");
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    policyDAO.addApiPolicy(apiPolicy);
    Assert.assertTrue(policyDAO.policyExists(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName()), "API" + " policy with name: " + apiPolicy.getPolicyName() + " does not exist");
    policyDAO.deletePolicyByUuid(APIMgtAdminService.PolicyLevel.api, apiPolicy.getUuid());
    Assert.assertFalse(policyDAO.policyExists(APIMgtAdminService.PolicyLevel.api, apiPolicy.getPolicyName()), "Deleted API policy with name: " + apiPolicy.getPolicyName() + " still exists");
}
Also used : 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) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 80 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testGetAPIWithAllowedLifecycleState.

@Test(description = "Test get API with allowed lifecycle states (Published, Prototyped, Deprecated)")
public void testGetAPIWithAllowedLifecycleState() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    Policy policy = new SubscriptionPolicy(UUID, TIER);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, TIER)).thenReturn(policy);
    APIStore apiStore = getApiStoreImpl(apiDAO, Mockito.mock(ApplicationDAO.class), Mockito.mock(APISubscriptionDAO.class), Mockito.mock(WorkflowDAO.class), Mockito.mock(APIGateway.class), policyDAO);
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
    String apiId = apiBuilder.getId();
    List<String> allowedStates = new ArrayList<String>() {

        {
            add(APIStatus.PROTOTYPED.getStatus());
            add(APIStatus.PUBLISHED.getStatus());
            add(APIStatus.DEPRECATED.getStatus());
        }
    };
    for (String allowedState : allowedStates) {
        apiBuilder.lifeCycleStatus(allowedState);
        API api = apiBuilder.build();
        Mockito.when(apiDAO.getAPI(apiId)).thenReturn(api);
        API returnedAPI = apiStore.getAPIbyUUID(apiId);
        Assert.assertEquals(returnedAPI.getLifeCycleStatus(), allowedState);
    }
}
Also used : 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) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) ArrayList(java.util.ArrayList) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)74 Test (org.testng.annotations.Test)41 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)33 API (org.wso2.carbon.apimgt.core.models.API)30 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)30 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)24 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)19 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)16 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)15 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)15 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)15 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)14 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)14 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)13 ArrayList (java.util.ArrayList)12 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)12 HashSet (java.util.HashSet)10 Application (org.wso2.carbon.apimgt.core.models.Application)9 SQLException (java.sql.SQLException)8