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

Commit 487eea4

Browse files
cschuylejoemoore
authored andcommitted
<implement-delete-end> Tests and implementation for Implement DELETE Lab
1 parent 4db819c commit 487eea4

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.net.URI;
1212
import java.security.Principal;
1313
import java.util.List;
14-
import java.security.Principal;
1514

1615
@RestController
1716
@RequestMapping("/cashcards")
@@ -65,6 +64,15 @@ private ResponseEntity<Void> putCashCard(@PathVariable Long requestedId, @Reques
6564
return ResponseEntity.notFound().build();
6665
}
6766

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+
6876
private CashCard findCashCard(Long requestedId, Principal principal) {
6977
return cashCardRepository.findByIdAndOwner(requestedId, principal.getName());
7078
}

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
public 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
@@ -189,4 +189,39 @@ void shouldNotUpdateACashCardThatDoesNotExist() {
189189
.exchange("/cashcards/99999", HttpMethod.PUT, request, Void.class);
190190
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
191191
}
192+
193+
@Test
194+
@DirtiesContext
195+
void shouldDeleteAnExistingCashCard() {
196+
ResponseEntity<Void> deleteResponse = restTemplate
197+
.withBasicAuth("sarah1", "abc123")
198+
.exchange("/cashcards/99", HttpMethod.DELETE, null, Void.class);
199+
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
200+
201+
ResponseEntity<String> getResponse = restTemplate
202+
.withBasicAuth("sarah1", "abc123")
203+
.getForEntity("/cashcards/99", String.class);
204+
assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
205+
}
206+
207+
@Test
208+
void shouldNotDeleteACashCardThatDoesNotExist() {
209+
ResponseEntity<Void> deleteResponse = restTemplate
210+
.withBasicAuth("sarah1", "abc123")
211+
.exchange("/cashcards/99999", HttpMethod.DELETE, null, Void.class);
212+
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
213+
}
214+
215+
@Test
216+
void shouldNotAllowDeletionOfCashCardsTheyDoNotOwn() {
217+
ResponseEntity<Void> deleteResponse = restTemplate
218+
.withBasicAuth("sarah1", "abc123")
219+
.exchange("/cashcards/102", HttpMethod.DELETE, null, Void.class);
220+
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
221+
222+
ResponseEntity<String> getResponse = restTemplate
223+
.withBasicAuth("kumar2", "xyz789")
224+
.getForEntity("/cashcards/102", String.class);
225+
assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
226+
}
192227
}

0 commit comments

Comments
 (0)