Compare commits
2 Commits
d3779e0ee2
...
0d207ca4ac
Author | SHA1 | Date | |
---|---|---|---|
|
0d207ca4ac | ||
|
9ecf0abda5 |
@ -1,19 +1,19 @@
|
||||
package com.example.demo.core.model;
|
||||
|
||||
public abstract class BaseEntity {
|
||||
protected long id;
|
||||
protected Long id;
|
||||
|
||||
protected BaseEntity(){
|
||||
|
||||
}
|
||||
protected BaseEntity(long id){
|
||||
protected BaseEntity(Long id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getId(){
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
public void setId(long id){
|
||||
public void setId(Long id){
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import java.util.TreeMap;
|
||||
|
||||
public abstract class MapRepository<E extends BaseEntity> implements CommonRepository<E, Long> {
|
||||
private final Map<Long, E> entities = new TreeMap<>();
|
||||
private long lastId = 0L;
|
||||
private Long lastId = 0L;
|
||||
protected MapRepository(){
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,58 @@
|
||||
package com.example.demo.games.api;
|
||||
|
||||
import com.example.demo.core.config.Constants;
|
||||
import com.example.demo.games.service.GameService;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import jakarta.validation.Valid;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/games")
|
||||
public class GameController {
|
||||
private final GameService gameService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public GameController(GameService gameService, ModelMapper modelMapper){
|
||||
this.gameService = gameService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private GameDTO toDTO(GameEntity entity){
|
||||
return modelMapper.map(entity, GameDTO.class);
|
||||
}
|
||||
|
||||
private GameEntity toEntity(GameDTO dto){
|
||||
return modelMapper.map(dto, GameEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<GameDTO> getAll()
|
||||
{
|
||||
return gameService.getAll().stream().map(this::toDTO).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public GameDTO get(@PathVariable(name = "id") long id)
|
||||
{
|
||||
return toDTO(gameService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public GameDTO create(@RequestBody @Valid GameDTO dto) {
|
||||
return toDTO(gameService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public GameDTO update(@PathVariable(name = "id") long id, @RequestBody GameDTO dto) {
|
||||
return toDTO(gameService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public GameDTO delete(@PathVariable(name = "id") long id)
|
||||
{
|
||||
return toDTO(gameService.delete(id));
|
||||
}
|
||||
}
|
26
demo/src/main/java/com/example/demo/games/api/GameDTO.java
Normal file
26
demo/src/main/java/com/example/demo/games/api/GameDTO.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.example.demo.games.api;
|
||||
|
||||
import com.example.demo.users.api.UserDTO;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class GameDTO {
|
||||
private Long id;
|
||||
@NotNull
|
||||
@Min(2)
|
||||
private String name;
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
public void setId(Long id){
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.example.demo.games.model;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class GameEntity extends BaseEntity {
|
||||
private String name;
|
||||
|
||||
public GameEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public GameEntity(Long id, String name) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.demo.games.repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class GameRepository extends MapRepository<GameEntity> {
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.example.demo.games.service;
|
||||
|
||||
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.users.model.UserEntity;
|
||||
import com.example.demo.users.repository.UserRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class GameService {
|
||||
private final GameRepository repository;
|
||||
public GameService(GameRepository repository){
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<GameEntity> getAll(){
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public GameEntity get(long id){
|
||||
return Optional
|
||||
.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
public GameEntity create(GameEntity entity){
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public GameEntity update(long id, GameEntity entity){
|
||||
final GameEntity existEntity = get(id);
|
||||
existEntity.setName(entity.getName().isEmpty() ? existEntity.getName() : entity.getName());
|
||||
return repository.update(existEntity);
|
||||
}
|
||||
|
||||
public GameEntity delete(Long id){
|
||||
return repository.delete(repository.get(id));
|
||||
}
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package com.example.demo.news.model;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class NewsEntity extends BaseEntity {
|
||||
private String title;
|
||||
private String text;
|
||||
private Date dateCreate;
|
||||
|
||||
public NewsEntity() {
|
||||
super();
|
||||
}
|
||||
|
||||
public NewsEntity(Long id, String name, String password, Date dateCreate){
|
||||
super(id);
|
||||
this.title = name;
|
||||
this.text = password;
|
||||
this.dateCreate = dateCreate;
|
||||
}
|
||||
|
||||
public String getTitle(){
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title){
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getText(){
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text){
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Date getDateCreate(){
|
||||
return dateCreate;
|
||||
}
|
||||
|
||||
public void setDateCreate(Date dateCreate){
|
||||
this.dateCreate = dateCreate;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.example.demo.tables.api;
|
||||
|
||||
import com.example.demo.core.config.Constants;
|
||||
import com.example.demo.tables.model.TableEntity;
|
||||
import com.example.demo.tables.service.TableService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/tables")
|
||||
public class TableController {
|
||||
private final TableService tableService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public TableController(TableService tableService, ModelMapper modelMapper){
|
||||
|
||||
this.tableService = tableService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private TableDTO toDTO(TableEntity entity) {
|
||||
return modelMapper.map(entity, TableDTO.class);
|
||||
}
|
||||
private TableEntity toEntity(TableDTO dto){
|
||||
return modelMapper.map(dto, TableEntity.class);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<TableDTO> getAll()
|
||||
{
|
||||
return tableService.getAll().stream().map(this::toDTO).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public TableDTO get(@PathVariable(name = "id") long id)
|
||||
{
|
||||
return toDTO(tableService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public TableDTO create(@RequestBody @Valid TableDTO dto) {
|
||||
return toDTO(tableService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public TableDTO update(@PathVariable(name = "id") long id, @RequestBody TableDTO dto) {
|
||||
return toDTO(tableService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public TableDTO delete(@PathVariable(name = "id") long id)
|
||||
{
|
||||
return toDTO(tableService.delete(id));
|
||||
}
|
||||
}
|
67
demo/src/main/java/com/example/demo/tables/api/TableDTO.java
Normal file
67
demo/src/main/java/com/example/demo/tables/api/TableDTO.java
Normal file
@ -0,0 +1,67 @@
|
||||
package com.example.demo.tables.api;
|
||||
|
||||
import com.example.demo.games.api.GameDTO;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import com.example.demo.games.repository.GameRepository;
|
||||
import com.example.demo.users.api.UserDTO;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class TableDTO {
|
||||
private Long id;
|
||||
private String description;
|
||||
@NotNull
|
||||
private Date date;
|
||||
@NotNull
|
||||
private UserDTO creator;
|
||||
@NotNull
|
||||
private GameDTO game;
|
||||
private List<UserDTO> gamers;
|
||||
|
||||
public String getDescription(){
|
||||
return description;
|
||||
}
|
||||
public void setDescription(String description){
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Date getDate(){
|
||||
return date;
|
||||
}
|
||||
public void setDate(Date date){
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public UserDTO geyCreator(){
|
||||
return creator;
|
||||
}
|
||||
public void setCreator(UserDTO creator){
|
||||
this.creator = creator;
|
||||
}
|
||||
|
||||
public GameDTO getGame(){
|
||||
return game;
|
||||
}
|
||||
public void setGame(GameDTO game){
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
public List<UserDTO> getGamers(){
|
||||
return gamers;
|
||||
}
|
||||
public void setGamers(List<UserDTO> gamers){
|
||||
this.gamers = gamers;
|
||||
}
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
public void setId(Long id){
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.example.demo.tables.model;
|
||||
|
||||
import com.example.demo.core.model.BaseEntity;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
|
||||
import java.time.Period;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class TableEntity extends BaseEntity {
|
||||
private String description;
|
||||
private Date date;
|
||||
private UserEntity creator;
|
||||
private GameEntity game;
|
||||
private List<UserEntity> gamers;
|
||||
|
||||
public TableEntity() {
|
||||
super();
|
||||
}
|
||||
public TableEntity(String description, Date date,
|
||||
UserEntity creator, GameEntity game, List<UserEntity> users){
|
||||
this.description = description;
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getDescription(){
|
||||
return description;
|
||||
}
|
||||
public void setDescription(String description){
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Date getDate(){
|
||||
return date;
|
||||
}
|
||||
public void setDate(Date date){
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public UserEntity geyCreator(){
|
||||
return creator;
|
||||
}
|
||||
public void setCreator(UserEntity creator){
|
||||
this.creator = creator;
|
||||
}
|
||||
|
||||
public GameEntity getGame(){
|
||||
return game;
|
||||
}
|
||||
public void setGame(GameEntity game){
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
public List<UserEntity> getGamers(){
|
||||
return gamers;
|
||||
}
|
||||
public void setGamers(List<UserEntity> gamers){
|
||||
this.gamers = gamers;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.demo.tables.repository;
|
||||
|
||||
import com.example.demo.core.repository.MapRepository;
|
||||
import com.example.demo.tables.model.TableEntity;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class TableRepository extends MapRepository<TableEntity> {
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.example.demo.tables.service;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.games.service.GameService;
|
||||
import com.example.demo.tables.model.TableEntity;
|
||||
import com.example.demo.tables.repository.TableRepository;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UserService;
|
||||
import jakarta.validation.OverridesAttribute;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class TableService {
|
||||
private final TableRepository repository;
|
||||
private final UserService userService;
|
||||
private final GameService gameService;
|
||||
public TableService(TableRepository repository, UserService userService, GameService gameService)
|
||||
{
|
||||
this.repository = repository;
|
||||
this.userService = userService;
|
||||
this.gameService = gameService;
|
||||
}
|
||||
|
||||
public List<TableEntity> getAll(){
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public TableEntity get(Long id){
|
||||
return Optional
|
||||
.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
public TableEntity create(TableEntity entity){
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public TableEntity update(Long id, TableEntity entity){
|
||||
final TableEntity existEntity = get(id);
|
||||
|
||||
existEntity.setDate(entity.getDate() == null ? existEntity.getDate() : entity.getDate());
|
||||
existEntity.setDescription(
|
||||
entity.getDescription().isEmpty() ? existEntity.getDescription() : entity.getDescription()
|
||||
);
|
||||
|
||||
existEntity.setGamers(
|
||||
entity.getGamers().isEmpty() ?
|
||||
existEntity.getGamers() :
|
||||
entity.getGamers()
|
||||
.stream()
|
||||
.map(g -> userService.get(g.getId()))
|
||||
.toList()
|
||||
);
|
||||
existEntity.setGame(
|
||||
entity.getGame().getId() == null ?
|
||||
existEntity.getGame() :
|
||||
(gameService.get(entity.getGame().getId())));
|
||||
|
||||
return repository.update(existEntity);
|
||||
}
|
||||
|
||||
public TableEntity delete(Long id){
|
||||
return repository.delete(repository.get(id));
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.example.demo.core.config.Constants;
|
||||
@ -29,11 +28,16 @@ public class UserController {
|
||||
}
|
||||
|
||||
private UserDTO toDTO(UserEntity entity){
|
||||
return modelMapper.map(entity, UserDTO.class);
|
||||
UserDTO dto = new UserDTO();
|
||||
dto.setId(entity.getId());
|
||||
dto.setName(entity.getName());
|
||||
dto.setLogin(entity.getLogin());
|
||||
dto.setPassword(entity.getPassword());
|
||||
return dto;
|
||||
}
|
||||
|
||||
private UserEntity toEntity(UserDTO dto){
|
||||
return new UserEntity(-1L, dto.getName(), dto.getPassword());
|
||||
return new UserEntity(dto.getId(), dto.getName(), dto.getLogin(), dto.getPassword());
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
|
@ -10,6 +10,9 @@ public class UserDTO {
|
||||
private String name;
|
||||
@NotNull
|
||||
@Min(4)
|
||||
private String login;
|
||||
@NotNull
|
||||
@Min(4)
|
||||
private String password;
|
||||
|
||||
public String getName() {
|
||||
@ -18,6 +21,13 @@ public class UserDTO {
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
public String getLogin()
|
||||
{
|
||||
return login;
|
||||
}
|
||||
public void setLogin(String login){
|
||||
this.login = login;
|
||||
}
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
@ -25,10 +35,10 @@ public class UserDTO {
|
||||
this.password = password;
|
||||
}
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public long getId(){
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
public void setId(long id){
|
||||
public void setId(Long id){
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package com.example.demo.users.api;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
|
||||
public class UserMapper extends ModelMapper {
|
||||
|
||||
}
|
@ -6,6 +6,7 @@ import java.util.Date;
|
||||
|
||||
public class UserEntity extends BaseEntity {
|
||||
private String name;
|
||||
private String login;
|
||||
private String password;
|
||||
private Date dateCreate;
|
||||
|
||||
@ -13,25 +14,39 @@ public class UserEntity extends BaseEntity {
|
||||
super();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, String name, String password) {
|
||||
public UserEntity(Long id, String name, String login, String password) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
this.dateCreate = new Date();
|
||||
}
|
||||
|
||||
public UserEntity(Long id, String name) {
|
||||
super(id);
|
||||
this.name = name;
|
||||
this.dateCreate = new Date();
|
||||
this.login = "";
|
||||
this.password = "";
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
public void setLogin(String login){
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getPassword(){
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password){
|
||||
this.password = password;
|
||||
}
|
||||
@ -39,7 +54,6 @@ public class UserEntity extends BaseEntity {
|
||||
public Date getDateCreate(){
|
||||
return dateCreate;
|
||||
}
|
||||
|
||||
public void setDateCreate(Date dateCreate){
|
||||
this.dateCreate = dateCreate;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.awt.event.ItemEvent;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@ -20,20 +21,22 @@ public class UserService {
|
||||
return repository.getAll();
|
||||
}
|
||||
|
||||
public UserEntity get(long id){
|
||||
public UserEntity get(Long id){
|
||||
return Optional
|
||||
.ofNullable(repository.get(id))
|
||||
.orElseThrow(() -> new NotFoundException(id));
|
||||
}
|
||||
|
||||
public UserEntity create(UserEntity entity){
|
||||
return repository.create(entity);
|
||||
}
|
||||
|
||||
public UserEntity update(long id, UserEntity entity){
|
||||
public UserEntity update(Long id, UserEntity entity){
|
||||
final UserEntity existEntity = get(id);
|
||||
existEntity.setName(entity.getName());
|
||||
existEntity.setPassword(entity.getPassword());
|
||||
|
||||
existEntity.setName(entity.getName().isEmpty() ? existEntity.getName() : entity.getName());
|
||||
existEntity.setLogin(entity.getLogin().isEmpty() ? existEntity.getLogin() : entity.getLogin());
|
||||
existEntity.setPassword(entity.getPassword().isEmpty() ? existEntity.getPassword() : entity.getPassword());
|
||||
|
||||
return repository.update(existEntity);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,65 @@
|
||||
package com.example.demo.DemoApplicationTests;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.games.model.GameEntity;
|
||||
import com.example.demo.games.service.GameService;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class GameServiceTest {
|
||||
@Autowired
|
||||
private GameService gameService;
|
||||
@Test
|
||||
void getTest(){
|
||||
Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest(){
|
||||
Assertions.assertEquals(0, gameService.getAll().size());
|
||||
gameService.create(new GameEntity(null, "Древний ужас"));
|
||||
Assertions.assertEquals(1, gameService.getAll().size());
|
||||
gameService.create(new GameEntity(null, "Покорение марса"));
|
||||
Assertions.assertEquals(2, gameService.getAll().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void UpdateTest(){
|
||||
var newEntity = new GameEntity(1L, "Аркхем");
|
||||
Assertions.assertNotEquals(newEntity, gameService.get(1L));
|
||||
final GameEntity newEntityCopy = gameService.update(1L, newEntity);
|
||||
|
||||
AssertionsEqualEntity(newEntityCopy, newEntity);
|
||||
|
||||
Assertions.assertEquals(2, gameService.getAll().size());
|
||||
|
||||
AssertionsEqualEntity(newEntity, gameService.get(1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void DeleteTest(){
|
||||
Assertions.assertThrows(NullPointerException.class, () -> gameService.delete(3L));
|
||||
Assertions.assertEquals(2L, gameService.getAll().size());
|
||||
gameService.delete(1L);
|
||||
Assertions.assertEquals(1L, gameService.getAll().size());
|
||||
final GameEntity last = gameService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final GameEntity newEntity = gameService.create(new GameEntity(null, "Крылья"));
|
||||
Assertions.assertEquals(2L, gameService.getAll().size());
|
||||
Assertions.assertEquals(3L, newEntity.getId());
|
||||
}
|
||||
|
||||
private void AssertionsEqualEntity(GameEntity l, GameEntity r){
|
||||
Assertions.assertEquals(l.getName(), r.getName());
|
||||
Assertions.assertEquals(l.getId(), r.getId());
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.example.demo.DemoApplicationTests;
|
||||
|
||||
import com.example.demo.core.error.NotFoundException;
|
||||
import com.example.demo.users.model.UserEntity;
|
||||
import com.example.demo.users.service.UserService;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@SpringBootTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class UserServiceTest {
|
||||
private boolean equalEntity(UserEntity l, UserEntity r){
|
||||
return l.getId() == r.getId() &&
|
||||
Objects.equals(l.getName(), r.getName()) &&
|
||||
Objects.equals(l.getLogin(), r.getLogin()) &&
|
||||
Objects.equals(l.getPassword(), r.getPassword());
|
||||
}
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Test
|
||||
void getTest(){
|
||||
Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTest(){
|
||||
Assertions.assertEquals(0, userService.getAll().size());
|
||||
userService.create(new UserEntity(null, "cawa", "cacawa", "1111"));
|
||||
Assertions.assertEquals(1, userService.getAll().size());
|
||||
userService.create(new UserEntity(null, "lambada", "lam", "0000"));
|
||||
Assertions.assertEquals(2, userService.getAll().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void UpdateTest(){
|
||||
var newEntity = new UserEntity(1L, "name", "login", "password");
|
||||
Assertions.assertNotEquals(newEntity, userService.get(1L));
|
||||
final UserEntity newEntityCopy = userService.update(
|
||||
1L, newEntity);
|
||||
Assertions.assertTrue(equalEntity(newEntityCopy, newEntity));
|
||||
Assertions.assertEquals(2L, userService.getAll().size());
|
||||
Assertions.assertTrue(equalEntity(newEntity, userService.get(1L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void DeleteTest(){
|
||||
Assertions.assertThrows(NullPointerException.class, () -> userService.delete(3L));
|
||||
Assertions.assertEquals(2L, userService.getAll().size());
|
||||
userService.delete(1L);
|
||||
Assertions.assertEquals(1L, userService.getAll().size());
|
||||
final UserEntity last = userService.get(2L);
|
||||
Assertions.assertEquals(2L, last.getId());
|
||||
|
||||
final UserEntity newEntity = userService.create(new UserEntity(null, "1", "1", "1"));
|
||||
Assertions.assertEquals(2L, userService.getAll().size());
|
||||
Assertions.assertEquals(3L, newEntity.getId());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user