Skip to content

Commit

Permalink
commit for changes in tests and Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-pohl committed Jun 11, 2024
1 parent 3f2fbe7 commit cc0ccd7
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.neuefische.team2.backend.restaurant;

import com.neuefische.team2.backend.restaurant.domain.Comment;
import com.neuefische.team2.backend.restaurant.domain.NewCommentDTO;
import com.neuefische.team2.backend.restaurant.domain.NewRestaurantDTO;
import com.neuefische.team2.backend.exceptions.ResourceNotFoundException;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Restaurant addComment(@PathVariable String id, @RequestBody NewCommentDTO
}

@GetMapping("/{id}/comments")
public List<Restaurant.Comment> getComments(@PathVariable String id) {
public List<Comment> getComments(@PathVariable String id) {
return restaurantService.getCommentsForRestaurant(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.neuefische.team2.backend.restaurant;

import com.neuefische.team2.backend.exceptions.ResourceNotFoundException;
import com.neuefische.team2.backend.restaurant.domain.Comment;
import com.neuefische.team2.backend.restaurant.domain.NewRestaurantDTO;
import com.neuefische.team2.backend.exceptions.NoSuchRestaurantException;
import com.neuefische.team2.backend.restaurant.domain.Restaurant;
Expand Down Expand Up @@ -67,8 +68,8 @@ public Restaurant addCommentToRestaurant(String id, String commentText) throws R
logger.info("Trying to add comment to restaurant with ID {}", id);

Restaurant restaurant = this.findRestaurantById(id);
List<Restaurant.Comment> updatedComments = new ArrayList<>(restaurant.comments() != null ? restaurant.comments() : new ArrayList<>());
Restaurant.Comment comment = new Restaurant.Comment(commentText, System.currentTimeMillis());
List<Comment> updatedComments = new ArrayList<>(restaurant.comments() != null ? restaurant.comments() : new ArrayList<>());
Comment comment = new Comment(commentText, System.currentTimeMillis());
updatedComments.add(comment);

Restaurant updatedRestaurant = new Restaurant(
Expand All @@ -81,7 +82,7 @@ public Restaurant addCommentToRestaurant(String id, String commentText) throws R
}


public List<Restaurant.Comment> getCommentsForRestaurant(String id) throws ResourceNotFoundException {
public List<Comment> getCommentsForRestaurant(String id) throws ResourceNotFoundException {
logger.info("Trying to get comments for restaurant with ID {}", id);

Restaurant restaurant = this.findRestaurantById(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.neuefische.team2.backend.restaurant.domain;

import jakarta.validation.constraints.NotBlank;

public record Comment(

@NotBlank(message = "Kommentar muss vorhanden sein.")
String text,
long createdAt
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public record Restaurant(
String city,
List<Comment> comments
) {
public record Comment(
String text,
long createdAt
) {}

public Restaurant(String id, String title, String city) {
this(id, title, city, List.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,70 @@ void deleteRestaurant_whenRestaurantInDB_thenDBDoesNotContainRestaurantAnymore()
.andExpect(MockMvcResultMatchers.jsonPath("$..title").value(expected.title()))
.andExpect(MockMvcResultMatchers.jsonPath("$..city").value(expected.city()));
}

@DirtiesContext
@Test
void addCommentToRestaurant_whenRestaurantExists_thenAddCommentAndReturnUpdatedRestaurant() throws Exception {
// Arrange: Add a restaurant to the DB
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"title": "The Mockingbird",
"city": "New York"
}
"""))
.andReturn();

ObjectMapper mapper = new ObjectMapper();
Restaurant restaurant = mapper.readValue(result.getResponse().getContentAsString(), Restaurant.class);

// Act: Add a comment to the restaurant
mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants/" + restaurant.id() + "/comments")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"text": "Great place!"
}
"""))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.comments[0].text").value("Great place!"));
}


@DirtiesContext
@Test
void getCommentsForRestaurant_whenRestaurantExists_thenReturnComments() throws Exception {
// Arrange: Add a restaurant to the DB
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"title": "The Mockingbird",
"city": "New York"
}
"""))
.andReturn();

ObjectMapper mapper = new ObjectMapper();
Restaurant restaurant = mapper.readValue(result.getResponse().getContentAsString(), Restaurant.class);

// Add a comment to the restaurant
mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants/" + restaurant.id() + "/comments")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"text": "Great place!"
}
"""))
.andExpect(MockMvcResultMatchers.status().isOk());

// Get comments for the restaurant
mockMvc.perform(MockMvcRequestBuilders.get("/api/restaurants/" + restaurant.id() + "/comments"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].text").value("Great place!"));

}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.neuefische.team2.backend.restaurant;

import com.neuefische.team2.backend.restaurant.domain.Comment;
import com.neuefische.team2.backend.restaurant.domain.NewRestaurantDTO;
import com.neuefische.team2.backend.exceptions.NoSuchRestaurantException;
import com.neuefische.team2.backend.restaurant.domain.Restaurant;
Expand Down Expand Up @@ -125,7 +126,7 @@ void addRestaurant_whenRestaurantToSave_thenReturnSavedRestaurantWithId() {
void deleteRestaurant_whenMethodCalled_thenDeleteMethodOnRepositoryWasCalledOnlyOnce() {
//GIVEN
String id = "123";
Restaurant restaurantToDelete = new Restaurant(id, "abc", "NY");
Restaurant restaurantToDelete = new Restaurant(id, "abc", "NY", Collections.emptyList());
when(mockRestaurantRepository.findById(id)).thenReturn(Optional.of(restaurantToDelete));

//WHEN
Expand All @@ -134,4 +135,39 @@ void deleteRestaurant_whenMethodCalled_thenDeleteMethodOnRepositoryWasCalledOnly
//THEN
verify(mockRestaurantRepository, times(1)).deleteById(id);
}

@Test
void addCommentToRestaurant_whenRestaurantExists_thenAddCommentAndReturnUpdatedRestaurant() {
//GIVEN
Restaurant existingRestaurant = new Restaurant("1", "The Mockingbird", "New York", Collections.emptyList());
Comment comment = new Comment("Great place!", System.currentTimeMillis());
Restaurant updatedRestaurant = new Restaurant("1", "The Mockingbird", "New York", List.of(comment));

when(mockRestaurantRepository.findById("1")).thenReturn(Optional.of(existingRestaurant));
when(mockRestaurantRepository.save(any(Restaurant.class))).thenReturn(updatedRestaurant);

// WHEN
Restaurant actual = restaurantService.addCommentToRestaurant("1", comment.text());

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

@Test
void getCommentsForRestaurant_whenRestaurantExists_thenReturnComments() {
//GIVEN
Comment comment = new Comment("Great place!", System.currentTimeMillis());
Restaurant restaurant = new Restaurant("1", "The Mockingbird", "New York", List.of(comment));

when(mockRestaurantRepository.findById("1")).thenReturn(Optional.of(restaurant));

// WHEN
List<Comment> actual = restaurantService.getCommentsForRestaurant("1");

//THEN
verify(mockRestaurantRepository).findById("1");
assertEquals(restaurant.comments(), actual);
}
}

0 comments on commit cc0ccd7

Please sign in to comment.