From 4287b963203a18383b10c6bb14b3231cc4b2f6e8 Mon Sep 17 00:00:00 2001 From: djmil Date: Tue, 25 Jul 2023 12:12:07 +0200 Subject: [PATCH] Update the resource with HTTP Put - do not crash while trying to update non exisiting resource --- build.gradle | 2 +- .../djmil/cashcard/CashCardController.java | 16 ++++++++- .../cashcard/CashcardApplicationTests.java | 35 +++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 72050b4..42f2f87 100644 --- a/build.gradle +++ b/build.gradle @@ -40,6 +40,6 @@ test { showStackTraces true // Change to `true` for more verbose test output - showStandardStreams = false + showStandardStreams = true } } \ No newline at end of file diff --git a/src/main/java/djmil/cashcard/CashCardController.java b/src/main/java/djmil/cashcard/CashCardController.java index 6787d20..f43de99 100644 --- a/src/main/java/djmil/cashcard/CashCardController.java +++ b/src/main/java/djmil/cashcard/CashCardController.java @@ -7,6 +7,7 @@ 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; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -74,4 +75,17 @@ public class CashCardController { return ResponseEntity.created(locationOfNewCashCard).build(); } -} + + @PutMapping("/{requestedId}") + private ResponseEntity putCashCard(@PathVariable Long requestedId, @RequestBody CashCard cashCardUpdate, Principal principal) { + CashCard cashCard = cashCardRepository.findByIdAndOwner(requestedId, principal.getName()); + + if (cashCard == null) { + return ResponseEntity.notFound().build(); + } + + CashCard updatedCashCard = new CashCard(cashCard.id(), cashCardUpdate.amount(), principal.getName()); + cashCardRepository.save(updatedCashCard); + return ResponseEntity.noContent().build(); + } +} \ No newline at end of file diff --git a/src/test/java/djmil/cashcard/CashcardApplicationTests.java b/src/test/java/djmil/cashcard/CashcardApplicationTests.java index a287d48..9dbbc9d 100644 --- a/src/test/java/djmil/cashcard/CashcardApplicationTests.java +++ b/src/test/java/djmil/cashcard/CashcardApplicationTests.java @@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -169,4 +171,37 @@ class CashcardApplicationTests { .getForEntity("/cashcards/102", String.class); // <<-- kumar2's data assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); } + + @Test + @DirtiesContext + void shouldUpdateAnExistingCashCard() { + CashCard cashCardUpdate = new CashCard(null, 19.99, null); + HttpEntity request = new HttpEntity<>(cashCardUpdate); + ResponseEntity response = restTemplate + .withBasicAuth("sarah1", "abc123") + .exchange("/cashcards/99", HttpMethod.PUT, request, Void.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); + + // Validte that the resource was updated + ResponseEntity getResponse = restTemplate + .withBasicAuth("sarah1", "abc123") + .getForEntity("/cashcards/99", String.class); + assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.OK); + + DocumentContext documentContext = JsonPath.parse(getResponse.getBody()); + Number id = documentContext.read("$.id"); + Double amount = documentContext.read("$.amount"); + assertThat(id).isEqualTo(99); + assertThat(amount).isEqualTo(19.99); + } + + @Test + void shouldNotUpdateACashCardThatDoesNotExist() { + CashCard unknownCard = new CashCard(null, 19.99, null); + HttpEntity request = new HttpEntity<>(unknownCard); + ResponseEntity response = restTemplate + .withBasicAuth("sarah1", "abc123") + .exchange("/cashcards/99999", HttpMethod.PUT, request, Void.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } }