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