Search in sources :

Example 1 with Subscription

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;
}
Also used : Subscription(io.gravitee.rest.api.portal.rest.model.Subscription)

Example 2 with Subscription

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();
}
Also used : PageRevisionId(io.gravitee.rest.api.model.PageEntity.PageRevisionId) Subscription(io.gravitee.rest.api.portal.rest.model.Subscription) ForbiddenAccessException(io.gravitee.rest.api.service.exceptions.ForbiddenAccessException) Key(io.gravitee.rest.api.portal.rest.model.Key)

Example 3 with Subscription

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());
}
Also used : ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) Subscription(io.gravitee.rest.api.portal.rest.model.Subscription) Test(org.junit.Test)

Example 4 with Subscription

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());
}
Also used : Instant(java.time.Instant) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Subscription(io.gravitee.rest.api.portal.rest.model.Subscription) Date(java.util.Date) Test(org.junit.Test)

Example 5 with Subscription

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);
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ForbiddenAccessException(io.gravitee.rest.api.service.exceptions.ForbiddenAccessException) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Subscription(io.gravitee.rest.api.portal.rest.model.Subscription)

Aggregations

Subscription (io.gravitee.rest.api.portal.rest.model.Subscription)8 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)3 Key (io.gravitee.rest.api.portal.rest.model.Key)3 ForbiddenAccessException (io.gravitee.rest.api.service.exceptions.ForbiddenAccessException)3 Test (org.junit.Test)3 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)2 Response (javax.ws.rs.core.Response)2 ApiKeyEntity (io.gravitee.rest.api.model.ApiKeyEntity)1 PageRevisionId (io.gravitee.rest.api.model.PageEntity.PageRevisionId)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)1 PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)1 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)1 Instant (java.time.Instant)1 Date (java.util.Date)1 Before (org.junit.Before)1