Pageble GET with defaults

This commit is contained in:
djmil 2023-07-22 15:04:56 +02:00
parent 19aa42941f
commit ca46b46029
6 changed files with 126 additions and 3 deletions

View File

@ -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,6 +38,23 @@ public class CashCardController {
}
}
// @GetMapping()
// public ResponseEntity<Iterable<CashCard>> findAll() {
// return ResponseEntity.ok(cashCardRepository.findAll());
// }
@GetMapping
public ResponseEntity<List<CashCard>> findAll(Pageable pageable) {
Page<CashCard> 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<Void> createCashCard(@RequestBody CashCard newCashCardRequest, UriComponentsBuilder ucb) {
CashCard savedCashCard = cashCardRepository.save(newCashCardRequest); // CRUD - Create

View File

@ -1,6 +1,11 @@
package djmil.cashcard;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface CashCardRepository extends CrudRepository<CashCard, Long> {
public interface
CashCardRepository
extends
CrudRepository<CashCard, Long>,
PagingAndSortingRepository<CashCard, Long> {
}

View File

@ -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<CashCard> json;
@Autowired
private JacksonTester<CashCard[]> 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);
}
}

View File

@ -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<Void> 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<String> 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<String> 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<String> 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<String> 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);
}
}

View File

@ -1 +1,3 @@
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);

View File

@ -0,0 +1,5 @@
[
{ "id": 99, "amount": 123.45 },
{ "id": 100, "amount": 1.0 },
{ "id": 101, "amount": 150.0 }
]