diff --git a/backend/src/main/java/com/neuefische/team2/backend/restaurant/RestaurantController.java b/backend/src/main/java/com/neuefische/team2/backend/restaurant/RestaurantController.java index 5c3edfd..01092fc 100644 --- a/backend/src/main/java/com/neuefische/team2/backend/restaurant/RestaurantController.java +++ b/backend/src/main/java/com/neuefische/team2/backend/restaurant/RestaurantController.java @@ -3,6 +3,8 @@ import com.neuefische.team2.backend.restaurant.domain.Restaurant; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.List; @@ -21,4 +23,9 @@ public RestaurantController(RestaurantService restaurantService) { List getRestaurants() { return restaurantService.getRestaurants(); } + + @DeleteMapping("{id}") + void deleteRestaurant(@PathVariable String id) { + restaurantService.deleteRestaurant(id); + } } diff --git a/backend/src/test/java/com/neuefische/team2/backend/restaurant/RestaurantControllerIntegrationTest.java b/backend/src/test/java/com/neuefische/team2/backend/restaurant/RestaurantControllerIntegrationTest.java index 0a41e81..9a5b67c 100644 --- a/backend/src/test/java/com/neuefische/team2/backend/restaurant/RestaurantControllerIntegrationTest.java +++ b/backend/src/test/java/com/neuefische/team2/backend/restaurant/RestaurantControllerIntegrationTest.java @@ -1,9 +1,11 @@ package com.neuefische.team2.backend.restaurant; +import com.neuefische.team2.backend.restaurant.domain.Restaurant; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -16,6 +18,9 @@ class RestaurantControllerIntegrationTest { @Autowired private MockMvc mockMvc; + @Autowired + private RestaurantRepository restaurantRepository; + @Test void getAllProducts_whenNoProductInDB_thenReturnEmptyList() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/api/restaurants")) @@ -23,5 +28,41 @@ void getAllProducts_whenNoProductInDB_thenReturnEmptyList() throws Exception { .andExpect(MockMvcResultMatchers.content().json("[]")); } + @Test + void deleteRestaurant_whenNoProductInDB_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_whenProductInDB_thenDBDoesNotContainProductAnymore() throws Exception { + //GIVEN + Restaurant r1 = new Restaurant(null, "test-title", "test-city"); + Restaurant r2 = new Restaurant(null, "test-title-to-remove", "test-city"); + Restaurant r1WithId = restaurantRepository.insert(r1); + Restaurant restaurantToDelete = restaurantRepository.insert(r2); + + //WHEN + mockMvc.perform(MockMvcRequestBuilders.delete("/api/restaurants/" + restaurantToDelete.id())); + + //THEN + mockMvc.perform(MockMvcRequestBuilders.get("/api/restaurants")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$..id").value(r1WithId.id())) + .andExpect(MockMvcResultMatchers.jsonPath("$..title").value(r1WithId.title())) + .andExpect(MockMvcResultMatchers.jsonPath("$..city").value(r1WithId.city())); + } + // TODO: Write a test to receive one restaurant as soon as POST endpoint is implemented. }