use of org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApi.
@Test(description = "Test add api with production endpoint")
public void testAddApi() throws APIManagementException, LifecycleException {
/**
* this test method verify the API Add with correct API object get invoked correctly
*/
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(SampleTestObjectCreator.getMockEndpointMap());
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
APIGateway gateway = Mockito.mock(APIGateway.class);
PolicyDAO policyDAO = Mockito.mock(PolicyDAO.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);
String endpointId = apiBuilder.getEndpoint().get("production").getId();
Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
apiPublisher.addAPI(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).addAPI(apiBuilder.build());
Mockito.verify(apiLifecycleManager, Mockito.times(1)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
// Error path
// When an APIMgtDAOException is being thrown when the API is created
Mockito.doThrow(APIMgtDAOException.class).when(apiDAO).addAPI(apiBuilder.build());
try {
apiPublisher.addAPI(apiBuilder);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while creating the API - " + apiBuilder.getName());
}
// Error path
// When an GatewayException is being thrown when an error occurred while adding API to the Gateway
Mockito.doThrow(GatewayException.class).when(gateway).addAPI(apiBuilder.build());
try {
apiPublisher.addAPI(apiBuilder);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while adding API - " + apiBuilder.getName() + " to gateway");
}
// Error path
// When an APITemplateException is being thrown when generating API configuration for API
Mockito.when(gatewaySourceGenerator.getConfigStringFromTemplate(Mockito.any())).thenThrow(APITemplateException.class);
try {
apiPublisher.addAPI(apiBuilder);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error generating API configuration for API " + apiBuilder.getName());
}
}
use of org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiSandboxEndpoint.
@Test(description = "Test add api with sandbox endpoint")
public void testAddApiSandboxEndpoint() throws APIManagementException, LifecycleException {
Map<String, Endpoint> endpointMap = new HashMap<>();
Map<String, Endpoint> resourceEndpointMap = new HashMap<>();
Map<String, UriTemplate> uriTemplateMap = new HashMap();
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
uriTemplateBuilder.endpoint(resourceEndpointMap);
uriTemplateBuilder.templateId("getApisApiIdGet");
uriTemplateBuilder.uriTemplate("/apis/");
uriTemplateBuilder.authType(APIMgtConstants.AUTH_APPLICATION_LEVEL_TOKEN);
uriTemplateBuilder.policy(APIUtils.getDefaultAPIPolicy());
uriTemplateBuilder.httpVerb("GET");
uriTemplateMap.put("getApisApiIdGet", uriTemplateBuilder.build());
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI().id("").endpoint(endpointMap).uriTemplates(uriTemplateMap);
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
String endpointId = String.valueOf(apiBuilder.getEndpoint().get("sandbox"));
Endpoint endpoint = new Endpoint.Builder().id(endpointId).name("testEndpoint").build();
Mockito.when(apiDAO.getEndpoint(endpointId)).thenReturn(endpoint);
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);
}
use of org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionPolicyArrayToListDTO.
/**
* Converts an array of Subscription Policy objects into a List DTO
*
* @param subscriptionPolicies Array of Subscription Policies
* @return A List DTO of converted Subscription Policies
* @throws SubscriptionThrottlePolicyException - If error occurs
*/
public static SubscriptionThrottlePolicyListDTO fromSubscriptionPolicyArrayToListDTO(List<SubscriptionPolicy> subscriptionPolicies) throws SubscriptionThrottlePolicyException {
SubscriptionThrottlePolicyListDTO listDTO = new SubscriptionThrottlePolicyListDTO();
List<SubscriptionThrottlePolicyDTO> subscriptionPolicyDTOList = new ArrayList<>();
if (subscriptionPolicies != null) {
for (SubscriptionPolicy policy : subscriptionPolicies) {
SubscriptionThrottlePolicyDTO dto = fromSubscriptionThrottlePolicyToDTO(policy);
subscriptionPolicyDTOList.add(dto);
}
}
listDTO.setCount(subscriptionPolicyDTOList.size());
listDTO.setList(subscriptionPolicyDTOList);
return listDTO;
}
use of org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy in project carbon-apimgt by wso2.
the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionThrottlePolicyToDTO.
/**
* Converts a single Subscription Policy model into REST API DTO
*
* @param policy Subscription Policy model object
* @return Converted Subscription policy REST API DTO object
* @throws SubscriptionThrottlePolicyException - If error occurs
*/
public static SubscriptionThrottlePolicyDTO fromSubscriptionThrottlePolicyToDTO(SubscriptionPolicy policy) throws SubscriptionThrottlePolicyException {
try {
SubscriptionThrottlePolicyDTO policyDTO = new SubscriptionThrottlePolicyDTO();
policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(policy, policyDTO);
SubscriptionPolicy subscriptionPolicy = policy;
policyDTO.setBillingPlan(subscriptionPolicy.getBillingPlan());
policyDTO.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
policyDTO.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
policyDTO.setStopOnQuotaReach(subscriptionPolicy.isStopOnQuotaReach());
byte[] customAttributes = subscriptionPolicy.getCustomAttributes();
if (customAttributes != null && customAttributes.length > 0) {
List<CustomAttributeDTO> customAttributeDTOs = new ArrayList<>();
JSONParser parser = new JSONParser();
JSONArray attributeArray = (JSONArray) parser.parse(new String(customAttributes, StandardCharsets.UTF_8));
for (Object attributeObj : attributeArray) {
JSONObject attribute = (JSONObject) attributeObj;
CustomAttributeDTO customAttributeDTO = CommonThrottleMappingUtil.getCustomAttribute(attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_NAME).toString(), attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_VALUE).toString());
customAttributeDTOs.add(customAttributeDTO);
}
policyDTO.setCustomAttributes(customAttributeDTOs);
}
if (policy.getDefaultQuotaPolicy() != null) {
policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(policy.getDefaultQuotaPolicy()));
}
return policyDTO;
} catch (ParseException | UnsupportedThrottleLimitTypeException e) {
throw new SubscriptionThrottlePolicyException(e.getMessage(), e);
}
}
use of org.wso2.carbon.apimgt.core.models.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("bandwidth");
BandwidthLimit bandwidthLimit = new BandwidthLimit("s", 60, 10, "mb");
quotaPolicy.setLimit(bandwidthLimit);
subscriptionPolicy.setDefaultQuotaPolicy(quotaPolicy);
return subscriptionPolicy;
}
Aggregations