use of io.gravitee.management.model.subscription.SubscriptionQuery 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.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class ApiSubscriptionsResource method listApiSubscriptions.
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "List subscriptions for the API", notes = "User must have the READ_SUBSCRIPTION permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Paged result of API's subscriptions", response = PagedResult.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.API_SUBSCRIPTION, acls = RolePermissionAction.READ) })
public PagedResult<SubscriptionEntity> listApiSubscriptions(@BeanParam SubscriptionParam subscriptionParam, @Valid @BeanParam Pageable pageable) {
// Transform query parameters to a subscription query
SubscriptionQuery subscriptionQuery = subscriptionParam.toQuery();
Page<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery, pageable.toPageable());
PagedResult<SubscriptionEntity> result = new PagedResult<>(subscriptions, pageable.getSize());
result.setMetadata(subscriptionService.getMetadata(subscriptions.getContent()).getMetadata());
return result;
}
use of io.gravitee.management.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class ApplicationServiceImpl method update.
@Override
public ApplicationEntity update(String applicationId, UpdateApplicationEntity updateApplicationEntity) {
try {
LOGGER.debug("Update application {}", applicationId);
if (updateApplicationEntity.getGroups() != null && !updateApplicationEntity.getGroups().isEmpty()) {
// throw a NotFoundException if the group doesn't exist
groupService.findByIds(updateApplicationEntity.getGroups());
}
Optional<Application> optApplicationToUpdate = applicationRepository.findById(applicationId);
if (!optApplicationToUpdate.isPresent()) {
throw new ApplicationNotFoundException(applicationId);
}
// If clientId is set, check for uniqueness
String clientId = updateApplicationEntity.getClientId();
if (clientId != null && !clientId.trim().isEmpty()) {
LOGGER.debug("Check that client_id is unique among all applications");
final Set<Application> applications = applicationRepository.findAll(ApplicationStatus.ACTIVE);
final Optional<Application> byClientId = applications.stream().filter(application -> clientId.equals(application.getClientId())).findAny();
if (byClientId.isPresent() && !byClientId.get().getId().equals(optApplicationToUpdate.get().getId())) {
LOGGER.error("An application already exists with the same client_id");
throw new ClientIdAlreadyExistsException(clientId);
}
}
Application application = convert(updateApplicationEntity);
application.setId(applicationId);
application.setStatus(ApplicationStatus.ACTIVE);
application.setCreatedAt(optApplicationToUpdate.get().getCreatedAt());
application.setUpdatedAt(new Date());
Application updatedApplication = applicationRepository.update(application);
// Audit
auditService.createApplicationAuditLog(updatedApplication.getId(), Collections.emptyMap(), APPLICATION_CREATED, updatedApplication.getUpdatedAt(), optApplicationToUpdate.get(), updatedApplication);
// Set correct client_id for all subscriptions
SubscriptionQuery subQuery = new SubscriptionQuery();
subQuery.setApplication(applicationId);
subQuery.setStatuses(Collections.singleton(SubscriptionStatus.ACCEPTED));
subscriptionService.search(subQuery).forEach(new Consumer<SubscriptionEntity>() {
@Override
public void accept(SubscriptionEntity subscriptionEntity) {
UpdateSubscriptionEntity updateSubscriptionEntity = new UpdateSubscriptionEntity();
updateSubscriptionEntity.setId(subscriptionEntity.getId());
updateSubscriptionEntity.setStartingAt(subscriptionEntity.getStartingAt());
updateSubscriptionEntity.setEndingAt(subscriptionEntity.getEndingAt());
subscriptionService.update(updateSubscriptionEntity, application.getClientId());
}
});
return convert(Collections.singleton(updatedApplication)).iterator().next();
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update application {}", applicationId, ex);
throw new TechnicalManagementException(String.format("An error occurs while trying to update application %s", applicationId), ex);
}
}
use of io.gravitee.management.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceImpl method findByApi.
@Override
public Collection<SubscriptionEntity> findByApi(String api) {
logger.debug("Find subscriptions by api {}", api);
SubscriptionQuery query = new SubscriptionQuery();
query.setApi(api);
return search(query);
}
use of io.gravitee.management.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionServiceImpl method findByPlan.
@Override
public Collection<SubscriptionEntity> findByPlan(String plan) {
logger.debug("Find subscriptions by plan {}", plan);
SubscriptionQuery query = new SubscriptionQuery();
query.setPlan(plan);
return search(query);
}
Aggregations