@Test @Transactional @Rollback(value = true) @DirtiesContext(hierarchyMode = HierarchyMode.CURRENT_LEVEL) public void test_Pageable_GetAllRecentUpdate() throws Exception { final int count = 10; Map<NEntityReference, Persistable<Long>> persistableMap = Maps.newHashMap(); for (int it = 0; it < count; it++) { final Persistable<Long> persistable = this.getSpiedPersistableFromSupported(it); Mockito.when(persistable.getId()).thenReturn((long) (it + 1)); final NEntityReference ref = (NEntityReference) this.entityReferenceHelper.toReference(persistable); this.recentlyUpdatedService.newRecentUpdate(persistable); persistableMap.put(ref, persistable); } final EntityReferenceHelper spy = Mockito.spy(this.entityReferenceHelper); for (final NEntityReference ref : persistableMap.keySet()) { Mockito.when(spy.fromReference(ref)).thenReturn(persistableMap.get(ref)); } ReflectionTestUtils.setField(this.recentlyUpdatedService, "entityReferenceHelper", spy); final Page<RecentUpdateBean> recentlyUpdated = this.recentlyUpdatedService.getRecentlyUpdated(new PageRequest(0, 50)); Assert.assertNotNull(recentlyUpdated); Assert.assertTrue(recentlyUpdated.getNumberOfElements() > 0); Assert.assertEquals(count, recentlyUpdated.getNumberOfElements()); }
@Test public void testPagination() { Pageable pageable = new PageRequest(0, 2); Page<ProductBean> page1 = repo.findByNameStartingWith("name", pageable); Assert.assertEquals(pageable.getPageSize(), page1.getNumberOfElements()); Assert.assertTrue(page1.hasNextPage()); Assert.assertEquals(3, page1.getTotalElements()); pageable = new PageRequest(1, 2); Page<ProductBean> page2 = repo.findByNameStartingWith("name", pageable); Assert.assertEquals(1, page2.getNumberOfElements()); Assert.assertFalse(page2.hasNextPage()); Assert.assertEquals(3, page2.getTotalElements()); }
@Test public void testPaginationNoElementsFound() { Pageable pageable = new PageRequest(0, 2); Page<ProductBean> page = repo.findByNameStartingWith("hpotsirhc", pageable); Assert.assertEquals(0, page.getNumberOfElements()); Assert.assertTrue(page.getContent().isEmpty()); }
/** * Add all bean properties from the supplied bean to the representation * * @param value * @return */ public ResponseEntityBuilder<ReadableRepresentation> withPage( Page<?> value, String uriTemplate, String... includeFields) { String[] fields = requestedFields == null ? includeFields : requestedFields; // Extract page data such as size, page number representation.withProperty("size", value.getSize()); representation.withProperty("number", value.getNumber()); representation.withProperty("numberOfElements", value.getNumberOfElements()); representation.withProperty("totalElements", value.getTotalElements()); // Next/back links if (value.hasNextPage()) { buildNextLink(representation, request); } if (value.hasPreviousPage()) { buildPreviousLink(representation, request); } // Build the content of the page for (Object object : value.getContent()) { Representation content = converter.convert(object, new UriTemplate(uriTemplate), fields); this.representation.withRepresentation("content", content); } return this; }
@Test @DirtiesContext(hierarchyMode = HierarchyMode.CURRENT_LEVEL) public void test_Pageable_GetAllRecentUpdates_NoUpdates() throws Exception { final Page<RecentUpdateBean> recentlyUpdated = this.recentlyUpdatedService.getRecentlyUpdated(new PageRequest(0, 10)); Assert.assertNotNull(recentlyUpdated); Assert.assertTrue(recentlyUpdated.getNumberOfElements() == 0); }
@Test public void getPublicActivity() { when(actionRepository.findAll(actionPageable)).thenReturn(actionResultsPage); Page<UserActivityDTO> methodResults = communityServiceImpl.getPublicActivity(actionPageable); assertEquals(actionList.size(), methodResults.getTotalElements()); assertEquals(countPublicActivitiesIn(actionList), methodResults.getNumberOfElements()); verify(actionRepository, times(1)).findAll(actionPageable); }
@Test public void testPaging() { Page<Institusi> hasilQuery = institusiDao.findAll(new PageRequest(0, 10)); Assert.assertNotNull(hasilQuery); Assert.assertEquals(1, hasilQuery.getTotalPages()); Assert.assertEquals(2, hasilQuery.getTotalElements()); Assert.assertEquals(2, hasilQuery.getNumberOfElements()); Assert.assertEquals(0, institusiDao.findAll(new PageRequest(1, 10)).getNumberOfElements()); }
@Test public void testFacetQuery() { FacetPage<Product> facetPage = repo.findProductCategoryFacets(new PageRequest(0, 100)); Assert.assertEquals(repo.findAllProducts().size(), facetPage.getNumberOfElements()); Page<FacetFieldEntry> page = facetPage.getFacetResultPage(SolrProductField.CATEGORY); Assert.assertEquals(INITIAL_CATEGORY_COUNT, page.getNumberOfElements()); for (FacetFieldEntry entry : page) { Assert.assertEquals(SolrProductField.CATEGORY.getName(), entry.getField().getName()); Assert.assertEquals(repo.findByCategory(entry.getValue()).size(), entry.getValueCount()); } }
private WebPage(Page<T> page) { number = page.getNumber(); size = page.getSize(); totalPages = page.getTotalPages(); numberOfElements = page.getNumberOfElements(); totalElements = page.getTotalElements(); hasPreviousPage = page.hasPreviousPage(); isFirstPage = page.isFirstPage(); hasNextPage = page.hasNextPage(); isLastPage = page.isLastPage(); content = page.getContent(); hasContent = page.hasContent(); sort = new WebSort(page.getSort()); }
@Test public void shouldSearchDocumentsGivenElasticsearchQuery() { // given String documentId = randomNumeric(5); SampleEntity sampleEntity = new SampleEntity(); sampleEntity.setId(documentId); sampleEntity.setMessage("hello world."); sampleEntity.setVersion(System.currentTimeMillis()); repository.save(sampleEntity); // when Page<SampleEntity> page = repository.search(termQuery("message", "world"), new PageRequest(0, 50)); // then assertThat(page, is(notNullValue())); assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1))); }
@Transactional(readOnly = true) @Override public Page<TodoDTO> findBySearchTerm(String searchTerm, Pageable pageRequest) { LOGGER.info( "Finding todo entries by search term: {} and page request: {}", searchTerm, pageRequest); Page<Todo> searchResultPage = repository.findAll(titleOrDescriptionContainsIgnoreCase(searchTerm), pageRequest); LOGGER.info( "Found {} todo entries. Returned page {} contains {} todo entries", searchResultPage.getTotalElements(), searchResultPage.getNumber(), searchResultPage.getNumberOfElements()); return TodoMapper.mapEntityPageIntoDTOPage(pageRequest, searchResultPage); }
@Test public void shouldSearchDocumentsGivenSearchQuery() { // given String documentId = randomNumeric(5); SampleEntity sampleEntity = new SampleEntity(); sampleEntity.setId(documentId); sampleEntity.setMessage("some test message"); sampleEntity.setVersion(System.currentTimeMillis()); repository.save(sampleEntity); SearchQuery query = new NativeSearchQueryBuilder().withQuery(termQuery("message", "test")).build(); // when Page<SampleEntity> page = repository.search(query); // then assertThat(page, is(notNullValue())); assertThat(page.getNumberOfElements(), is(greaterThanOrEqualTo(1))); }
public PageResource(Page<T> page, String pageParam, String sizeParam) { super(); number = page.getNumber(); size = page.getSize(); numberOfElements = page.getNumberOfElements(); content = page.getContent(); sort = page.getSort(); firstPage = page.isFirst(); lastPage = page.isLast(); totalPages = page.getTotalPages(); totalElements = page.getTotalElements(); addPreviousLink(page, pageParam, sizeParam); addNextLink(page, pageParam, sizeParam); addFirstLink(page, pageParam, sizeParam); addLastLink(page, pageParam, sizeParam); addSelfLink(page, pageParam, sizeParam); }
@Test public void getPlaylists() { mockUnauthorizedServer .expect(requestTo("https://api.soundcloud.com/users/3510549?client_id=someApiKey")) .andExpect(method(GET)) .andRespond(withResponse(jsonResource("testdata/userprofile"), responseHeaders)); mockUnauthorizedServer .expect( requestTo("https://api.soundcloud.com/users/3510549/playlists?client_id=someApiKey")) .andExpect(method(GET)) .andRespond(withResponse(jsonResource("testdata/playlists"), responseHeaders)); Page<Playlist> playlists = unauthorizedSoundCloud.usersOperations().userOperations(3510549).getPlaylists(); assertEquals(1, playlists.getNumberOfElements()); Playlist playlist = playlists.getContent().get(0); assertPlaylistData(playlist); }