Search in sources :

Example 66 with SubscriptionPolicy

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());
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) ThrottleProperties(org.wso2.carbon.apimgt.impl.dto.ThrottleProperties) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 67 with SubscriptionPolicy

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);
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 68 with SubscriptionPolicy

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);
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 69 with SubscriptionPolicy

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();
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy) List(java.util.List) ArrayList(java.util.ArrayList) SubscriptionValidationDAO(org.wso2.carbon.apimgt.impl.dao.SubscriptionValidationDAO)

Example 70 with SubscriptionPolicy

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;
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy) QuotaPolicy(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.QuotaPolicy)

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