Skip to content

Commit

Permalink
Add delete endpoint and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashonecker committed Jun 6, 2024
1 parent b5a69bc commit a87b2a3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -47,4 +48,9 @@ public Restaurant addRestaurant(@RequestBody @Valid NewRestaurantDTO newRestaura
Restaurant putRestaurant(@Valid @RequestBody NewRestaurantDTO newRestaurantDTO, @PathVariable String id) throws ResourceNotFoundException {
return restaurantService.updateRestaurant(newRestaurantDTO, id);
}

@DeleteMapping("{id}")
void deleteRestaurant(@PathVariable String id) {
restaurantService.deleteRestaurant(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ void addRestaurant_whenTitleAndCityContainOnlySpaces_thenReturnException() throw
}
"""));
}

@Test
void getRestaurantById_whenRestaurantExists_thenReturnRestaurant() throws Exception {
//GIVEN
Expand Down Expand Up @@ -222,11 +223,6 @@ void getRestaurantById_whenRestaurantDoesNotExist_thenReturnNotFound() throws Ex
.andExpect(MockMvcResultMatchers.content().string("Restaurant with id 999 not found"));
}



// TODO: Write a test to receive one restaurant as soon as POST endpoint is implemented.


@Test
void updateRestaurant_whenRestaurantExists_thenReturnUpdatedRestaurant() throws Exception {
// Arrange: Add a restaurant to the DB
Expand Down Expand Up @@ -258,4 +254,58 @@ void updateRestaurant_whenRestaurantDoesNotExist_thenReturnNotFound() throws Exc
.content(objectMapper.writeValueAsString(updatedRestaurant)))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}

@Test
void deleteRestaurant_whenNoRestaurantInDB_thenDBStaysEmpty() throws Exception {
//GIVEN
String id = "123";

//WHEN
mockMvc.perform(MockMvcRequestBuilders.delete("/api/restaurants/" + id))
//THEN
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string(""));

mockMvc.perform(MockMvcRequestBuilders.get("/api/restaurants"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json("[]"));
}

@Test
@DirtiesContext
void deleteRestaurant_whenRestaurantInDB_thenDBDoesNotContainRestaurantAnymore() throws Exception {
//GIVEN
MvcResult resultRestaurantToDelete = mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"title": "test-title-to-remove",
"city": "test-city"
}
""")).andReturn();

MvcResult resultRestaurantToKeep = mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"title": "test-title",
"city": "test-city"
}
""")).andReturn();


ObjectMapper mapper = new ObjectMapper();
String idToDelete = mapper.readValue(resultRestaurantToDelete.getResponse().getContentAsString(), Restaurant.class).id();

//WHEN
mockMvc.perform(MockMvcRequestBuilders.delete("/api/restaurants/" + idToDelete));

//THEN
Restaurant expected = mapper.readValue(resultRestaurantToKeep.getResponse().getContentAsString(), Restaurant.class);
mockMvc.perform(MockMvcRequestBuilders.get("/api/restaurants"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$..id").value(expected.id()))
.andExpect(MockMvcResultMatchers.jsonPath("$..title").value(expected.title()))
.andExpect(MockMvcResultMatchers.jsonPath("$..city").value(expected.city()));
}
}

0 comments on commit a87b2a3

Please sign in to comment.