Search in sources :

Example 1 with SortableImpl

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

the class ApisResource method searchApis.

@POST
@Path("_search/_paged")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Search for API using the search engine")
@ApiResponses({ @ApiResponse(code = 200, message = "List accessible APIs for current user", response = ApiListItem.class), @ApiResponse(code = 500, message = "Internal server error") })
public PagedResult<ApiListItem> searchApis(@ApiParam(name = "q", required = true) @NotNull @QueryParam("q") String query, @ApiParam(name = "order") @QueryParam("order") String order, @Valid @BeanParam Pageable pageable) {
    final ApiQuery apiQuery = new ApiQuery();
    Map<String, Object> filters = new HashMap<>();
    Sortable sortable = null;
    if (!StringUtils.isEmpty(order)) {
        final OrderParam orderParam = new OrderParam(order);
        sortable = new SortableImpl(orderParam.getValue().getField(), orderParam.getValue().isOrder());
    }
    io.gravitee.rest.api.model.common.Pageable commonPageable = null;
    if (pageable != null) {
        commonPageable = pageable.toPageable();
    }
    if (!isAdmin()) {
        filters.put("api", apiService.findIdsByUser(getAuthenticatedUser(), apiQuery, false));
    }
    final boolean isRatingServiceEnabled = ratingService.isEnabled();
    final Page<ApiEntity> apis = apiService.search(query, filters, sortable, commonPageable);
    return new PagedResult<>(apis.getContent().stream().map(apiEntity -> this.convert(apiEntity, isRatingServiceEnabled)).collect(toList()), apis.getPageNumber(), (int) apis.getPageElements(), (int) apis.getTotalElements());
}
Also used : SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) OrderParam(io.gravitee.rest.api.management.rest.resource.param.OrderParam) Sortable(io.gravitee.rest.api.model.common.Sortable) io.gravitee.rest.api.model.api(io.gravitee.rest.api.model.api) PagedResult(io.gravitee.rest.api.management.rest.model.PagedResult)

Example 2 with SortableImpl

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

the class PlatformTicketsResourceTest method shouldSearchTickets.

@Test
public void shouldSearchTickets() {
    ArgumentCaptor<TicketQuery> queryCaptor = ArgumentCaptor.forClass(TicketQuery.class);
    ArgumentCaptor<SortableImpl> sortableCaptor = ArgumentCaptor.forClass(SortableImpl.class);
    when(ticketService.search(queryCaptor.capture(), sortableCaptor.capture(), any())).thenReturn(new Page<>(singletonList(fakeTicketEntity), 1, 1, 1));
    Response response = envTarget().queryParam("page", 1).queryParam("size", 10).queryParam("order", "-subject").request().get();
    assertEquals(OK_200, response.getStatus());
    TicketQuery query = queryCaptor.getValue();
    SortableImpl sortable = sortableCaptor.getValue();
    assertEquals("Query user", USER_NAME, query.getFromUser());
    assertEquals("Sort field", "subject", sortable.getField());
    assertEquals("Sort order", false, sortable.isAscOrder());
    verify(ticketService, Mockito.times(1)).search(any(), argThat(o -> o.getField().equals("subject") && !o.isAscOrder()), argThat(o -> o.getPageNumber() == 1 && o.getPageSize() == 10));
}
Also used : Response(javax.ws.rs.core.Response) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) OK_200(io.gravitee.common.http.HttpStatusCode.OK_200) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) NOT_FOUND_404(io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404) Page(io.gravitee.common.data.domain.Page) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collections.singletonList(java.util.Collections.singletonList) Mockito.verify(org.mockito.Mockito.verify) TicketEntity(io.gravitee.rest.api.model.TicketEntity) Mockito(org.mockito.Mockito) TicketNotFoundException(io.gravitee.rest.api.service.exceptions.TicketNotFoundException) ArgumentCaptor(org.mockito.ArgumentCaptor) Response(javax.ws.rs.core.Response) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) TicketQuery(io.gravitee.rest.api.model.api.TicketQuery) Mockito.reset(org.mockito.Mockito.reset) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) TicketQuery(io.gravitee.rest.api.model.api.TicketQuery) Test(org.junit.Test)

Example 3 with SortableImpl

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

the class TicketServiceTest method shouldSearchForTicketsFromUser.

@Test
public void shouldSearchForTicketsFromUser() 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);
    ApiEntity apiEntity = new ApiEntity();
    ApplicationEntity appEntity = new ApplicationEntity();
    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))).thenReturn(new Page<>(ticketList, 0, 20, 20));
    when(apiService.findById(API_ID)).thenReturn(apiEntity);
    when(applicationService.findById(APPLICATION_ID)).thenReturn(appEntity);
    TicketQuery query = new TicketQuery();
    query.setFromUser("fromUser");
    Page<TicketEntity> searchResult = ticketService.search(query, new SortableImpl("subject", true), new PageableImpl(1, Integer.MAX_VALUE));
    assertEquals(searchResult.getContent().size(), 20);
    assertEquals(searchResult.getPageNumber(), 1);
    assertEquals(searchResult.getTotalElements(), 20);
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) Ticket(io.gravitee.repository.management.model.Ticket) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TicketQuery(io.gravitee.rest.api.model.api.TicketQuery) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) Pageable(io.gravitee.repository.management.api.search.Pageable) Sortable(io.gravitee.repository.management.api.search.Sortable) TicketCriteria(io.gravitee.repository.management.api.search.TicketCriteria) Test(org.junit.Test)

Example 4 with SortableImpl

use of io.gravitee.rest.api.model.common.SortableImpl 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 5 with SortableImpl

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

Aggregations

SortableImpl (io.gravitee.rest.api.model.common.SortableImpl)13 TicketQuery (io.gravitee.rest.api.model.api.TicketQuery)6 Test (org.junit.Test)6 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)5 PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)5 Page (io.gravitee.common.data.domain.Page)4 Sortable (io.gravitee.rest.api.model.common.Sortable)4 TicketEntity (io.gravitee.rest.api.model.TicketEntity)3 Collections.singletonList (java.util.Collections.singletonList)3 Response (javax.ws.rs.core.Response)3 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 Api (io.gravitee.repository.management.model.Api)2 Ticket (io.gravitee.repository.management.model.Ticket)2 PagedResult (io.gravitee.rest.api.management.rest.model.PagedResult)2 OrderParam (io.gravitee.rest.api.management.rest.resource.param.OrderParam)2 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)2 PromotionEntity (io.gravitee.rest.api.model.promotion.PromotionEntity)2