use of io.gravitee.rest.api.portal.rest.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionMapper method convert.
public Subscription convert(SubscriptionEntity subscriptionEntity) {
final Subscription subscriptionItem = new Subscription();
subscriptionItem.setId(subscriptionEntity.getId());
subscriptionItem.setApi(subscriptionEntity.getApi());
subscriptionItem.setApplication(subscriptionEntity.getApplication());
subscriptionItem.setCreatedAt(getDate(subscriptionEntity.getCreatedAt()));
subscriptionItem.setEndAt(getDate(subscriptionEntity.getEndingAt()));
subscriptionItem.setProcessedAt(getDate(subscriptionEntity.getProcessedAt()));
subscriptionItem.setStartAt(getDate(subscriptionEntity.getStartingAt()));
subscriptionItem.setPausedAt(getDate(subscriptionEntity.getPausedAt()));
subscriptionItem.setClosedAt(getDate(subscriptionEntity.getClosedAt()));
subscriptionItem.setPausedAt(getDate(subscriptionEntity.getPausedAt()));
subscriptionItem.setPlan(subscriptionEntity.getPlan());
subscriptionItem.setRequest(subscriptionEntity.getRequest());
subscriptionItem.setReason(subscriptionEntity.getReason());
subscriptionItem.setStatus(Subscription.StatusEnum.fromValue(subscriptionEntity.getStatus().name()));
subscriptionItem.setSubscribedBy(subscriptionEntity.getSubscribedBy());
return subscriptionItem;
}
use of io.gravitee.rest.api.portal.rest.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionsResource method createSubscription.
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createSubscription(@Valid @NotNull(message = "Input must not be null.") SubscriptionInput subscriptionInput) {
if (hasPermission(RolePermission.APPLICATION_SUBSCRIPTION, subscriptionInput.getApplication(), RolePermissionAction.CREATE)) {
NewSubscriptionEntity newSubscriptionEntity = new NewSubscriptionEntity();
newSubscriptionEntity.setApplication(subscriptionInput.getApplication());
newSubscriptionEntity.setPlan(subscriptionInput.getPlan());
newSubscriptionEntity.setRequest(subscriptionInput.getRequest());
newSubscriptionEntity.setGeneralConditionsAccepted(subscriptionInput.getGeneralConditionsAccepted());
if (subscriptionInput.getGeneralConditionsContentRevision() != null) {
final PageRevisionId generalConditionsContentRevision = new PageRevisionId(subscriptionInput.getGeneralConditionsContentRevision().getPageId(), subscriptionInput.getGeneralConditionsContentRevision().getRevision());
newSubscriptionEntity.setGeneralConditionsContentRevision(generalConditionsContentRevision);
}
SubscriptionEntity createdSubscription = subscriptionService.create(newSubscriptionEntity);
// For consumer convenience, fetch the keys just after the subscription has been created.
List<Key> keys = apiKeyService.findBySubscription(createdSubscription.getId()).stream().sorted((o1, o2) -> o2.getCreatedAt().compareTo(o1.getCreatedAt())).map(keyMapper::convert).collect(Collectors.toList());
final Subscription subscription = subscriptionMapper.convert(createdSubscription);
subscription.setKeys(keys);
return Response.ok(subscription).build();
}
throw new ForbiddenAccessException();
}
use of io.gravitee.rest.api.portal.rest.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionResourceTest method shouldGetSubscription.
@Test
public void shouldGetSubscription() {
Response response = target(SUBSCRIPTION).request().get();
assertEquals(HttpStatusCode.OK_200, response.getStatus());
Mockito.verify(subscriptionMapper).convert(subscriptionEntity);
Subscription subscription = response.readEntity(Subscription.class);
assertNotNull(subscription);
assertNull(subscription.getKeys());
}
use of io.gravitee.rest.api.portal.rest.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionMapperTest method testConvert.
@Test
public void testConvert() {
Instant now = Instant.now();
Date nowDate = Date.from(now);
// init
subscriptionEntity = new SubscriptionEntity();
subscriptionEntity.setApi(SUBSCRIPTION_API);
subscriptionEntity.setApplication(SUBSCRIPTION_APPLICATION);
subscriptionEntity.setClientId(SUBSCRIPTION_CLIENT_ID);
subscriptionEntity.setClosedAt(nowDate);
subscriptionEntity.setCreatedAt(nowDate);
subscriptionEntity.setEndingAt(nowDate);
subscriptionEntity.setId(SUBSCRIPTION_ID);
subscriptionEntity.setPausedAt(nowDate);
subscriptionEntity.setPlan(SUBSCRIPTION_PLAN);
subscriptionEntity.setProcessedAt(nowDate);
subscriptionEntity.setProcessedBy(SUBSCRIPTION_PROCESSED_BY);
subscriptionEntity.setReason(SUBSCRIPTION_REASON);
subscriptionEntity.setRequest(SUBSCRIPTION_REQUEST);
subscriptionEntity.setStartingAt(nowDate);
subscriptionEntity.setStatus(SubscriptionStatus.ACCEPTED);
subscriptionEntity.setSubscribedBy(SUBSCRIPTION_SUBSCRIBED_BY);
subscriptionEntity.setUpdatedAt(nowDate);
// Test
Subscription subscription = subscriptionMapper.convert(subscriptionEntity);
assertNotNull(subscription);
assertEquals(SUBSCRIPTION_API, subscription.getApi());
assertEquals(SUBSCRIPTION_APPLICATION, subscription.getApplication());
assertEquals(now.toEpochMilli(), subscription.getCreatedAt().toInstant().toEpochMilli());
assertEquals(now.toEpochMilli(), subscription.getEndAt().toInstant().toEpochMilli());
assertEquals(SUBSCRIPTION_ID, subscription.getId());
assertEquals(SUBSCRIPTION_PLAN, subscription.getPlan());
assertEquals(now.toEpochMilli(), subscription.getProcessedAt().toInstant().toEpochMilli());
assertEquals(SUBSCRIPTION_REQUEST, subscription.getRequest());
assertEquals(now.toEpochMilli(), subscription.getStartAt().toInstant().toEpochMilli());
assertEquals(StatusEnum.ACCEPTED, subscription.getStatus());
}
use of io.gravitee.rest.api.portal.rest.model.Subscription in project gravitee-management-rest-api by gravitee-io.
the class SubscriptionsResource method getSubscriptions.
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSubscriptions(@QueryParam("apiId") String apiId, @QueryParam("applicationId") String applicationId, @QueryParam("statuses") List<SubscriptionStatus> statuses, @BeanParam PaginationParam paginationParam) {
final boolean withoutPagination = paginationParam.getSize() != null && paginationParam.getSize().equals(-1);
final SubscriptionQuery query = new SubscriptionQuery();
query.setApi(apiId);
query.setApplication(applicationId);
final Map<String, Map<String, Object>> metadata = new HashMap<>();
if (applicationId == null) {
final Set<ApplicationListItem> applications = applicationService.findByUser(getAuthenticatedUser());
if (applications == null || applications.isEmpty()) {
return createListResponse(emptyList(), paginationParam, !withoutPagination);
}
query.setApplications(applications.stream().map(ApplicationListItem::getId).collect(toSet()));
applications.forEach(application -> {
final Map<String, Object> m = new HashMap<>();
m.put("name", application.getName());
metadata.put(application.getId(), m);
});
} else if (!hasPermission(RolePermission.APPLICATION_SUBSCRIPTION, applicationId, RolePermissionAction.READ)) {
throw new ForbiddenAccessException();
}
if (statuses != null && !statuses.isEmpty()) {
query.setStatuses(statuses);
}
final Collection<SubscriptionEntity> subscriptions;
if (withoutPagination) {
subscriptions = subscriptionService.search(query);
} else {
final Page<SubscriptionEntity> pagedSubscriptions = subscriptionService.search(query, new PageableImpl(paginationParam.getPage(), paginationParam.getSize()));
if (pagedSubscriptions == null) {
subscriptions = emptyList();
} else {
subscriptions = pagedSubscriptions.getContent();
}
}
final List<Subscription> subscriptionList = subscriptions.stream().map(subscriptionMapper::convert).collect(Collectors.toList());
subscriptionList.forEach(subscription -> {
final ApiEntity api = apiService.findById(subscription.getApi());
if (api != null) {
final Map<String, Object> m = new HashMap<>();
m.put("name", api.getName());
m.put("pictureUrl", apiMapper.computeApiLinks(PortalApiLinkHelper.apisURL(uriInfo.getBaseUriBuilder(), api.getId()), api.getUpdatedAt()).getPicture());
m.put("state", api.getLifecycleState());
m.put("version", api.getVersion());
m.put("entrypoints", api.getEntrypoints());
metadata.put(api.getId(), m);
}
final PlanEntity plan = planService.findById(subscription.getPlan());
if (plan != null) {
final Map<String, Object> m = new HashMap<>();
m.put("name", plan.getName());
metadata.put(plan.getId(), m);
}
final UserEntity user = userService.findById(subscription.getSubscribedBy(), true);
if (user != null) {
final Map<String, Object> m = new HashMap<>();
m.put("name", user.getDisplayName());
metadata.put(user.getId(), m);
}
});
return createListResponse(subscriptionList, paginationParam, metadata, !withoutPagination);
}
Aggregations