Skip to content

Commit

Permalink
fixed bugs from merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabi911 committed Aug 19, 2024
1 parent 29d21dd commit 1756124
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@ public class MovieController {
private final MovieService movieService;

@GetMapping()

public List<Movie> getMovies() {
return movieService.getAllMovies();
}


@GetMapping("{id}")
public ResponseEntity<Movie> getMovieById(@PathVariable String id) {
Movie movie= movieService.getMovieById(id);
Movie movie = movieService.getMovieById(id);
if (movie == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(movie);

}
@PostMapping()
public Movie postMovie(@RequestBody MovieDto userEntries){
public Movie postMovie (@RequestBody MovieDto userEntries) {
return movieService.addMovie(userEntries);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public List<Movie> getAllMovies() {
@Override
public Movie getMovieById(String id) {
return movieRepository.findById(id).orElse(null);

}
@Override
public Movie addMovie(MovieDto userEntries){
Movie newMovie = new Movie(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package de.webdev.backend.controller;

import de.webdev.backend.model.Movie;
import de.webdev.backend.service.MovieService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.boot.test.mock.mockito.MockBean;

import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;

Expand All @@ -17,53 +13,44 @@
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;


import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.time.LocalDateTime;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;


@SpringBootTest
@AutoConfigureMockMvc
class MovieControllerTest {

LocalDateTime publicationDate = LocalDateTime.of(2023, 8, 16, 14, 30, 0);
@Autowired
private MockMvc mockMvc;

@MockBean
private MovieService movieService;


@Test
@DirtiesContext
void getMovie_shouldRetunEmptyList_whenCallInitially() throws Exception {
void getMovie_shouldReturnEmptyList_whenCallInitially() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/api/movies"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json("[]"));
}

@Test
void getMovieById_shouldReturnMovie_whenIdExists() throws Exception {
Movie movie = new Movie("1", "Title", "Author");
when(movieService.getMovieById("1")).thenReturn(movie);

mockMvc.perform(MockMvcRequestBuilders.get("/api/movies/1"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json("{\"id\":\"1\",\"title\":\"Title\",\"author\":\"Author\"}"));
}

@Test
@DirtiesContext
void getMovieById_shouldReturnNotFound_whenIdDoesNotExist() throws Exception {
when(movieService.getMovieById("999")).thenReturn(null);
mockMvc.perform(MockMvcRequestBuilders.get("/api/movies/999"))
.andExpect(MockMvcResultMatchers.status().isNotFound());
}

@Test
@DirtiesContext
void postMovie_shouldReturnANewMovie() throws Exception{
mockMvc.perform(MockMvcRequestBuilders
.post("/api/movies")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"author": "exampleAuthor",
"title": "exampleTitle",
Expand All @@ -75,13 +62,14 @@ void postMovie_shouldReturnANewMovie() throws Exception{
))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json("""
{
"author": "exampleAuthor",
"title": "exampleTitle",
"genre": "drama",
"publicationDate": "2023-08-16T14:30:00"
}
"""
"""
))
.andExpect(jsonPath("$.id").exists());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void getAllMovies() {
@Test

void getMovieById_shouldReturnMovie_whenIdExists() {
Movie movie = new Movie("1", "First movie", "First author");
Movie movie = new Movie("1", "First movie", "First author", "exampleGenre", publicationDate);
when(movieRepository.findById("1")).thenReturn(Optional.of(movie));

Movie actual = movieService.getMovieById("1");
Expand All @@ -57,7 +57,8 @@ void getMovieById_shouldReturnNull_whenIdDoesNotExist() {

assertNull(actual);
verify(movieRepository).findById("999");

}
@Test
void addMovie() {


Expand Down

0 comments on commit 1756124

Please sign in to comment.