Skip to content

Commit

Permalink
<implement-delete-end> Tests and implementation for Implement DELETE Lab
Browse files Browse the repository at this point in the history
  • Loading branch information
cschuyle authored and joemoore committed Jul 12, 2024
1 parent 4afa8d2 commit d20a087
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/example/cashcard/CashCardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ private ResponseEntity<Void> putCashCard(@PathVariable Long requestedId, @Reques
return ResponseEntity.notFound().build();
}

@DeleteMapping("/{id}")
private ResponseEntity<Void> 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());
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/example/cashcard/CashCardRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
interface CashCardRepository extends CrudRepository<CashCard, Long>, PagingAndSortingRepository<CashCard, Long> {
CashCard findByIdAndOwner(Long id, String owner);

boolean existsByIdAndOwner(Long id, String owner);

Page<CashCard> findByOwner(String owner, PageRequest pageRequest);
}
1 change: 0 additions & 1 deletion src/main/resources/application.properties

This file was deleted.

35 changes: 35 additions & 0 deletions src/test/java/example/cashcard/CashCardApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> deleteResponse = restTemplate
.withBasicAuth("sarah1", "abc123")
.exchange("/cashcards/99", HttpMethod.DELETE, null, Void.class);
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);

ResponseEntity<String> getResponse = restTemplate
.withBasicAuth("sarah1", "abc123")
.getForEntity("/cashcards/99", String.class);
assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}

@Test
void shouldNotDeleteACashCardThatDoesNotExist() {
ResponseEntity<Void> deleteResponse = restTemplate
.withBasicAuth("sarah1", "abc123")
.exchange("/cashcards/99999", HttpMethod.DELETE, null, Void.class);
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}

@Test
void shouldNotAllowDeletionOfCashCardsTheyDoNotOwn() {
ResponseEntity<Void> deleteResponse = restTemplate
.withBasicAuth("sarah1", "abc123")
.exchange("/cashcards/102", HttpMethod.DELETE, null, Void.class);
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);

ResponseEntity<String> getResponse = restTemplate
.withBasicAuth("kumar2", "xyz789")
.getForEntity("/cashcards/102", String.class);
assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}

0 comments on commit d20a087

Please sign in to comment.