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