Search in sources :

Example 41 with Subscription

use of org.wso2.carbon.apimgt.keymgt.model.entity.Subscription in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testGetSubscriptionPolicies.

@Test(description = "Get Subscription Policies")
public void testGetSubscriptionPolicies() throws Exception {
    SubscriptionPolicy policy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    SubscriptionPolicy policy2 = SampleTestObjectCreator.createSubscriptionPolicyWithBandwithLimit();
    // policy 1 has following 2 true. checking for fault scenario from policy2
    policy2.setStopOnQuotaReach(false);
    policy2.setDeployed(false);
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    // add policy
    policyDAO.addSubscriptionPolicy(policy);
    policyDAO.addSubscriptionPolicy(policy2);
    List<SubscriptionPolicy> policyList = policyDAO.getSubscriptionPolicies();
    Assert.assertNotNull(policyList);
    SubscriptionPolicy retrievedPolicy = null;
    SubscriptionPolicy retrievedPolicy2 = null;
    // there are defaut policies already there
    for (Iterator iterator = policyList.iterator(); iterator.hasNext(); ) {
        SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) iterator.next();
        if (subscriptionPolicy.getPolicyName().equals(policy.getPolicyName())) {
            retrievedPolicy = policy;
        }
        if (subscriptionPolicy.getPolicyName().equals(policy2.getPolicyName())) {
            retrievedPolicy2 = policy2;
        }
    }
    Assert.assertNotNull(retrievedPolicy, "Policy " + policy.getPolicyName() + " not in DB");
    Assert.assertNotNull(retrievedPolicy2, "Policy " + policy2.getPolicyName() + " not in DB");
    Assert.assertEquals(retrievedPolicy.getPolicyName(), policy.getPolicyName(), "Subscription policy name mismatch");
    Assert.assertEquals(retrievedPolicy.getDisplayName(), policy.getDisplayName(), "Subscription policy display name mismatch");
    Assert.assertEquals(retrievedPolicy.getDescription(), policy.getDescription(), "Subscription policy description mismatch");
    Assert.assertEquals(retrievedPolicy.isDeployed(), policy.isDeployed(), "Subscription policy isDeployed mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitCount(), policy.getRateLimitCount(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitTimeUnit(), policy.getRateLimitTimeUnit(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.isStopOnQuotaReach(), policy.isStopOnQuotaReach(), "Subscription policy stop on quota reach mismatch");
    Assert.assertEquals(retrievedPolicy.getCustomAttributes(), policy.getCustomAttributes(), "Subscription policy custom attribute mismatch");
    Assert.assertEquals(retrievedPolicy.getDefaultQuotaPolicy().getType(), policy.getDefaultQuotaPolicy().getType(), "Subscription policy default quota type mismatch");
    RequestCountLimit limitRetrieved = (RequestCountLimit) retrievedPolicy.getDefaultQuotaPolicy().getLimit();
    RequestCountLimit policyLimit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
    Assert.assertEquals(limitRetrieved.getRequestCount(), policyLimit.getRequestCount(), "Subscription policy default quota count mismatch");
    Assert.assertEquals(limitRetrieved.getTimeUnit(), policyLimit.getTimeUnit(), "Subscription policy default quota time unit mismatch");
    Assert.assertEquals(limitRetrieved.getUnitTime(), policyLimit.getUnitTime(), "Subscription policy default quota unit time mismatch");
    // check policy related properties for policy 2
    Assert.assertEquals(retrievedPolicy2.getPolicyName(), policy2.getPolicyName(), "Subscription policy name mismatch");
    Assert.assertEquals(retrievedPolicy2.getDisplayName(), policy2.getDisplayName(), "Subscription policy display name mismatch");
    Assert.assertEquals(retrievedPolicy2.getDescription(), policy2.getDescription(), "Subscription policy description mismatch");
    Assert.assertEquals(retrievedPolicy2.isDeployed(), policy2.isDeployed(), "Subscription policy isDeployed mismatch");
    Assert.assertEquals(retrievedPolicy2.getRateLimitCount(), policy2.getRateLimitCount(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy2.getRateLimitTimeUnit(), policy2.getRateLimitTimeUnit(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy2.isStopOnQuotaReach(), policy2.isStopOnQuotaReach(), "Subscription policy stop on quota reach mismatch");
    Assert.assertEquals(retrievedPolicy2.getCustomAttributes(), policy2.getCustomAttributes(), "Subscription policy custom attribute mismatch");
    Assert.assertEquals(retrievedPolicy2.getDefaultQuotaPolicy().getType(), policy2.getDefaultQuotaPolicy().getType(), "Subscription policy default quota type mismatch");
    BandwidthLimit limitRetrieved2 = (BandwidthLimit) retrievedPolicy2.getDefaultQuotaPolicy().getLimit();
    BandwidthLimit policyLimit2 = (BandwidthLimit) policy2.getDefaultQuotaPolicy().getLimit();
    Assert.assertEquals(limitRetrieved2.getDataAmount(), policyLimit2.getDataAmount(), "Subscription policy default quota data amount mismatch");
    Assert.assertEquals(limitRetrieved2.getDataUnit(), policyLimit2.getDataUnit(), "Subscription policy default quota data amount mismatch");
    Assert.assertEquals(limitRetrieved2.getTimeUnit(), policyLimit2.getTimeUnit(), "Subscription policy default quota time unit mismatch");
    Assert.assertEquals(limitRetrieved2.getUnitTime(), policyLimit2.getUnitTime(), "Subscription policy default quota unit time mismatch");
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Iterator(java.util.Iterator) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) Test(org.testng.annotations.Test)

Example 42 with Subscription

use of org.wso2.carbon.apimgt.keymgt.model.entity.Subscription in project carbon-apimgt by wso2.

the class WorkflowDAOIT method testGetWorkflowsList.

@Test
public void testGetWorkflowsList() throws Exception {
    WorkflowDAO workflowDAO = DAOFactory.getWorkflowDAO();
    // pending application 1
    String workflowExtRefId1 = UUID.randomUUID().toString();
    String applicationId1 = UUID.randomUUID().toString();
    Workflow workflow1 = SampleTestObjectCreator.createWorkflow(workflowExtRefId1);
    workflow1.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
    workflow1.setWorkflowReference(applicationId1);
    workflow1.setStatus(WorkflowStatus.CREATED);
    workflowDAO.addWorkflowEntry(workflow1);
    // pending application 2
    String workflowExtRefId2 = UUID.randomUUID().toString();
    String applicationId2 = UUID.randomUUID().toString();
    Workflow workflow2 = SampleTestObjectCreator.createWorkflow(workflowExtRefId2);
    workflow2.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
    workflow2.setWorkflowReference(applicationId2);
    workflow2.setStatus(WorkflowStatus.CREATED);
    workflowDAO.addWorkflowEntry(workflow2);
    // completed application 1
    String workflowExtRefId3 = UUID.randomUUID().toString();
    String applicationId3 = UUID.randomUUID().toString();
    Workflow workflow3 = SampleTestObjectCreator.createWorkflow(workflowExtRefId3);
    workflow3.setWorkflowType(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
    workflow3.setWorkflowReference(applicationId3);
    workflow3.setStatus(WorkflowStatus.APPROVED);
    workflowDAO.addWorkflowEntry(workflow3);
    // pending subscription 1
    String workflowExtRefId4 = UUID.randomUUID().toString();
    String applicationId4 = UUID.randomUUID().toString();
    Workflow workflow4 = SampleTestObjectCreator.createWorkflow(workflowExtRefId4);
    workflow4.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
    workflow4.setWorkflowReference(applicationId4);
    workflow4.setStatus(WorkflowStatus.CREATED);
    workflowDAO.addWorkflowEntry(workflow4);
    // completed subscription 1
    String workflowExtRefId5 = UUID.randomUUID().toString();
    String applicationId5 = UUID.randomUUID().toString();
    Workflow workflow5 = SampleTestObjectCreator.createWorkflow(workflowExtRefId5);
    workflow5.setWorkflowType(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION);
    workflow5.setWorkflowReference(applicationId5);
    workflow5.setStatus(WorkflowStatus.APPROVED);
    workflowDAO.addWorkflowEntry(workflow5);
    List<Workflow> list = workflowDAO.retrieveUncompleteWorkflows(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION);
    for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
        Workflow workflow = (Workflow) iterator.next();
        // check whether there are any completed workflows
        Assert.assertFalse(WorkflowStatus.APPROVED == workflow.getStatus(), "Retrieved list contains approved workflows");
        // check whether it has subscription workflow entry
        Assert.assertFalse(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION.equals(workflow.getWorkflowType()), "Application workflow list contains subscription workflow entry");
    }
    // get all the pending workflows
    List<Workflow> fullList = workflowDAO.retrieveUncompleteWorkflows();
    Assert.assertEquals(fullList.size(), 3, "Retrived workflow list does not contain valid entires");
}
Also used : WorkflowDAO(org.wso2.carbon.apimgt.core.dao.WorkflowDAO) Iterator(java.util.Iterator) Workflow(org.wso2.carbon.apimgt.core.workflow.Workflow) Test(org.testng.annotations.Test)

Example 43 with Subscription

use of org.wso2.carbon.apimgt.keymgt.model.entity.Subscription in project carbon-apimgt by wso2.

the class SubscriptionDAOImplIT method testAddAndGetSubscription.

@Test
public void testAddAndGetSubscription() throws Exception {
    // add new app
    Application app = TestUtil.addTestApplication();
    // add new api
    API api = TestUtil.addTestAPI();
    // add subscription
    String subscriptionTier = GOLD_TIER;
    APISubscriptionDAO apiSubscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    String uuid = UUID.randomUUID().toString();
    apiSubscriptionDAO.addAPISubscription(uuid, api.getId(), app.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    // get subscription
    Subscription subscription = apiSubscriptionDAO.getAPISubscription(uuid);
    // validate
    Assert.assertNotNull(subscription);
    Assert.assertEquals(subscription.getId(), uuid);
    Assert.assertEquals(subscription.getStatus(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    Assert.assertEquals(subscription.getPolicy().getPolicyName(), subscriptionTier);
    Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api)));
    Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app)));
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 44 with Subscription

use of org.wso2.carbon.apimgt.keymgt.model.entity.Subscription in project carbon-apimgt by wso2.

the class SubscriptionDAOImplIT method testUpdateSubscription.

@Test
public void testUpdateSubscription() throws Exception {
    // add new app
    Application app = TestUtil.addTestApplication();
    // add new api
    API api = TestUtil.addTestAPI();
    // add subscription
    String subscriptionPolicy = GOLD_TIER;
    APISubscriptionDAO apiSubscriptionDAO = DAOFactory.getAPISubscriptionDAO();
    String uuid = UUID.randomUUID().toString();
    apiSubscriptionDAO.addAPISubscription(uuid, api.getId(), app.getId(), goldSubscriptionPolicy.getUuid(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    // get subscription
    Subscription subscription = apiSubscriptionDAO.getAPISubscription(uuid);
    // validate fingerprint
    String fingerprintBeforeUpdate = ETagUtils.generateETag(apiSubscriptionDAO.getLastUpdatedTimeOfSubscription(uuid));
    Assert.assertNotNull(fingerprintBeforeUpdate);
    // validate tier and status
    Assert.assertNotNull(subscription);
    Assert.assertEquals(subscription.getId(), uuid);
    Assert.assertEquals(subscription.getStatus(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    Assert.assertEquals(subscription.getPolicy().getPolicyName(), subscriptionPolicy);
    // update subscription policy
    String newSubscriptionPolicy = SILVER_TIER;
    apiSubscriptionDAO.updateSubscriptionPolicy(uuid, newSubscriptionPolicy);
    // get subscription
    subscription = apiSubscriptionDAO.getAPISubscription(uuid);
    // validate
    Assert.assertNotNull(subscription);
    Assert.assertEquals(subscription.getId(), uuid);
    Assert.assertEquals(subscription.getStatus(), APIMgtConstants.SubscriptionStatus.ACTIVE);
    Assert.assertEquals(subscription.getPolicy().getPolicyName(), newSubscriptionPolicy);
    Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api)));
    Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app)));
    // update subscription status
    APIMgtConstants.SubscriptionStatus newSubscriptionStatus = APIMgtConstants.SubscriptionStatus.PROD_ONLY_BLOCKED;
    apiSubscriptionDAO.updateSubscriptionStatus(uuid, newSubscriptionStatus);
    // get subscription
    subscription = apiSubscriptionDAO.getAPISubscription(uuid);
    String fingerprintAfterUpdate = ETagUtils.generateETag(apiSubscriptionDAO.getLastUpdatedTimeOfSubscription(uuid));
    // validate
    Assert.assertNotNull(subscription);
    Assert.assertNotNull(fingerprintAfterUpdate);
    Assert.assertEquals(subscription.getId(), uuid);
    Assert.assertEquals(subscription.getStatus(), newSubscriptionStatus);
    Assert.assertEquals(subscription.getPolicy().getPolicyName(), newSubscriptionPolicy);
    Assert.assertEquals(subscription.getApi(), TestUtil.createSummaryAPI(api), TestUtil.printDiff(subscription.getApi(), TestUtil.createSummaryAPI(api)));
    Assert.assertEquals(subscription.getApplication(), TestUtil.createSummaryApplication(app), TestUtil.printDiff(subscription.getApplication(), TestUtil.createSummaryApplication(app)));
    Assert.assertNotEquals(fingerprintBeforeUpdate, fingerprintAfterUpdate);
}
Also used : APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) APIMgtConstants(org.wso2.carbon.apimgt.core.util.APIMgtConstants) Test(org.testng.annotations.Test)

Example 45 with Subscription

use of org.wso2.carbon.apimgt.keymgt.model.entity.Subscription in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testGetSubscriptionPolicyByUUIDandName.

@Test(description = "Get Subscription Policy by Name and UUID")
public void testGetSubscriptionPolicyByUUIDandName() throws Exception {
    SubscriptionPolicy policy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    // add policy
    policyDAO.addSubscriptionPolicy(policy);
    SubscriptionPolicy retrievedPolicy = policyDAO.getSubscriptionPolicy(policy.getPolicyName());
    Assert.assertNotNull(retrievedPolicy);
    Assert.assertEquals(retrievedPolicy.getPolicyName(), policy.getPolicyName(), "Subscription policy name mismatch");
    Assert.assertEquals(retrievedPolicy.getDisplayName(), policy.getDisplayName(), "Subscription policy display name mismatch");
    Assert.assertEquals(retrievedPolicy.getDescription(), policy.getDescription(), "Subscription policy description mismatch");
    Assert.assertEquals(retrievedPolicy.isDeployed(), policy.isDeployed(), "Subscription policy isDeployed mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitCount(), policy.getRateLimitCount(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitTimeUnit(), policy.getRateLimitTimeUnit(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.isStopOnQuotaReach(), policy.isStopOnQuotaReach(), "Subscription policy stop on quota reach mismatch");
    Assert.assertEquals(retrievedPolicy.getCustomAttributes(), policy.getCustomAttributes(), "Subscription policy custom attribute mismatch");
    Assert.assertEquals(retrievedPolicy.getDefaultQuotaPolicy().getType(), policy.getDefaultQuotaPolicy().getType(), "Subscription policy default quota type mismatch");
    RequestCountLimit limitRetrieved = (RequestCountLimit) retrievedPolicy.getDefaultQuotaPolicy().getLimit();
    RequestCountLimit policyLimit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
    Assert.assertEquals(limitRetrieved.getRequestCount(), policyLimit.getRequestCount(), "Subscription policy default quota count mismatch");
    Assert.assertEquals(limitRetrieved.getTimeUnit(), policyLimit.getTimeUnit(), "Subscription policy default quota time unit mismatch");
    Assert.assertEquals(limitRetrieved.getUnitTime(), policyLimit.getUnitTime(), "Subscription policy default quota unit time mismatch");
    // get the UUID of the created policy. since this value is generated in the code, we use previously retrieved
    // policy to get the uuid and again query the policy
    String uuid = retrievedPolicy.getUuid();
    retrievedPolicy = policyDAO.getSubscriptionPolicyByUuid(uuid);
    Assert.assertNotNull(retrievedPolicy);
    Assert.assertEquals(retrievedPolicy.getPolicyName(), policy.getPolicyName(), "Subscription policy name mismatch");
    Assert.assertEquals(retrievedPolicy.getDisplayName(), policy.getDisplayName(), "Subscription policy display name mismatch");
    Assert.assertEquals(retrievedPolicy.getDescription(), policy.getDescription(), "Subscription policy description mismatch");
    Assert.assertEquals(retrievedPolicy.isDeployed(), policy.isDeployed(), "Subscription policy isDeployed mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitCount(), policy.getRateLimitCount(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.getRateLimitTimeUnit(), policy.getRateLimitTimeUnit(), "Subscription policy rate limit mismatch");
    Assert.assertEquals(retrievedPolicy.isStopOnQuotaReach(), policy.isStopOnQuotaReach(), "Subscription policy stop on quota reach mismatch");
    Assert.assertEquals(retrievedPolicy.getCustomAttributes(), policy.getCustomAttributes(), "Subscription policy custom attribute mismatch");
    Assert.assertEquals(retrievedPolicy.getDefaultQuotaPolicy().getType(), policy.getDefaultQuotaPolicy().getType(), "Subscription policy default quota type mismatch");
    limitRetrieved = (RequestCountLimit) retrievedPolicy.getDefaultQuotaPolicy().getLimit();
    policyLimit = (RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit();
    Assert.assertEquals(limitRetrieved.getRequestCount(), policyLimit.getRequestCount(), "Subscription policy default quota count mismatch");
    Assert.assertEquals(limitRetrieved.getTimeUnit(), policyLimit.getTimeUnit(), "Subscription policy default quota time unit mismatch");
    Assert.assertEquals(limitRetrieved.getUnitTime(), policyLimit.getUnitTime(), "Subscription policy default quota unit time mismatch");
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 ArrayList (java.util.ArrayList)69 Test (org.testng.annotations.Test)58 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)58 Test (org.junit.Test)56 SQLException (java.sql.SQLException)55 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)51 Connection (java.sql.Connection)49 PreparedStatement (java.sql.PreparedStatement)48 ResultSet (java.sql.ResultSet)39 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)37 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)35 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)34 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)34 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)31 Application (org.wso2.carbon.apimgt.core.models.Application)30 API (org.wso2.carbon.apimgt.core.models.API)28 Response (javax.ws.rs.core.Response)24 Application (org.wso2.carbon.apimgt.api.model.Application)22 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)22