use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.
the class RestApiUtilTestCase method testFindPolicy.
@Test(description = "Test Find Policy given tier name")
public void testFindPolicy() throws Exception {
Policy mockedPolicy = Mockito.mock(Policy.class);
when(mockedPolicy.getPolicyName()).thenReturn("silver");
List<Policy> policyList = new ArrayList<>();
Policy silverPolicy = Mockito.mock(Policy.class);
silverPolicy.setPolicyName("silver");
when(silverPolicy.getPolicyName()).thenReturn("silver");
policyList.add(silverPolicy);
Policy policyFound1 = RestApiUtil.findPolicy(policyList, "silver");
Assert.assertEquals(policyFound1, silverPolicy);
Policy policyFound2 = RestApiUtil.findPolicy(policyList, null);
Assert.assertEquals(policyFound2, null);
Policy policyFound3 = RestApiUtil.findPolicy(policyList, "testTier");
Assert.assertEquals(policyFound3, null);
}
use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesTierLevelGet.
/**
* Retrieve a list of tiers for a particular tier level
*
* @param tierLevel Tier level
* @param limit maximum number of tiers to return
* @param offset starting position of the pagination
* @param ifNoneMatch If-Non-Match header value
* @param request msf4j request object
* @return A list of qualifying tiers
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response policiesTierLevelGet(String tierLevel, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
TierListDTO tierListDTO = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
List<Policy> tierList = apiStore.getPolicies(RestApiUtil.mapRestApiPolicyLevelToPolicyLevelEnum(tierLevel));
tierListDTO = TierMappingUtil.fromTierListToDTO(tierList, tierLevel, limit, offset);
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving tiers";
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.TIER_LEVEL, tierLevel);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().entity(tierListDTO).build();
}
use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.
the class TierMappingUtil method fromTierToDTO.
/**
* Converts a Tier object into TierDTO
*
* @param tier Tier object
* @param tierLevel tier level (api/application or resource)
* @return TierDTO corresponds to Tier object
*/
public static TierDTO fromTierToDTO(Policy tier, String tierLevel) {
TierDTO dto = new TierDTO();
dto.setName(tier.getPolicyName());
dto.setDescription(tier.getDescription());
dto.setTierLevel(TierDTO.TierLevelEnum.valueOf(StringUtils.upperCase(tierLevel)));
dto.setUnitTime((long) tier.getDefaultQuotaPolicy().getLimit().getUnitTime());
Limit limit = tier.getDefaultQuotaPolicy().getLimit();
if (limit instanceof RequestCountLimit) {
dto.setRequestCount((long) ((RequestCountLimit) limit).getRequestCount());
} else if (limit instanceof BandwidthLimit) {
dto.setRequestCount((long) ((BandwidthLimit) limit).getDataAmount());
}
// // TODO: 08/12/16 More fields to map
return dto;
}
use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testUpdateAPIStatusDeprecatePreviousVersionsAndNotRequireReSubscription.
@Test(description = "Update api status with deprecating previous versions and not require re-subscriptions")
public void testUpdateAPIStatusDeprecatePreviousVersionsAndNotRequireReSubscription() throws APIManagementException, LifecycleException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, applicationDAO, apiSubscriptionDAO, apiLifecycleManager, gatewaySourceGenerator, workflowDAO, gateway);
API previousApi = SampleTestObjectCreator.createDefaultAPI().build();
String previousApiUUID = previousApi.getId();
String lifecycleIdPrevious = previousApi.getLifecycleInstanceId();
String lcStatePrevious = previousApi.getLifeCycleStatus();
LifecycleState previousLifecycleState = SampleTestObjectCreator.getMockLifecycleStateObject(lifecycleIdPrevious);
List<AvailableTransitionBean> list = new ArrayList<>();
AvailableTransitionBean bean = new AvailableTransitionBean("Deprecate", APIStatus.DEPRECATED.getStatus());
list.add(bean);
previousLifecycleState.setAvailableTransitionBeanList(list);
Mockito.when(apiLifecycleManager.getLifecycleDataForState(lifecycleIdPrevious, lcStatePrevious)).thenReturn(previousLifecycleState);
Mockito.when(apiLifecycleManager.executeLifecycleEvent(APIStatus.PUBLISHED.getStatus(), APIStatus.DEPRECATED.getStatus(), lifecycleIdPrevious, USER, previousApi)).thenReturn(previousLifecycleState);
previousLifecycleState.setState(APIStatus.DEPRECATED.getStatus());
API api = SampleTestObjectCreator.createDefaultAPI().copiedFromApiId(previousApiUUID).build();
String uuid = api.getId();
String lcState = api.getLifeCycleStatus();
String lifecycleId = api.getLifecycleInstanceId();
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
LifecycleState lifecycleState = SampleTestObjectCreator.getMockLifecycleStateObject(lifecycleId);
Mockito.when(apiLifecycleManager.getLifecycleDataForState(lifecycleId, lcState)).thenReturn(lifecycleState);
Mockito.when(apiLifecycleManager.executeLifecycleEvent(APIStatus.CREATED.getStatus(), APIStatus.PUBLISHED.getStatus(), lifecycleId, USER, api)).thenReturn(lifecycleState);
lifecycleState.setState(APIStatus.PUBLISHED.getStatus());
API.APIBuilder apiBuilder = new API.APIBuilder(api);
apiBuilder.lifecycleState(lifecycleState);
apiBuilder.updatedBy(USER);
api = apiBuilder.build();
Mockito.when(apiDAO.getAPI(previousApiUUID)).thenReturn(previousApi);
Map<String, Boolean> checklist = new HashMap<>();
checklist.put(APIMgtConstants.DEPRECATE_PREVIOUS_VERSIONS, true);
Application application = SampleTestObjectCreator.createDefaultApplication();
List<Subscription> subscriptions = new ArrayList<>();
Subscription subscription = new Subscription(previousApiUUID, application, previousApi, new SubscriptionPolicy(TIER));
subscriptions.add(subscription);
Mockito.when(apiSubscriptionDAO.getAPISubscriptionsByAPI(previousApiUUID)).thenReturn(subscriptions);
apiPublisher.updateAPIStatus(uuid, APIStatus.PUBLISHED.getStatus(), checklist);
Mockito.verify(apiLifecycleManager, Mockito.times(1)).executeLifecycleEvent(APIStatus.CREATED.getStatus(), APIStatus.PUBLISHED.getStatus(), lifecycleId, USER, api);
}
use of org.wso2.carbon.apimgt.api.model.Tier in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testAddSubscriptionForInvalidApplication.
@Test(description = "Add subscription without a valid app", expectedExceptions = APIManagementException.class)
public void testAddSubscriptionForInvalidApplication() throws APIManagementException {
ApplicationDAO applicationDAO = Mockito.mock(ApplicationDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
WorkflowDAO workflowDAO = Mockito.mock(WorkflowDAO.class);
APIStore apiStore = getApiStoreImpl(apiDAO, applicationDAO, apiSubscriptionDAO, workflowDAO, apiGateway);
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
API api = apiBuilder.build();
String apiId = api.getId();
/* Application application = new Application("TestApp", USER_ID);
application.setId(UUID);*/
Mockito.when(apiDAO.getAPI(apiId)).thenReturn(api);
Mockito.when(applicationDAO.getApplication(UUID)).thenReturn(null);
SubscriptionResponse subscriptionResponse = apiStore.addApiSubscription(apiId, UUID, TIER);
String subscriptionId = subscriptionResponse.getSubscriptionUUID();
Assert.assertNotNull(subscriptionId);
// subscription should not be added
Mockito.verify(apiSubscriptionDAO, Mockito.times(0)).addAPISubscription(subscriptionId, apiId, UUID, TIER, APIMgtConstants.SubscriptionStatus.ON_HOLD);
}
Aggregations