diff --git a/data.mv.db b/data.mv.db index 1d03883..8989e4e 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index 6ef2691..766cd00 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -47,29 +47,29 @@ public class DemoApplication implements CommandLineRunner { @Override public void run(String... args) throws Exception{ log.info("start"); - final var type1 = typeService.create(new TypeEntity("ААА")); - final var type2 = typeService.create(new TypeEntity("АА")); + // final var type1 = typeService.create(new TypeEntity("ААА")); + // final var type2 = typeService.create(new TypeEntity("АА")); - final var genre1 = genreService.create(new GenreEntity("Приключения")); - final var genre2 = genreService.create(new GenreEntity("Симулятор")); + // final var genre1 = genreService.create(new GenreEntity("Приключения")); + // final var genre2 = genreService.create(new GenreEntity("Симулятор")); - final List genres1 = new ArrayList(); - genres1.add(genre1); - genres1.add(genre2); + // final List genres1 = new ArrayList(); + // genres1.add(genre1); + // genres1.add(genre2); - final List genres2 = new ArrayList(); - genres2.add(genre2); + // final List genres2 = new ArrayList(); + // genres2.add(genre2); - final var game1 = gameService.create(new GameEntity(type1,"Game1",2100.0,"good game", genres1)); - final var game2 = gameService.create(new GameEntity( type2, "Game2", 1200.0,"bad game", genres2)); - final List games = new ArrayList(); - games.add(game1); - games.add(game2); + // final var game1 = gameService.create(new GameEntity(type1,"Game1",2100.0,"good game", genres1)); + // final var game2 = gameService.create(new GameEntity( type2, "Game2", 1200.0,"bad game", genres2)); + // final List games = new ArrayList(); + // games.add(game1); + // games.add(game2); - var user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123")); - var user2 = userService.create(new UserEntity( "login2", "email@gmail.com", "qwerty1234")); + // var user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123")); + // var user2 = userService.create(new UserEntity( "login2", "email@gmail.com", "qwerty1234")); - orderService.create(7, new OrderEntity(user1,games)); - orderService.create(8, new OrderEntity(user2,games)); + // orderService.create(7, new OrderEntity(user1,games)); + // orderService.create(8, new OrderEntity(user2,games)); } } diff --git a/src/main/java/com/example/demo/games/api/GameController.java b/src/main/java/com/example/demo/games/api/GameController.java index f45650a..7b8f82f 100644 --- a/src/main/java/com/example/demo/games/api/GameController.java +++ b/src/main/java/com/example/demo/games/api/GameController.java @@ -1,5 +1,7 @@ package com.example.demo.games.api; +import java.util.List; + import org.modelmapper.ModelMapper; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -53,8 +55,9 @@ public class GameController { final GameEntity entity = modelMapper.map(dto, GameEntity.class); entity.setType(typeService.get(dto.getTypeId())); var genres = dto.getGenres(); - for(var genre : genres){ - entity.setGenres(genreService.get(genre)); + List genresList = genreService.getAllById(genres); + for(var genre : genresList){ + entity.setGenres(genre); } return entity; } diff --git a/src/main/java/com/example/demo/games/repository/GameRepository.java b/src/main/java/com/example/demo/games/repository/GameRepository.java index ffbd8c0..f140065 100644 --- a/src/main/java/com/example/demo/games/repository/GameRepository.java +++ b/src/main/java/com/example/demo/games/repository/GameRepository.java @@ -1,5 +1,6 @@ package com.example.demo.games.repository; +import java.util.List; import java.util.Optional; import org.springframework.data.domain.Page; @@ -9,16 +10,32 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; import com.example.demo.games.model.GameEntity; -import com.example.demo.genres.model.GenreEntity; public interface GameRepository extends CrudRepository, PagingAndSortingRepository { + + @Query("select distinct g from GameEntity g join fetch g.genres ge join fetch g.type ty where g.id = ?1") Optional findOneById(long id); - Page findByTypeIdAndGenres(long typeId, GenreEntity genre, Pageable pageable); + + @Query("select distinct g from GameEntity g join fetch g.genres ge join fetch g.type ty where ge.id = ?2 and ty.id = ?1") + Page findByTypeIdAndGenres(long typeId, long genreId, Pageable pageable); + @Query("select distinct g from GameEntity g join fetch g.genres ge join fetch g.type ty where ge.id = ?2 and ty.id = ?1") + List findByTypeIdAndGenres(long typeId, long genreId); + + @Query("select distinct g from GameEntity g join fetch g.type ty where ty.id = ?1") Page findByTypeId(long typeId, Pageable pageable); - //Page findByGenres(GenreEntity genre, Pageable pageable); + @Query("select distinct g from GameEntity g join fetch g.type ty where ty.id = ?1") + List findByTypeId(long typeId); @Query("select distinct g from GameEntity g join fetch g.genres ge where ge.id = ?1") Page findByGenres(long genre, Pageable pageable); + @Query("select distinct g from GameEntity g join fetch g.genres ge where ge.id = ?1") + List findByGenres(long genre); + + @Query("select distinct g from GameEntity g join fetch g.genres join fetch g.type ty") + PagefindAll(Pageable pageable); + @Query("select distinct g from GameEntity g join fetch g.genres join fetch g.type ty") + ListfindAll(); + } /* "select " diff --git a/src/main/java/com/example/demo/games/service/GameService.java b/src/main/java/com/example/demo/games/service/GameService.java index 22974df..cd9279a 100644 --- a/src/main/java/com/example/demo/games/service/GameService.java +++ b/src/main/java/com/example/demo/games/service/GameService.java @@ -1,5 +1,6 @@ package com.example.demo.games.service; +import java.util.List; //import java.util.List; import java.util.Objects; import org.springframework.data.domain.Page; @@ -11,46 +12,37 @@ import org.springframework.data.domain.Pageable; import com.example.demo.core.error.NotFoundException; import com.example.demo.games.model.GameEntity; import com.example.demo.games.repository.GameRepository; -import com.example.demo.genres.model.GenreEntity; import com.example.demo.genres.service.GenreService; @Service public class GameService { private final GameRepository repository; - private final GenreService genreService; public GameService(GameRepository repository, GenreService genreService){ this.repository = repository; - this.genreService = genreService; } - // @Transactional(readOnly = true) - // public Page getAll(Long typeId, List genres, int page, int size){ - // final Pageable pageRequest = PageRequest.of(page, size); + @Transactional(readOnly = true) + public List getAll(long typeId, long genre){ - // if(!Objects.equals(typeId, 0L) && !Objects.equals(genres.size(), 0)){ - // return repository.findByTypeIdAndGenres(typeId, genres, pageRequest); - // } - // if(Objects.equals(typeId, 0L) && !Objects.equals(genres.size(), 0)){ - // return repository.findByGenres(genres, pageRequest); - // } - // if(!Objects.equals(typeId, 0L) && Objects.equals(genres.size(), 0)){ - // return repository.findByTypeId(typeId, pageRequest); - // } - // return repository.findAll(PageRequest.of(page, size)); - // } - //getAll.stream().filter(game -> game.getType().getId().equals(typeId)).toList(); + if(!Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){ + return repository.findByTypeIdAndGenres(typeId, genre); + } + if(Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){ + return repository.findByGenres(genre); + } + if(!Objects.equals(typeId, 0L) && Objects.equals(genre, 0L)){ + return repository.findByTypeId(typeId); + } + return repository.findAll(); + } @Transactional(readOnly = true) public Page getAll(long typeId, long genre, int page, int size){ final Pageable pageRequest = PageRequest.of(page, size); - GenreEntity NeedGenre = null; - if(genre != 0){ - NeedGenre = genreService.get(genre); - } if(!Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){ - return repository.findByTypeIdAndGenres(typeId, NeedGenre, pageRequest); + return repository.findByTypeIdAndGenres(typeId, genre, pageRequest); } if(Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){ return repository.findByGenres(genre, pageRequest); diff --git a/src/main/java/com/example/demo/genres/service/GenreService.java b/src/main/java/com/example/demo/genres/service/GenreService.java index aea07a9..bb5d62a 100644 --- a/src/main/java/com/example/demo/genres/service/GenreService.java +++ b/src/main/java/com/example/demo/genres/service/GenreService.java @@ -23,6 +23,11 @@ public class GenreService { public List getAll(){ return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); } + + @Transactional(readOnly = true) + public List getAllById(List listIds){ + return StreamSupport.stream(repository.findAllById(listIds).spliterator(),false).toList(); + } @Transactional(readOnly = true) public GenreEntity get(Long id){ diff --git a/src/main/java/com/example/demo/orders/model/OrderEntity.java b/src/main/java/com/example/demo/orders/model/OrderEntity.java index 732317a..e71cd60 100644 --- a/src/main/java/com/example/demo/orders/model/OrderEntity.java +++ b/src/main/java/com/example/demo/orders/model/OrderEntity.java @@ -24,7 +24,7 @@ public class OrderEntity extends BaseEntity{ @ManyToOne @JoinColumn(name = "userId", nullable = false) private UserEntity user; - @ManyToMany() //cделать запрос jpa + @ManyToMany() private Set games = new HashSet<>(); public OrderEntity(){ diff --git a/src/main/java/com/example/demo/orders/repository/OrderRepository.java b/src/main/java/com/example/demo/orders/repository/OrderRepository.java index 5fa9925..31d4cdf 100644 --- a/src/main/java/com/example/demo/orders/repository/OrderRepository.java +++ b/src/main/java/com/example/demo/orders/repository/OrderRepository.java @@ -19,5 +19,7 @@ public interface OrderRepository extends CrudRepository, Pagi @Query("select o from OrderEntity o join fetch o.games where o.user.id = ?1") Page findByUserId(long userId, Pageable pageable); + List findAll(); + //Можно сделать запрос на сумму JPQL } diff --git a/src/main/java/com/example/demo/orders/service/OrderService.java b/src/main/java/com/example/demo/orders/service/OrderService.java index 0ef6826..af1bf3e 100644 --- a/src/main/java/com/example/demo/orders/service/OrderService.java +++ b/src/main/java/com/example/demo/orders/service/OrderService.java @@ -1,5 +1,7 @@ package com.example.demo.orders.service; +import java.util.List; + import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -30,6 +32,15 @@ public class OrderService { return repository.findByUserId(userId, pageRequest); } + @Transactional(readOnly = true) + public List getAll(long userId){ + userService.get(userId); + return repository.findByUserId(userId); + } + public List getAll(){ + return repository.findAll(); + } + @Transactional(readOnly = true) public OrderEntity get(long userId, long id){ userService.get(userId); diff --git a/src/main/java/com/example/demo/types/repository/TypeRepository.java b/src/main/java/com/example/demo/types/repository/TypeRepository.java index 9fea284..432cedc 100644 --- a/src/main/java/com/example/demo/types/repository/TypeRepository.java +++ b/src/main/java/com/example/demo/types/repository/TypeRepository.java @@ -8,4 +8,5 @@ import com.example.demo.types.model.TypeEntity; public interface TypeRepository extends CrudRepository { Optional findByNameIgnoreCase(String name); + Optional findById(long id); } diff --git a/src/test/java/com/example/demo/GameServiceTests.java b/src/test/java/com/example/demo/GameServiceTests.java new file mode 100644 index 0000000..487cee1 --- /dev/null +++ b/src/test/java/com/example/demo/GameServiceTests.java @@ -0,0 +1,101 @@ +package com.example.demo; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.games.model.GameEntity; +import com.example.demo.games.service.GameService; +import com.example.demo.genres.model.GenreEntity; +import com.example.demo.genres.service.GenreService; +import com.example.demo.types.model.TypeEntity; +import com.example.demo.types.service.TypeService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class GameServiceTests { + @Autowired + private GameService gameService; + @Autowired + private TypeService typeService; + @Autowired + private GenreService genreService; + + private GenreEntity genre1; + private GenreEntity genre2; + + private TypeEntity type1; + private TypeEntity type2; + + private GameEntity game1; + private GameEntity game2; + + @BeforeEach + void createData(){ + removeData(); + + genre1 = genreService.create(new GenreEntity("Приключения")); + genre2 = genreService.create(new GenreEntity("Симулятор")); + type1 = typeService.create(new TypeEntity("Игра")); + type2 = typeService.create(new TypeEntity("Программа")); + + final List genres1 = new ArrayList(); + genres1.add(genre1); + genres1.add(genre2); + + final List genres2 = new ArrayList(); + genres2.add(genre2); + + game1 = gameService.create(new GameEntity(type1,"Game1",2100.0,"good game", genres1)); + game2 = gameService.create(new GameEntity( type2, "Game2", 1200.0,"bad game", genres2)); + + } + + @AfterEach + void removeData(){ + gameService.getAll(0,0).forEach(item -> gameService.delete(item.getId())); + typeService.getAll().forEach(item -> typeService.delete(item.getId())); + genreService.getAll().forEach(item -> genreService.delete(item.getId())); + } + + @Test + void getTest(){ + Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L)); + } + + @Test + @Order(1) + void createTest(){ + Assertions.assertEquals(2, gameService.getAll(0,0).size()); + } + + @Test + @Order(2) + void updateTest(){ + genre1 = genreService.create(new GenreEntity("Симулятор2")); + type1 = typeService.create(new TypeEntity("Игра2")); + final List genres1 = new ArrayList(); + genres1.add(genre1); + gameService.update(game1.getId(), new GameEntity(type1, "testGame", 1200.0, "hehgame", genres1)); + Assertions.assertEquals(2, gameService.getAll(0,0).size()); + } + + @Test + @Order(3) + void deleteTest(){ + gameService.delete(game1.getId()); + Assertions.assertEquals(1, gameService.getAll(0,0).size()); + final GameEntity last = gameService.get(game2.getId()); + Assertions.assertEquals(game2.getId(), last.getId()); + } +} diff --git a/src/test/java/com/example/demo/GenreServiceTests.java b/src/test/java/com/example/demo/GenreServiceTests.java new file mode 100644 index 0000000..75f94a5 --- /dev/null +++ b/src/test/java/com/example/demo/GenreServiceTests.java @@ -0,0 +1,68 @@ +package com.example.demo; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.genres.model.GenreEntity; +import com.example.demo.genres.service.GenreService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class GenreServiceTests { + @Autowired + private GenreService genreService; + + private GenreEntity genre1; + private GenreEntity genre2; + + @BeforeEach + void createData(){ + removeData(); + + genre1 = genreService.create(new GenreEntity("Приключения")); + genre2 = genreService.create(new GenreEntity("Симулятор")); + } + + @AfterEach + void removeData(){ + genreService.getAll().forEach(item -> genreService.delete(item.getId())); + } + + @Test + void getTest(){ + Assertions.assertThrows(NotFoundException.class, () -> genreService.get(0L)); + } + + @Test + @Order(1) + void createTest(){ + Assertions.assertEquals(2, genreService.getAll().size()); + } + + @Test + @Order(2) + void updateTest(){ + final String test = "TEST"; + final GenreEntity newEntity = genreService.update(genre2.getId(), new GenreEntity(test)); + Assertions.assertEquals(2, genreService.getAll().size()); + Assertions.assertEquals(newEntity, genreService.get(genre2.getId())); + Assertions.assertEquals(test, newEntity.getName()); + } + + @Test + @Order(3) + void deleteTest(){ + genreService.delete(genre1.getId()); + Assertions.assertEquals(1, genreService.getAll().size()); + final GenreEntity last = genreService.get(genre2.getId()); + Assertions.assertEquals(genre2.getId(), last.getId()); + } +} diff --git a/src/test/java/com/example/demo/OrderServiceTest.java b/src/test/java/com/example/demo/OrderServiceTest.java new file mode 100644 index 0000000..9124b3d --- /dev/null +++ b/src/test/java/com/example/demo/OrderServiceTest.java @@ -0,0 +1,130 @@ +package com.example.demo; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.games.model.GameEntity; +import com.example.demo.games.service.GameService; +import com.example.demo.genres.model.GenreEntity; +import com.example.demo.genres.service.GenreService; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; +import com.example.demo.types.model.TypeEntity; +import com.example.demo.types.service.TypeService; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class OrderServiceTest { + @Autowired + private GameService gameService; + @Autowired + private TypeService typeService; + @Autowired + private GenreService genreService; + @Autowired + private UserService userService; + @Autowired + private OrderService orderService; + + private GenreEntity genre1; + private GenreEntity genre2; + + private TypeEntity type1; + private TypeEntity type2; + private List genres1 = new ArrayList<>(); + private List genres2 = new ArrayList<>(); + private GameEntity game1; + private GameEntity game2; + private List games = new ArrayList<>(); + private UserEntity user1; + private OrderEntity order1; + + @BeforeEach + void createData(){ + genre1 = genreService.create(new GenreEntity("Приключения")); + genre2 = genreService.create(new GenreEntity("Симулятор")); + type1 = typeService.create(new TypeEntity("Игра")); + type2 = typeService.create(new TypeEntity("Программа")); + + genres1 = new ArrayList(); + genres1.add(genre1); + genres1.add(genre2); + + genres2 = new ArrayList(); + genres2.add(genre2); + + game1 = gameService.create(new GameEntity(type1,"Game1",2100.0,"good game", genres1)); + game2 = gameService.create(new GameEntity( type2, "Game2", 1200.0,"bad game", genres2)); + games = new ArrayList(); + games.add(game1); + games.add(game2); + + user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123")); + order1 = orderService.create(user1.getId(), new OrderEntity(user1,games)); + removeData(); + + genre1 = genreService.create(new GenreEntity("Приключения")); + genre2 = genreService.create(new GenreEntity("Симулятор")); + type1 = typeService.create(new TypeEntity("Игра")); + type2 = typeService.create(new TypeEntity("Программа")); + + genres1 = new ArrayList(); + genres1.add(genre1); + genres1.add(genre2); + + genres2 = new ArrayList(); + genres2.add(genre2); + + game1 = gameService.create(new GameEntity(type1,"Game1",2100.0,"good game", genres1)); + game2 = gameService.create(new GameEntity( type2, "Game2", 1200.0,"bad game", genres2)); + games = new ArrayList(); + games.add(game1); + games.add(game2); + + user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123")); + order1 = orderService.create(user1.getId(), new OrderEntity(user1,games)); + orderService.create(user1.getId(), new OrderEntity(user1,games)); + } + + @AfterEach + void removeData(){ + orderService.getAll().forEach(item -> orderService.delete(item.getUser().getId(),item.getId())); + userService.getAll().forEach(item -> userService.delete(item.getId())); + gameService.getAll(0,0).forEach(item -> gameService.delete(item.getId())); + typeService.getAll().forEach(item -> typeService.delete(item.getId())); + genreService.getAll().forEach(item -> genreService.delete(item.getId())); + } + + @Test + void getTest(){ + Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L)); + } + + @Test + @Order(1) + void createTest(){ + Assertions.assertEquals(2, orderService.getAll(user1.getId()).size()); + } + + @Test + @Order(2) + void deleteTest(){ + orderService.delete(user1.getId(),order1.getId()); + Assertions.assertEquals(1, orderService.getAll(user1.getId()).size()); + } +} + +//зависимости, core->security, user(loadUserByUserName, userrole), core->configuration, дохуя контроллеров юзера core->session diff --git a/src/test/java/com/example/demo/TypeServiceTests.java b/src/test/java/com/example/demo/TypeServiceTests.java index 4e72ded..63e9a3d 100644 --- a/src/test/java/com/example/demo/TypeServiceTests.java +++ b/src/test/java/com/example/demo/TypeServiceTests.java @@ -1,76 +1,77 @@ -// package com.example.demo; +package com.example.demo; -// import org.junit.jupiter.api.AfterEach; -// import org.junit.jupiter.api.Assertions; -// import org.junit.jupiter.api.BeforeEach; -// import org.junit.jupiter.api.Order; -// import org.junit.jupiter.api.Test; -// import org.junit.jupiter.api.TestMethodOrder; -// import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; -// import org.springframework.beans.factory.annotation.Autowired; -// import org.springframework.boot.test.context.SpringBootTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; -// import com.example.demo.core.error.NotFoundException; -// import com.example.demo.types.model.TypeEntity; -// import com.example.demo.types.service.TypeService; +import com.example.demo.core.error.NotFoundException; +import com.example.demo.types.model.TypeEntity; +import com.example.demo.types.service.TypeService; -// @SpringBootTest -// @TestMethodOrder(OrderAnnotation.class) -// class TypeServiceTests { -// @Autowired -// private TypeService typeService; +import jakarta.transaction.Transactional; -// private TypeEntity type; +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class TypeServiceTests { + @Autowired + private TypeService typeService; -// @BeforeEach -// void createData() { -// removeData(); + private TypeEntity type1; + private TypeEntity type2; -// type = typeService.create(new TypeEntity("Ноутбук")); -// typeService.create(new TypeEntity("Телефон")); -// typeService.create(new TypeEntity("Игровая приставка")); -// } + @BeforeEach + void createData() { + removeData(); -// @AfterEach -// void removeData() { -// typeService.getAll().forEach(item -> typeService.delete(item.getId())); -// } + + type2 = typeService.create(new TypeEntity("Программа")); + + type1 = typeService.create(new TypeEntity("Игра")); + } + + @AfterEach + void removeData() { + typeService.getAll().forEach(item -> typeService.delete(item.getId())); + } -// @Test -// void getTest(){ -// Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L)); -// } + @Test + @Transactional + void getTest(){ + Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L)); + } -// @Test -// @Order(1) -// void createTest(){ -// typeService.create(new TypeEntity("Игра")); -// typeService.create(new TypeEntity("Программа")); -// final TypeEntity last = typeService.create(new TypeEntity("Игра2")); -// Assertions.assertEquals(5, typeService.getAll().size()); -// Assertions.assertEquals(last, typeService.get(10L)); -// } + @Test + @Order(1) + @Transactional + void createTest(){ + final TypeEntity last = typeService.create(new TypeEntity("Игра2")); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(last, typeService.get(3L)); + } -// @Test -// @Order(2) -// void updateTest(){ -// final String test = "TEST"; -// final TypeEntity newEntity = typeService.update(9L, new TypeEntity(test)); -// Assertions.assertEquals(5, typeService.getAll().size()); -// Assertions.assertEquals(newEntity, typeService.get(9L)); -// Assertions.assertEquals(test, newEntity.getName()); -// } + @Test + @Order(2) + @Transactional + void updateTest(){ + final String test = "TEST"; + final TypeEntity newEntity = typeService.update(type1.getId(), new TypeEntity(test)); + Assertions.assertEquals(2, typeService.getAll().size()); + Assertions.assertEquals(test, newEntity.getName()); + } -// @Test -// @Order(3) -// void deleteTest(){ -// typeService.delete(9L); -// Assertions.assertEquals(8, typeService.getAll().size()); -// final TypeEntity last = typeService.get(8L); -// Assertions.assertEquals(8L, last.getId()); - -// final TypeEntity newEntity = typeService.create(new TypeEntity("Игра")); -// Assertions.assertEquals(9, typeService.getAll().size()); -// Assertions.assertEquals(11L, newEntity.getId()); -// } -// } + @Test + @Order(3) + @Transactional + void deleteTest(){ + typeService.delete(type1.getId()); + Assertions.assertEquals(1, typeService.getAll().size()); + final TypeEntity last = typeService.get(type2.getId()); + Assertions.assertEquals(6L, last.getId()); + } +} diff --git a/src/test/java/com/example/demo/UserServiceTests.java b/src/test/java/com/example/demo/UserServiceTests.java new file mode 100644 index 0000000..b6b3f06 --- /dev/null +++ b/src/test/java/com/example/demo/UserServiceTests.java @@ -0,0 +1,67 @@ +package com.example.demo; + + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class UserServiceTests { + @Autowired + private UserService userService; + + private UserEntity user1; + private UserEntity user2; + + @BeforeEach + void createData() { + removeData(); + + user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123")); + user2 = userService.create(new UserEntity( "login2", "email@gmail.com", "qwerty1234")); + } + + @AfterEach + void removeData() { + userService.getAll().forEach(item -> userService.delete(item.getId())); + } + + @Test + void getTest(){ + Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L)); + } + + @Test + @Order(1) + void createTest(){ + Assertions.assertEquals(2, userService.getAll().size()); + } + + @Test + @Order(2) + void updateTest(){ + final UserEntity newEntity = userService.update(user2.getId(), new UserEntity("user11", "mail11", "qwerty11")); + Assertions.assertEquals(2, userService.getAll().size()); + Assertions.assertEquals(newEntity.getLogin(), "user11"); + } + + @Test + @Order(3) + void deleteTest(){ + userService.delete(user2.getId()); + Assertions.assertEquals(1, userService.getAll().size()); + final UserEntity last = userService.get(user1.getId()); + Assertions.assertEquals(user1.getId(), last.getId()); + } +}