Example usage for org.springframework.data.domain PageRequest getPageSize

List of usage examples for org.springframework.data.domain PageRequest getPageSize

Introduction

In this page you can find the example usage for org.springframework.data.domain PageRequest getPageSize.

Prototype

public int getPageSize() 

Source Link

Usage

From source file:com.luna.common.repository.PageAndSortUserRepositoryIT.java

@Test
public void testFindAllForPage() {
    for (int i = 0; i < 15; i++) {
        userRepository.save(createUser());
    }//from  w  w  w . j  a  v a  2s . co  m
    PageRequest pageRequest = new PageRequest(1, 5);
    Page<User> page = userRepository.findAll(pageRequest);

    assertEquals(pageRequest.getPageSize(), page.getNumberOfElements());
    assertEquals(3, page.getTotalPages());
}

From source file:com.luna.common.repository.PageAndSortUserRepositoryIT.java

@Test
public void testFindAllForPageAndSort() {
    for (int i = 0; i < 15; i++) {
        userRepository.save(createUser());
    }/*from   w  w  w  . j a va2s.co  m*/

    Sort.Order idAsc = new Sort.Order(Sort.Direction.ASC, "id");
    Sort.Order usernameDesc = new Sort.Order(Sort.Direction.DESC, "username");
    Sort sort = new Sort(idAsc, usernameDesc);

    PageRequest pageRequest = new PageRequest(1, 5, sort);
    Page<User> page = userRepository.findAll(pageRequest);

    assertEquals(pageRequest.getPageSize(), page.getNumberOfElements());
    assertEquals(3, page.getTotalPages());

    assertTrue(page.getContent().get(0).getId() < page.getContent().get(1).getId());

}

From source file:com.ushahidi.swiftriver.core.api.service.DropIndexServiceTest.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test/*from ww  w. j  ava 2  s .  c  o  m*/
public void findDrops() {
    DropDocument dropDocument = new DropDocument();
    dropDocument.setId("1");
    dropDocument.setChannel("channel");
    dropDocument.setTitle("title");
    dropDocument.setContent("content");

    List<DropDocument> dropDocuments = new ArrayList<DropDocument>();
    dropDocuments.add(dropDocument);

    when(mockRepository.find(anyString(), any(Pageable.class))).thenReturn(dropDocuments);
    dropIndexService.findDrops("keyword", 30, 6);

    // First, we verify that a search is performed against the index
    ArgumentCaptor<PageRequest> pageRequestArgument = ArgumentCaptor.forClass(PageRequest.class);
    ArgumentCaptor<String> searchTermArgument = ArgumentCaptor.forClass(String.class);

    verify(mockRepository).find(searchTermArgument.capture(), pageRequestArgument.capture());

    // Assert that "keyword" is the value used for the search
    String searchTerm = searchTermArgument.getValue();
    assertEquals("keyword", searchTerm);

    // Validate the Pageable instance passed to DropDocumentRepository
    PageRequest pageRequest = pageRequestArgument.getValue();
    assertEquals(5, pageRequest.getPageNumber());
    assertEquals(30, pageRequest.getPageSize());

    // Then we verify that the drops & their metadata are retrieved from the DB 
    ArgumentCaptor<List> dropIdArgument = ArgumentCaptor.forClass(List.class);
    verify(mockDropDao).findAll(dropIdArgument.capture());
}

From source file:com.luna.common.repository.PageAndSortUserRepositoryIT.java

@Test
public void testFindByBaseInfoSexForPage() {
    for (int i = 0; i < 15; i++) {
        userRepository.save(createUser());
    }//ww w .j a v a  2  s.  c om
    PageRequest pageRequest = new PageRequest(1, 5);
    Page<User> page = userRepository.findByBaseInfoSex(Sex.male, pageRequest);

    assertEquals(pageRequest.getPageSize(), page.getNumberOfElements());
    assertEquals(3, page.getTotalPages());

    page = userRepository.findByBaseInfoSex(Sex.female, pageRequest);

    assertEquals(0, page.getNumberOfElements());
    assertEquals(0, page.getTotalPages());
}

From source file:org.duracloud.snapshot.rest.SnapshotResourceTest.java

@Test
public void testGetSnapshotContent() {
    String snapshotId = "snapshot-id";
    String prefix = "prefix";
    int page = 1;
    int pageSize = 5;
    String metaName = "metadata-name";
    String metaValue = "metadata-value";
    Long count = 1000l;//from  w  w  w  . j a v  a 2s .  c om

    Capture<PageRequest> pageRequestCapture = new Capture<>();

    SnapshotContentItem item = new SnapshotContentItem();
    item.setContentId("test");
    item.setMetadata("{\"" + metaName + "\" : \"" + metaValue + "\"}");

    List<SnapshotContentItem> contentIds = Arrays.asList(new SnapshotContentItem[] { item });
    EasyMock.expect(snapshotContentItemRepo.findBySnapshotNameAndContentIdStartingWithOrderByContentIdAsc(
            EasyMock.eq(snapshotId), EasyMock.eq(prefix), EasyMock.capture(pageRequestCapture)))
            .andReturn(contentIds);

    EasyMock.expect(snapshotContentItemRepo.countBySnapshotName(EasyMock.eq(snapshotId))).andReturn(count);

    replayAll();

    Response response = resource.getContent(snapshotId, page, pageSize, prefix);
    GetSnapshotContentBridgeResult result = (GetSnapshotContentBridgeResult) response.getEntity();

    PageRequest pageRequest = pageRequestCapture.getValue();
    Assert.assertEquals(page, pageRequest.getPageNumber());
    Assert.assertEquals(pageSize, pageRequest.getPageSize());

    org.duracloud.snapshot.dto.SnapshotContentItem resultItem = result.getContentItems().get(0);
    Assert.assertEquals("test", resultItem.getContentId());
    Assert.assertEquals(metaValue, resultItem.getContentProperties().get(metaName));
    Assert.assertEquals(count, result.getTotalCount());

}

From source file:com.alliander.osgp.adapter.ws.core.application.services.DeviceManagementService.java

@Transactional(value = "transactionManager")
public Page<Event> findEvents(@Identification final String organisationIdentification,
        final String deviceIdentification, final Integer pageSize, final Integer pageNumber,
        final DateTime from, final DateTime until, final List<EventType> eventTypes)
        throws FunctionalException {

    LOGGER.debug("findEvents called for organisation {} and device {}", organisationIdentification,
            deviceIdentification);//from   www. j a v  a 2 s .c  om

    final Organisation organisation = this.domainHelperService.findOrganisation(organisationIdentification);

    this.pagingSettings.updatePagingSettings(pageSize, pageNumber);

    final PageRequest request = new PageRequest(this.pagingSettings.getPageNumber(),
            this.pagingSettings.getPageSize(), Sort.Direction.DESC, "dateTime");

    Specifications<Event> specifications = null;

    try {
        if (deviceIdentification != null && !deviceIdentification.isEmpty()) {
            final Device device = this.domainHelperService.findDevice(deviceIdentification);
            this.domainHelperService.isAllowed(organisation, device, DeviceFunction.GET_EVENT_NOTIFICATIONS);

            specifications = where(this.eventSpecifications.isFromDevice(device));
        } else {
            specifications = where(this.eventSpecifications.isAuthorized(organisation));
        }

        if (from != null) {
            specifications = specifications.and(this.eventSpecifications.isCreatedAfter(from.toDate()));
        }

        if (until != null) {
            specifications = specifications.and(this.eventSpecifications.isCreatedBefore(until.toDate()));
        }

        if (eventTypes != null && !eventTypes.isEmpty()) {
            specifications = specifications.and(this.eventSpecifications.hasEventTypes(eventTypes));
        }
    } catch (final ArgumentNullOrEmptyException e) {
        throw new FunctionalException(FunctionalExceptionType.ARGUMENT_NULL, ComponentType.WS_CORE, e);
    }

    LOGGER.debug("request offset     : {}", request.getOffset());
    LOGGER.debug("        pageNumber : {}", request.getPageNumber());
    LOGGER.debug("        pageSize   : {}", request.getPageSize());
    LOGGER.debug("        sort       : {}", request.getSort());

    return this.eventRepository.findAll(specifications, request);
}

From source file:org.apache.shiro.web.session.mgt.OnlineWebSessionManager.java

/**
 * ?session? session/*from  ww w  .jav a 2  s. c o m*/
 */
@Override
public void validateSessions() {
    if (log.isInfoEnabled()) {
        log.info("invalidation sessions...");
    }

    int invalidCount = 0;

    int timeout = (int) getGlobalSessionTimeout();
    Date expiredDate = DateUtils.addMilliseconds(new Date(), 0 - timeout);
    PageRequest pageRequest = new PageRequest(0, 100);
    Page<UserOnline> page = userOnlineService.findExpiredUserOnlineList(expiredDate, pageRequest);

    //??
    while (page.hasContent()) {
        List<String> needOfflineIdList = Lists.newArrayList();
        for (UserOnline userOnline : page.getContent()) {
            try {
                SessionKey key = new DefaultSessionKey(userOnline.getId());
                Session session = retrieveSession(key);
                //cache db
                if (session != null) {
                    session.setAttribute(ShiroConstants.ONLY_CLEAR_CACHE, true);
                }
                validate(session, key);
            } catch (InvalidSessionException e) {
                if (log.isDebugEnabled()) {
                    boolean expired = (e instanceof ExpiredSessionException);
                    String msg = "Invalidated session with id [" + userOnline.getId() + "]"
                            + (expired ? " (expired)" : " (stopped)");
                    log.debug(msg);
                }
                invalidCount++;
                needOfflineIdList.add(userOnline.getId());
            }

        }
        if (needOfflineIdList.size() > 0) {
            try {
                userOnlineService.batchOffline(needOfflineIdList);
            } catch (Exception e) {
                log.error("batch delete db session error.", e);
            }
        }
        pageRequest = new PageRequest(0, pageRequest.getPageSize());
        page = userOnlineService.findExpiredUserOnlineList(expiredDate, pageRequest);
    }

    if (log.isInfoEnabled()) {
        String msg = "Finished invalidation session.";
        if (invalidCount > 0) {
            msg += "  [" + invalidCount + "] sessions were stopped.";
        } else {
            msg += "  No sessions were stopped.";
        }
        log.info(msg);
    }

}

From source file:org.lazulite.boot.autoconfigure.osaam.shiro.web.session.mgt.OnlineWebSessionManager.java

/**
 * ?session? session/*w w w.  j  a  v  a2  s. co  m*/
 */
@Override
public void validateSessions() {
    if (log.isInfoEnabled()) {
        log.info("invalidation sessions...");
    }

    int invalidCount = 0;

    int timeout = (int) getGlobalSessionTimeout();
    Date expiredDate = DateUtils.addMilliseconds(new Date(), 0 - timeout);
    PageRequest pageRequest = new PageRequest(0, 100);
    Page<UserOnline> page = userOnlineService.findExpiredUserOnlineList(expiredDate, pageRequest);

    //??
    while (page.hasContent()) {
        List<Long> needOfflineIdList = Lists.newArrayList();
        for (UserOnline userOnline : page.getContent()) {
            try {
                SessionKey key = new DefaultSessionKey(userOnline.getId());
                Session session = retrieveSession(key);
                //cache db
                if (session != null) {
                    session.setAttribute(ShiroConstants.ONLY_CLEAR_CACHE, true);
                }
                validate(session, key);
            } catch (InvalidSessionException e) {
                if (log.isDebugEnabled()) {
                    boolean expired = (e instanceof ExpiredSessionException);
                    String msg = "Invalidated session with id [" + userOnline.getId() + "]"
                            + (expired ? " (expired)" : " (stopped)");
                    log.debug(msg);
                }
                invalidCount++;
                needOfflineIdList.add(userOnline.getId());
            }

        }
        if (needOfflineIdList.size() > 0) {
            try {
                userOnlineService.batchOffline(needOfflineIdList);
            } catch (Exception e) {
                log.error("batch delete db session error.", e);
            }
        }
        pageRequest = new PageRequest(0, pageRequest.getPageSize());
        page = userOnlineService.findExpiredUserOnlineList(expiredDate, pageRequest);
    }

    if (log.isInfoEnabled()) {
        String msg = "Finished invalidation session.";
        if (invalidCount > 0) {
            msg += "  [" + invalidCount + "] sessions were stopped.";
        } else {
            msg += "  No sessions were stopped.";
        }
        log.info(msg);
    }

}