From 38f86076c2360628e5d0b27d8234725db4369b2d Mon Sep 17 00:00:00 2001 From: djmil Date: Thu, 20 Jul 2023 12:31:04 +0200 Subject: [PATCH] H2 DB - used for tests - no DB implenention for prod --- build.gradle | 20 ++++++++++++++++++- src/main/java/djmil/cashcard/CashCard.java | 5 +++-- .../djmil/cashcard/CashCardController.java | 17 ++++++++++++---- .../djmil/cashcard/CashCardRepository.java | 6 ++++++ src/test/resources/data.sql | 1 + src/test/resources/schema.sql | 5 +++++ 6 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 src/main/java/djmil/cashcard/CashCardRepository.java create mode 100644 src/test/resources/data.sql create mode 100644 src/test/resources/schema.sql diff --git a/build.gradle b/build.gradle index 0b44fbf..57368e1 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ group = 'djmil' version = '0.0.1-SNAPSHOT' java { - sourceCompatibility = '17' + sourceCompatibility = '20' } repositories { @@ -18,8 +18,26 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' + + implementation 'org.springframework.data:spring-data-jdbc' + testImplementation 'com.h2database:h2' } tasks.named('test') { useJUnitPlatform() } + +// This section causes useful test output to go to the terminal. +test { + testLogging { + events "passed", "skipped", "failed" //, "standardOut", "standardError" + + showExceptions true + exceptionFormat "full" + showCauses true + showStackTraces true + + // Change to `true` for more verbose test output + showStandardStreams = false + } +} \ No newline at end of file diff --git a/src/main/java/djmil/cashcard/CashCard.java b/src/main/java/djmil/cashcard/CashCard.java index 4bc5397..0ebdfc9 100644 --- a/src/main/java/djmil/cashcard/CashCard.java +++ b/src/main/java/djmil/cashcard/CashCard.java @@ -1,5 +1,6 @@ package djmil.cashcard; -public record CashCard(Long id, Double amount) { - +import org.springframework.data.annotation.Id; + +public record CashCard(@Id Long id, Double amount) { } diff --git a/src/main/java/djmil/cashcard/CashCardController.java b/src/main/java/djmil/cashcard/CashCardController.java index c3771f4..fb4b57f 100644 --- a/src/main/java/djmil/cashcard/CashCardController.java +++ b/src/main/java/djmil/cashcard/CashCardController.java @@ -6,17 +6,26 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.Optional; + @RestController @RequestMapping("/cashcards") public class CashCardController { + private CashCardRepository cashCardRepository; + + public CashCardController(CashCardRepository cashCardRepository) { + this.cashCardRepository = cashCardRepository; + } + @GetMapping("/{requestedId}") public ResponseEntity findById(@PathVariable Long requestedId) { - if (requestedId.equals(99L)) { - CashCard cashCard = new CashCard(99L, 123.45); - return ResponseEntity.ok(cashCard); + Optional cashCardOptional = cashCardRepository.findById(requestedId); + + if (cashCardOptional.isPresent()) { + return ResponseEntity.ok(cashCardOptional.get()); } else { return ResponseEntity.notFound().build(); - } + } } } diff --git a/src/main/java/djmil/cashcard/CashCardRepository.java b/src/main/java/djmil/cashcard/CashCardRepository.java new file mode 100644 index 0000000..23795e1 --- /dev/null +++ b/src/main/java/djmil/cashcard/CashCardRepository.java @@ -0,0 +1,6 @@ +package djmil.cashcard; + +import org.springframework.data.repository.CrudRepository; + +public interface CashCardRepository extends CrudRepository { +} diff --git a/src/test/resources/data.sql b/src/test/resources/data.sql new file mode 100644 index 0000000..9f6290a --- /dev/null +++ b/src/test/resources/data.sql @@ -0,0 +1 @@ +INSERT INTO CASH_CARD(ID, AMOUNT) VALUES (99, 123.45); \ No newline at end of file diff --git a/src/test/resources/schema.sql b/src/test/resources/schema.sql new file mode 100644 index 0000000..8c35969 --- /dev/null +++ b/src/test/resources/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE cash_card +( + ID BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + AMOUNT NUMBER NOT NULL DEFAULT 0 +); \ No newline at end of file