Search in sources :

Example 6 with PageableImpl

use of io.gravitee.rest.api.model.common.PageableImpl in project gravitee-management-rest-api by gravitee-io.

the class ApiService_SearchTest method shouldSearchPaginated.

@Test
public void shouldSearchPaginated() {
    final Api api1 = new Api();
    api1.setId("api1");
    api1.setName("api1");
    final Api api2 = new Api();
    api2.setId("api2");
    api2.setName("api2");
    MembershipEntity membership1 = new MembershipEntity();
    membership1.setId("id1");
    membership1.setMemberId(USER_NAME);
    membership1.setMemberType(MembershipMemberType.USER);
    membership1.setReferenceId(api1.getId());
    membership1.setReferenceType(MembershipReferenceType.API);
    membership1.setRoleId("API_USER");
    MembershipEntity membership2 = new MembershipEntity();
    membership2.setId(api2.getId());
    membership2.setMemberId(USER_NAME);
    membership2.setMemberType(MembershipMemberType.USER);
    membership2.setReferenceId("api2");
    membership2.setReferenceType(MembershipReferenceType.API);
    membership2.setRoleId("API_USER");
    when(apiRepository.search(new ApiCriteria.Builder().environmentId("DEFAULT").build())).thenReturn(Arrays.asList(api1, api2));
    RoleEntity poRole = new RoleEntity();
    poRole.setId("API_PRIMARY_OWNER");
    when(roleService.findPrimaryOwnerRoleByOrganization(any(), any())).thenReturn(poRole);
    MemberEntity poMember = new MemberEntity();
    poMember.setId("admin");
    poMember.setRoles(Collections.singletonList(poRole));
    when(membershipService.getMembersByReferencesAndRole(MembershipReferenceType.API, Collections.singletonList(api1.getId()), "API_PRIMARY_OWNER")).thenReturn(new HashSet<>(singletonList(poMember)));
    final ApiQuery apiQuery = new ApiQuery();
    final Page<ApiEntity> apiPage = apiService.search(apiQuery, new SortableImpl("name", false), new PageableImpl(2, 1));
    assertNotNull(apiPage);
    assertEquals(1, apiPage.getContent().size());
    assertEquals(api1.getId(), apiPage.getContent().get(0).getId());
    assertEquals(2, apiPage.getPageNumber());
    assertEquals(1, apiPage.getPageElements());
    assertEquals(2, apiPage.getTotalElements());
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) ApiCriteria(io.gravitee.repository.management.api.search.ApiCriteria) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Api(io.gravitee.repository.management.model.Api) Test(org.junit.Test)

Example 7 with PageableImpl

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

Example 8 with PageableImpl

use of io.gravitee.rest.api.model.common.PageableImpl in project gravitee-management-rest-api by gravitee-io.

the class TicketsResource method getTickets.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTickets(@Valid @BeanParam PaginationParam paginationParam, @BeanParam TicketsParam ticketsParam) {
    TicketQuery query = new TicketQuery();
    query.setApi(ticketsParam.getApi());
    query.setApplication(ticketsParam.getApplication());
    query.setApi(ticketsParam.getApi());
    query.setFromUser(getAuthenticatedUser());
    Sortable sortable = null;
    if (ticketsParam.getOrder() != null) {
        sortable = new SortableImpl(ticketsParam.getOrder().getField(), ticketsParam.getOrder().isSorted());
    }
    Page<TicketEntity> tickets = ticketService.search(query, sortable, new PageableImpl(paginationParam.getPage(), paginationParam.getSize()));
    final Map<String, Object> metadataTotal = new HashMap<>();
    metadataTotal.put(METADATA_DATA_TOTAL_KEY, tickets.getTotalElements());
    final Map<String, Map<String, Object>> metadata = new HashMap<>();
    metadata.put(METADATA_DATA_KEY, metadataTotal);
    return createListResponse(tickets.getContent(), paginationParam, metadata, false);
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) Sortable(io.gravitee.rest.api.model.common.Sortable) HashMap(java.util.HashMap) TicketEntity(io.gravitee.rest.api.model.TicketEntity) HashMap(java.util.HashMap) Map(java.util.Map) TicketQuery(io.gravitee.rest.api.model.api.TicketQuery)

Example 9 with PageableImpl

use of io.gravitee.rest.api.model.common.PageableImpl in project gravitee-management-rest-api by gravitee-io.

the class ApiService_FindByUserTest method shouldFindByUserPaginated.

@Test
public void shouldFindByUserPaginated() throws TechnicalException {
    final Api api1 = new Api();
    api1.setId("api1");
    api1.setName("api1");
    final Api api2 = new Api();
    api2.setId("api2");
    api2.setName("api2");
    MembershipEntity membership1 = new MembershipEntity();
    membership1.setId("id1");
    membership1.setMemberId(USER_NAME);
    membership1.setMemberType(MembershipMemberType.USER);
    membership1.setReferenceId(api1.getId());
    membership1.setReferenceType(MembershipReferenceType.API);
    membership1.setRoleId("API_USER");
    MembershipEntity membership2 = new MembershipEntity();
    membership2.setId(api2.getId());
    membership2.setMemberId(USER_NAME);
    membership2.setMemberType(MembershipMemberType.USER);
    membership2.setReferenceId("api2");
    membership2.setReferenceType(MembershipReferenceType.API);
    membership2.setRoleId("API_USER");
    when(membershipService.getMembershipsByMemberAndReference(MembershipMemberType.USER, USER_NAME, MembershipReferenceType.API)).thenReturn(new HashSet<>(Arrays.asList(membership1, membership2)));
    when(apiRepository.search(new ApiCriteria.Builder().environmentId("DEFAULT").ids(api1.getId(), api2.getId()).build())).thenReturn(Arrays.asList(api1, api2));
    RoleEntity poRole = new RoleEntity();
    poRole.setId("API_PRIMARY_OWNER");
    when(roleService.findPrimaryOwnerRoleByOrganization(any(), any())).thenReturn(poRole);
    MemberEntity poMember = new MemberEntity();
    poMember.setId("admin");
    poMember.setRoles(Collections.singletonList(poRole));
    when(membershipService.getMembersByReferencesAndRole(MembershipReferenceType.API, Collections.singletonList(api1.getId()), "API_PRIMARY_OWNER")).thenReturn(new HashSet<>(singletonList(poMember)));
    final Page<ApiEntity> apiPage = apiService.findByUser(USER_NAME, null, new SortableImpl("name", false), new PageableImpl(2, 1), false);
    assertNotNull(apiPage);
    assertEquals(1, apiPage.getContent().size());
    assertEquals(api1.getId(), apiPage.getContent().get(0).getId());
    assertEquals(2, apiPage.getPageNumber());
    assertEquals(1, apiPage.getPageElements());
    assertEquals(2, apiPage.getTotalElements());
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Api(io.gravitee.repository.management.model.Api) Test(org.junit.Test)

Example 10 with PageableImpl

use of io.gravitee.rest.api.model.common.PageableImpl in project gravitee-management-rest-api by gravitee-io.

the class TicketServiceTest method shouldNotSearchSearchIfRepositoryThrowException.

@Test(expected = TechnicalManagementException.class)
public void shouldNotSearchSearchIfRepositoryThrowException() throws TechnicalException {
    Ticket ticket = new Ticket();
    ticket.setId("generatedId");
    ticket.setApi(API_ID);
    ticket.setApplication(APPLICATION_ID);
    ticket.setSubject(EMAIL_SUBJECT);
    ticket.setContent(EMAIL_CONTENT);
    ticket.setCreatedAt(new Date());
    ticket.setFromUser(USERNAME);
    List<Ticket> ticketList = new ArrayList<>();
    for (int i = 0; i < 20; i++) {
        Ticket t = new Ticket(ticket);
        t.setId("ticket" + i);
        ticketList.add(t);
    }
    when(ticketRepository.search(any(TicketCriteria.class), any(Sortable.class), any(Pageable.class))).thenThrow(new TechnicalException());
    TicketQuery query = new TicketQuery();
    query.setFromUser("fromUser");
    ticketService.search(query, new SortableImpl("subject", true), new PageableImpl(1, Integer.MAX_VALUE));
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) Ticket(io.gravitee.repository.management.model.Ticket) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) Pageable(io.gravitee.repository.management.api.search.Pageable) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Sortable(io.gravitee.repository.management.api.search.Sortable) TicketCriteria(io.gravitee.repository.management.api.search.TicketCriteria) TicketQuery(io.gravitee.rest.api.model.api.TicketQuery) Test(org.junit.Test)

Aggregations

PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)10 SortableImpl (io.gravitee.rest.api.model.common.SortableImpl)5 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)4 Test (org.junit.Test)4 TicketQuery (io.gravitee.rest.api.model.api.TicketQuery)3 Page (io.gravitee.common.data.domain.Page)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Pageable (io.gravitee.repository.management.api.search.Pageable)2 Sortable (io.gravitee.repository.management.api.search.Sortable)2 TicketCriteria (io.gravitee.repository.management.api.search.TicketCriteria)2 UserCriteria (io.gravitee.repository.management.api.search.UserCriteria)2 Api (io.gravitee.repository.management.model.Api)2 Ticket (io.gravitee.repository.management.model.Ticket)2 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)2 ApiCriteria (io.gravitee.repository.management.api.search.ApiCriteria)1 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)1 Workflow (io.gravitee.repository.management.model.Workflow)1 PageEntity (io.gravitee.rest.api.model.PageEntity)1 TicketEntity (io.gravitee.rest.api.model.TicketEntity)1 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)1