[BAEL-6105] code for list to page article

This commit is contained in:
uzma 2023-05-05 14:52:08 +01:00
parent 555662907a
commit e9ad048d8d
3 changed files with 13 additions and 36 deletions

View File

@ -15,7 +15,7 @@ public class CustomerService {
this.customerRepository = customerRepository; this.customerRepository = customerRepository;
} }
private final CustomerRepository customerRepository; private final CustomerRepository customerRepository;
public Page<Customer> getCustomers(int page, int size) { public Page<Customer> getCustomers(int page, int size) {
@ -25,13 +25,11 @@ public class CustomerService {
int start = (int) pageRequest.getOffset(); int start = (int) pageRequest.getOffset();
int end = Math.min((start + pageRequest.getPageSize()), allCustomers.size()); int end = Math.min((start + pageRequest.getPageSize()), allCustomers.size());
List<Customer> pageContent = allCustomers.subList(start, end);
return new PageImpl<>(allCustomers.subList(start, end), pageRequest, allCustomers.size()); return new PageImpl<>(pageContent, pageRequest, allCustomers.size());
} }
private Pageable createPageRequestUsing(int page, int size) { private Pageable createPageRequestUsing(int page, int size) {
return PageRequest.of(page, size); return PageRequest.of(page, size);
} }
} }

View File

@ -9,7 +9,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import org.assertj.core.api.Assertions;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -23,7 +22,6 @@ import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
@WebMvcTest(CustomerRestController.class) @WebMvcTest(CustomerRestController.class)
public class CustomerControllerTest { public class CustomerControllerTest {
@ -62,15 +60,10 @@ public class CustomerControllerTest {
MockHttpServletResponse response = result.getResponse(); MockHttpServletResponse response = result.getResponse();
JSONObject jsonObject = new JSONObject(response.getContentAsString()); JSONObject jsonObject = new JSONObject(response.getContentAsString());
assertThat(jsonObject.get("totalPages")) assertThat(jsonObject.get("totalPages")).isEqualTo(4);
.isEqualTo(4); assertThat(jsonObject.get("totalElements")).isEqualTo(20);
assertThat(jsonObject.get("totalElements")) assertThat(jsonObject.get("number")).isEqualTo(1);
.isEqualTo(20); assertThat(jsonObject.get("size")).isEqualTo(5);
assertThat(jsonObject.get("number")) assertThat(jsonObject.get("content")).isNotNull();
.isEqualTo(1);
assertThat(jsonObject.get("size"))
.isEqualTo(5);
assertThat(jsonObject.get("content"))
.isNotNull();
} }
} }

View File

@ -8,7 +8,6 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
@ -27,16 +26,9 @@ public class CustomerServiceTest {
@InjectMocks @InjectMocks
private CustomerService customerService; private CustomerService customerService;
private static final List<Customer> ALL_CUSTOMERS = Arrays.asList( private static final List<Customer> ALL_CUSTOMERS = Arrays.asList(new Customer("Ali"), new Customer("Brian"), new Customer("Coddy"), new Customer("Di"), new Customer("Eve"), new Customer("Fin"), new Customer("Grace"), new Customer("Harry"),
new Customer("Ali"), new Customer("Brian"), new Customer("Coddy"), new Customer("Ivan"), new Customer("Judy"), new Customer("Kasim"), new Customer("Liam"), new Customer("Mike"), new Customer("Nick"), new Customer("Omar"), new Customer("Penny"), new Customer("Queen"), new Customer("Rob"), new Customer("Sue"),
new Customer("Di"), new Customer("Eve"), new Customer("Fin"), new Customer("Tammy"));
new Customer("Grace"), new Customer("Harry"),
new Customer("Ivan"), new Customer("Judy"),
new Customer("Kasim"), new Customer("Liam"),
new Customer("Mike"),new Customer("Nick"),
new Customer("Omar"), new Customer("Penny"),
new Customer("Queen"),new Customer("Rob"),
new Customer("Sue"),new Customer("Tammy"));
private static final List<String> PAGE_1_CONTENTS = Arrays.asList("Ali", "Brian", "Coddy", "Di", "Eve"); private static final List<String> PAGE_1_CONTENTS = Arrays.asList("Ali", "Brian", "Coddy", "Di", "Eve");
@ -47,20 +39,14 @@ public class CustomerServiceTest {
private static final List<String> PAGE_4_CONTENTS = Arrays.asList("Penny", "Queen", "Rob", "Sue", "Tammy"); private static final List<String> PAGE_4_CONTENTS = Arrays.asList("Penny", "Queen", "Rob", "Sue", "Tammy");
private static final List<String> EMPTY_PAGE = Arrays.asList(); private static final List<String> EMPTY_PAGE = Arrays.asList();
@BeforeEach @BeforeEach
void setup() { void setup() {
when(customerRepository.findAll()).thenReturn(ALL_CUSTOMERS); when(customerRepository.findAll()).thenReturn(ALL_CUSTOMERS);
} }
private static Collection<Object[]> testIO() { private static Collection<Object[]> testIO() {
return Arrays.asList( return Arrays.asList(new Object[][] { { 0, 5, PAGE_1_CONTENTS, 20L, 4L }, { 1, 5, PAGE_2_CONTENTS, 20L, 4L }, { 2, 5, PAGE_3_CONTENTS, 20L, 4L }, { 3, 5, PAGE_4_CONTENTS, 20L, 4L }, { 4, 5, EMPTY_PAGE, 20L, 4L } });
new Object[][] {
{ 0, 5, PAGE_1_CONTENTS, 20L, 4L },
{ 1, 5, PAGE_2_CONTENTS, 20L, 4L },
{ 2, 5, PAGE_3_CONTENTS, 20L, 4L },
{ 3, 5, PAGE_4_CONTENTS, 20L, 4L },
{ 4, 5, EMPTY_PAGE, 20L, 4L } }
);
} }
@ParameterizedTest @ParameterizedTest