SpringSecurity: add owner to the CashCard data contract

updated:
- tests
- sql scripts
This commit is contained in:
djmil 2023-07-23 14:08:37 +02:00
parent ca46b46029
commit 6cd4088522
7 changed files with 22 additions and 19 deletions

View File

@ -2,5 +2,5 @@ package djmil.cashcard;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
public record CashCard(@Id Long id, Double amount) { public record CashCard(@Id Long id, Double amount, String owner) {
} }

View File

@ -25,9 +25,9 @@ public class CashCardJsonTest {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
cashCards = Arrays.array( cashCards = Arrays.array(
new CashCard(99L, 123.45), new CashCard(99L, 123.45, "sarah1"),
new CashCard(100L, 1.00), new CashCard(100L, 1.00, "sarah1"),
new CashCard(101L, 150.00)); new CashCard(101L, 150.00, "sarah1"));
} }
@Test @Test
@ -37,7 +37,7 @@ public class CashCardJsonTest {
@Test @Test
public void cashCardSerializationTest() throws IOException { public void cashCardSerializationTest() throws IOException {
CashCard cashCard = new CashCard(99L, 123.45); CashCard cashCard = new CashCard(99L, 123.45, "sarah1");
assertThat(json.write(cashCard)).isStrictlyEqualToJson("expected.json"); assertThat(json.write(cashCard)).isStrictlyEqualToJson("expected.json");
@ -55,11 +55,12 @@ public class CashCardJsonTest {
String expected = """ String expected = """
{ {
"id":1000, "id":1000,
"amount":67.89 "amount":67.89,
"owner": "sarah1"
} }
"""; """;
assertThat(json.parse(expected)).isEqualTo(new CashCard(1000L, 67.89)); assertThat(json.parse(expected)).isEqualTo(new CashCard(1000L, 67.89, "sarah1"));
assertThat(json.parseObject(expected).id()).isEqualTo(1000); assertThat(json.parseObject(expected).id()).isEqualTo(1000);
assertThat(json.parseObject(expected).amount()).isEqualTo(67.89); assertThat(json.parseObject(expected).amount()).isEqualTo(67.89);
@ -74,9 +75,9 @@ public class CashCardJsonTest {
void cashCardListDeserializationTest() throws IOException { void cashCardListDeserializationTest() throws IOException {
String expected=""" String expected="""
[ [
{ "id": 99, "amount": 123.45 }, { "id": 99, "amount": 123.45, "owner": "sarah1" },
{ "id": 100, "amount": 1.00 }, { "id": 100, "amount": 1.00, "owner": "sarah1" },
{ "id": 101, "amount": 150.00 } { "id": 101, "amount": 150.00, "owner": "sarah1" }
] ]
"""; """;
assertThat(jsonList.parse(expected)).isEqualTo(cashCards); assertThat(jsonList.parse(expected)).isEqualTo(cashCards);

View File

@ -55,7 +55,7 @@ class CashcardApplicationTests {
@Test @Test
@DirtiesContext @DirtiesContext
void shouldCreateANewCashCard() { void shouldCreateANewCashCard() {
CashCard newCashCard = new CashCard(null, 250.00); CashCard newCashCard = new CashCard(null, 250.00, "sarah1");
ResponseEntity<Void> createResponse = restTemplate.postForEntity("/cashcards", newCashCard, Void.class ); ResponseEntity<Void> createResponse = restTemplate.postForEntity("/cashcards", newCashCard, Void.class );
assertThat(createResponse.getStatusCode()).isEqualTo(HttpStatus.CREATED); assertThat(createResponse.getStatusCode()).isEqualTo(HttpStatus.CREATED);

View File

@ -1,3 +1,3 @@
INSERT INTO CASH_CARD(ID, AMOUNT) VALUES (99, 123.45); INSERT INTO CASH_CARD(ID, AMOUNT, OWNER) VALUES (99, 123.45, 'sarah1');
INSERT INTO CASH_CARD(ID, AMOUNT) VALUES (100, 1.00); INSERT INTO CASH_CARD(ID, AMOUNT, OWNER) VALUES (100, 1.00, 'sarah1');
INSERT INTO CASH_CARD(ID, AMOUNT) VALUES (101, 150.00); INSERT INTO CASH_CARD(ID, AMOUNT, OWNER) VALUES (101, 150.00, 'sarah1');

View File

@ -1,4 +1,5 @@
{ {
"id": 99, "id": 99,
"amount": 123.45 "amount": 123.45,
"owner": "sarah1"
} }

View File

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

View File

@ -1,5 +1,6 @@
CREATE TABLE cash_card CREATE TABLE cash_card
( (
ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
AMOUNT NUMBER NOT NULL DEFAULT 0 AMOUNT NUMBER NOT NULL DEFAULT 0,
OWNER VARCHAR(256) NOT NULL
); );