use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIUtilTierTest method testGetAllTiers.
@Test
public void testGetAllTiers() throws APIManagementException, RegistryException {
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
ThrottleProperties throttleProperties = Mockito.mock(ThrottleProperties.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ApiMgtDAO.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
PowerMockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(amConfigService);
Mockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
Mockito.when(amConfig.getThrottleProperties()).thenReturn(throttleProperties);
Mockito.when(throttleProperties.isEnableUnlimitedTier()).thenReturn(true);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getGovernanceSystemRegistry()).thenReturn(userRegistry);
SubscriptionPolicy[] policies = new SubscriptionPolicy[3];
policies[0] = TestUtils.getUniqueSubscriptionPolicyWithBandwidthLimit();
policies[1] = TestUtils.getUniqueSubscriptionPolicyWithRequestCountLimit();
policies[2] = TestUtils.getUniqueSubscriptionPolicyWithBandwidthLimit();
Mockito.when(apiMgtDAO.getSubscriptionPolicies(Mockito.anyInt())).thenReturn(policies);
// IsEnabled true scenario
Assert.assertEquals(3, APIUtil.getAllTiers().size());
}
use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIMgtDAOTest method testUpdateSubscriptionPolicy.
@Test
public void testUpdateSubscriptionPolicy() throws APIManagementException {
String policyName = "TestUpdateSubPolicy";
SubscriptionPolicy policy = (SubscriptionPolicy) getSubscriptionPolicy(policyName);
policy.setMonetizationPlan(APIConstants.Monetization.FIXED_RATE);
policy.setMonetizationPlanProperties(new HashMap<String, String>());
apiMgtDAO.addSubscriptionPolicy(policy);
policy = (SubscriptionPolicy) getSubscriptionPolicy(policyName);
policy.setDescription("Updated subscription description");
policy.setGraphQLMaxComplexity(200);
policy.setGraphQLMaxDepth(7);
apiMgtDAO.updateSubscriptionPolicy(policy);
}
use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIMgtDAOTest method testAddAndGetSubscriptionPolicy.
@Test
public void testAddAndGetSubscriptionPolicy() throws Exception {
SubscriptionPolicy subscriptionPolicy = (SubscriptionPolicy) getSubscriptionPolicy("testAddAndGetSubscriptionPolicy");
String customAttributes = "{api:abc}";
subscriptionPolicy.setTenantId(-1234);
subscriptionPolicy.setCustomAttributes(customAttributes.getBytes());
subscriptionPolicy.setMonetizationPlan(APIConstants.Monetization.FIXED_RATE);
subscriptionPolicy.setMonetizationPlanProperties(new HashMap<String, String>());
apiMgtDAO.addSubscriptionPolicy(subscriptionPolicy);
SubscriptionPolicy retrievedPolicy = apiMgtDAO.getSubscriptionPolicy(subscriptionPolicy.getPolicyName(), -1234);
SubscriptionPolicy retrievedPolicyFromUUID = apiMgtDAO.getSubscriptionPolicyByUUID(retrievedPolicy.getUUID());
assertEquals(retrievedPolicy.getDescription(), retrievedPolicyFromUUID.getDescription());
assertEquals(retrievedPolicy.getDisplayName(), retrievedPolicyFromUUID.getDisplayName());
assertEquals(retrievedPolicy.getRateLimitCount(), retrievedPolicyFromUUID.getRateLimitCount());
assertEquals(retrievedPolicy.getRateLimitTimeUnit(), retrievedPolicyFromUUID.getRateLimitTimeUnit());
retrievedPolicyFromUUID.setCustomAttributes(customAttributes.getBytes());
apiMgtDAO.updateSubscriptionPolicy(retrievedPolicyFromUUID);
retrievedPolicyFromUUID.setPolicyName(null);
apiMgtDAO.updateSubscriptionPolicy(retrievedPolicyFromUUID);
SubscriptionPolicy[] subscriptionPolicies = apiMgtDAO.getSubscriptionPolicies(-1234);
apiMgtDAO.updateThrottleTierPermissions(subscriptionPolicy.getPolicyName(), "allow", "internal/everyone", -1234);
Set<TierPermissionDTO> throttleTierPermissions = apiMgtDAO.getThrottleTierPermissions(-1234);
for (TierPermissionDTO tierPermissionDTO : throttleTierPermissions) {
if (subscriptionPolicy.getPolicyName().equals(tierPermissionDTO.getTierName())) {
assertTrue(true);
break;
}
}
apiMgtDAO.updateThrottleTierPermissions(subscriptionPolicy.getPolicyName(), "deny", "internal/everyone", -1234);
assertNotNull(apiMgtDAO.getThrottleTierPermission(subscriptionPolicy.getPolicyName(), -1234));
assertTrue(subscriptionPolicies.length > 0);
apiMgtDAO.setPolicyDeploymentStatus(PolicyConstants.POLICY_LEVEL_SUB, subscriptionPolicy.getPolicyName(), -1234, true);
assertTrue(apiMgtDAO.getPolicyNames(PolicyConstants.POLICY_LEVEL_SUB, "admin").length > 0);
assertTrue(apiMgtDAO.isPolicyDeployed(PolicyConstants.POLICY_LEVEL_SUB, -1234, subscriptionPolicy.getPolicyName()));
assertTrue(apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_SUB, -1234, subscriptionPolicy.getPolicyName()));
apiMgtDAO.removeThrottlePolicy(PolicyConstants.POLICY_LEVEL_SUB, "testAddAndGetSubscriptionPolicy", -1234);
}
use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class SubscriptionPoliciesApiServiceImpl method subscriptionPoliciesGet.
@Override
public Response subscriptionPoliciesGet(String xWSO2Tenant, String policyName, MessageContext messageContext) {
SubscriptionValidationDAO subscriptionValidationDAO = new SubscriptionValidationDAO();
xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
if (StringUtils.isNotEmpty(xWSO2Tenant)) {
if (APIConstants.CHAR_ASTERIX.equals(xWSO2Tenant)) {
return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(subscriptionValidationDAO.getAllSubscriptionPolicies())).build();
} else if (StringUtils.isNotEmpty(policyName)) {
List<SubscriptionPolicy> model = new ArrayList<>();
SubscriptionPolicy subscriptionPolicy = subscriptionValidationDAO.getSubscriptionPolicyByNameForTenant(policyName, xWSO2Tenant);
if (subscriptionPolicy != null) {
model.add(subscriptionPolicy);
}
return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(model)).build();
} else {
return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(subscriptionValidationDAO.getAllSubscriptionPolicies(xWSO2Tenant))).build();
}
} else {
if (StringUtils.isNotEmpty(policyName)) {
return Response.status(Response.Status.BAD_REQUEST.getStatusCode(), "X-WSo2-Tenant header is missing.").build();
}
}
return Response.ok().entity(SubscriptionValidationDataUtil.fromSubscriptionPolicyToSubscriptionPolicyListDTO(subscriptionValidationDAO.getAllSubscriptionPolicies())).build();
}
use of org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class TestUtil method getPolicySubLevel.
/**
* Generate a dummy Subscription Policy object
*
* @return an Subscription Policy
*/
public static SubscriptionPolicy getPolicySubLevel() {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy();
subscriptionPolicy.setId(1);
subscriptionPolicy.setTenantId(-1234);
subscriptionPolicy.setTenantDomain("carbon.super");
subscriptionPolicy.setName("policy1");
subscriptionPolicy.setQuotaType("requestCount");
QuotaPolicy defaultLimit = new QuotaPolicy();
defaultLimit.setQuotaType("requestCount");
RequestCountLimit requestCountLimit = new RequestCountLimit();
requestCountLimit.setRequestCount(1);
requestCountLimit.setTimeUnit("min");
requestCountLimit.setUnitTime(1);
defaultLimit.setRequestCount(requestCountLimit);
subscriptionPolicy.setDefaultLimit(defaultLimit);
subscriptionPolicy.setGraphQLMaxComplexity(0);
subscriptionPolicy.setGraphQLMaxDepth(0);
subscriptionPolicy.setRateLimitCount(0);
subscriptionPolicy.setRateLimitTimeUnit(null);
subscriptionPolicy.setStopOnQuotaReach(true);
subscriptionPolicy.setSubscriberCount(0);
return subscriptionPolicy;
}
Aggregations