use of org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class TestMappingUtilTestCase method testSubscriptionListToSubscriptionListDTOMapping.
@Test(description = "Subscription list to Subscription DTO list mapping")
void testSubscriptionListToSubscriptionListDTOMapping() {
Policy subscriptionPolicy1 = SampleTestObjectCreator.goldSubscriptionPolicy;
API api1 = SampleTestObjectCreator.createDefaultAPI().name("newName1").build();
Application application1 = SampleTestObjectCreator.createDefaultApplication();
application1.setName("newNameApp1");
String uuid1 = UUID.randomUUID().toString();
Subscription subscription1 = new Subscription(uuid1, application1, api1, subscriptionPolicy1);
subscription1.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
Policy subscriptionPolicy2 = SampleTestObjectCreator.silverSubscriptionPolicy;
API api2 = SampleTestObjectCreator.createDefaultAPI().name("newName2").build();
Application application2 = SampleTestObjectCreator.createDefaultApplication();
application1.setName("newNameApp2");
String uuid2 = UUID.randomUUID().toString();
Subscription subscription2 = new Subscription(uuid2, application2, api2, subscriptionPolicy2);
subscription2.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
List<Subscription> subscriptions = new ArrayList<>();
subscriptions.add(subscription1);
subscriptions.add(subscription2);
SubscriptionListDTO subscriptionListDTO = MappingUtil.fromSubscriptionListToDTO(subscriptions, 10, 0);
assertEquals((Integer) subscriptions.size(), subscriptionListDTO.getCount());
assertEquals(subscription1.getId(), subscriptionListDTO.getList().get(0).getSubscriptionId());
assertEquals(subscription1.getStatus().name(), subscriptionListDTO.getList().get(0).getSubscriptionStatus().name());
assertEquals(subscription1.getApplication().getId(), subscriptionListDTO.getList().get(0).getApplicationInfo().getApplicationId());
assertEquals(subscription1.getPolicy().getPolicyName(), subscriptionListDTO.getList().get(0).getPolicy());
assertEquals(subscription2.getId(), subscriptionListDTO.getList().get(1).getSubscriptionId());
assertEquals(subscription2.getStatus().name(), subscriptionListDTO.getList().get(1).getSubscriptionStatus().name());
assertEquals(subscription2.getApplication().getId(), subscriptionListDTO.getList().get(1).getApplicationInfo().getApplicationId());
assertEquals(subscription2.getPolicy().getPolicyName(), subscriptionListDTO.getList().get(1).getPolicy());
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
/**
* Retrieve all subscriptions for a particular API
*
* @param apiId ID of the API
* @param limit Maximum subscriptions to return
* @param offset Starting position of the pagination
* @param ifNoneMatch If-Match header value
* @param request ms4j request object
* @return List of qualifying subscriptions DTOs as the response
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response subscriptionsGet(String apiId, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
List<Subscription> subscriptionList;
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
if (StringUtils.isNotEmpty(apiId)) {
subscriptionList = apiPublisher.getSubscriptionsByAPI(apiId);
SubscriptionListDTO subscriptionListDTO = MappingUtil.fromSubscriptionListToDTO(subscriptionList, limit, offset);
return Response.ok().entity(subscriptionListDTO).build();
} else {
RestApiUtil.handleBadRequest("API ID can not be null", log);
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving subscriptions of API " + apiId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method subscriptionsApiVersionEmptyGetTest.
@Test
public void subscriptionsApiVersionEmptyGetTest() throws Exception {
Response response = getResponse(API_CONTEXT, null);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
Assert.assertEquals(((SubscriptionListDTO) response.getEntity()).getList().size(), 2);
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method subscriptionsApiContextEmptyGetTest.
@Test
public void subscriptionsApiContextEmptyGetTest() throws Exception {
Response response = getResponse(null, API_VERSION);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
Assert.assertEquals(((SubscriptionListDTO) response.getEntity()).getList().size(), 2);
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO in project carbon-apimgt by wso2.
the class SubscriptionMappingUtil method fromSubscriptionListToDTO.
/**
* Converts a List object of SubscribedAPIs into a DTO
*
* @param subscriptions a list of SubscribedAPI objects
* @param limit max number of objects returned
* @param offset starting index
* @return SubscriptionListDTO object containing SubscriptionDTOs
*/
public static SubscriptionListDTO fromSubscriptionListToDTO(List<Subscription> subscriptions, Integer limit, Integer offset) {
SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
List<SubscriptionDTO> subscriptionDTOs = subscriptionListDTO.getList();
if (subscriptionDTOs == null) {
subscriptionDTOs = new ArrayList<>();
subscriptionListDTO.setList(subscriptionDTOs);
}
// identifying the proper start and end indexes
int size = subscriptions.size();
int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
for (int i = start; i <= end; i++) {
Subscription subscription = subscriptions.get(i);
subscriptionDTOs.add(fromSubscriptionToDTO(subscription));
}
subscriptionListDTO.setCount(subscriptionDTOs.size());
return subscriptionListDTO;
}
Aggregations