Search in sources :

Example 11 with ApplicationListItem

use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.

the class ApiSubscribersResource method getSubscriberApplicationsByApiId.

@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubscriberApplicationsByApiId(@BeanParam PaginationParam paginationParam, @PathParam("apiId") String apiId, @QueryParam("statuses") List<SubscriptionStatus> statuses) {
    String currentUser = getAuthenticatedUserOrNull();
    final ApiQuery apiQuery = new ApiQuery();
    apiQuery.setIds(Collections.singletonList(apiId));
    Collection<ApiEntity> userApis = apiService.findPublishedByUser(currentUser, apiQuery);
    Optional<ApiEntity> optionalApi = userApis.stream().filter(a -> a.getId().equals(apiId)).findFirst();
    if (optionalApi.isPresent()) {
        SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
        subscriptionQuery.setApi(apiId);
        subscriptionQuery.setStatuses(statuses);
        ApiEntity api = optionalApi.get();
        if (!api.getPrimaryOwner().getId().equals(currentUser)) {
            Set<ApplicationListItem> userApplications = this.applicationService.findByUser(currentUser);
            if (userApplications == null || userApplications.isEmpty()) {
                return createListResponse(Collections.emptyList(), paginationParam);
            }
            subscriptionQuery.setApplications(userApplications.stream().map(ApplicationListItem::getId).collect(Collectors.toList()));
        }
        Map<String, Long> nbHitsByApp = getNbHitsByApplication(apiId);
        Collection<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery);
        List<Application> subscribersApplication = subscriptions.stream().map(SubscriptionEntity::getApplication).distinct().map(application -> applicationService.findById(application)).map(application -> applicationMapper.convert(application, uriInfo)).sorted((o1, o2) -> compareApp(nbHitsByApp, o1, o2)).collect(Collectors.toList());
        return createListResponse(subscribersApplication, paginationParam);
    }
    throw new ApiNotFoundException(apiId);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) ApplicationMapper(io.gravitee.rest.api.portal.rest.mapper.ApplicationMapper) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Application(io.gravitee.rest.api.portal.rest.model.Application) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Inject(javax.inject.Inject) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) MediaType(io.gravitee.common.http.MediaType) ChronoUnit(java.time.temporal.ChronoUnit) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) PaginationParam(io.gravitee.rest.api.portal.rest.resource.param.PaginationParam) ApplicationService(io.gravitee.rest.api.service.ApplicationService) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) AnalyticsService(io.gravitee.rest.api.service.AnalyticsService) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Application(io.gravitee.rest.api.portal.rest.model.Application)

Example 12 with ApplicationListItem

use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.

the class ApplicationsResourceTest method shouldGetApplicationsOrderByNbSubscriptionsDesc.

@Test
public void shouldGetApplicationsOrderByNbSubscriptionsDesc() {
    FilteredEntities<ApplicationListItem> mockFilteredApp = new FilteredEntities<ApplicationListItem>(Collections.emptyList(), null);
    doReturn(mockFilteredApp).when(filteringService).getEntitiesOrderByNumberOfSubscriptions(anyCollection(), isNull(), eq(false));
    final Response response = target().queryParam("order", "-nbSubscriptions").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    Mockito.verify(filteringService).getEntitiesOrderByNumberOfSubscriptions(anyCollection(), isNull(), eq(false));
}
Also used : Response(javax.ws.rs.core.Response) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) Test(org.junit.Test)

Example 13 with ApplicationListItem

use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.

the class ApplicationsResourceTest method init.

@Before
public void init() {
    resetAllMocks();
    ApplicationListItem applicationListItem1 = new ApplicationListItem();
    applicationListItem1.setId("A");
    applicationListItem1.setName("A");
    ApplicationListItem applicationListItem2 = new ApplicationListItem();
    applicationListItem2.setId("B");
    applicationListItem2.setName("B");
    Set<ApplicationListItem> mockApplications = new HashSet<>(Arrays.asList(applicationListItem1, applicationListItem2));
    doReturn(mockApplications).when(applicationService).findByUser(any());
    doReturn(new Application().id("A").name("A")).when(applicationMapper).convert(eq(applicationListItem1), any());
    doReturn(new Application().id("B").name("B")).when(applicationMapper).convert(eq(applicationListItem2), any());
    ApplicationEntity createdEntity = mock(ApplicationEntity.class);
    doReturn("NEW").when(createdEntity).getId();
    doReturn(createdEntity).when(applicationService).create(any(), any());
    doReturn(new Application().id("NEW")).when(applicationMapper).convert(eq(createdEntity), any());
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) NewApplicationEntity(io.gravitee.rest.api.model.NewApplicationEntity) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) Before(org.junit.Before)

Example 14 with ApplicationListItem

use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.

the class ApplicationsResourceTest method shouldGetApplicationsOrderByName.

@Test
public void shouldGetApplicationsOrderByName() {
    ApplicationListItem applicationListItem1 = new ApplicationListItem();
    applicationListItem1.setId("A");
    applicationListItem1.setName("A");
    ApplicationListItem applicationListItem2 = new ApplicationListItem();
    applicationListItem2.setId("B");
    applicationListItem2.setName("b");
    ApplicationListItem applicationListItem3 = new ApplicationListItem();
    applicationListItem3.setId("C");
    applicationListItem3.setName("C");
    ApplicationListItem applicationListItem4 = new ApplicationListItem();
    applicationListItem4.setId("D");
    applicationListItem4.setName("d");
    Set<ApplicationListItem> mockApplications = new HashSet<>(Arrays.asList(applicationListItem1, applicationListItem2, applicationListItem3, applicationListItem4));
    doReturn(mockApplications).when(applicationService).findByUser(any());
    doReturn(new Application().id("A").name("A")).when(applicationMapper).convert(eq(applicationListItem1), any());
    doReturn(new Application().id("B").name("b")).when(applicationMapper).convert(eq(applicationListItem2), any());
    doReturn(new Application().id("C").name("C")).when(applicationMapper).convert(eq(applicationListItem3), any());
    doReturn(new Application().id("D").name("d")).when(applicationMapper).convert(eq(applicationListItem4), any());
    final Response response = target().request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    ApplicationsResponse applicationsResponse = response.readEntity(ApplicationsResponse.class);
    assertEquals(4, applicationsResponse.getData().size());
    assertEquals("A", applicationsResponse.getData().get(0).getId());
    assertEquals("B", applicationsResponse.getData().get(1).getId());
    assertEquals("C", applicationsResponse.getData().get(2).getId());
    assertEquals("D", applicationsResponse.getData().get(3).getId());
    Mockito.verify(filteringService, times(0)).getEntitiesOrderByNumberOfSubscriptions(anyCollection(), eq(false), eq(false));
}
Also used : Response(javax.ws.rs.core.Response) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) Test(org.junit.Test)

Example 15 with ApplicationListItem

use of io.gravitee.rest.api.model.application.ApplicationListItem 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)

Aggregations

ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)29 Test (org.junit.Test)16 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)11 Response (javax.ws.rs.core.Response)9 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)8 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)7 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)6 Collectors (java.util.stream.Collectors)6 ApplicationService (io.gravitee.rest.api.service.ApplicationService)5 java.util (java.util)5 MediaType (io.gravitee.common.http.MediaType)4 FilterableItem (io.gravitee.rest.api.model.filtering.FilterableItem)4 Inject (javax.inject.Inject)4 Before (org.junit.Before)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 SimpleEvent (io.gravitee.common.event.impl.SimpleEvent)3 UserEntity (io.gravitee.rest.api.model.UserEntity)3 AlertTriggerEntity (io.gravitee.rest.api.model.alert.AlertTriggerEntity)3 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)3 Application (io.gravitee.rest.api.portal.rest.model.Application)3