Skip to content

Commit

Permalink
commit for updating the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-pohl committed Jun 11, 2024
1 parent 03c90f7 commit 3f2fbe7
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@ void deleteRestaurant(@PathVariable String id) {
restaurantService.deleteRestaurant(id);
}


@PostMapping("/{id}/comments")
public Restaurant addComment(@PathVariable String id, @RequestBody NewCommentDTO comment) {
return restaurantService.addCommentToRestaurant(id, comment.text());
}


@GetMapping("/{id}/comments")
public List<Restaurant.Comment> getComments(@PathVariable String id) {
return restaurantService.getCommentsForRestaurant(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ public Restaurant addRestaurant(Restaurant restaurant) {

public Restaurant updateRestaurant(NewRestaurantDTO updatedRestaurantDTO, String id) throws ResourceNotFoundException {
logger.info("Trying to update restaurant with ID {}", id);
Restaurant existingRestaurant = this.findRestaurantById(id);
this.findRestaurantById(id);
Restaurant updatedRestaurant = new Restaurant(
id,
updatedRestaurantDTO.title().trim(),
updatedRestaurantDTO.city().trim(),
existingRestaurant.comments()
this.findRestaurantById(id).comments()
);
Restaurant savedRestaurant = restaurantRepository.save(updatedRestaurant);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public record Comment(
String text,
long createdAt
) {}

public Restaurant(String id, String title, String city) {
this(id, title, city, List.of());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,7 @@ void updateRestaurant_whenRestaurantExists_thenReturnUpdatedRestaurant() throws
.andExpect(MockMvcResultMatchers.jsonPath("$.city").value("New City"));
}

@Test
void updateRestaurant_whenRestaurantDoesNotExist_thenReturnNotFound() throws Exception {
// Act: Try to update a non-existing restaurant
Restaurant updatedRestaurant = new Restaurant(null, "New Name", "New City");
mockMvc.perform(MockMvcRequestBuilders.put("/api/restaurants/999")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(updatedRestaurant)))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}


@Test
void deleteRestaurant_whenNoRestaurantInDB_thenDBStaysEmpty() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void updateRestaurant_whenRestaurantExists_thenUpdateAndReturnRestaurant() {
Restaurant actual = restaurantService.updateRestaurant(updatedRestaurantData, "1");

//THEN
verify(mockRestaurantRepository).findById("1");
verify(mockRestaurantRepository, times(2)).findById("1");
verify(mockRestaurantRepository).save(any(Restaurant.class));
assertEquals(updatedRestaurant, actual);
}
Expand Down

0 comments on commit 3f2fbe7

Please sign in to comment.