diff --git a/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/CustomPageImpl.java b/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/CustomPageImpl.java index 4531446da7..946447db51 100644 --- a/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/CustomPageImpl.java +++ b/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/CustomPageImpl.java @@ -14,12 +14,13 @@ import com.fasterxml.jackson.databind.JsonNode; @JsonIgnoreProperties(ignoreUnknown = true) public class CustomPageImpl extends PageImpl { - @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - public CustomPageImpl(@JsonProperty("content") List content, @JsonProperty("number") int number, @JsonProperty("size") int size, @JsonProperty("totalElements") Long totalElements, @JsonProperty("pageable") JsonNode pageable, - @JsonProperty("last") boolean last, @JsonProperty("totalPages") int totalPages, @JsonProperty("sort") JsonNode sort, @JsonProperty("numberOfElements") int numberOfElements) { + public CustomPageImpl(@JsonProperty("content") List content, @JsonProperty("number") int number, + @JsonProperty("size") int size, @JsonProperty("totalElements") Long totalElements, + @JsonProperty("pageable") JsonNode pageable, @JsonProperty("last") boolean last, + @JsonProperty("totalPages") int totalPages, @JsonProperty("sort") JsonNode sort, + @JsonProperty("numberOfElements") int numberOfElements) { super(content, PageRequest.of(number, 1), 10); - } public CustomPageImpl(List content, Pageable pageable, long total) { diff --git a/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/EmployeeClient.java b/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/EmployeeClient.java index ebb5a8ef93..56757626e1 100644 --- a/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/EmployeeClient.java +++ b/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/EmployeeClient.java @@ -24,8 +24,9 @@ public class EmployeeClient { .queryParam("page", pageable.getPageNumber()) .queryParam("size", pageable.getPageSize()); - ResponseEntity> responseEntity = restTemplate.exchange(uriBuilder.toUriString(), HttpMethod.GET, null, new ParameterizedTypeReference>() { - }); + ResponseEntity> responseEntity = restTemplate.exchange(uriBuilder.toUriString(), + HttpMethod.GET, null, new ParameterizedTypeReference>() { + }); return responseEntity.getBody(); } diff --git a/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/EmployeeController.java b/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/EmployeeController.java index 1bbb763491..f9f32efb53 100644 --- a/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/EmployeeController.java +++ b/persistence-modules/spring-data-rest-2/src/main/java/com/baeldung/pageentityresponse/EmployeeController.java @@ -30,7 +30,8 @@ public class EmployeeController { } @GetMapping("/data") - public ResponseEntity> getData(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { + public ResponseEntity> getData(@RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "10") int size) { List empList = listImplementation(); int totalSize = empList.size(); diff --git a/persistence-modules/spring-data-rest-2/src/test/java/com/baeldung/pageentityresponse/EmployeeControllerIntegrationTest.java b/persistence-modules/spring-data-rest-2/src/test/java/com/baeldung/pageentityresponse/EmployeeControllerIntegrationTest.java index b2d5b23889..81e994cbee 100644 --- a/persistence-modules/spring-data-rest-2/src/test/java/com/baeldung/pageentityresponse/EmployeeControllerIntegrationTest.java +++ b/persistence-modules/spring-data-rest-2/src/test/java/com/baeldung/pageentityresponse/EmployeeControllerIntegrationTest.java @@ -1,8 +1,10 @@ package com.baeldung.pageentityresponse; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.List; + import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -14,51 +16,47 @@ import org.springframework.data.domain.PageImpl; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; -import static org.junit.jupiter.api.Assertions.assertEquals; - -@SpringBootTest(classes = PageEntityResponseApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(classes = PageEntityResponseApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc public class EmployeeControllerIntegrationTest { - @LocalServerPort - private int port; + @LocalServerPort + private int port; - @Autowired - private TestRestTemplate restTemplate; + @Autowired + private TestRestTemplate restTemplate; -@Test -void givenGetData_whenRestTemplateExchange_thenReturnsPageOfEmployee() { - ResponseEntity> responseEntity = restTemplate.exchange( - "http://localhost:" + port + "/organisation/data", - HttpMethod.GET, - null, - new ParameterizedTypeReference>() {} - ); + @Test + void givenGetData_whenRestTemplateExchange_thenReturnsPageOfEmployee() { + ResponseEntity> responseEntity = restTemplate.exchange( + "http://localhost:" + port + "/organisation/data", HttpMethod.GET, null, + new ParameterizedTypeReference>() { + }); - assertEquals(200, responseEntity.getStatusCodeValue()); - PageImpl restPage = responseEntity.getBody(); - assertNotNull(restPage); + assertEquals(200, responseEntity.getStatusCodeValue()); + PageImpl restPage = responseEntity.getBody(); + assertNotNull(restPage); - assertEquals(10, restPage.getTotalElements()); + assertEquals(10, restPage.getTotalElements()); - List content = restPage.getContent(); - assertNotNull(content); - assertEquals(3, content.size()); + List content = restPage.getContent(); + assertNotNull(content); + assertEquals(3, content.size()); - EmployeeDto employee1 = content.get(0); - assertEquals("Jane", employee1.getName()); - assertEquals("Finance", employee1.getDept()); - assertEquals(50000, employee1.getSalary()); + EmployeeDto employee1 = content.get(0); + assertEquals("Jane", employee1.getName()); + assertEquals("Finance", employee1.getDept()); + assertEquals(50000, employee1.getSalary()); - EmployeeDto employee2 = content.get(1); - assertEquals("Sarah", employee2.getName()); - assertEquals("IT", employee2.getDept()); - assertEquals(70000, employee2.getSalary()); + EmployeeDto employee2 = content.get(1); + assertEquals("Sarah", employee2.getName()); + assertEquals("IT", employee2.getDept()); + assertEquals(70000, employee2.getSalary()); - EmployeeDto employee3 = content.get(2); - assertEquals("John", employee3.getName()); - assertEquals("IT", employee3.getDept()); - assertEquals(90000, employee3.getSalary()); -} + EmployeeDto employee3 = content.get(2); + assertEquals("John", employee3.getName()); + assertEquals("IT", employee3.getDept()); + assertEquals(90000, employee3.getSalary()); + } }