добавлены тесты и функция вывода количества просмотров у фильма

This commit is contained in:
ekallin 2024-03-18 19:19:34 +04:00
parent 0fba5a7053
commit 94c977fc81
8 changed files with 252 additions and 19 deletions

View File

@ -17,9 +17,7 @@ public class TestDTO {
public TestDTO(
@JsonProperty(value = "id") Integer id,
@JsonProperty(value = "name") String name,
@JsonProperty(value = "img") String img
)
{
@JsonProperty(value = "img") String img) {
this.id = id;
this.name = name;
this.img = img;
@ -35,7 +33,7 @@ public class TestDTO {
return name;
}
public String getImg(){
public String getImg() {
return img;
}
@ -49,7 +47,7 @@ public class TestDTO {
name = Name;
}
public void setImg(String Img){
public void setImg(String Img) {
img = Img;
}
}

View File

@ -70,4 +70,9 @@ public class MovieController {
return toDto(movieService.delete(id));
}
@GetMapping("/countView")
public Integer countView(@RequestParam(name = "movieId", defaultValue = "0") Integer movieId) {
return movieService.countViews(movieId);
}
}

View File

@ -9,14 +9,17 @@ import org.springframework.stereotype.Service;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.movies.model.MovieEntity;
import com.example.backend.movies.repository.MovieRepository;
import com.example.backend.viewed.service.ViewedService;
@Service
public class MovieService {
private final MovieRepository repository;
private final ViewedService viewedService;
public MovieService(MovieRepository repository) {
public MovieService(MovieRepository repository, ViewedService viewedService) {
this.repository = repository;
this.viewedService = viewedService;
}
public List<MovieEntity> getAll(Integer categorieId) {
@ -50,4 +53,10 @@ public class MovieService {
final MovieEntity exisEntity = get(id);
return repository.delete(exisEntity);
}
public Integer countViews(Integer movieId) {
return viewedService.countViewed(movieId);
}
}

View File

@ -27,6 +27,19 @@ public class ViewedService {
.toList();
}
public Integer countViewed(Integer movieId) {
if (Objects.equals(movieId, 0)) {
return repository.getAll().size();
}
return repository.getAll().stream()
.filter(item -> item.getMovie().getId().equals(
movieId))
.toList().size();
// return (int) repository.getAll().stream().filter(item ->
// item.getMovie().getId().equals(movieId)).count();
}
public ViewedEntity get(Integer id) {
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}

View File

@ -1,13 +0,0 @@
package com.example.backend;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class BackendApplicationTests {
@Test
void contextLoads() {
}
}

View File

@ -0,0 +1,76 @@
package com.example.backend;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.favorites.model.FavoriteEntity;
import com.example.backend.favorites.service.FavoriteService;
import com.example.backend.movies.model.MovieEntity;
import com.example.backend.users.model.UserEntity;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class FavoriteServiceTests {
@Autowired
private FavoriteService favoriteService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> favoriteService.get(0));
}
@Test
@Order(1)
void createTest() {
final CategorieEntity cat1 = new CategorieEntity(1, "Драма", "");
final MovieEntity mov1 = new MovieEntity(1, cat1, "Хатико", "какой-то фильм", "", "");
final MovieEntity mov2 = new MovieEntity(2, cat1, "Унесеснные призраками", "какой-то фильм", "", "");
final UserEntity us1 = new UserEntity(1, "elina", "123", true);
favoriteService.create(new FavoriteEntity(1, us1, mov1));
final FavoriteEntity lastFavorite = favoriteService.create(new FavoriteEntity(2, us1, mov2));
Assertions.assertEquals(lastFavorite, favoriteService.get(2));
Assertions.assertEquals(2, favoriteService.getAll(1).size());
}
@Test
@Order(2)
void update() {
final CategorieEntity cat1 = new CategorieEntity(1, "Драма", "");
final MovieEntity mov2 = new MovieEntity(1, cat1, "Унесеснные призраками",
"какой-то фильм", "", "");
final UserEntity us1 = new UserEntity(1, "elina", "123", true);
final FavoriteEntity fav = new FavoriteEntity(1, us1, mov2);
final FavoriteEntity oldFav = favoriteService.get(1);
final FavoriteEntity newFav = favoriteService.update(2, fav);
Assertions.assertNotEquals(oldFav.getMovie().getName(),
newFav.getMovie().getName());
}
@Test
@Order(3)
void delete() {
favoriteService.delete(1);
Assertions.assertEquals(1, favoriteService.getAll(1).size());
}
}

View File

@ -0,0 +1,88 @@
package com.example.backend;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.backend.categories.model.CategorieEntity;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.movies.model.MovieEntity;
import com.example.backend.movies.service.MovieService;
import com.example.backend.users.model.UserEntity;
import com.example.backend.viewed.model.ViewedEntity;
import com.example.backend.viewed.service.ViewedService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class MovieServiceTest {
@Autowired
private MovieService movieService;
@Autowired
private ViewedService viewedService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> movieService.get(0));
Assertions.assertThrows(NotFoundException.class, () -> viewedService.get(0));
}
@Test
@Order(1)
void createTest() {
final CategorieEntity cat1 = new CategorieEntity(1, "Драма", "");
movieService.create(new MovieEntity(1, cat1, "Хатико", "какой-то фильм", "", ""));
final MovieEntity lastMovie = movieService.create(new MovieEntity(2, cat1, "Любовь и голуби", "", "", ""));
Assertions.assertEquals(lastMovie, movieService.get(2));
Assertions.assertEquals(2, movieService.getAll(1).size());
}
@Test
@Order(2)
void update() {
final CategorieEntity newCategorie = new CategorieEntity(1, "Комедия", "");
final MovieEntity entity = movieService.get(2);
final String oldName = entity.getCategorie().getName();
final MovieEntity newEntity = movieService.update(2,
new MovieEntity(2, newCategorie, "Любовь и голуби", "", "", ""));
Assertions.assertEquals(newCategorie.getName(), newEntity.getCategorie().getName());
Assertions.assertNotEquals(oldName, "Аниме");
}
@Test
@Order(3)
void delete() {
movieService.delete(2);
Assertions.assertNotEquals(2, movieService.getAll(1).size());
}
@Test
@Order(4)
void checkCounViewed() {
final UserEntity us1 = new UserEntity(1, "", "", true);
final UserEntity us2 = new UserEntity(2, "", "", true);
final UserEntity us3 = new UserEntity(3, "", "", true);
final CategorieEntity cat1 = new CategorieEntity(1, "", "");
final MovieEntity mov1 = new MovieEntity(1, cat1, "", "", "", "");
viewedService.create(new ViewedEntity(1, us1, mov1));
viewedService.create(new ViewedEntity(2, us2, mov1));
viewedService.create(new ViewedEntity(3, us3, mov1));
final Integer count = movieService.countViews(1);
Assertions.assertEquals(3, count);
}
}

View File

@ -0,0 +1,57 @@
package com.example.backend;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.users.model.UserEntity;
import com.example.backend.users.service.UserService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
public class UserServiceTests {
@Autowired
private UserService userService;
@Test
void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> userService.get(0));
}
@Test
@Order(1)
void createTest() {
final UserEntity firstUser = userService.create(new UserEntity(null, "elina", "123", true));
userService.create(new UserEntity(null, "oleg", "789", false));
userService.create(new UserEntity(null, "nikita", "456", false));
Assertions.assertEquals(firstUser, userService.get(1));
Assertions.assertEquals(3, userService.getAll().size());
}
@Test
@Order(2)
void update() {
final String newPassword = "000";
final UserEntity existEntity = userService.get(1);
final String oldPassword = existEntity.getPassword();
final UserEntity entity = new UserEntity(1, existEntity.getUsername(), newPassword, existEntity.getIsAdmin());
final UserEntity newEntity = userService.update(1, entity);
Assertions.assertEquals(newPassword, newEntity.getPassword());
Assertions.assertNotEquals(oldPassword, newEntity.getPassword());
}
@Test
@Order(3)
void delete() {
userService.delete(1);
Assertions.assertEquals(2, userService.getAll().size());
}
}