From 6cd40885228df3134f5aa7cc27ff5606e0e72076 Mon Sep 17 00:00:00 2001 From: djmil Date: Sun, 23 Jul 2023 14:08:37 +0200 Subject: [PATCH] SpringSecurity: add owner to the CashCard data contract updated: - tests - sql scripts --- src/main/java/djmil/cashcard/CashCard.java | 2 +- .../java/djmil/cashcard/CashCardJsonTest.java | 19 ++++++++++--------- .../cashcard/CashcardApplicationTests.java | 2 +- src/test/resources/data.sql | 6 +++--- .../resources/djmil/cashcard/expected.json | 3 ++- src/test/resources/djmil/cashcard/list.json | 6 +++--- src/test/resources/schema.sql | 3 ++- 7 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/djmil/cashcard/CashCard.java b/src/main/java/djmil/cashcard/CashCard.java index 0ebdfc9..446b2d4 100644 --- a/src/main/java/djmil/cashcard/CashCard.java +++ b/src/main/java/djmil/cashcard/CashCard.java @@ -2,5 +2,5 @@ package djmil.cashcard; import org.springframework.data.annotation.Id; -public record CashCard(@Id Long id, Double amount) { +public record CashCard(@Id Long id, Double amount, String owner) { } diff --git a/src/test/java/djmil/cashcard/CashCardJsonTest.java b/src/test/java/djmil/cashcard/CashCardJsonTest.java index 51c7978..67b71e0 100644 --- a/src/test/java/djmil/cashcard/CashCardJsonTest.java +++ b/src/test/java/djmil/cashcard/CashCardJsonTest.java @@ -25,9 +25,9 @@ public class CashCardJsonTest { @BeforeEach void setUp() { cashCards = Arrays.array( - new CashCard(99L, 123.45), - new CashCard(100L, 1.00), - new CashCard(101L, 150.00)); + new CashCard(99L, 123.45, "sarah1"), + new CashCard(100L, 1.00, "sarah1"), + new CashCard(101L, 150.00, "sarah1")); } @Test @@ -37,7 +37,7 @@ public class CashCardJsonTest { @Test 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"); @@ -55,11 +55,12 @@ public class CashCardJsonTest { String expected = """ { "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).amount()).isEqualTo(67.89); @@ -74,9 +75,9 @@ public class CashCardJsonTest { void cashCardListDeserializationTest() throws IOException { String expected=""" [ - { "id": 99, "amount": 123.45 }, - { "id": 100, "amount": 1.00 }, - { "id": 101, "amount": 150.00 } + { "id": 99, "amount": 123.45, "owner": "sarah1" }, + { "id": 100, "amount": 1.00, "owner": "sarah1" }, + { "id": 101, "amount": 150.00, "owner": "sarah1" } ] """; 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 79dc2c1..e158cc0 100644 --- a/src/test/java/djmil/cashcard/CashcardApplicationTests.java +++ b/src/test/java/djmil/cashcard/CashcardApplicationTests.java @@ -55,7 +55,7 @@ class CashcardApplicationTests { @Test @DirtiesContext void shouldCreateANewCashCard() { - CashCard newCashCard = new CashCard(null, 250.00); + CashCard newCashCard = new CashCard(null, 250.00, "sarah1"); ResponseEntity createResponse = restTemplate.postForEntity("/cashcards", newCashCard, Void.class ); assertThat(createResponse.getStatusCode()).isEqualTo(HttpStatus.CREATED); diff --git a/src/test/resources/data.sql b/src/test/resources/data.sql index 2fb63d0..2134f06 100644 --- a/src/test/resources/data.sql +++ b/src/test/resources/data.sql @@ -1,3 +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); \ No newline at end of file +INSERT INTO CASH_CARD(ID, AMOUNT, OWNER) VALUES (99, 123.45, 'sarah1'); +INSERT INTO CASH_CARD(ID, AMOUNT, OWNER) VALUES (100, 1.00, 'sarah1'); +INSERT INTO CASH_CARD(ID, AMOUNT, OWNER) VALUES (101, 150.00, 'sarah1'); \ No newline at end of file diff --git a/src/test/resources/djmil/cashcard/expected.json b/src/test/resources/djmil/cashcard/expected.json index 82eacd0..784d363 100644 --- a/src/test/resources/djmil/cashcard/expected.json +++ b/src/test/resources/djmil/cashcard/expected.json @@ -1,4 +1,5 @@ { "id": 99, - "amount": 123.45 + "amount": 123.45, + "owner": "sarah1" } \ No newline at end of file diff --git a/src/test/resources/djmil/cashcard/list.json b/src/test/resources/djmil/cashcard/list.json index 2ae8f09..360817e 100644 --- a/src/test/resources/djmil/cashcard/list.json +++ b/src/test/resources/djmil/cashcard/list.json @@ -1,5 +1,5 @@ [ - { "id": 99, "amount": 123.45 }, - { "id": 100, "amount": 1.0 }, - { "id": 101, "amount": 150.0 } + { "id": 99, "amount": 123.45, "owner": "sarah1" }, + { "id": 100, "amount": 1.0, "owner": "sarah1" }, + { "id": 101, "amount": 150.0, "owner": "sarah1" } ] \ No newline at end of file diff --git a/src/test/resources/schema.sql b/src/test/resources/schema.sql index 8c35969..26bb5c5 100644 --- a/src/test/resources/schema.sql +++ b/src/test/resources/schema.sql @@ -1,5 +1,6 @@ CREATE TABLE cash_card ( 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 ); \ No newline at end of file