use of org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionMappingUtilTestCase method testFromSubscriptionListToDTO.
@Test
public void testFromSubscriptionListToDTO() {
String subUuid1 = UUID.randomUUID().toString();
String subUuid2 = UUID.randomUUID().toString();
Subscription subscription1 = SampleTestObjectCreator.createSubscription(subUuid1);
Subscription subscription2 = SampleTestObjectCreator.createSubscription(subUuid2);
List<Subscription> subscriptionList = new ArrayList<>();
subscriptionList.add(subscription1);
subscriptionList.add(subscription2);
SubscriptionListDTO subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscriptionList, 10, 0);
assertEquals(subscriptionListDTO.getCount(), (Integer) subscriptionList.size());
assertEquals(subscription1.getId(), subscriptionListDTO.getList().get(0).getSubscriptionId());
assertEquals(subscription1.getApi().getName(), subscriptionListDTO.getList().get(0).getApiName());
assertEquals(subscription1.getApi().getId(), subscriptionListDTO.getList().get(0).getApiIdentifier());
assertEquals(subscription1.getApi().getVersion(), subscriptionListDTO.getList().get(0).getApiVersion());
assertEquals(SubscriptionDTO.LifeCycleStatusEnum.valueOf(subscription1.getStatus().toString()).name(), subscriptionListDTO.getList().get(0).getLifeCycleStatus().name());
assertEquals(subscriptionListDTO.getCount(), (Integer) subscriptionList.size());
assertEquals(subscription2.getId(), subscriptionListDTO.getList().get(1).getSubscriptionId());
assertEquals(subscription2.getApi().getName(), subscriptionListDTO.getList().get(1).getApiName());
assertEquals(subscription2.getApi().getId(), subscriptionListDTO.getList().get(1).getApiIdentifier());
assertEquals(subscription2.getApi().getVersion(), subscriptionListDTO.getList().get(1).getApiVersion());
assertEquals(SubscriptionDTO.LifeCycleStatusEnum.valueOf(subscription2.getStatus().toString()).name(), subscriptionListDTO.getList().get(1).getLifeCycleStatus().name());
}
use of org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
/**
* Retrieve subscriptions
*
* @param apiContext Context of the API
* @param apiVersion API version
* @param limit Limit value
* @return Subscriptions of the API
* @throws NotFoundException If failed to retrieve subscriptions
*/
@Override
public Response subscriptionsGet(String apiContext, String apiVersion, Integer limit, String accept, Request request) throws NotFoundException {
try {
APIMgtAdminService apiMgtAdminService = APIManagerFactory.getInstance().getAPIMgtAdminService();
List<SubscriptionValidationData> subscriptionsOfApi;
if (StringUtils.isEmpty(apiContext) || StringUtils.isEmpty(apiVersion)) {
APIUtils.logDebug("API Context or version is null or empty. Retrieving subscriptions of all APIs", log);
subscriptionsOfApi = apiMgtAdminService.getAPISubscriptions(limit);
} else {
subscriptionsOfApi = apiMgtAdminService.getAPISubscriptionsOfApi(apiContext, apiVersion);
}
SubscriptionListDTO subscriptionsList = new SubscriptionListDTO();
subscriptionsList.setList(MappingUtil.convertToSubscriptionListDto(subscriptionsOfApi));
return Response.ok(subscriptionsList).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving subscriptions.";
HashMap<String, String> paramList = new HashMap<String, String>();
if (!StringUtils.isEmpty(apiContext)) {
paramList.put(APIMgtConstants.ExceptionsConstants.API_CONTEXT, apiContext);
}
if (!StringUtils.isEmpty(apiVersion)) {
paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, apiVersion);
}
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
/**
* Get all subscriptions.
* {@code <p/>}
* If apiId is specified this will return the subscribed applications of that api
* If application id is specified this will return the api subscriptions of that application
*
* @param apiId ID of the API
* @param applicationId ID of the Application
* @param offset offset value
* @param limit limit value
* @param ifNoneMatch If-None-Match header value
* @param request msf4j request object
* @return Subscription List
* @throws NotFoundException If failed to get the subscription
*/
@Override
public Response subscriptionsGet(String apiId, String applicationId, String apiType, Integer offset, Integer limit, String ifNoneMatch, Request request) throws NotFoundException {
List<Subscription> subscribedApiList = null;
SubscriptionListDTO subscriptionListDTO = null;
String username = RestApiUtil.getLoggedInUsername(request);
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
if (!StringUtils.isEmpty(apiId)) {
subscribedApiList = apiStore.getSubscriptionsByAPI(apiId);
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
} else if (!StringUtils.isEmpty(applicationId)) {
Application application = apiStore.getApplicationByUuid(applicationId);
if (application != null) {
if (!StringUtils.isEmpty(apiType)) {
ApiType apiTypeEnum = ApiType.fromString(apiType);
if (apiTypeEnum == null) {
throw new APIManagementException("API Type specified is invalid", ExceptionCodes.API_TYPE_INVALID);
}
subscribedApiList = apiStore.getAPISubscriptionsByApplication(application, apiTypeEnum);
} else {
subscribedApiList = apiStore.getAPISubscriptionsByApplication(application);
}
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
} else {
String errorMessage = "Application not found: " + applicationId;
APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.APPLICATION_NOT_FOUND);
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
} else {
// mandatory parameters not provided
String errorMessage = "Either applicationId or apiId should be provided";
ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving subscriptions";
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, applicationId);
paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().entity(subscriptionListDTO).build();
}
use of org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class MappingUtilTestCase method convertToSubscriptionListDTOTest.
@Test
public void convertToSubscriptionListDTOTest() {
List<SubscriptionValidationData> validationDataList = new ArrayList<>();
validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
validationDataList.add(SampleTestObjectCreator.createSubscriptionValidationData());
List<SubscriptionDTO> subscriptionListDTO = MappingUtil.convertToSubscriptionListDto(validationDataList);
Assert.assertNotNull(subscriptionListDTO);
Assert.assertEquals(validationDataList.size(), subscriptionListDTO.size());
for (int i = 0; i < subscriptionListDTO.size(); i++) {
Assert.assertEquals(subscriptionListDTO.get(i).getApiName(), validationDataList.get(i).getApiName());
Assert.assertEquals(subscriptionListDTO.get(i).getApiContext(), validationDataList.get(i).getApiContext());
Assert.assertEquals(subscriptionListDTO.get(i).getApiProvider(), validationDataList.get(i).getApiProvider());
Assert.assertEquals(subscriptionListDTO.get(i).getApiVersion(), validationDataList.get(i).getApiVersion());
}
}
use of org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method subscriptionsGetTest.
@Test
public void subscriptionsGetTest() throws Exception {
APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
PowerMockito.mockStatic(APIManagerFactory.class);
PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
Mockito.when(instance.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
Mockito.when(apiMgtAdminService.getAPISubscriptionsOfApi(API_CONTEXT, API_VERSION)).thenReturn(createSubscriptionValidationDataList());
Response response = subscriptionsApiService.subscriptionsGet(API_CONTEXT, API_VERSION, LIMIT, null, getRequest());
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
Assert.assertEquals(((SubscriptionListDTO) response.getEntity()).getList().size(), 2);
}
Aggregations