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 5, 2024
1 parent a8645f3 commit 7d5b01e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -38,4 +39,9 @@ public Restaurant addRestaurant(@RequestBody @Valid NewRestaurantDTO newRestaura
Restaurant restaurant = new Restaurant(null, newRestaurantDTO.title(), newRestaurantDTO.city());
return restaurantService.addRestaurant(restaurant);
}

@DeleteMapping("{id}")
void deleteRestaurant(@PathVariable String id) {
restaurantService.deleteRestaurant(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,41 @@ void getRestaurantById_whenRestaurantDoesNotExist_thenReturnNotFound() throws Ex



@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 7d5b01e

Please sign in to comment.