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 3, 2024
1 parent dc38f2d commit 317793b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,4 +23,9 @@ public RestaurantController(RestaurantService restaurantService) {
List<Restaurant> getRestaurants() {
return restaurantService.getRestaurants();
}

@DeleteMapping("{id}")
void deleteRestaurant(@PathVariable String id) {
restaurantService.deleteRestaurant(id);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,12 +18,51 @@ class RestaurantControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;

@Autowired
private RestaurantRepository restaurantRepository;

@Test
void getAllProducts_whenNoProductInDB_thenReturnEmptyList() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/api/restaurants"))
.andExpect(MockMvcResultMatchers.status().isOk())
.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.
}

0 comments on commit 317793b

Please sign in to comment.