BAEL-7283: Convert a page of objects to list in Spring Data (#15347)
This commit is contained in:
		
							parent
							
								
									64b63ab33d
								
							
						
					
					
						commit
						0b8873f03e
					
				| @ -1,5 +1,6 @@ | ||||
| package com.baeldung.spring.data.jpa.paging; | ||||
| 
 | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import org.springframework.data.domain.Page; | ||||
| @ -29,6 +30,13 @@ public class CustomerService { | ||||
|         return new PageImpl<>(pageContent, pageRequest, allCustomers.size()); | ||||
|     } | ||||
| 
 | ||||
|     public List<Customer> getCustomerListFromPage(int page, int size) { | ||||
|         Pageable pageRequest = createPageRequestUsing(page, size); | ||||
|         Page<Customer> allCustomers = customerRepository.findAll(pageRequest); | ||||
| 
 | ||||
|         return allCustomers.hasContent() ? allCustomers.getContent() : Collections.emptyList(); | ||||
|     } | ||||
| 
 | ||||
|     private Pageable createPageRequestUsing(int page, int size) { | ||||
|         return PageRequest.of(page, size); | ||||
|     } | ||||
|  | ||||
| @ -1,14 +1,17 @@ | ||||
| package com.baeldung.spring.data.jpa.paging; | ||||
| 
 | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| import static org.mockito.ArgumentMatchers.any; | ||||
| import static org.mockito.Mockito.when; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.Collection; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.stream.Collectors; | ||||
| 
 | ||||
| import org.junit.jupiter.api.BeforeEach; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.junit.jupiter.api.extension.ExtendWith; | ||||
| import org.junit.jupiter.params.ParameterizedTest; | ||||
| import org.junit.jupiter.params.provider.MethodSource; | ||||
| @ -16,6 +19,8 @@ import org.mockito.InjectMocks; | ||||
| import org.mockito.Mock; | ||||
| import org.mockito.junit.jupiter.MockitoExtension; | ||||
| import org.springframework.data.domain.Page; | ||||
| import org.springframework.data.domain.PageImpl; | ||||
| import org.springframework.data.domain.Pageable; | ||||
| 
 | ||||
| @ExtendWith(MockitoExtension.class) | ||||
| public class CustomerServiceUnitTest { | ||||
| @ -46,12 +51,7 @@ public class CustomerServiceUnitTest { | ||||
| 
 | ||||
|     private static final List<String> PAGE_4_CONTENTS = Arrays.asList("Penny", "Queen", "Rob", "Sue", "Tammy"); | ||||
| 
 | ||||
|     private static final List<String> EMPTY_PAGE = Arrays.asList(); | ||||
| 
 | ||||
|     @BeforeEach | ||||
|     void setup() { | ||||
|         when(customerRepository.findAll()).thenReturn(ALL_CUSTOMERS); | ||||
|     } | ||||
|     private static final List<String> EMPTY_PAGE = Collections.emptyList(); | ||||
| 
 | ||||
|     private static Collection<Object[]> testIO() { | ||||
|         return Arrays.asList(new Object[][] { | ||||
| @ -66,6 +66,7 @@ public class CustomerServiceUnitTest { | ||||
|     @ParameterizedTest | ||||
|     @MethodSource("testIO") | ||||
|     void givenAListOfCustomers_whenGetCustomers_thenReturnsDesiredDataAlongWithPagingInformation(int page, int size, List<String> expectedNames, long expectedTotalElements, long expectedTotalPages) { | ||||
|         when(customerRepository.findAll()).thenReturn(ALL_CUSTOMERS); | ||||
|         Page<Customer> customers = customerService.getCustomers(page, size); | ||||
|         List<String> names = customers.getContent() | ||||
|           .stream() | ||||
| @ -77,4 +78,27 @@ public class CustomerServiceUnitTest { | ||||
|         assertEquals(expectedTotalElements, customers.getTotalElements()); | ||||
|         assertEquals(expectedTotalPages, customers.getTotalPages()); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     void givenAPageOfCustomers_whenGetCustomerList_thenReturnsList() { | ||||
|         Page<Customer> pagedResponse = new PageImpl<Customer>(ALL_CUSTOMERS.subList(0, 5)); | ||||
|         when(customerRepository.findAll(any(Pageable.class))).thenReturn(pagedResponse); | ||||
| 
 | ||||
|         List<Customer> customers = customerService.getCustomerListFromPage(0, 5); | ||||
|         List<String> customerNames = customers.stream() | ||||
|           .map(Customer::getName) | ||||
|           .collect(Collectors.toList()); | ||||
| 
 | ||||
|         assertEquals(PAGE_1_CONTENTS.size(), customers.size()); | ||||
|         assertEquals(PAGE_1_CONTENTS, customerNames); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     void givenAnEmptyPageOfCustomers_whenGetCustomerList_thenReturnsEmptyList() { | ||||
|         Page<Customer> emptyPage = Page.empty(); | ||||
|         when(customerRepository.findAll(any(Pageable.class))).thenReturn(emptyPage); | ||||
|         List<Customer> customers = customerService.getCustomerListFromPage(0, 5); | ||||
| 
 | ||||
|         assertThat(customers).isEmpty(); | ||||
|     } | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user