Example usage for org.springframework.data.domain Page getNumberOfElements

List of usage examples for org.springframework.data.domain Page getNumberOfElements

Introduction

In this page you can find the example usage for org.springframework.data.domain Page getNumberOfElements.

Prototype

int getNumberOfElements();

Source Link

Document

Returns the number of elements currently on this Slice .

Usage

From source file:org.jblogcms.core.blog.repository.BlogRepositoryItTest.java

@Test
@Transactional/*from   ww  w.j  a  v a2s .  co m*/
public void testFindAllFavoriteBlogs_returnOneBlog() throws Exception {
    Page<Blog> blogs = blogRepository.findFavoriteBlogs(ACCOUNT_2_ID, pageable);

    Assert.assertEquals(1, blogs.getNumberOfElements());

    Assert.assertEquals(BLOG_2_ID, blogs.getContent().get(0).getId());
    Assert.assertEquals(BLOG_2_NAME, blogs.getContent().get(0).getName());
    Assert.assertEquals(BLOG_2_URL_NAME, blogs.getContent().get(0).getUrlName());
}

From source file:com.luna.common.service.UserServiceTest.java

@Test
public void testFindAllByPageableAndSortDesc() {
    int count = 15;
    User lastUser = null;/*from   w w  w  .j a  v a2  s. co m*/
    for (int i = 0; i < count; i++) {
        lastUser = userService.save(createUser());
    }

    Sort sortDesc = new Sort(Sort.Direction.DESC, "id");
    Pageable pageable = new PageRequest(0, 5, sortDesc);
    Page<User> userPage = userService.findAll(pageable);

    assertEquals(5, userPage.getNumberOfElements());
    assertTrue(userPage.getContent().contains(lastUser));
    assertTrue(userPage.getContent().get(0).getId() > userPage.getContent().get(1).getId());
}

From source file:com.luna.common.service.UserServiceTest.java

@Test
public void testFindAllByPageableAndSortAsc() {
    int count = 15;
    User lastUser = null;//from w ww  .  j  a v a 2s .  c  om
    for (int i = 0; i < count; i++) {
        lastUser = userService.save(createUser());
    }

    Sort sortAsc = new Sort(Sort.Direction.ASC, "id");
    Pageable pageable = new PageRequest(0, 5, sortAsc);
    Page<User> userPage = userService.findAll(pageable);

    assertEquals(5, userPage.getNumberOfElements());
    assertFalse(userPage.getContent().contains(lastUser));
    assertTrue(userPage.getContent().get(0).getId() < userPage.getContent().get(1).getId());
}

From source file:com.luna.common.service.UserServiceTest.java

@Test
public void testFindAllBySearchAndPageableAndSortAsc() {
    int count = 15;
    User lastUser = null;//ww  w  .ja  v  a  2  s  .c  om
    for (int i = 0; i < count; i++) {
        lastUser = userService.save(createUser());
    }

    Sort sortAsc = new Sort(Sort.Direction.ASC, "id");
    Pageable pageable = new PageRequest(0, 5, sortAsc);
    Map<String, Object> searchParams = new HashMap<String, Object>();
    searchParams.put("username_like", "zhang");
    Searchable search = Searchable.newSearchable(searchParams).setPage(pageable);

    Page<User> userPage = userService.findAll(search);
    assertEquals(5, userPage.getNumberOfElements());
    assertFalse(userPage.getContent().contains(lastUser));
    assertTrue(userPage.getContent().get(0).getId() < userPage.getContent().get(1).getId());
}

From source file:org.jblogcms.core.blog.repository.BlogRepositoryItTest.java

@Test
@Transactional//from www  .  j  a v a2 s  . c  o  m
public void testFindFavoriteBlogs_returnTwoBlogs() throws Exception {
    Page<Blog> blogs = blogRepository.findFavoriteBlogs(ACCOUNT_1_ID, pageable);

    Assert.assertEquals(2, blogs.getNumberOfElements());

    Assert.assertEquals(BLOG_1_ID, blogs.getContent().get(0).getId());
    Assert.assertEquals(BLOG_1_NAME, blogs.getContent().get(0).getName());
    Assert.assertEquals(BLOG_1_URL_NAME, blogs.getContent().get(0).getUrlName());

    Assert.assertEquals(BLOG_2_ID, blogs.getContent().get(1).getId());
    Assert.assertEquals(BLOG_2_NAME, blogs.getContent().get(1).getName());
    Assert.assertEquals(BLOG_2_URL_NAME, blogs.getContent().get(1).getUrlName());
}

From source file:de.hska.ld.content.controller.UserContentController.java

@Secured(Core.ROLE_USER)
@RequestMapping(method = RequestMethod.GET, value = "/{userId}/tags")
@Transactional(readOnly = true)/*from w  w w .j ava 2s  .c o m*/
public Callable getTagsPage(@PathVariable Long userId,
        @RequestParam(value = "page-number", defaultValue = "0") Integer pageNumber,
        @RequestParam(value = "page-size", defaultValue = "10") Integer pageSize,
        @RequestParam(value = "sort-direction", defaultValue = "DESC") String sortDirection,
        @RequestParam(value = "sort-property", defaultValue = "id") String sortProperty) {
    return () -> {
        Page<Tag> tagsPage = userContentInfoService.getUserContentTagsPage(userId, pageNumber, pageSize,
                sortDirection, sortProperty);
        if (tagsPage != null && tagsPage.getNumberOfElements() > 0) {
            return new ResponseEntity<>(tagsPage, HttpStatus.OK);
        } else {

            User userAux = userService.findById(userId);
            if (userAux == null) {
                throw new NotFoundException();
            } else {
                //User exists but doesn't have any tag
                return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
            }
        }
    };
}

From source file:org.jblogcms.core.blog.repository.BlogRepositoryItTest.java

@Test
@Transactional//from  ww w  .  j a  v  a2  s.c  o  m
public void testFindAllFavoriteBlogs_returnNoBlogs() throws Exception {
    Page<Blog> blogs = blogRepository.findFavoriteBlogs(ACCOUNT_WITH_NO_FAVORITE_BLOG, pageable);

    Assert.assertEquals(0, blogs.getNumberOfElements());
}

From source file:example.springdata.redis.repositories.PersonRepositoryTests.java

/**
 * Find entities in range defined by {@link Pageable}.
 *///from   www  .j  a v  a2  s.  c  o  m
@Test
public void findByReturingPage() {

    flushTestUsers();

    Page<Person> page1 = repository.findPersonByLastname(eddard.getLastname(), new PageRequest(0, 5));

    assertThat(page1.getNumberOfElements(), is(5));
    assertThat(page1.getTotalElements(), is(6L));

    Page<Person> page2 = repository.findPersonByLastname(eddard.getLastname(), new PageRequest(1, 5));

    assertThat(page2.getNumberOfElements(), is(1));
    assertThat(page2.getTotalElements(), is(6L));
}

From source file:ymanv.forex.web.RateControllerTest.java

@DirtiesContext
@Sql("/sql/insert_data.sql")
@Test/* w  w  w  .ja va2s.co m*/
public void testFindLatestByCriteria() throws Exception {
    // when
    Page<LatestRate> result = controller.findLatestByCriteria(USD, EUR, null, null, null, null);

    // then
    assertThat(result.getTotalElements()).isEqualTo(1);
    assertThat(result.getNumberOfElements()).isEqualTo(1);

    RateEntity r = result.getContent().get(0);

    assertThat(r.getFromcur()).isEqualTo(USD);
    assertThat(r.getTocur()).isEqualTo(EUR);
    assertThat(r.getDate()).hasSameTimeAs(DATE_TIME_WITH_TZ.parse("2015-01-30 13:55:00.0 CET"));
    assertThat(r.getValue()).isEqualByComparingTo("0.88");
}

From source file:de.rahn.finances.services.securities.SecuritiesServiceImplTest.java

/**
 * Test method for {@link SecuritiesServiceImpl#getSecurities(Pageable)}.
 *//*from  w ww  . j  av a  2 s  .  c  o  m*/
@Test
public void testGetSecuritiesPageable() {
    Page<Security> page = classUnderTests.getSecurities(null);

    assertThat(page, notNullValue());
    assertThat(page.getNumberOfElements(), is(1));
    assertThat(page.getContent(), notNullValue());
    assertThat(page.getContent().size(), is(1));
    assertThat(page.getContent(), contains(testSecurity));

    Pageable pageable = page.nextPageable();
    assertThat(pageable, notNullValue());

    page = classUnderTests.getSecurities(pageable);
    assertThat(page, notNullValue());
    assertThat(page.getNumberOfElements(), is(0));
    assertThat(page.getContent(), empty());

    page = classUnderTests.getSecurities(zeroPage);
    assertThat(page, notNullValue());
    assertThat(page.getNumberOfElements(), is(0));
    assertThat(page.getContent(), empty());

    page = classUnderTests.getSecurities(new PageRequest(10, 10));
    assertThat(page, notNullValue());
    assertThat(page.getNumberOfElements(), is(0));
    assertThat(page.getContent(), empty());

    page = classUnderTests.getSecurities(false, stock, null);
    assertThat(page, notNullValue());
    assertThat(page.getNumberOfElements(), is(1));
    assertThat(page.getContent(), notNullValue());
    assertThat(page.getContent().size(), is(1));
    assertThat(page.getContent(), contains(testSecurity));
}