Search in sources :

Example 86 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImplTestCase method testGetSubscriptionPolicy.

@Test(description = "Test getting Subscription policy")
public void testGetSubscriptionPolicy() throws APIManagementException {
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    APIMgtAdminServiceImpl adminService = getAPIMgtAdminServiceImpl(policyDAO);
    SubscriptionPolicy subscriptionPolicy = SampleTestObjectCreator.createDefaultSubscriptionPolicy();
    Mockito.when(policyDAO.getSubscriptionPolicy(subscriptionPolicy.getPolicyName())).thenReturn(subscriptionPolicy);
    adminService.getSubscriptionPolicy(subscriptionPolicy.getPolicyName());
    Mockito.verify(policyDAO, Mockito.times(1)).getSubscriptionPolicy(subscriptionPolicy.getPolicyName());
    // Error path
    Mockito.when(policyDAO.getSubscriptionPolicy(subscriptionPolicy.getPolicyName())).thenThrow(APIMgtDAOException.class);
    try {
        adminService.getSubscriptionPolicy(subscriptionPolicy.getPolicyName());
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Couldn't retrieve Subscription policy with name: " + subscriptionPolicy.getPolicyName());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Example 87 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.

the class PolicyExportManager method createArchiveFromExecutionPlans.

public String createArchiveFromExecutionPlans(String exportedPoliciesDirName, String archiveDir, String archiveName) throws APIManagementException {
    try {
        // retrieve all policies under each policy level
        List<APIPolicy> apiPolicies = apiMgtAdminService.getApiPolicies();
        List<ApplicationPolicy> applicationPolicies = apiMgtAdminService.getApplicationPolicies();
        List<SubscriptionPolicy> subscriptionPolicies = apiMgtAdminService.getSubscriptionPolicies();
        List<CustomPolicy> customPolicies = apiMgtAdminService.getCustomRules();
        // write all execution Plans/Siddhi Apps to exportPoliciesDirName directory
        String dirLocation = exportedPoliciesDirName + File.separator + EXPORT_POLICIES;
        APIFileUtils.createDirectory(dirLocation);
        if (!apiPolicies.isEmpty()) {
            for (Map<String, String> map : getApiPolicySiddhiApps(apiPolicies)) {
                prepareFile(dirLocation, map);
            }
        }
        if (!applicationPolicies.isEmpty()) {
            prepareFile(dirLocation, getAppPolicySiddhiApps(applicationPolicies));
        }
        if (!subscriptionPolicies.isEmpty()) {
            prepareFile(dirLocation, getSubscriptionPolicySiddhiApps(subscriptionPolicies));
        }
        if (!customPolicies.isEmpty()) {
            prepareFile(dirLocation, getCustomPolicySiddhiApps(customPolicies));
        }
        // create archive and get the archive location
        String zippedFilePath = createArchiveFromPolicies(exportedPoliciesDirName, archiveDir, archiveName);
        APIFileUtils.deleteDirectory(exportedPoliciesDirName);
        return zippedFilePath;
    } catch (APIManagementException e) {
        String errorMessage = "Error while exporting policies";
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e);
    }
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 88 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createSubscriptionPolicyWithBndwidthLimit.

public static SubscriptionPolicy createSubscriptionPolicyWithBndwidthLimit(String name) {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(name);
    subscriptionPolicy.setDescription("testDescription");
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType("requestCount");
    BandwidthLimit bandwidthLimit = new BandwidthLimit("s", 60, 10, "mb");
    quotaPolicy.setLimit(bandwidthLimit);
    subscriptionPolicy.setDefaultQuotaPolicy(quotaPolicy);
    return subscriptionPolicy;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 89 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultAPIWithApiLevelEndpoint.

public static API.APIBuilder createDefaultAPIWithApiLevelEndpoint() {
    Set<String> transport = new HashSet<>();
    transport.add(HTTP);
    transport.add(HTTPS);
    Set<String> tags = new HashSet<>();
    tags.add(TAG_CLIMATE);
    Set<Policy> policies = new HashSet<>();
    policies.add(new SubscriptionPolicy(GOLD_TIER));
    policies.add(new SubscriptionPolicy(SILVER_TIER));
    policies.add(new SubscriptionPolicy(BRONZE_TIER));
    BusinessInformation businessInformation = new BusinessInformation();
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    String permissionJson = "[{\"groupId\" : \"developer\", \"permission\" : " + "[\"READ\",\"UPDATE\"]},{\"groupId\" : \"admin\", \"permission\" : [\"READ\",\"UPDATE\"," + "\"DELETE\", \"MANAGE_SUBSCRIPTION\"]}]";
    Map<String, Endpoint> endpointMap = new HashMap<>();
    endpointMap.put(APIMgtConstants.PRODUCTION_ENDPOINT, new Endpoint.Builder().id(endpointId).name("api1-production--endpint").applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).build());
    API.APIBuilder apiBuilder = new API.APIBuilder(ADMIN, "WeatherAPI", API_VERSION).id(UUID.randomUUID().toString()).context("weather").description("Get Weather Info").lifeCycleStatus(APIStatus.CREATED.getStatus()).lifecycleInstanceId(UUID.randomUUID().toString()).endpoint(endpointMap).wsdlUri("http://localhost:9443/echo?wsdl").isResponseCachingEnabled(false).cacheTimeout(60).isDefaultVersion(false).apiPolicy(APIUtils.getDefaultAPIPolicy()).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.PUBLIC).visibleRoles(new HashSet<>()).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy(ADMIN).updatedBy(ADMIN).lastUpdatedTime(LocalDateTime.now()).apiPermission(permissionJson).uriTemplates(getMockUriTemplates()).apiDefinition(apiDefinition);
    Map map = new HashMap();
    map.put(DEVELOPER_ROLE_ID, 6);
    map.put(ADMIN_ROLE_ID, 15);
    apiBuilder.permissionMap(map);
    return apiBuilder;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) HashMap(java.util.HashMap) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) Map(java.util.Map) HashedMap(org.apache.commons.collections.map.HashedMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 90 with SubscriptionPolicy

use of org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testAddApiWithEmptyTemplateIdAndApiDefinition.

@Test(description = "Test add api with empty templateId and api definition")
public void testAddApiWithEmptyTemplateIdAndApiDefinition() throws APIManagementException, LifecycleException {
    Map<String, UriTemplate> uriTemplateMap = new HashMap();
    UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
    uriTemplateBuilder.endpoint(Collections.emptyMap());
    uriTemplateBuilder.templateId("");
    uriTemplateBuilder.uriTemplate("/apis/");
    uriTemplateBuilder.authType(APIMgtConstants.AUTH_APPLICATION_LEVEL_TOKEN);
    uriTemplateBuilder.policy(APIUtils.getDefaultAPIPolicy());
    uriTemplateBuilder.httpVerb("GET");
    uriTemplateMap.put("", uriTemplateBuilder.build());
    API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().uriTemplates(uriTemplateMap).apiDefinition("");
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
    Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    PolicyDAO policyDAO = Mockito.mock(PolicyDAO.class);
    LabelDAO labelDao = Mockito.mock(LabelDAO.class);
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.api, APIMgtConstants.DEFAULT_API_POLICY)).thenReturn(new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, GOLD_TIER)).thenReturn(new SubscriptionPolicy(GOLD_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, SILVER_TIER)).thenReturn(new SubscriptionPolicy(SILVER_TIER));
    Mockito.when(policyDAO.getSimplifiedPolicyByLevelAndName(APIMgtAdminService.PolicyLevel.subscription, BRONZE_TIER)).thenReturn(new SubscriptionPolicy(BRONZE_TIER));
    APIPublisherImpl apiPublisher = getApiPublisherImpl(null, apiDAO, null, null, policyDAO, apiLifecycleManager, labelDao, null, null, null, gatewaySourceGenerator, gateway);
    apiPublisher.addAPI(apiBuilder);
    Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
    Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Also used : HashMap(java.util.HashMap) APIBuilder(org.wso2.carbon.apimgt.core.models.API.APIBuilder) LifecycleState(org.wso2.carbon.lcm.core.impl.LifecycleState) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) APILifecycleManager(org.wso2.carbon.apimgt.core.api.APILifecycleManager) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) API(org.wso2.carbon.apimgt.core.models.API) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

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