Search in sources :

Example 1 with SubscriptionEntity

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;
}
Also used : SubscriptionEntity(io.gravitee.management.model.SubscriptionEntity)

Example 2 with 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");
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity) SubscriptionEntity(io.gravitee.management.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.management.model.subscription.SubscriptionQuery) Date(java.util.Date) Test(org.junit.Test)

Example 3 with SubscriptionEntity

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());
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity) SubscriptionEntity(io.gravitee.management.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.management.model.subscription.SubscriptionQuery) Date(java.util.Date)

Example 4 with SubscriptionEntity

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());
}
Also used : SubscriptionQuery(io.gravitee.management.model.subscription.SubscriptionQuery) Permission(io.gravitee.management.rest.security.Permission) PathParam(javax.ws.rs.PathParam) Context(javax.ws.rs.core.Context) SubscriptionService(io.gravitee.management.service.SubscriptionService) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Collection(java.util.Collection) RolePermission(io.gravitee.management.model.permissions.RolePermission) RolePermissionAction(io.gravitee.management.model.permissions.RolePermissionAction) ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Permissions(io.gravitee.management.rest.security.Permissions) ApiResponses(io.swagger.annotations.ApiResponses) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ApplicationService(io.gravitee.management.service.ApplicationService) Inject(javax.inject.Inject) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(io.gravitee.common.http.MediaType) ApiResponse(io.swagger.annotations.ApiResponse) ResourceContext(javax.ws.rs.container.ResourceContext) SubscriptionEntity(io.gravitee.management.model.SubscriptionEntity) Api(io.swagger.annotations.Api) SubscriptionEntity(io.gravitee.management.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.management.model.subscription.SubscriptionQuery) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) Permissions(io.gravitee.management.rest.security.Permissions) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

SubscriptionEntity (io.gravitee.management.model.SubscriptionEntity)4 SubscriptionQuery (io.gravitee.management.model.subscription.SubscriptionQuery)3 ApiEntity (io.gravitee.management.model.ApiEntity)2 Date (java.util.Date)2 MediaType (io.gravitee.common.http.MediaType)1 ApplicationEntity (io.gravitee.management.model.ApplicationEntity)1 RolePermission (io.gravitee.management.model.permissions.RolePermission)1 RolePermissionAction (io.gravitee.management.model.permissions.RolePermissionAction)1 Permission (io.gravitee.management.rest.security.Permission)1 Permissions (io.gravitee.management.rest.security.Permissions)1 ApplicationService (io.gravitee.management.service.ApplicationService)1 SubscriptionService (io.gravitee.management.service.SubscriptionService)1 Api (io.swagger.annotations.Api)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponse (io.swagger.annotations.ApiResponse)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Collection (java.util.Collection)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1