Skip to content

Commit

Permalink
Add new endpoint DELETE and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashonecker committed Jun 30, 2024
1 parent a2cd177 commit c70154e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public Ticket updateTicket(UpdateTicketDTO updateTicketDTO) {
existingTicket.author()
));
}

public void deleteTicket(String id) {
Ticket ticket = getTicketById(id);
ticketRepository.delete(ticket);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit c70154e

Please sign in to comment.