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