use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.
the class PermissionsResourceTest method init.
@Before
public void init() {
resetAllMocks();
ApiEntity mockApi = new ApiEntity();
mockApi.setId(API);
Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
doReturn(mockApis).when(apiService).findPublishedByUser(any(), any());
ApplicationListItem mockAppListItem = new ApplicationListItem();
mockAppListItem.setId(APPLICATION);
Set<ApplicationListItem> mockApps = new HashSet<>(Arrays.asList(mockAppListItem));
doReturn(mockApps).when(applicationService).findByUser(any());
ApplicationEntity mockAppEntity = new ApplicationEntity();
mockAppEntity.setId(APPLICATION);
doReturn(mockAppEntity).when(applicationService).findById(any());
}
use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.
the class FilteringServiceTest method shouldGetApplicationsOrderByNbSubscriptionsDesc.
@Test
public void shouldGetApplicationsOrderByNbSubscriptionsDesc() {
ApplicationListItem applicationListItem1 = new ApplicationListItem();
applicationListItem1.setId("A");
applicationListItem1.setName("A");
ApplicationListItem applicationListItem2 = new ApplicationListItem();
applicationListItem2.setId("B");
applicationListItem2.setName("B");
Set<FilterableItem> mockApplications = new HashSet<>(Arrays.asList(applicationListItem1, applicationListItem2));
SubscriptionEntity subA1 = new SubscriptionEntity();
subA1.setApplication("A");
SubscriptionEntity subA2 = new SubscriptionEntity();
subA2.setApplication("A");
SubscriptionEntity subB1 = new SubscriptionEntity();
subB1.setApplication("B");
SubscriptionEntity subB2 = new SubscriptionEntity();
subB2.setApplication("B");
SubscriptionEntity subB3 = new SubscriptionEntity();
subB3.setApplication("B");
doReturn(Arrays.asList(subA1, subA2, subB1, subB2, subB3)).when(subscriptionService).search(any());
FilteredEntities<FilterableItem> applicationListItemFilteredEntities = filteringService.getEntitiesOrderByNumberOfSubscriptions(mockApplications, false, false);
List<FilterableItem> filteredItems = applicationListItemFilteredEntities.getFilteredItems();
assertEquals("B", filteredItems.get(0).getId());
assertEquals("A", filteredItems.get(1).getId());
Map<String, Object> subscriptionsMetadata = applicationListItemFilteredEntities.getMetadata().get("subscriptions");
assertEquals(3L, subscriptionsMetadata.get("B"));
assertEquals(2L, subscriptionsMetadata.get("A"));
}
use of io.gravitee.rest.api.model.application.ApplicationListItem 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);
}
use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.
the class ApplicationSubscribersResource method getSubscriberApisByApplicationId.
@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubscriberApisByApplicationId(@BeanParam PaginationParam paginationParam, @PathParam("applicationId") String applicationId, @QueryParam("statuses") List<SubscriptionStatus> statuses) {
String currentUser = getAuthenticatedUserOrNull();
Collection<ApplicationListItem> userApplications = applicationService.findByUser(currentUser);
Optional<ApplicationListItem> optionalApplication = userApplications.stream().filter(a -> a.getId().equals(applicationId)).findFirst();
if (optionalApplication.isPresent()) {
SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
subscriptionQuery.setApplication(applicationId);
subscriptionQuery.setStatuses(statuses);
ApplicationListItem application = optionalApplication.get();
if (!application.getPrimaryOwner().getId().equals(currentUser)) {
Set<ApiEntity> userApis = this.apiService.findPublishedByUser(currentUser);
if (userApis == null || userApis.isEmpty()) {
return createListResponse(Collections.emptyList(), paginationParam);
}
subscriptionQuery.setApis(userApis.stream().map(ApiEntity::getId).collect(Collectors.toList()));
}
Map<String, Long> nbHitsByApp = getNbHitsByApplication(applicationId);
Collection<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery);
List<Api> subscribersApis = subscriptions.stream().map(SubscriptionEntity::getApi).distinct().map(api -> apiService.findById(api)).map(apiMapper::convert).sorted((o1, o2) -> compareApp(nbHitsByApp, o1, o2)).collect(Collectors.toList());
return createListResponse(subscribersApis, paginationParam);
}
throw new ApplicationNotFoundException(applicationId);
}
use of io.gravitee.rest.api.model.application.ApplicationListItem in project gravitee-management-rest-api by gravitee-io.
the class PlatformAnalyticsResource method getPlatformAnalytics.
@GET
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get platform analytics", notes = "User must have the MANAGEMENT_PLATFORM[READ] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Platform analytics"), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = ENVIRONMENT_PLATFORM, acls = READ) })
public Response getPlatformAnalytics(@BeanParam AnalyticsParam analyticsParam) {
analyticsParam.validate();
Analytics analytics = null;
// add filter by Apis or Applications
String extraFilter = null;
if (!isAdmin()) {
String fieldName;
List<String> ids;
if ("application".equals(analyticsParam.getField())) {
fieldName = "application";
ids = applicationService.findByUser(getAuthenticatedUser()).stream().filter(app -> permissionService.hasPermission(APPLICATION_ANALYTICS, app.getId(), READ)).map(ApplicationListItem::getId).collect(Collectors.toList());
} else {
fieldName = "api";
ids = apiService.findByUser(getAuthenticatedUser(), null, false).stream().filter(api -> permissionService.hasPermission(API_ANALYTICS, api.getId(), READ)).map(ApiEntity::getId).collect(Collectors.toList());
}
if (ids.isEmpty()) {
return Response.noContent().build();
}
extraFilter = getExtraFilter(fieldName, ids);
}
if (analyticsParam.getQuery() != null) {
analyticsParam.setQuery(analyticsParam.getQuery().replaceAll("\\?", "1"));
}
switch(analyticsParam.getTypeParam().getValue()) {
case DATE_HISTO:
analytics = executeDateHisto(analyticsParam, extraFilter);
break;
case GROUP_BY:
analytics = executeGroupBy(analyticsParam, extraFilter);
break;
case COUNT:
analytics = executeCount(analyticsParam, extraFilter);
break;
case STATS:
analytics = executeStats(analyticsParam, extraFilter);
break;
}
return Response.ok(analytics).build();
}
Aggregations