use of org.wso2.carbon.apimgt.api.model.policy.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;
}
use of org.wso2.carbon.apimgt.api.model.policy.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);
}
use of org.wso2.carbon.apimgt.api.model.policy.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);
}
}
use of org.wso2.carbon.apimgt.api.model.policy.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);
}
use of org.wso2.carbon.apimgt.api.model.policy.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);
}
Aggregations