From d20a0877f8c920287318e6a0ba40c827701150bd Mon Sep 17 00:00:00 2001 From: Carlton Schuyler Date: Tue, 21 Feb 2023 10:51:13 -0700 Subject: [PATCH] Tests and implementation for Implement DELETE Lab --- .../example/cashcard/CashCardController.java | 9 +++++ .../example/cashcard/CashCardRepository.java | 2 ++ src/main/resources/application.properties | 1 - .../cashcard/CashCardApplicationTests.java | 35 +++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) delete mode 100644 src/main/resources/application.properties diff --git a/src/main/java/example/cashcard/CashCardController.java b/src/main/java/example/cashcard/CashCardController.java index 50335e6..85def36 100644 --- a/src/main/java/example/cashcard/CashCardController.java +++ b/src/main/java/example/cashcard/CashCardController.java @@ -64,6 +64,15 @@ private ResponseEntity putCashCard(@PathVariable Long requestedId, @Reques return ResponseEntity.notFound().build(); } + @DeleteMapping("/{id}") + private ResponseEntity deleteCashCard(@PathVariable Long id, Principal principal) { + if (cashCardRepository.existsByIdAndOwner(id, principal.getName())) { + cashCardRepository.deleteById(id); + return ResponseEntity.noContent().build(); + } + return ResponseEntity.notFound().build(); + } + private CashCard findCashCard(Long requestedId, Principal principal) { return cashCardRepository.findByIdAndOwner(requestedId, principal.getName()); } diff --git a/src/main/java/example/cashcard/CashCardRepository.java b/src/main/java/example/cashcard/CashCardRepository.java index bfd64c5..875b0d2 100644 --- a/src/main/java/example/cashcard/CashCardRepository.java +++ b/src/main/java/example/cashcard/CashCardRepository.java @@ -8,5 +8,7 @@ interface CashCardRepository extends CrudRepository, PagingAndSortingRepository { CashCard findByIdAndOwner(Long id, String owner); + boolean existsByIdAndOwner(Long id, String owner); + Page findByOwner(String owner, PageRequest pageRequest); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/test/java/example/cashcard/CashCardApplicationTests.java b/src/test/java/example/cashcard/CashCardApplicationTests.java index 57da7d1..946d540 100644 --- a/src/test/java/example/cashcard/CashCardApplicationTests.java +++ b/src/test/java/example/cashcard/CashCardApplicationTests.java @@ -199,4 +199,39 @@ void shouldNotUpdateACashCardThatIsOwnedBySomeoneElse() { .exchange("/cashcards/102", HttpMethod.PUT, request, Void.class); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); } + + @Test + @DirtiesContext + void shouldDeleteAnExistingCashCard() { + ResponseEntity deleteResponse = restTemplate + .withBasicAuth("sarah1", "abc123") + .exchange("/cashcards/99", HttpMethod.DELETE, null, Void.class); + assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT); + + ResponseEntity getResponse = restTemplate + .withBasicAuth("sarah1", "abc123") + .getForEntity("/cashcards/99", String.class); + assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } + + @Test + void shouldNotDeleteACashCardThatDoesNotExist() { + ResponseEntity deleteResponse = restTemplate + .withBasicAuth("sarah1", "abc123") + .exchange("/cashcards/99999", HttpMethod.DELETE, null, Void.class); + assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + } + + @Test + void shouldNotAllowDeletionOfCashCardsTheyDoNotOwn() { + ResponseEntity deleteResponse = restTemplate + .withBasicAuth("sarah1", "abc123") + .exchange("/cashcards/102", HttpMethod.DELETE, null, Void.class); + assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); + + ResponseEntity getResponse = restTemplate + .withBasicAuth("kumar2", "xyz789") + .getForEntity("/cashcards/102", String.class); + assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.OK); + } } \ No newline at end of file