Skip to content

Commit

Permalink
Put Movie details added / Tests implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Krisssssssssssssssssssssss committed Oct 16, 2024
1 parent 8cb633e commit 09bc8c3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ public List<MovieResponse> getAll(){
return movieService.getAllMovies().stream().map(MovieResponse::from).collect(Collectors.toList());
}

@GetMapping("/{movieId}")
public MovieResponse getByMovieId(@PathVariable @NonNull Long movieId) {
Movie searchedMovie = movieService.getMovieById(movieId);
@GetMapping("/{id}")
public MovieResponse getByMovieId(@PathVariable @NonNull Long id) {
Movie searchedMovie = movieService.getMovieById(id);
return MovieResponse.from(searchedMovie);
}

@PutMapping("/{id}")
public MovieResponse update(@RequestBody @NotNull CreateMovieRequest movieRequest, @PathVariable Long id) {
Movie movie = movieRequest.toMovie();
movie.setId(id);
return MovieResponse.from(movieService.updateMovie(movie));
}

@DeleteMapping("/{id}")
public void delete(@PathVariable Long id){
movieService.deleteMovie(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ public void deleteMovie(Long movieId) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie with id " + movieId + " does not exist");
}
}
public Movie updateMovie(Movie movie) {
if (movieRepository.existsById(movie.getId())) {
return movieRepository.save(movie);
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie with id " + movie.getId() + " does not exist");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ void getAll() throws Exception {
.andExpect(jsonPath("$[1].name").value(NAME_DEADPOOL));

}

@Test
@DirtiesContext
void getMovie_By_ID_Test() throws Exception {
Expand All @@ -115,6 +116,7 @@ void getMovie_By_ID_Test() throws Exception {
.andExpect(jsonPath("$.id").value(ID_FIRST));

}

@Test
@DirtiesContext
void getMovie_By_NonExistingID() throws Exception {
Expand All @@ -129,6 +131,63 @@ void getMovie_By_NonExistingID() throws Exception {

}

@Test
@DirtiesContext
void updateMovie_Sucessfull() throws Exception {
movieRepository.saveAll(
List.of(
Movie.builder().name(NAME_MEMENTO).build(),
Movie.builder().name(NAME_DEADPOOL).build()
)
);
mockMvc.perform(MockMvcRequestBuilders.put(URL_BASE + "/" + ID_FIRST)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"name": "Memento Edited",
"isWatched": true,
"rating": 10
}
"""
))
.andExpect(MockMvcResultMatchers.status().isOk());
mockMvc.perform(MockMvcRequestBuilders.get(URL_BASE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].name").value("Memento Edited"))
.andExpect(jsonPath("$[1].name").value(NAME_DEADPOOL));
}
@Test
@DirtiesContext
void updateMovie_NonExistand_ID() throws Exception {
movieRepository.saveAll(
List.of(
Movie.builder().name(NAME_MEMENTO).build(),
Movie.builder().name(NAME_DEADPOOL).build()
)
);
mockMvc.perform(MockMvcRequestBuilders.put(URL_BASE + "/" + 3)
.contentType(MediaType.APPLICATION_JSON)
.content(
"""
{
"name": "Memento Edited",
"isWatched": true,
"rating": 10
}
"""
))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
mockMvc.perform(MockMvcRequestBuilders.get(URL_BASE))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].name").value(NAME_MEMENTO))
.andExpect(jsonPath("$[1].name").value(NAME_DEADPOOL));
}

@Test
@DirtiesContext
void deleteTest_Successful() throws Exception {
Expand All @@ -146,6 +205,7 @@ void deleteTest_Successful() throws Exception {
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].name").value(NAME_DEADPOOL));
}

@Test
@DirtiesContext
void deleteTest_NonExisting_ID() throws Exception {
Expand Down

0 comments on commit 09bc8c3

Please sign in to comment.