Skip to content

Commit

Permalink
Add tests for empty and whitespace-only input validation
Browse files Browse the repository at this point in the history
In the RestaurantControllerIntegrationTest.java file, integration tests were added that validate the behavior of the backend when receiving requests to add a restaurant with an empty title, empty city, or both. These tests ensure that the system throws a Bad Request exception and displays an appropriate error message.
  • Loading branch information
josch87 committed Jun 3, 2024
1 parent e15c992 commit d89a7c2
Showing 1 changed file with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void getAllProducts_whenOneProductInDB_thenReturnListOfOne() throws Exception {
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].id").value(restaurant.id()));
}


@DirtiesContext
@Test
void addRestaurant_whenNewRestaurantDTO_thenReturnSavedRestaurantWithId() throws Exception {
Expand All @@ -78,4 +77,108 @@ void addRestaurant_whenNewRestaurantDTO_thenReturnSavedRestaurantWithId() throws
"""))
.andExpect(MockMvcResultMatchers.jsonPath("$.id").exists());
}

@DirtiesContext
@Test
void addRestaurant_whenTitleEmptyString_thenReturnException() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"title": "",
"city": "New York"
}
"""))
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.content().json("""
{
"errors": [
{
"field": "title",
"message": "Title must not be empty"
}
]
}
"""));
}

@DirtiesContext
@Test
void addRestaurant_whenCityEmptyString_thenReturnException() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"title": "The Mockingbird",
"city": ""
}
"""))
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.content().json("""
{
"errors": [
{
"field": "city",
"message": "City must not be empty"
}
]
}
"""));
}

@DirtiesContext
@Test
void addRestaurant_whenTitleAndCityEmptyString_thenReturnException() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"title": "",
"city": ""
}
"""))
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.content().json("""
{
"errors": [
{
"field": "city",
"message": "City must not be empty"
},
{
"field": "title",
"message": "Title must not be empty"
}
]
}
"""));
}

@DirtiesContext
@Test
void addRestaurant_whenTitleAndCityContainOnlySpaces_thenReturnException() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/api/restaurants")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"title": " ",
"city": " "
}
"""))
.andExpect(MockMvcResultMatchers.status().isBadRequest())
.andExpect(MockMvcResultMatchers.content().json("""
{
"errors": [
{
"field": "city",
"message": "City must not be empty"
},
{
"field": "title",
"message": "Title must not be empty"
}
]
}
"""));
}
}

0 comments on commit d89a7c2

Please sign in to comment.