From ca46b46029b9de514ff10ac4a299aa8127297ccd Mon Sep 17 00:00:00 2001 From: djmil Date: Sat, 22 Jul 2023 15:04:56 +0200 Subject: [PATCH] Pageble GET with defaults --- .../djmil/cashcard/CashCardController.java | 24 +++++++- .../djmil/cashcard/CashCardRepository.java | 7 ++- .../java/djmil/cashcard/CashCardJsonTest.java | 32 +++++++++++ .../cashcard/CashcardApplicationTests.java | 57 +++++++++++++++++++ src/test/resources/data.sql | 4 +- src/test/resources/djmil/cashcard/list.json | 5 ++ 6 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/djmil/cashcard/list.json diff --git a/src/main/java/djmil/cashcard/CashCardController.java b/src/main/java/djmil/cashcard/CashCardController.java index c443639..011f697 100644 --- a/src/main/java/djmil/cashcard/CashCardController.java +++ b/src/main/java/djmil/cashcard/CashCardController.java @@ -1,5 +1,9 @@ package djmil.cashcard; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -10,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.util.UriComponentsBuilder; import java.net.URI; +import java.util.List; import java.util.Optional; @RestController @@ -33,10 +38,27 @@ public class CashCardController { } } + // @GetMapping() + // public ResponseEntity> findAll() { + // return ResponseEntity.ok(cashCardRepository.findAll()); + // } + + @GetMapping + public ResponseEntity> findAll(Pageable pageable) { + Page page = cashCardRepository.findAll( + PageRequest.of( + pageable.getPageNumber(), + pageable.getPageSize(), + //pageable.getSort() + pageable.getSortOr(Sort.by(Sort.Direction.ASC, "amount")) + )); + return ResponseEntity.ok(page.getContent()); + } + @PostMapping private ResponseEntity createCashCard(@RequestBody CashCard newCashCardRequest, UriComponentsBuilder ucb) { CashCard savedCashCard = cashCardRepository.save(newCashCardRequest); // CRUD - Create - + URI locationOfNewCashCard = ucb .path("cashcards/{id}") .buildAndExpand(savedCashCard.id()) diff --git a/src/main/java/djmil/cashcard/CashCardRepository.java b/src/main/java/djmil/cashcard/CashCardRepository.java index 23795e1..9818157 100644 --- a/src/main/java/djmil/cashcard/CashCardRepository.java +++ b/src/main/java/djmil/cashcard/CashCardRepository.java @@ -1,6 +1,11 @@ package djmil.cashcard; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; -public interface CashCardRepository extends CrudRepository { +public interface + CashCardRepository +extends + CrudRepository, + PagingAndSortingRepository { } diff --git a/src/test/java/djmil/cashcard/CashCardJsonTest.java b/src/test/java/djmil/cashcard/CashCardJsonTest.java index df620f2..51c7978 100644 --- a/src/test/java/djmil/cashcard/CashCardJsonTest.java +++ b/src/test/java/djmil/cashcard/CashCardJsonTest.java @@ -1,5 +1,7 @@ package djmil.cashcard; +import org.assertj.core.util.Arrays; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.json.JsonTest; @@ -15,6 +17,19 @@ public class CashCardJsonTest { @Autowired private JacksonTester json; + @Autowired + private JacksonTester jsonList; + + private CashCard[] cashCards; + + @BeforeEach + void setUp() { + cashCards = Arrays.array( + new CashCard(99L, 123.45), + new CashCard(100L, 1.00), + new CashCard(101L, 150.00)); + } + @Test public void myFirstTest() { assertThat(1).isEqualTo(1); @@ -49,4 +64,21 @@ public class CashCardJsonTest { assertThat(json.parseObject(expected).id()).isEqualTo(1000); assertThat(json.parseObject(expected).amount()).isEqualTo(67.89); } + + @Test + void cashCardListSerializationTest() throws IOException { + assertThat(jsonList.write(cashCards)).isStrictlyEqualToJson("list.json"); + } + + @Test + void cashCardListDeserializationTest() throws IOException { + String expected=""" + [ + { "id": 99, "amount": 123.45 }, + { "id": 100, "amount": 1.00 }, + { "id": 101, "amount": 150.00 } + ] + """; + assertThat(jsonList.parse(expected)).isEqualTo(cashCards); + } } diff --git a/src/test/java/djmil/cashcard/CashcardApplicationTests.java b/src/test/java/djmil/cashcard/CashcardApplicationTests.java index e4ebcdf..79dc2c1 100644 --- a/src/test/java/djmil/cashcard/CashcardApplicationTests.java +++ b/src/test/java/djmil/cashcard/CashcardApplicationTests.java @@ -7,15 +7,19 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.test.annotation.DirtiesContext; import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.JsonPath; +import net.minidev.json.JSONArray; + import java.net.URI; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +//@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) class CashcardApplicationTests { @Test @@ -49,6 +53,7 @@ class CashcardApplicationTests { } @Test + @DirtiesContext void shouldCreateANewCashCard() { CashCard newCashCard = new CashCard(null, 250.00); ResponseEntity createResponse = restTemplate.postForEntity("/cashcards", newCashCard, Void.class ); @@ -67,4 +72,56 @@ class CashcardApplicationTests { assertThat(id).isNotNull(); assertThat(amount).isEqualTo(250.00); } + + @Test + void shouldReturnAllCashCardsWhenListIsRequested() { + ResponseEntity response = restTemplate.getForEntity("/cashcards", String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + DocumentContext documentContext = JsonPath.parse(response.getBody()); + int cashCardCount = documentContext.read("$.length()"); + assertThat(cashCardCount).isEqualTo(3); + + JSONArray ids = documentContext.read("$..id"); + assertThat(ids).containsExactlyInAnyOrder(99, 100, 101); + + JSONArray amounts = documentContext.read("$..amount"); + assertThat(amounts).containsExactlyInAnyOrder(123.45, 1.0, 150.00); + } + + @Test + void shouldReturnAPageOfCashCards() { + ResponseEntity response = restTemplate.getForEntity("/cashcards?page=0&size=1", String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + DocumentContext documentContext = JsonPath.parse(response.getBody()); + JSONArray page = documentContext.read("$[*]"); + assertThat(page.size()).isEqualTo(1); + } + + @Test + void shouldReturnASortedPageOfCashCards() { + ResponseEntity response = restTemplate.getForEntity("/cashcards?page=0&size=1&sort=amount,desc", String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + DocumentContext documentContext = JsonPath.parse(response.getBody()); + JSONArray read = documentContext.read("$[*]"); + assertThat(read.size()).isEqualTo(1); + + double amount = documentContext.read("$[0].amount"); + assertThat(amount).isEqualTo(150.00); + } + + @Test + void shouldReturnASortedPageOfCashCardsWithNoParametersAndUseDefaultValues() { + ResponseEntity response = restTemplate.getForEntity("/cashcards", String.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + DocumentContext documentContext = JsonPath.parse(response.getBody()); + JSONArray page = documentContext.read("$[*]"); + assertThat(page.size()).isEqualTo(3); + + JSONArray amounts = documentContext.read("$..amount"); + assertThat(amounts).containsExactly(1.00, 123.45, 150.00); + } } diff --git a/src/test/resources/data.sql b/src/test/resources/data.sql index 9f6290a..2fb63d0 100644 --- a/src/test/resources/data.sql +++ b/src/test/resources/data.sql @@ -1 +1,3 @@ -INSERT INTO CASH_CARD(ID, AMOUNT) VALUES (99, 123.45); \ No newline at end of file +INSERT INTO CASH_CARD(ID, AMOUNT) VALUES (99, 123.45); +INSERT INTO CASH_CARD(ID, AMOUNT) VALUES (100, 1.00); +INSERT INTO CASH_CARD(ID, AMOUNT) VALUES (101, 150.00); \ No newline at end of file diff --git a/src/test/resources/djmil/cashcard/list.json b/src/test/resources/djmil/cashcard/list.json new file mode 100644 index 0000000..2ae8f09 --- /dev/null +++ b/src/test/resources/djmil/cashcard/list.json @@ -0,0 +1,5 @@ +[ + { "id": 99, "amount": 123.45 }, + { "id": 100, "amount": 1.0 }, + { "id": 101, "amount": 150.0 } +] \ No newline at end of file