diff --git a/backend/src/main/java/com/github/jonashonecker/backend/ticket/TicketController.java b/backend/src/main/java/com/github/jonashonecker/backend/ticket/TicketController.java index 5592e8d..3661849 100644 --- a/backend/src/main/java/com/github/jonashonecker/backend/ticket/TicketController.java +++ b/backend/src/main/java/com/github/jonashonecker/backend/ticket/TicketController.java @@ -31,4 +31,9 @@ public Ticket createTicket(@Valid @RequestBody NewTicketDTO newTicketDTO) { public Ticket updateTicket(@Valid @RequestBody UpdateTicketDTO updateTicketDTO) { return ticketService.updateTicket(updateTicketDTO); } + + @DeleteMapping("/{id}") + public void deleteTicket(@PathVariable String id) { + ticketService.deleteTicket(id); + } } diff --git a/backend/src/main/java/com/github/jonashonecker/backend/ticket/TicketService.java b/backend/src/main/java/com/github/jonashonecker/backend/ticket/TicketService.java index 84ed6bd..4449a9c 100644 --- a/backend/src/main/java/com/github/jonashonecker/backend/ticket/TicketService.java +++ b/backend/src/main/java/com/github/jonashonecker/backend/ticket/TicketService.java @@ -54,4 +54,9 @@ public Ticket updateTicket(UpdateTicketDTO updateTicketDTO) { existingTicket.author() )); } + + public void deleteTicket(String id) { + Ticket ticket = getTicketById(id); + ticketRepository.delete(ticket); + } } \ No newline at end of file diff --git a/backend/src/test/java/com/github/jonashonecker/backend/ticket/TicketControllerTest.java b/backend/src/test/java/com/github/jonashonecker/backend/ticket/TicketControllerTest.java index 3e1f499..b2f153a 100644 --- a/backend/src/test/java/com/github/jonashonecker/backend/ticket/TicketControllerTest.java +++ b/backend/src/test/java/com/github/jonashonecker/backend/ticket/TicketControllerTest.java @@ -14,10 +14,13 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.anyOf; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oidcLogin; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @SpringBootTest @@ -325,4 +328,23 @@ void updateTicket_whenInvalidIdAndTitleAndDescription_thenReturnApiErrorMessage( .andExpect(jsonPath("$.error", containsString("title (Title must not be empty)"))); } + @Test + @WithMockUser + @DirtiesContext + void deleteTicket_whenDeletingTicketFromRepository_thenOnlyTheSpecifiedTicketIsDeleted() throws Exception { + //GIVEN + TicketScoutUser ticketScoutUser = new TicketScoutUser("test-name", "test-avatarUrl"); + Ticket ticketToDelete = new Ticket("1", "projectName", "titleToDelete", "description", Status.OPEN, ticketScoutUser); + Ticket ticketToKeep = new Ticket("2", "projectName", "titleToKeep", "description", Status.OPEN, ticketScoutUser); + ticketRepository.insert(ticketToDelete); + ticketRepository.insert(ticketToKeep); + + //WHEN + mockMvc.perform(delete("/api/ticket/{id}", ticketToDelete.id())) + //THEN + .andExpect(status().isOk()); + + assertFalse(ticketRepository.findById(ticketToDelete.id()).isPresent()); + assertTrue(ticketRepository.findById(ticketToKeep.id()).isPresent()); + } } \ No newline at end of file diff --git a/backend/src/test/java/com/github/jonashonecker/backend/ticket/TicketServiceTest.java b/backend/src/test/java/com/github/jonashonecker/backend/ticket/TicketServiceTest.java index c93e51f..d432f0c 100644 --- a/backend/src/test/java/com/github/jonashonecker/backend/ticket/TicketServiceTest.java +++ b/backend/src/test/java/com/github/jonashonecker/backend/ticket/TicketServiceTest.java @@ -159,4 +159,39 @@ void updateTicket_whenUpdateTicketTitle_returnTicketWithUpdatedTitle() { verify(ticketRepository, times(1)).findById(id); assertEquals(expected, actual); } + + @Test + void deleteTicket_whenIdDoesNotExist_throwNoSuchTicketException() { + //GIVEN + String id = "test-id"; + when(ticketRepository.findById(id)).thenReturn(Optional.empty()); + + //WHEN + NoSuchTicketException actual = assertThrows(NoSuchTicketException.class, () -> ticketService.deleteTicket(id)); + + //THEN + assertEquals("Could not find ticket with id: " + id, actual.getMessage()); + } + + @Test + void deleteTicket_whenIdExist_thenGetTicketByIdAndDeleteTicketByIdIsCalled() { + //GIVEN + String id = "test-id"; + Ticket ticket = new Ticket( + id, + "test-projectName", + "test-title", + "test-description", + Status.OPEN, + new TicketScoutUser("test-name", "test-avatarUrl") + ); + when(ticketRepository.findById(id)).thenReturn(Optional.of(ticket)); + + //WHEN + ticketService.deleteTicket(id); + + //THEN + verify(ticketRepository, times(1)).findById(id); + verify(ticketRepository, times(1)).delete(ticket); + } } \ No newline at end of file