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);
}
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));
}
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());
}
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));
}
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());
}
Aggregations