Search in sources :

Example 6 with SubscriptionQuery

use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.

the class ApiMetricsResource method getApiNbSubscribers.

private Number getApiNbSubscribers(String apiId) {
    // find all subscribed apis
    SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
    subscriptionQuery.setApi(apiId);
    subscriptionQuery.setStatuses(Arrays.asList(SubscriptionStatus.ACCEPTED, SubscriptionStatus.PAUSED));
    // group by apis
    Collection<SubscriptionEntity> searchResult = subscriptionService.search(subscriptionQuery);
    if (searchResult != null) {
        return searchResult.stream().collect(Collectors.groupingBy(SubscriptionEntity::getApi, Collectors.counting())).get(apiId);
    }
    return null;
}
Also used : SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery)

Example 7 with SubscriptionQuery

use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceImpl method findByApplicationAndPlan.

@Override
public Collection<SubscriptionEntity> findByApplicationAndPlan(String application, String plan) {
    logger.debug("Find subscriptions by application {} and plan {}", application, plan);
    SubscriptionQuery query = new SubscriptionQuery();
    if (plan != null) {
        query.setPlan(plan);
    }
    if (application != null && !application.trim().isEmpty()) {
        query.setApplication(application);
    } else if (isAuthenticated()) {
        Set<ApplicationListItem> applications = applicationService.findByUser(getAuthenticatedUsername());
        query.setApplications(applications.stream().map(ApplicationListItem::getId).collect(toList()));
    }
    return search(query);
}
Also used : SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery)

Example 8 with SubscriptionQuery

use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.

the class ScheduledSubscriptionPreExpirationNotificationService method findSubscriptionExpirationsToNotify.

@VisibleForTesting
Collection<SubscriptionEntity> findSubscriptionExpirationsToNotify(Instant now, Integer daysToExpiration) {
    long expirationStartingTime = now.plus(Duration.ofDays((long) daysToExpiration)).getEpochSecond() * 1000;
    SubscriptionQuery query = new SubscriptionQuery();
    query.setStatuses(Arrays.asList(SubscriptionStatus.ACCEPTED, SubscriptionStatus.PAUSED));
    query.setEndingAtAfter(expirationStartingTime);
    query.setEndingAtBefore(expirationStartingTime + cronPeriodInMs);
    return subscriptionService.search(query);
}
Also used : SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with SubscriptionQuery

use of io.gravitee.rest.api.model.subscription.SubscriptionQuery in project gravitee-management-rest-api by gravitee-io.

the class ApiSubscribersResourceTest method shouldGetApiSubscribersNotAsPrimaryOwner.

@Test
public void shouldGetApiSubscribersNotAsPrimaryOwner() {
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    UserEntity user = new UserEntity();
    user.setId("ANOTHER_NAME");
    PrimaryOwnerEntity primaryOwner = new PrimaryOwnerEntity(user);
    mockApi.setPrimaryOwner(primaryOwner);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any(), argThat(q -> singletonList(API).equals(q.getIds())));
    TopHitsAnalytics mockAnalytics = new TopHitsAnalytics();
    Map<String, Long> mockedValues = new HashMap<>();
    mockedValues.put("A", 10L);
    mockedValues.put("B", 20L);
    mockedValues.put("C", 30L);
    mockAnalytics.setValues(mockedValues);
    doReturn(mockAnalytics).when(analyticsService).execute(any(GroupByQuery.class));
    SubscriptionEntity subA1 = new SubscriptionEntity();
    subA1.setApplication("A");
    subA1.setApi(API);
    SubscriptionEntity subC1 = new SubscriptionEntity();
    subC1.setApplication("C");
    subC1.setApi(API);
    doReturn(Arrays.asList(subA1, subC1)).when(subscriptionService).search(any());
    ApplicationEntity appA = new ApplicationEntity();
    appA.setId("A");
    doReturn(appA).when(applicationService).findById("A");
    doReturn(new Application().id("A")).when(applicationMapper).convert(eq(appA), any());
    ApplicationEntity appC = new ApplicationEntity();
    appC.setId("C");
    doReturn(appC).when(applicationService).findById("C");
    doReturn(new Application().id("C")).when(applicationMapper).convert(eq(appC), any());
    ApplicationListItem appLIA = new ApplicationListItem();
    appLIA.setId("A");
    ApplicationListItem appLIC = new ApplicationListItem();
    appLIC.setId("C");
    doReturn(new HashSet<>(Arrays.asList(appLIA, appLIC))).when(applicationService).findByUser(USER_NAME);
    final Response response = target(API).path("subscribers").request().get();
    assertEquals(OK_200, response.getStatus());
    ArgumentCaptor<SubscriptionQuery> queryCaptor = ArgumentCaptor.forClass(SubscriptionQuery.class);
    Mockito.verify(subscriptionService).search(queryCaptor.capture());
    SubscriptionQuery value = queryCaptor.getValue();
    assertNotNull(value.getApplications());
    assertEquals(2, value.getApplications().size());
    assertTrue(value.getApplications().contains("A"));
    assertTrue(value.getApplications().contains("C"));
    final ApplicationsResponse applicationsResponse = response.readEntity(ApplicationsResponse.class);
    assertNotNull(applicationsResponse);
    assertEquals(2, applicationsResponse.getData().size());
    assertEquals("C", applicationsResponse.getData().get(0).getId());
    assertEquals("A", applicationsResponse.getData().get(1).getId());
}
Also used : java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) OK_200(io.gravitee.common.http.HttpStatusCode.OK_200) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Error(io.gravitee.rest.api.portal.rest.model.Error) Collections.singletonList(java.util.Collections.singletonList) Application(io.gravitee.rest.api.portal.rest.model.Application) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ArgumentCaptor(org.mockito.ArgumentCaptor) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Collections.emptySet(java.util.Collections.emptySet) NOT_FOUND_404(io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404) IOException(java.io.IOException) Test(org.junit.Test) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Mockito(org.mockito.Mockito) Response(javax.ws.rs.core.Response) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) Assert(org.junit.Assert) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) UserEntity(io.gravitee.rest.api.model.UserEntity) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Response(javax.ws.rs.core.Response) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) Application(io.gravitee.rest.api.portal.rest.model.Application) ApplicationsResponse(io.gravitee.rest.api.portal.rest.model.ApplicationsResponse) Test(org.junit.Test)

Example 10 with SubscriptionQuery

use of io.gravitee.rest.api.model.subscription.SubscriptionQuery 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

SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)20 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)9 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)8 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)7 java.util (java.util)7 Collectors (java.util.stream.Collectors)7 SubscriptionService (io.gravitee.rest.api.service.SubscriptionService)5 MediaType (io.gravitee.common.http.MediaType)4 ApplicationService (io.gravitee.rest.api.service.ApplicationService)4 Inject (javax.inject.Inject)4 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 io.gravitee.rest.api.model (io.gravitee.rest.api.model)3 PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)3 RolePermissionAction (io.gravitee.rest.api.model.permissions.RolePermissionAction)3 io.gravitee.rest.api.service (io.gravitee.rest.api.service)3 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Page (io.gravitee.common.data.domain.Page)2 io.gravitee.repository.management.model (io.gravitee.repository.management.model)2