use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.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;
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.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");
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.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));
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.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);
}
use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.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);
}
Aggregations