ЛР3 добавление тестов

This commit is contained in:
ityurner02@mail.ru 2023-03-27 19:36:00 +04:00
parent 3ef4a530ee
commit 1880d0dc53
12 changed files with 323 additions and 28 deletions

Binary file not shown.

View File

@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.is.lab1.DataBase.model.Collection; import ru.ulstu.is.lab1.DataBase.model.Collection;
import ru.ulstu.is.lab1.DataBase.model.Film;
import ru.ulstu.is.lab1.DataBase.service.CollectionService; import ru.ulstu.is.lab1.DataBase.service.CollectionService;
import java.util.List; import java.util.List;
@ -49,6 +50,11 @@ public class CollectionController {
return collectionService.addFilm(id, film_id); return collectionService.addFilm(id, film_id);
} }
@DeleteMapping("/remove_film/{id}")
public Collection removeFilm(@PathVariable Long id, @RequestParam Long film_id) {
return collectionService.removeFilm(id, film_id);
}
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public Collection deleteCollection(@PathVariable Long id) { public Collection deleteCollection(@PathVariable Long id) {
return collectionService.deleteCollection(id); return collectionService.deleteCollection(id);

View File

@ -52,6 +52,11 @@ public class FilmController {
return filmService.addGenre(id, genre_id); return filmService.addGenre(id, genre_id);
} }
@DeleteMapping("/remove_genre/{id}")
public Film removeGenre(@PathVariable Long id, @RequestParam Long genre_id) {
return filmService.removeGenre(id, genre_id);
}
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public Film deleteFilm(@PathVariable Long id) { public Film deleteFilm(@PathVariable Long id) {
return filmService.deleteFilm(id); return filmService.deleteFilm(id);

View File

@ -2,7 +2,9 @@ package ru.ulstu.is.lab1.DataBase.model;
import javax.persistence.*; import javax.persistence.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
@Entity @Entity
public class Collection { public class Collection {
@ -36,8 +38,33 @@ public class Collection {
this.name = name; this.name = name;
} }
public void addFilm(Film f) { public void addFilm(Film film) {
films.add(f); if (films == null){
films = new ArrayList<>();
}
this.films.add(film);
}
public void removeFilm(Film film){
if(films != null){
films.remove(film);
}
}
public List<Film> getFilms() {
return films;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Collection collection = (Collection) o;
return Objects.equals(id, collection.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
} }
@Override @Override

View File

@ -3,6 +3,8 @@ package ru.ulstu.is.lab1.DataBase.model;
import javax.persistence.*; import javax.persistence.*;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Objects;
@Entity @Entity
public class Film { public class Film {
@ -36,8 +38,33 @@ public class Film {
this.name = name; this.name = name;
} }
public void addGenre(Genre g) { public void addGenre(Genre genre) {
genres.add(g); if (genres == null){
genres = new ArrayList<>();
}
this.genres.add(genre);
}
public void removeGenre(Genre genre){
if(genres != null){
genres.remove(genre);
}
}
public List<Genre> getGenres() {
return genres;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Film film = (Film) o;
return Objects.equals(id, film.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
} }
@Override @Override

View File

@ -3,6 +3,7 @@ package ru.ulstu.is.lab1.DataBase.model;
import javax.persistence.*; import javax.persistence.*;
import java.util.List; import java.util.List;
import java.util.Objects;
@Entity @Entity
public class Genre { public class Genre {
@ -32,6 +33,18 @@ public class Genre {
this.name = name; this.name = name;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Genre genre = (Genre) o;
return Objects.equals(id, genre.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override @Override
public String toString() { public String toString() {
return "Genre{" + return "Genre{" +

View File

@ -43,6 +43,17 @@ public class CollectionService {
return em.merge(collection); return em.merge(collection);
} }
@Transactional
public Collection removeFilm(Long collectionId, Long filmId){
final Collection collection = em.find(Collection.class, collectionId);
final Film film = em.find(Film.class, filmId);
if(filmId <= 0 || film == null){
throw new IllegalArgumentException("Film with id [%s] is not found");
}
collection.removeFilm(film);
return em.merge(collection);
}
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Collection findCollection(Long id) { public Collection findCollection(Long id) {
final Collection collection = em.find(Collection.class, id); final Collection collection = em.find(Collection.class, id);

View File

@ -42,6 +42,17 @@ public class FilmService {
return em.merge(film); return em.merge(film);
} }
@Transactional
public Film removeGenre(Long filmId, Long genreId){
final Film film = em.find(Film.class, filmId);
final Genre genre = em.find(Genre.class, genreId);
if(genreId <= 0 || genre == null){
throw new IllegalArgumentException("Genre with id [%s] is not found");
}
film.removeGenre(genre);
return em.merge(film);
}
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Film findFilm(Long id) { public Film findFilm(Long id) {
final Film film = em.find(Film.class, id); final Film film = em.find(Film.class, id);

View File

@ -0,0 +1,80 @@
package ru.ulstu.is.lab1;
import ru.ulstu.is.lab1.DataBase.model.Film;
import ru.ulstu.is.lab1.DataBase.model.Genre;
import ru.ulstu.is.lab1.DataBase.service.FilmService;
import ru.ulstu.is.lab1.DataBase.model.Collection;
import ru.ulstu.is.lab1.DataBase.service.CollectionService;
import javax.persistence.EntityNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.lab1.DataBase.service.GenreService;
import java.util.List;
@SpringBootTest
public class JpaCollectionTests {
private static final Logger log = LoggerFactory.getLogger(JpaFilmTests.class);
@Autowired
private CollectionService collectionService;
@Autowired
private FilmService filmService;
@Test
void testCollectionsCreate(){
collectionService.deleteAllCollections();
filmService.deleteAllFilms();
final Film film1 = filmService.addFilm("Джокер");
final Film film2 = filmService.addFilm("Учитель на замену");
final Collection collection1 = collectionService.addCollection("Грустное");
collection1.addFilm(film1);
collection1.addFilm(film2);
final Collection collection2 = collectionService.addCollection("Об антигерое");
collection2.addFilm(film1);
log.info("testCreateCollection " + collection1.toString());
log.info("testCreateCollection " + collection2.toString());
Assertions.assertNotNull(collection1.getId());
Assertions.assertNotNull(collection2.getId());
}
@Test
void testCollectionRead() {
collectionService.deleteAllCollections();
filmService.deleteAllFilms();
final Film film = filmService.addFilm("Астрал");
final Collection collection = collectionService.addCollection("Пугающее");
collection.addFilm(film);
log.info("testCollectionRead[0]: " + collection.toString());
final Collection findCollection = collectionService.findCollection(collection.getId());
log.info("testCollectionRead[1]: " + findCollection.toString());
Assertions.assertEquals(collection, findCollection);
}
@Test
void testAddFilms(){
collectionService.deleteAllCollections();
filmService.deleteAllFilms();
final Film film1 = filmService.addFilm("Начало");
final Film film2 = filmService.addFilm("Пункт назначения");
final Film film3 = filmService.addFilm("День сурка");
final Collection collection = collectionService.addCollection("Избранное");
collection.addFilm(film1);
collection.addFilm(film2);
collection.addFilm(film3);
log.info("testAddFilms[0]: " + collection.toString());
Assertions.assertEquals(collection.getFilms().size(), 3);
}
@Test
void testCollectionReadNotFound() {
collectionService.deleteAllCollections();
Assertions.assertThrows(EntityNotFoundException.class, () -> collectionService.findCollection(-1L));
}
@Test
void testCollectionsReadAllEmpty() {
collectionService.deleteAllCollections();
final List<Collection> collections = collectionService.findAllCollections();
log.info("testCollectionReadAllEmpty: " + collections.toString());
Assertions.assertEquals(collections.size(), 0);
}
}

View File

@ -0,0 +1,78 @@
package ru.ulstu.is.lab1;
import ru.ulstu.is.lab1.DataBase.model.Genre;
import ru.ulstu.is.lab1.DataBase.service.GenreService;
import ru.ulstu.is.lab1.DataBase.model.Film;
import ru.ulstu.is.lab1.DataBase.service.FilmService;
import javax.persistence.EntityNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class JpaFilmTests {
private static final Logger log = LoggerFactory.getLogger(JpaFilmTests.class);
@Autowired
private FilmService filmService;
@Autowired
private GenreService genreService;
@Test
void testFilmsCreate(){
filmService.deleteAllFilms();
genreService.deleteAllGenres();
final Genre genre1 = genreService.addGenre("Фантастика");
final Genre genre2 = genreService.addGenre("Комедия");
final Film film1 = filmService.addFilm("Сапожник");
film1.addGenre(genre1);
film1.addGenre(genre2);
final Film film2 = filmService.addFilm("Маска");
film2.addGenre(genre1);
log.info("testCreateFilm " + film1.toString());
log.info("testCreateFilm " + film2.toString());
Assertions.assertNotNull(film1.getId());
Assertions.assertNotNull(film2.getId());
}
@Test
void testFilmRead() {
filmService.deleteAllFilms();
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Ужасы");
final Film film = filmService.addFilm("Оно");
film.addGenre(genre);
log.info("testFilmRead[0]: " + film.toString());
final Film findFilm = filmService.findFilm(film.getId());
log.info("testFilmRead[1]: " + findFilm.toString());
Assertions.assertEquals(film, findFilm);
}
@Test
void testAddGenres(){
filmService.deleteAllFilms();
genreService.deleteAllGenres();
final Genre genre1 = genreService.addGenre("Драма");
final Genre genre2 = genreService.addGenre("Триллер");
final Genre genre3 = genreService.addGenre("Боевик");
final Film film = filmService.addFilm("Брат");
film.addGenre(genre1);
film.addGenre(genre2);
film.addGenre(genre3);
log.info("testAddGenres[0]: " + film.toString());
Assertions.assertEquals(film.getGenres().size(), 3);
}
@Test
void testFilmReadNotFound() {
filmService.deleteAllFilms();
Assertions.assertThrows(EntityNotFoundException.class, () -> filmService.findFilm(-1L));
}
@Test
void testFilmsReadAllEmpty() {
filmService.deleteAllFilms();
final List<Film> films = filmService.findAllFilms();
log.info("testFilmReadAllEmpty: " + films.toString());
Assertions.assertEquals(films.size(), 0);
}
}

View File

@ -0,0 +1,61 @@
package ru.ulstu.is.lab1;
import ru.ulstu.is.lab1.DataBase.model.Genre;
import ru.ulstu.is.lab1.DataBase.service.GenreService;
import javax.persistence.EntityNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class JpaGenreTests {
private static final Logger log = LoggerFactory.getLogger(JpaGenreTests.class);
@Autowired
private GenreService genreService;
@Test
void testGenresCreate(){
genreService.deleteAllGenres();
final Genre genre1 = genreService.addGenre("Фантастика");
final Genre genre2 = genreService.addGenre("Комедия");
log.info("testCreateGenre " + genre1.toString());
log.info("testCreateGenre " + genre2.toString());
Assertions.assertNotNull(genre1.getId());
Assertions.assertNotNull(genre2.getId());
}
@Test
void testGenreRead() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Ужасы");
log.info("testGenreRead[0]: " + genre.toString());
final Genre findGenre = genreService.findGenre(genre.getId());
log.info("testGenreRead[1]: " + findGenre.toString());
Assertions.assertEquals(genre, findGenre);
}
@Test
void testGenreReadNotFound() {
genreService.deleteAllGenres();
Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(-1L));
}
@Test
void testGenreReadAll() {
genreService.deleteAllGenres();
genreService.addGenre("Драма");
genreService.addGenre("Триллер");
final List<Genre> genres = genreService.findAllGenres();
log.info("testGenreReadAll: " + genres.toString());
Assertions.assertEquals(genres.size(), 2);
genreService.deleteAllGenres();
}
@Test
void testGenreReadAllEmpty() {
genreService.deleteAllGenres();
final List<Genre> genres = genreService.findAllGenres();
log.info("testGenreReadAllEmpty: " + genres.toString());
Assertions.assertEquals(genres.size(), 0);
}
}

View File

@ -1,24 +0,0 @@
package ru.ulstu.is.lab1;
import ru.ulstu.is.lab1.DataBase.model.Film;
import ru.ulstu.is.lab1.DataBase.model.Collection;
import ru.ulstu.is.lab1.DataBase.model.Genre;
import ru.ulstu.is.lab1.DataBase.service.FilmService;
import ru.ulstu.is.lab1.DataBase.service.CollectionService;
import ru.ulstu.is.lab1.DataBase.service.GenreService;
import javax.persistence.EntityNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@SpringBootTest
class Lab1ApplicationTests {
}