Сделал тесты, правки

This commit is contained in:
Максим Яковлев 2024-04-29 18:09:24 +04:00
parent fd9982fdcb
commit 86435c3fbe
15 changed files with 510 additions and 112 deletions

Binary file not shown.

View File

@ -47,29 +47,29 @@ public class DemoApplication implements CommandLineRunner {
@Override @Override
public void run(String... args) throws Exception{ public void run(String... args) throws Exception{
log.info("start"); log.info("start");
final var type1 = typeService.create(new TypeEntity("ААА")); // final var type1 = typeService.create(new TypeEntity("ААА"));
final var type2 = typeService.create(new TypeEntity("АА")); // final var type2 = typeService.create(new TypeEntity("АА"));
final var genre1 = genreService.create(new GenreEntity("Приключения")); // final var genre1 = genreService.create(new GenreEntity("Приключения"));
final var genre2 = genreService.create(new GenreEntity("Симулятор")); // final var genre2 = genreService.create(new GenreEntity("Симулятор"));
final List<GenreEntity> genres1 = new ArrayList<GenreEntity>(); // final List<GenreEntity> genres1 = new ArrayList<GenreEntity>();
genres1.add(genre1); // genres1.add(genre1);
genres1.add(genre2); // genres1.add(genre2);
final List<GenreEntity> genres2 = new ArrayList<GenreEntity>(); // final List<GenreEntity> genres2 = new ArrayList<GenreEntity>();
genres2.add(genre2); // genres2.add(genre2);
final var game1 = gameService.create(new GameEntity(type1,"Game1",2100.0,"good game", genres1)); // 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 var game2 = gameService.create(new GameEntity( type2, "Game2", 1200.0,"bad game", genres2));
final List<GameEntity> games = new ArrayList<GameEntity>(); // final List<GameEntity> games = new ArrayList<GameEntity>();
games.add(game1); // games.add(game1);
games.add(game2); // games.add(game2);
var user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123")); // var user1 = userService.create(new UserEntity( "login1", "email@mail.com", "qwerty123"));
var user2 = userService.create(new UserEntity( "login2", "email@gmail.com", "qwerty1234")); // var user2 = userService.create(new UserEntity( "login2", "email@gmail.com", "qwerty1234"));
orderService.create(7, new OrderEntity(user1,games)); // orderService.create(7, new OrderEntity(user1,games));
orderService.create(8, new OrderEntity(user2,games)); // orderService.create(8, new OrderEntity(user2,games));
} }
} }

View File

@ -1,5 +1,7 @@
package com.example.demo.games.api; package com.example.demo.games.api;
import java.util.List;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -53,8 +55,9 @@ public class GameController {
final GameEntity entity = modelMapper.map(dto, GameEntity.class); final GameEntity entity = modelMapper.map(dto, GameEntity.class);
entity.setType(typeService.get(dto.getTypeId())); entity.setType(typeService.get(dto.getTypeId()));
var genres = dto.getGenres(); var genres = dto.getGenres();
for(var genre : genres){ List<GenreEntity> genresList = genreService.getAllById(genres);
entity.setGenres(genreService.get(genre)); for(var genre : genresList){
entity.setGenres(genre);
} }
return entity; return entity;
} }

View File

@ -1,5 +1,6 @@
package com.example.demo.games.repository; package com.example.demo.games.repository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -9,16 +10,32 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.PagingAndSortingRepository;
import com.example.demo.games.model.GameEntity; import com.example.demo.games.model.GameEntity;
import com.example.demo.genres.model.GenreEntity;
public interface GameRepository extends CrudRepository<GameEntity, Long>, PagingAndSortingRepository<GameEntity, Long> { public interface GameRepository extends CrudRepository<GameEntity, Long>, PagingAndSortingRepository<GameEntity, Long> {
@Query("select distinct g from GameEntity g join fetch g.genres ge join fetch g.type ty where g.id = ?1")
Optional<GameEntity> findOneById(long id); Optional<GameEntity> findOneById(long id);
Page<GameEntity> 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<GameEntity> 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<GameEntity> findByTypeIdAndGenres(long typeId, long genreId);
@Query("select distinct g from GameEntity g join fetch g.type ty where ty.id = ?1")
Page<GameEntity> findByTypeId(long typeId, Pageable pageable); Page<GameEntity> findByTypeId(long typeId, Pageable pageable);
//Page<GameEntity> findByGenres(GenreEntity genre, Pageable pageable); @Query("select distinct g from GameEntity g join fetch g.type ty where ty.id = ?1")
List<GameEntity> findByTypeId(long typeId);
@Query("select distinct g from GameEntity g join fetch g.genres ge where ge.id = ?1") @Query("select distinct g from GameEntity g join fetch g.genres ge where ge.id = ?1")
Page<GameEntity> findByGenres(long genre, Pageable pageable); Page<GameEntity> findByGenres(long genre, Pageable pageable);
@Query("select distinct g from GameEntity g join fetch g.genres ge where ge.id = ?1")
List<GameEntity> findByGenres(long genre);
@Query("select distinct g from GameEntity g join fetch g.genres join fetch g.type ty")
Page<GameEntity>findAll(Pageable pageable);
@Query("select distinct g from GameEntity g join fetch g.genres join fetch g.type ty")
List<GameEntity>findAll();
} }
/* "select " /* "select "

View File

@ -1,5 +1,6 @@
package com.example.demo.games.service; package com.example.demo.games.service;
import java.util.List;
//import java.util.List; //import java.util.List;
import java.util.Objects; import java.util.Objects;
import org.springframework.data.domain.Page; 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.core.error.NotFoundException;
import com.example.demo.games.model.GameEntity; import com.example.demo.games.model.GameEntity;
import com.example.demo.games.repository.GameRepository; import com.example.demo.games.repository.GameRepository;
import com.example.demo.genres.model.GenreEntity;
import com.example.demo.genres.service.GenreService; import com.example.demo.genres.service.GenreService;
@Service @Service
public class GameService { public class GameService {
private final GameRepository repository; private final GameRepository repository;
private final GenreService genreService;
public GameService(GameRepository repository, GenreService genreService){ public GameService(GameRepository repository, GenreService genreService){
this.repository = repository; this.repository = repository;
this.genreService = genreService;
} }
// @Transactional(readOnly = true) @Transactional(readOnly = true)
// public Page<GameEntity> getAll(Long typeId, List<Long> genres, int page, int size){ public List<GameEntity> getAll(long typeId, long genre){
// final Pageable pageRequest = PageRequest.of(page, size);
// if(!Objects.equals(typeId, 0L) && !Objects.equals(genres.size(), 0)){ if(!Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){
// return repository.findByTypeIdAndGenres(typeId, genres, pageRequest); return repository.findByTypeIdAndGenres(typeId, genre);
// } }
// if(Objects.equals(typeId, 0L) && !Objects.equals(genres.size(), 0)){ if(Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){
// return repository.findByGenres(genres, pageRequest); return repository.findByGenres(genre);
// } }
// if(!Objects.equals(typeId, 0L) && Objects.equals(genres.size(), 0)){ if(!Objects.equals(typeId, 0L) && Objects.equals(genre, 0L)){
// return repository.findByTypeId(typeId, pageRequest); return repository.findByTypeId(typeId);
// } }
// return repository.findAll(PageRequest.of(page, size)); return repository.findAll();
// } }
//getAll.stream().filter(game -> game.getType().getId().equals(typeId)).toList();
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Page<GameEntity> getAll(long typeId, long genre, int page, int size){ public Page<GameEntity> getAll(long typeId, long genre, int page, int size){
final Pageable pageRequest = PageRequest.of(page, 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)){ 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)){ if(Objects.equals(typeId, 0L) && !Objects.equals(genre, 0L)){
return repository.findByGenres(genre, pageRequest); return repository.findByGenres(genre, pageRequest);

View File

@ -23,6 +23,11 @@ public class GenreService {
public List<GenreEntity> getAll(){ public List<GenreEntity> getAll(){
return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
} }
@Transactional(readOnly = true)
public List<GenreEntity> getAllById(List<Long> listIds){
return StreamSupport.stream(repository.findAllById(listIds).spliterator(),false).toList();
}
@Transactional(readOnly = true) @Transactional(readOnly = true)
public GenreEntity get(Long id){ public GenreEntity get(Long id){

View File

@ -24,7 +24,7 @@ public class OrderEntity extends BaseEntity{
@ManyToOne @ManyToOne
@JoinColumn(name = "userId", nullable = false) @JoinColumn(name = "userId", nullable = false)
private UserEntity user; private UserEntity user;
@ManyToMany() //елать запрос jpa @ManyToMany()
private Set<GameEntity> games = new HashSet<>(); private Set<GameEntity> games = new HashSet<>();
public OrderEntity(){ public OrderEntity(){

View File

@ -19,5 +19,7 @@ public interface OrderRepository extends CrudRepository<OrderEntity, Long>, Pagi
@Query("select o from OrderEntity o join fetch o.games where o.user.id = ?1") @Query("select o from OrderEntity o join fetch o.games where o.user.id = ?1")
Page<OrderEntity> findByUserId(long userId, Pageable pageable); Page<OrderEntity> findByUserId(long userId, Pageable pageable);
List<OrderEntity> findAll();
//Можно сделать запрос на сумму JPQL //Можно сделать запрос на сумму JPQL
} }

View File

@ -1,5 +1,7 @@
package com.example.demo.orders.service; package com.example.demo.orders.service;
import java.util.List;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -30,6 +32,15 @@ public class OrderService {
return repository.findByUserId(userId, pageRequest); return repository.findByUserId(userId, pageRequest);
} }
@Transactional(readOnly = true)
public List<OrderEntity> getAll(long userId){
userService.get(userId);
return repository.findByUserId(userId);
}
public List<OrderEntity> getAll(){
return repository.findAll();
}
@Transactional(readOnly = true) @Transactional(readOnly = true)
public OrderEntity get(long userId, long id){ public OrderEntity get(long userId, long id){
userService.get(userId); userService.get(userId);

View File

@ -8,4 +8,5 @@ import com.example.demo.types.model.TypeEntity;
public interface TypeRepository extends CrudRepository<TypeEntity, Long> { public interface TypeRepository extends CrudRepository<TypeEntity, Long> {
Optional<TypeEntity> findByNameIgnoreCase(String name); Optional<TypeEntity> findByNameIgnoreCase(String name);
Optional<TypeEntity> findById(long id);
} }

View File

@ -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<GenreEntity> genres1 = new ArrayList<GenreEntity>();
genres1.add(genre1);
genres1.add(genre2);
final List<GenreEntity> genres2 = new ArrayList<GenreEntity>();
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<GenreEntity> genres1 = new ArrayList<GenreEntity>();
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());
}
}

View File

@ -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());
}
}

View File

@ -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<GenreEntity> genres1 = new ArrayList<>();
private List<GenreEntity> genres2 = new ArrayList<>();
private GameEntity game1;
private GameEntity game2;
private List<GameEntity> 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<GenreEntity>();
genres1.add(genre1);
genres1.add(genre2);
genres2 = new ArrayList<GenreEntity>();
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<GameEntity>();
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<GenreEntity>();
genres1.add(genre1);
genres1.add(genre2);
genres2 = new ArrayList<GenreEntity>();
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<GameEntity>();
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

View File

@ -1,76 +1,77 @@
// package com.example.demo; package com.example.demo;
// import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
// import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
// import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
// import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
// import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
// import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestMethodOrder;
// import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
// import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
// import com.example.demo.core.error.NotFoundException; import com.example.demo.core.error.NotFoundException;
// import com.example.demo.types.model.TypeEntity; import com.example.demo.types.model.TypeEntity;
// import com.example.demo.types.service.TypeService; import com.example.demo.types.service.TypeService;
// @SpringBootTest import jakarta.transaction.Transactional;
// @TestMethodOrder(OrderAnnotation.class)
// class TypeServiceTests {
// @Autowired
// private TypeService typeService;
// private TypeEntity type; @SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class TypeServiceTests {
@Autowired
private TypeService typeService;
// @BeforeEach private TypeEntity type1;
// void createData() { private TypeEntity type2;
// removeData();
// type = typeService.create(new TypeEntity("Ноутбук")); @BeforeEach
// typeService.create(new TypeEntity("Телефон")); void createData() {
// typeService.create(new TypeEntity("Игровая приставка")); removeData();
// }
// @AfterEach
// void removeData() { type2 = typeService.create(new TypeEntity("Программа"));
// typeService.getAll().forEach(item -> typeService.delete(item.getId()));
// } type1 = typeService.create(new TypeEntity("Игра"));
}
@AfterEach
void removeData() {
typeService.getAll().forEach(item -> typeService.delete(item.getId()));
}
// @Test @Test
// void getTest(){ @Transactional
// Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L)); void getTest(){
// } Assertions.assertThrows(NotFoundException.class, () -> typeService.get(0L));
}
// @Test @Test
// @Order(1) @Order(1)
// void createTest(){ @Transactional
// typeService.create(new TypeEntity("Игра")); void createTest(){
// typeService.create(new TypeEntity("Программа")); final TypeEntity last = typeService.create(new TypeEntity("Игра2"));
// final TypeEntity last = typeService.create(new TypeEntity("Игра2")); Assertions.assertEquals(3, typeService.getAll().size());
// Assertions.assertEquals(5, typeService.getAll().size()); Assertions.assertEquals(last, typeService.get(3L));
// Assertions.assertEquals(last, typeService.get(10L)); }
// }
// @Test @Test
// @Order(2) @Order(2)
// void updateTest(){ @Transactional
// final String test = "TEST"; void updateTest(){
// final TypeEntity newEntity = typeService.update(9L, new TypeEntity(test)); final String test = "TEST";
// Assertions.assertEquals(5, typeService.getAll().size()); final TypeEntity newEntity = typeService.update(type1.getId(), new TypeEntity(test));
// Assertions.assertEquals(newEntity, typeService.get(9L)); Assertions.assertEquals(2, typeService.getAll().size());
// Assertions.assertEquals(test, newEntity.getName()); Assertions.assertEquals(test, newEntity.getName());
// } }
// @Test @Test
// @Order(3) @Order(3)
// void deleteTest(){ @Transactional
// typeService.delete(9L); void deleteTest(){
// Assertions.assertEquals(8, typeService.getAll().size()); typeService.delete(type1.getId());
// final TypeEntity last = typeService.get(8L); Assertions.assertEquals(1, typeService.getAll().size());
// Assertions.assertEquals(8L, last.getId()); final TypeEntity last = typeService.get(type2.getId());
Assertions.assertEquals(6L, last.getId());
// final TypeEntity newEntity = typeService.create(new TypeEntity("Игра")); }
// Assertions.assertEquals(9, typeService.getAll().size()); }
// Assertions.assertEquals(11L, newEntity.getId());
// }
// }

View File

@ -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());
}
}