Search in sources :

Example 1 with FilterableItem

use of io.gravitee.rest.api.model.filtering.FilterableItem 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"));
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem) Test(org.junit.Test)

Example 2 with FilterableItem

use of io.gravitee.rest.api.model.filtering.FilterableItem in project gravitee-management-rest-api by gravitee-io.

the class FilteringServiceImpl method getEntitiesOrderByNumberOfSubscriptions.

/**
 * Filters and sorts input entities by number of subscriptions.
 *
 * @param items    Can be {@link ApiEntity} or {@link ApplicationListItem}
 * @param excluded If set to true, only entities without subscriptions are returned. Else, only entities with subscriptions are returned.
 * @return a {@link FilteredEntities} object with the filtered and sorted list of items and a metadata map.
 */
@Override
public <T extends FilterableItem> FilteredEntities<T> getEntitiesOrderByNumberOfSubscriptions(Collection<T> items, Boolean excluded, boolean isAsc) {
    if (items == null || items.isEmpty()) {
        return new FilteredEntities<>(Collections.emptyList(), new HashMap<>());
    }
    Function<SubscriptionEntity, String> getItemFunction;
    if (items.toArray()[0] instanceof ApiEntity) {
        getItemFunction = (SubscriptionEntity sub) -> sub.getApi();
    } else if (items.toArray()[0] instanceof ApplicationListItem) {
        getItemFunction = (SubscriptionEntity sub) -> sub.getApplication();
    } else {
        throw new IllegalStateException("Only ApiEntity and ApplicationListItem are allowed");
    }
    // find all subscribed items
    SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
    subscriptionQuery.setApis(items.stream().map(FilterableItem::getId).collect(Collectors.toList()));
    subscriptionQuery.setStatuses(Arrays.asList(SubscriptionStatus.ACCEPTED, SubscriptionStatus.PAUSED));
    // group by items
    Map<String, Long> subscribedItemsWithCount = subscriptionService.search(subscriptionQuery).stream().collect(Collectors.groupingBy(getItemFunction, Collectors.counting()));
    // link an item with its nb of subscriptions
    Map<FilterableItem, Long> itemsWithCount = new HashMap<>();
    Map<String, Map<String, Object>> itemsMetadata = new HashMap<>();
    Map<String, Object> subscriptionsMetadata = new HashMap<>();
    itemsMetadata.put("subscriptions", subscriptionsMetadata);
    items.forEach(item -> {
        Long itemSubscriptionsCount = subscribedItemsWithCount.get(item.getId());
        if ((excluded == null) || (!excluded && itemSubscriptionsCount != null) || (excluded && itemSubscriptionsCount == null)) {
            // creation of a map which will be sorted to retrieve items in the right order
            itemsWithCount.put(item, itemSubscriptionsCount == null ? 0L : itemSubscriptionsCount);
            // creation of a metadata map
            subscriptionsMetadata.put(item.getId(), itemSubscriptionsCount == null ? 0L : itemSubscriptionsCount);
        }
    });
    // order the list
    Comparator<Map.Entry<FilterableItem, Long>> comparingByValue = Map.Entry.comparingByValue();
    if (!isAsc) {
        comparingByValue = comparingByValue.reversed();
    }
    return new FilteredEntities(itemsWithCount.entrySet().stream().sorted(Map.Entry.<FilterableItem, Long>comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey(Comparator.comparing(FilterableItem::getName, String.CASE_INSENSITIVE_ORDER)))).map(Map.Entry::getKey).collect(Collectors.toList()), itemsMetadata);
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem)

Example 3 with FilterableItem

use of io.gravitee.rest.api.model.filtering.FilterableItem in project gravitee-management-rest-api by gravitee-io.

the class FilteringServiceTest method shouldGetApplicationsOrderByNbSubscriptionsDescAndName.

@Test
public void shouldGetApplicationsOrderByNbSubscriptionsDescAndName() {
    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<FilterableItem> mockApplications = new HashSet<>(Arrays.asList(applicationListItem1, applicationListItem2, applicationListItem3, applicationListItem4));
    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");
    SubscriptionEntity subC1 = new SubscriptionEntity();
    subC1.setApplication("C");
    SubscriptionEntity subC2 = new SubscriptionEntity();
    subC2.setApplication("C");
    SubscriptionEntity subC3 = new SubscriptionEntity();
    subC3.setApplication("C");
    SubscriptionEntity subD1 = new SubscriptionEntity();
    subD1.setApplication("D");
    SubscriptionEntity subD2 = new SubscriptionEntity();
    subD2.setApplication("D");
    doReturn(Arrays.asList(subA1, subA2, subB1, subB2, subB3, subC1, subC2, subC3, subD1, subD2)).when(subscriptionService).search(any());
    FilteredEntities<FilterableItem> applicationListItemFilteredEntities = filteringService.getEntitiesOrderByNumberOfSubscriptions(mockApplications, false, false);
    List<FilterableItem> filteredItems = applicationListItemFilteredEntities.getFilteredItems();
    assertEquals("B", filteredItems.get(0).getId());
    assertEquals("C", filteredItems.get(1).getId());
    assertEquals("A", filteredItems.get(2).getId());
    assertEquals("D", filteredItems.get(3).getId());
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem) Test(org.junit.Test)

Aggregations

SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)3 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)3 FilterableItem (io.gravitee.rest.api.model.filtering.FilterableItem)3 Test (org.junit.Test)2 TopApiEntity (io.gravitee.rest.api.model.TopApiEntity)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 FilteredEntities (io.gravitee.rest.api.model.filtering.FilteredEntities)1 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)1