Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 061d2d8

Browse files
cschuylejoemoore
authored andcommitted
<implement-delete-end> Tests and implementation for Implement DELETE Lab
1 parent 9a7206c commit 061d2d8

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/main/java/example/cashcard/CashCardController.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ private ResponseEntity<Void> putCashCard(@PathVariable Long requestedId, @Reques
6464
return ResponseEntity.notFound().build();
6565
}
6666

67+
@DeleteMapping("/{id}")
68+
private ResponseEntity<Void> deleteCashCard(@PathVariable Long id, Principal principal) {
69+
if (cashCardRepository.existsByIdAndOwner(id, principal.getName())) {
70+
cashCardRepository.deleteById(id);
71+
return ResponseEntity.noContent().build();
72+
}
73+
return ResponseEntity.notFound().build();
74+
}
75+
6776
private CashCard findCashCard(Long requestedId, Principal principal) {
6877
return cashCardRepository.findByIdAndOwner(requestedId, principal.getName());
6978
}

src/main/java/example/cashcard/CashCardRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
interface CashCardRepository extends CrudRepository<CashCard, Long>, PagingAndSortingRepository<CashCard, Long> {
99
CashCard findByIdAndOwner(Long id, String owner);
1010

11+
boolean existsByIdAndOwner(Long id, String owner);
12+
1113
Page<CashCard> findByOwner(String owner, PageRequest pageRequest);
1214
}

src/main/resources/application.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/test/java/example/cashcard/CashCardApplicationTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,39 @@ void shouldNotUpdateACashCardThatIsOwnedBySomeoneElse() {
199199
.exchange("/cashcards/102", HttpMethod.PUT, request, Void.class);
200200
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
201201
}
202+
203+
@Test
204+
@DirtiesContext
205+
void shouldDeleteAnExistingCashCard() {
206+
ResponseEntity<Void> deleteResponse = restTemplate
207+
.withBasicAuth("sarah1", "abc123")
208+
.exchange("/cashcards/99", HttpMethod.DELETE, null, Void.class);
209+
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
210+
211+
ResponseEntity<String> getResponse = restTemplate
212+
.withBasicAuth("sarah1", "abc123")
213+
.getForEntity("/cashcards/99", String.class);
214+
assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
215+
}
216+
217+
@Test
218+
void shouldNotDeleteACashCardThatDoesNotExist() {
219+
ResponseEntity<Void> deleteResponse = restTemplate
220+
.withBasicAuth("sarah1", "abc123")
221+
.exchange("/cashcards/99999", HttpMethod.DELETE, null, Void.class);
222+
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
223+
}
224+
225+
@Test
226+
void shouldNotAllowDeletionOfCashCardsTheyDoNotOwn() {
227+
ResponseEntity<Void> deleteResponse = restTemplate
228+
.withBasicAuth("sarah1", "abc123")
229+
.exchange("/cashcards/102", HttpMethod.DELETE, null, Void.class);
230+
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
231+
232+
ResponseEntity<String> getResponse = restTemplate
233+
.withBasicAuth("kumar2", "xyz789")
234+
.getForEntity("/cashcards/102", String.class);
235+
assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
236+
}
202237
}

0 commit comments

Comments
 (0)