Commit 7e4ad684 authored by Maxim Svistunov's avatar Maxim Svistunov
Browse files

Implement milestone 2 feedback for Movies (mainly tests)

parent 6068347f
package cz.fi.muni.pa165.movierecommender.api.dto;
import cz.fi.muni.pa165.movierecommender.api.dto.enums.Genre;
import lombok.Data;
import lombok.EqualsAndHashCode;
......
package cz.fi.muni.pa165.movierecommender.api.dto.create;
import cz.fi.muni.pa165.movierecommender.api.dto.PersonDto;
import cz.fi.muni.pa165.movierecommender.api.dto.ReviewDto;
import cz.fi.muni.pa165.movierecommender.api.dto.SimpleUserDto;
import cz.fi.muni.pa165.movierecommender.api.dto.enums.Genre;
import lombok.Data;
import javax.validation.constraints.Min;
......
package cz.fi.muni.pa165.movierecommender.service.mapper;
import cz.fi.muni.pa165.movierecommender.api.dto.ReviewDto;
import cz.fi.muni.pa165.movierecommender.api.dto.SimpleMovieDto;
import cz.fi.muni.pa165.movierecommender.persistence.entity.Movie;
import cz.fi.muni.pa165.movierecommender.persistence.entity.Review;
import org.mapstruct.Mapper;
@Mapper
......
......@@ -4,6 +4,11 @@ import cz.fi.muni.pa165.movierecommender.persistence.entity.Movie;
import java.util.List;
/**
* @author Maxim Svistunov
*
* A service used for a movie.
*/
public interface MovieService extends GenericService<Movie> {
List<Movie> findByName(String name);
......
......@@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MovieServiceImpl extends GenericServiceImpl<Movie> implements MovieService {
......
......@@ -149,7 +149,7 @@ public class MovieRecommendationServiceTest extends ServiceTestBase {
@Test
public void recommendedForNewSharingOnlyDirector() {
Movie newQuentinMovie = new Movie("New Historical Tarantino",120,"Poster",new HashSet<Genre>(List.of(Genre.HISTORY)),"desc",1234,null,MockedEntities.QUENTIN,null);
Movie newQuentinMovie = new Movie("New Historical Tarantino",120,"Poster",new HashSet<>(List.of(Genre.HISTORY)),"desc",1234,null,MockedEntities.QUENTIN,null);
newQuentinMovie.setId(10L);
Mockito.when(movieDao.findById(newQuentinMovie.getId())).thenReturn(newQuentinMovie);
Mockito.when(movieDao.findByName(newQuentinMovie.getName())).thenReturn(List.of(newQuentinMovie));
......
......@@ -2,106 +2,107 @@ package cz.fi.muni.pa165.movierecommender.service;
import cz.fi.muni.pa165.movierecommender.persistence.dao.MovieDao;
import cz.fi.muni.pa165.movierecommender.persistence.entity.Movie;
import cz.fi.muni.pa165.movierecommender.persistence.entity.Person;
import cz.fi.muni.pa165.movierecommender.persistence.enums.Genre;
import cz.fi.muni.pa165.movierecommender.service.config.ServiceConfiguration;
import cz.fi.muni.pa165.movierecommender.service.service.MovieService;
import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.time.LocalDate;
import java.time.Month;
import java.util.HashSet;
import cz.fi.muni.pa165.movierecommender.service.service.MovieServiceImpl;
import cz.fi.muni.pa165.movierecommender.service.service.exception.BadArgumentException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import java.util.List;
import static org.mockito.Mockito.mock;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.AssertJUnit.assertEquals;
@ContextConfiguration(classes = ServiceConfiguration.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@Transactional
public class MovieServiceTest extends AbstractTestNGSpringContextTests {
private MovieDao movieDao;
@Autowired
MovieService movieService;
@BeforeMethod
public void setup() {
movieDao = mock(MovieDao.class);
Movie reservoirdogs = new Movie();
reservoirdogs.setName("Reservoir Dogs");
reservoirdogs.setDescription("Tarantino's first really successful film");
reservoirdogs.setDuration(120);
reservoirdogs.setReleaseYear(1992);
reservoirdogs.setPoster("https://img.csfd.cz/files/images/user/profile/159/803/159803949_909f86.jpg");
HashSet<Genre> genres = new HashSet<>();
genres.add(Genre.CRIME);
genres.add(Genre.ACTION);
reservoirdogs.setGenres(genres);
Movie dogs = new Movie();
dogs.setName("Dogs");
// dogs.setGenres(new HashSet<>(Genre.CRIME, Genre.ACTION));
dogs.setDuration(75);
Movie fiction = new Movie();
fiction.setName("Pulp Fiction");
dogs.setDuration(145);
Movie jackie = new Movie();
jackie.setName("Jackie Brown");
jackie.setDuration(154);
Person tim = new Person();
tim.setName("Tim Roth");
tim.setBirth(LocalDate.of(1961, Month.MAY, 14));
tim.setAbout("About Tim");
tim.setPicture("https://m.media-amazon.com/images/M/MV5BMjA5NTA3MDQyOV5BMl5BanBnXkFtZTcwODM4NDE3Mw@@._V1_UY1200_CR153,0,630,1200_AL_.jpg");
Person quentin = new Person();
quentin.setName("Quentin Tarantino");
quentin.setBirth(LocalDate.of(1963, Month.MARCH, 27));
quentin.setAbout("About Quentin");
quentin.setPicture("https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Quentin_Tarantino_by_Gage_Skidmore.jpg/225px-Quentin_Tarantino_by_Gage_Skidmore.jpg");
Person michael = new Person();
michael.setName("Michael Madsen");
michael.setBirth(LocalDate.of(1957, Month.SEPTEMBER, 25));
michael.setAbout("About Michael");
michael.setPicture("https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcQ0-eFDys64AccHCm7s8vnoVwDFh95EFa56Gs2JPJAexGc_xNKb");
reservoirdogs.setDirector(quentin);
HashSet<Person> actors = new HashSet<>();
actors.add(tim);
actors.add(quentin);
actors.add(michael);
reservoirdogs.setActors(actors);
when(movieDao.findAll()).thenReturn(java.util.Arrays.stream(Arrays.array(dogs, fiction, jackie)).toList());
when(movieDao.findByName("Dogs")).thenReturn(java.util.Arrays.stream(Arrays.array(dogs, reservoirdogs)).toList());
/**
* @author Maxim Svistunov
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class MovieServiceTest extends ServiceTestBase {
@Mock
private MovieDao dao;
MovieService service;
Movie movie1 = MockedEntities.RESERVOIR_DOGS;
Movie movie2 = MockedEntities.MONTY_PYTHON;
Movie movie3 = MockedEntities.PULP_FICTION;
Movie nonExistingMovie = MockedEntities.NON_EXISTENT_MOVIE;
@Override
protected void assignService() {
service = new MovieServiceImpl(dao);
}
@Override
protected void mockRepositoryMethods() {
List<Movie> allMovies = List.of(movie1, movie2, movie3);
when(dao.findAll()).thenReturn(allMovies);
for (Movie movie: allMovies) {
when(dao.findById(movie.getId())).thenReturn(movie);
when(dao.findByName(movie.getName())).thenReturn(List.of(movie));
}
}
@Test
public void findAll() {
List<Movie> movieList = movieDao.findAll();
assertEquals(movieList.size(), 3);
List<Movie> movies = service.findAll();
assertThat(movies).isNotNull();
assertThat(movies).containsExactlyInAnyOrder(movie1, movie2, movie3);
}
@Test
public void findByName() {
List<Movie> movieList = movieDao.findByName("Dogs");
assertEquals(movieList.size(), 2);
List<Movie> movies = service.findByName(movie1.getName());
assertThat(movies).isNotNull();
assertThat(movies).containsExactlyInAnyOrder(movie1);
}
@Test
public void create() {
service.create(nonExistingMovie);
verify(dao, Mockito.times(1)).create(nonExistingMovie);
}
@Test
public void createNull() {
Assertions.assertThatThrownBy(() -> service.create(null)).isInstanceOf(BadArgumentException.class);
}
@Test
public void update() {
Movie entityToUpdate = service.findById(movie1.getId());
entityToUpdate.setName("different name");
service.update(entityToUpdate);
verify(dao, Mockito.times(1)).update(entityToUpdate);
}
@Test
public void updateNull() {
Assertions.assertThatThrownBy(() -> service.update(null)).isInstanceOf(BadArgumentException.class);
}
@Test
public void findByIdNull() {
Assertions.assertThatThrownBy(() -> service.findById(null)).isInstanceOf(BadArgumentException.class);
}
@Test
public void delete() {
service.delete(movie1);
Mockito.verify(dao, Mockito.times(1)).delete(movie1);
}
@Test
public void deleteNull() {
Assertions.assertThatThrownBy(() -> service.delete(null)).isInstanceOf(BadArgumentException.class);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment