Search in sources :

Example 26 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createSubscriptionPolicyWithRequestLimit.

public static SubscriptionPolicy createSubscriptionPolicyWithRequestLimit(String name) {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(name);
    subscriptionPolicy.setDescription("testDescription");
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType("requestCount");
    RequestCountLimit requestCountLimit = new RequestCountLimit("s", 60, 10);
    quotaPolicy.setLimit(requestCountLimit);
    subscriptionPolicy.setDefaultQuotaPolicy(quotaPolicy);
    return subscriptionPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)

Example 27 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy 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 28 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testFingerprintAfterUpdatingSubscriptionPolicy.

@Test
public void testFingerprintAfterUpdatingSubscriptionPolicy() throws Exception {
    SubscriptionPolicy policy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    policyDAO.addSubscriptionPolicy(policy);
    String fingerprintBeforeUpdatingPolicy = ETagUtils.generateETag(policyDAO.getLastUpdatedTimeOfThrottlingPolicy(APIMgtAdminService.PolicyLevel.subscription, policy.getPolicyName()));
    Assert.assertNotNull(fingerprintBeforeUpdatingPolicy);
    SubscriptionPolicy updatedPolicy = SampleTestObjectCreator.updateSubscriptionPolicy(policy);
    Thread.sleep(100L);
    policyDAO.updateSubscriptionPolicy(updatedPolicy);
    String fingerprintAfterUpdatingPolicy = ETagUtils.generateETag(policyDAO.getLastUpdatedTimeOfThrottlingPolicy(APIMgtAdminService.PolicyLevel.subscription, updatedPolicy.getPolicyName()));
    Assert.assertNotNull(fingerprintAfterUpdatingPolicy);
    Assert.assertNotEquals(fingerprintBeforeUpdatingPolicy, fingerprintAfterUpdatingPolicy, TestUtil.printDiff(fingerprintBeforeUpdatingPolicy, fingerprintAfterUpdatingPolicy));
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 29 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testGetAllPolicies.

@Test
public void testGetAllPolicies() throws Exception {
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    int size = policyDAO.getAllPolicies().size();
    APIPolicy apiPolicy = SampleTestObjectCreator.createDefaultAPIPolicy();
    ApplicationPolicy applicationPolicy = SampleTestObjectCreator.createDefaultApplicationPolicy();
    SubscriptionPolicy subscriptionPolicy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    policyDAO.addApiPolicy(apiPolicy);
    policyDAO.addApplicationPolicy(applicationPolicy);
    policyDAO.addSubscriptionPolicy(subscriptionPolicy);
    Set<PolicyValidationData> policyValidationData = policyDAO.getAllPolicies();
    Assert.assertTrue(policyValidationData.size() == size + 3);
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 30 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy 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)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)74 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)42 Test (org.testng.annotations.Test)41 ArrayList (java.util.ArrayList)36 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 Test (org.junit.Test)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25 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 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)18 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)16 SQLException (java.sql.SQLException)15 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)15 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)15 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)15