Update MovieControllerIntegrationTest.java

This commit is contained in:
Pallavi Priyadarshani 2019-08-02 16:45:16 +05:30 committed by GitHub
parent a5f0631850
commit 077f38e318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,6 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import com.baeldung.validation.listvalidation.model.Actor;
import com.baeldung.validation.listvalidation.model.Movie; import com.baeldung.validation.listvalidation.model.Movie;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -30,21 +29,31 @@ public class MovieControllerIntegrationTest {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
@Test @Test
public void given1Genre_whenAddingMovie_thenThrowBadRequest() throws Exception { public void givenValidMovieList_whenAddingMovieList_thenIsOK() throws Exception {
Actor actor = new Actor("Actor1"); List<Movie> movies = new ArrayList<>();
Movie movie = new Movie("Movie1", Arrays.asList("Drama"), Arrays.asList(actor)); Movie movie = new Movie("Movie3");
mvc.perform(MockMvcRequestBuilders.post("/movie") movies.add(movie);
mvc.perform(MockMvcRequestBuilders.post("/movies")
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(movie))) .content(objectMapper.writeValueAsString(movies)))
.andExpect(MockMvcResultMatchers.status()
.isOk());
}
@Test
public void givenEmptyMovieList_whenAddingMovieList_thenThrowBadRequest() throws Exception {
List<Movie> movies = new ArrayList<>();
mvc.perform(MockMvcRequestBuilders.post("/movies")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(movies)))
.andExpect(MockMvcResultMatchers.status() .andExpect(MockMvcResultMatchers.status()
.isBadRequest()); .isBadRequest());
} }
@Test @Test
public void givenWithoutActor_whenAddingMovieList_thenThrowBadRequest() throws Exception { public void givenEmptyMovieName_whenAddingMovieList_thenThrowBadRequest() throws Exception {
List<Actor> actors = new ArrayList<>(); Movie movie = new Movie("");
Movie movie = new Movie("Movie2", Arrays.asList("Action", "Thriller"), actors); mvc.perform(MockMvcRequestBuilders.post("/movies")
mvc.perform(MockMvcRequestBuilders.post("/movie/batch")
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(Arrays.asList(movie)))) .content(objectMapper.writeValueAsString(Arrays.asList(movie))))
.andExpect(MockMvcResultMatchers.status() .andExpect(MockMvcResultMatchers.status()
@ -52,12 +61,11 @@ public class MovieControllerIntegrationTest {
} }
@Test @Test
public void givenEmptyMovieName_whenAddingMovie_thenThrowBadRequest() throws Exception { public void givenInvalidMovieName_whenAddingMovieList_thenThrowBadRequest() throws Exception {
Actor actor = new Actor("Actor1"); Movie movie = new Movie("$Movie2");
Movie movie = new Movie("", Arrays.asList("Drama", "History"), Arrays.asList(actor)); mvc.perform(MockMvcRequestBuilders.post("/movies")
mvc.perform(MockMvcRequestBuilders.post("/movie")
.contentType(MediaType.APPLICATION_JSON_UTF8) .contentType(MediaType.APPLICATION_JSON_UTF8)
.content(objectMapper.writeValueAsString(movie))) .content(objectMapper.writeValueAsString(Arrays.asList(movie))))
.andExpect(MockMvcResultMatchers.status() .andExpect(MockMvcResultMatchers.status()
.isBadRequest()); .isBadRequest());
} }