use of io.gravitee.management.model.SubscriptionEntity in project gravitee-management-rest-api by gravitee-io.
the class ScheduledSubscriptionsServiceTest method createSubscription.
private SubscriptionEntity createSubscription(String id, SubscriptionStatus status, Date endingDate) {
SubscriptionEntity subscriptionEntity = mock(SubscriptionEntity.class);
when(subscriptionEntity.getId()).thenReturn(id);
when(subscriptionEntity.getStatus()).thenReturn(status);
when(subscriptionEntity.getEndingAt()).thenReturn(endingDate);
return subscriptionEntity;
}
use of io.gravitee.management.model.SubscriptionEntity in project gravitee-management-rest-api by gravitee-io.
the class ScheduledSubscriptionsServiceTest method shouldCloseOutdatedSubscriptions.
@Test
public void shouldCloseOutdatedSubscriptions() {
ApiEntity apiEntity = mock(ApiEntity.class);
when(apiEntity.getId()).thenReturn("API_ID");
SubscriptionEntity endDateInThePast = createSubscription("end_date_in_the_past", SubscriptionStatus.ACCEPTED, new Date(0));
SubscriptionEntity noEndDate = createSubscription("no_end_date", SubscriptionStatus.ACCEPTED, null);
SubscriptionEntity endDateInTheFuture = createSubscription("end_date_in_the_future", SubscriptionStatus.ACCEPTED, new Date(Long.MAX_VALUE));
when(apiService.findAll()).thenReturn(Collections.singleton(apiEntity));
SubscriptionQuery query = new SubscriptionQuery();
query.setApi(apiEntity.getId());
query.setStatuses(Collections.singleton(SubscriptionStatus.ACCEPTED));
when(subscriptionService.search(query)).thenReturn(new HashSet<>(Arrays.asList(endDateInThePast, noEndDate, endDateInTheFuture)));
service.run();
verify(apiService, times(1)).findAll();
verify(subscriptionService, times(1)).search(query);
verify(subscriptionService, times(1)).close("end_date_in_the_past");
verify(subscriptionService, never()).close("no_end_date");
verify(subscriptionService, never()).close("end_date_in_the_future");
}
use of io.gravitee.management.model.SubscriptionEntity in project gravitee-management-rest-api by gravitee-io.
the class ScheduledSubscriptionsService method run.
@Override
public void run() {
logger.debug("Refresh subscriptions #{} started at {}", counter.incrementAndGet(), Instant.now().toString());
Date now = new Date();
Set<ApiEntity> apis = apiService.findAll();
for (ApiEntity api : apis) {
// TODO: this service must be optimized by providing a better way to search for subscription
// Something like the Event Repository API
SubscriptionQuery query = new SubscriptionQuery();
query.setApi(api.getId());
query.setStatuses(Collections.singleton(SubscriptionStatus.ACCEPTED));
Collection<SubscriptionEntity> subscriptions = subscriptionService.search(query);
subscriptions.forEach(subscription -> {
if (subscription.getEndingAt() != null && subscription.getEndingAt().before(now)) {
subscriptionService.close(subscription.getId());
}
});
}
logger.debug("Refresh subscriptions #{} ended at {}", counter.get(), Instant.now().toString());
}
use of io.gravitee.management.model.SubscriptionEntity in project gravitee-management-rest-api by gravitee-io.
the class ApiSubscribersResource method listApiSubscribers.
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List subscribers for the API", notes = "User must have the MANAGE_SUBSCRIPTIONS permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Paged result of API subscribers", response = ApplicationEntity.class, responseContainer = "List"), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.API_SUBSCRIPTION, acls = RolePermissionAction.READ) })
public Collection<ApplicationEntity> listApiSubscribers(@PathParam("api") String api) {
SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
subscriptionQuery.setApi(api);
Collection<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery);
return subscriptions.stream().map(SubscriptionEntity::getApplication).distinct().map(application -> applicationService.findById(application)).sorted((o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getName(), o2.getName())).collect(Collectors.toList());
}
Aggregations