Search in sources :

Example 56 with QuotaPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.

the class TestUtils method getUniqueSubscriptionPolicyWithRequestCountLimit.

/**
 * Return unique subscription policy with rate limit
 *
 * @return unique subscription policy
 */
public static SubscriptionPolicy getUniqueSubscriptionPolicyWithRequestCountLimit() {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(UUID.randomUUID().toString());
    Limit limit = new RequestCountLimit();
    limit.setTimeUnit("seconds");
    limit.setUnitTime(10);
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setLimit(limit);
    subscriptionPolicy.setDefaultQuotaPolicy(quotaPolicy);
    return subscriptionPolicy;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) Limit(org.wso2.carbon.apimgt.api.model.policy.Limit)

Example 57 with QuotaPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.

the class APIUtilTest method testGetAPI.

@Test
public void testGetAPI() throws Exception {
    API expectedAPI = getUniqueAPI();
    final String provider = expectedAPI.getId().getProviderName();
    final String tenantDomain = org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    final int tenantId = -1234;
    GovernanceArtifact artifact = Mockito.mock(GovernanceArtifact.class);
    Registry registry = Mockito.mock(Registry.class);
    ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
    Resource resource = Mockito.mock(Resource.class);
    ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
    RealmService realmService = Mockito.mock(RealmService.class);
    TenantManager tenantManager = Mockito.mock(TenantManager.class);
    APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
    ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
    SubscriptionPolicy policy = Mockito.mock(SubscriptionPolicy.class);
    SubscriptionPolicy[] policies = new SubscriptionPolicy[] { policy };
    QuotaPolicy quotaPolicy = Mockito.mock(QuotaPolicy.class);
    RequestCountLimit limit = Mockito.mock(RequestCountLimit.class);
    PowerMockito.mockStatic(ApiMgtDAO.class);
    PowerMockito.mockStatic(MultitenantUtils.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
    Mockito.when(apiMgtDAO.getAPIID(Mockito.any(String.class))).thenReturn(123);
    Mockito.when(artifact.getId()).thenReturn("");
    Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER)).thenReturn(provider);
    Mockito.when(MultitenantUtils.getTenantDomain(provider)).thenReturn(tenantDomain);
    Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
    Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
    Mockito.when(tenantManager.getTenantId(tenantDomain)).thenReturn(tenantId);
    String artifactPath = "";
    PowerMockito.mockStatic(GovernanceUtils.class);
    Mockito.when(GovernanceUtils.getArtifactPath(registry, "")).thenReturn(artifactPath);
    Mockito.when(registry.get(artifactPath)).thenReturn(resource);
    Mockito.when(resource.getLastModified()).thenReturn(expectedAPI.getLastUpdated());
    Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
    Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
    Mockito.when(apiManagerConfiguration.getThrottleProperties()).thenReturn(throttleProperties);
    Mockito.when(apiMgtDAO.getSubscriptionPolicies(tenantId)).thenReturn(policies);
    Mockito.when(policy.getPolicyName()).thenReturn("policy");
    Mockito.when(policy.getDefaultQuotaPolicy()).thenReturn(quotaPolicy);
    Mockito.when(quotaPolicy.getLimit()).thenReturn(limit);
    Mockito.when(registry.getTags(artifactPath)).thenReturn(getTagsFromSet(expectedAPI.getTags()));
    HashMap<String, String> urlPatterns = getURLTemplatePattern(expectedAPI.getUriTemplates());
    Mockito.when(apiMgtDAO.getURITemplatesPerAPIAsString(Mockito.any(String.class))).thenReturn(urlPatterns);
    CORSConfiguration corsConfiguration = expectedAPI.getCorsConfiguration();
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_HEADERS)).thenReturn(corsConfiguration.getAccessControlAllowHeaders().toString());
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_METHODS)).thenReturn(corsConfiguration.getAccessControlAllowMethods().toString());
    Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.CORS_CONFIGURATION_ACCESS_CTL_ALLOW_ORIGIN)).thenReturn(corsConfiguration.getAccessControlAllowOrigins().toString());
    API api = APIUtil.getAPI(artifact, registry);
    Assert.assertNotNull(api);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) Resource(org.wso2.carbon.registry.core.Resource) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) APIUtil.getOAuthConfigurationFromTenantRegistry(org.wso2.carbon.apimgt.impl.utils.APIUtil.getOAuthConfigurationFromTenantRegistry) Registry(org.wso2.carbon.registry.core.Registry) CORSConfiguration(org.wso2.carbon.apimgt.api.model.CORSConfiguration) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) RealmService(org.wso2.carbon.user.core.service.RealmService) API(org.wso2.carbon.apimgt.api.model.API) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) TenantManager(org.wso2.carbon.user.core.tenant.TenantManager) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 58 with QuotaPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.

the class SubscriptionValidationDAO method setCommonProperties.

private void setCommonProperties(Policy policy, ResultSet resultSet) throws SQLException {
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    String prefix = "";
    if (policy instanceof APIPolicy) {
        prefix = "DEFAULT_";
    }
    quotaPolicy.setType(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
    if (quotaPolicy.getType() != null) {
        if (PolicyConstants.REQUEST_COUNT_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
            RequestCountLimit reqLimit = new RequestCountLimit();
            reqLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
            reqLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
            reqLimit.setRequestCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
            quotaPolicy.setLimit(reqLimit);
        } else if (PolicyConstants.BANDWIDTH_TYPE.equalsIgnoreCase(quotaPolicy.getType())) {
            BandwidthLimit bandLimit = new BandwidthLimit();
            bandLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
            bandLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
            bandLimit.setDataAmount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
            bandLimit.setDataUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_QUOTA_UNIT));
            quotaPolicy.setLimit(bandLimit);
        } else if (PolicyConstants.EVENT_COUNT_TYPE.equals(quotaPolicy.getType())) {
            EventCountLimit eventCountLimit = new EventCountLimit();
            eventCountLimit.setEventCount(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_QUOTA));
            eventCountLimit.setTimeUnit(resultSet.getString(prefix + ThrottlePolicyConstants.COLUMN_TIME_UNIT));
            eventCountLimit.setUnitTime(resultSet.getInt(prefix + ThrottlePolicyConstants.COLUMN_UNIT_TIME));
            quotaPolicy.setLimit(eventCountLimit);
        }
        policy.setQuotaPolicy(quotaPolicy);
    }
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) EventCountLimit(org.wso2.carbon.apimgt.api.model.policy.EventCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.subscription.APIPolicy) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 59 with QuotaPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.

the class APIUtilTierTest method generateApiPolicies.

private APIPolicy[] generateApiPolicies(String[] policyNames) {
    List<APIPolicy> policyList = new ArrayList<APIPolicy>();
    for (String policyName : policyNames) {
        APIPolicy policy = new APIPolicy(policyName);
        QuotaPolicy quotaPolicy = new QuotaPolicy();
        RequestCountLimit countLimit = new RequestCountLimit();
        countLimit.setRequestCount(123);
        quotaPolicy.setLimit(countLimit);
        policy.setDefaultQuotaPolicy(quotaPolicy);
        policy.setDescription(policyName);
        policyList.add(policy);
    }
    APIPolicy[] array = {};
    return policyList.toArray(array);
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) ArrayList(java.util.ArrayList) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) Matchers.anyString(org.mockito.Matchers.anyString)

Example 60 with QuotaPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy in project carbon-apimgt by wso2.

the class APIUtilTierTest method generateApiPoliciesBandwidth.

private APIPolicy[] generateApiPoliciesBandwidth(String[] policyNames) {
    List<APIPolicy> policyList = new ArrayList<APIPolicy>();
    for (String policyName : policyNames) {
        APIPolicy policy = new APIPolicy(policyName);
        QuotaPolicy quotaPolicy = new QuotaPolicy();
        BandwidthLimit bandwidthLimit = new BandwidthLimit();
        bandwidthLimit.setDataAmount(1000);
        bandwidthLimit.setDataUnit("seconds");
        quotaPolicy.setLimit(bandwidthLimit);
        policy.setDefaultQuotaPolicy(quotaPolicy);
        policy.setDescription(policyName);
        policyList.add(policy);
    }
    APIPolicy[] array = {};
    return policyList.toArray(array);
}
Also used : ArrayList(java.util.ArrayList) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) Matchers.anyString(org.mockito.Matchers.anyString) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Aggregations

QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)34 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)27 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)24 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)23 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)16 ArrayList (java.util.ArrayList)13 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)12 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)12 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)10 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)9 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)9 EventCountLimit (org.wso2.carbon.apimgt.api.model.policy.EventCountLimit)7 Test (org.testng.annotations.Test)6 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)6 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)6 ApiMgtDAO (org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)6 Test (org.junit.Test)5 Matchers.anyString (org.mockito.Matchers.anyString)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)5