From 3c6cc35264e80c216247e04a4079e71ec4eab492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Sat, 13 Apr 2024 14:01:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=202?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/demo/DemoApplication.java | 6 +- .../demo/orders/api/OrderController.java | 16 +++--- .../com/example/demo/orders/api/OrderDto.java | 11 ++++ .../demo/orders/model/OrderEntity.java | 12 +++- .../demo/orders/service/OrderService.java | 25 ++------ .../demo/users/api/UserController.java | 4 -- .../com/example/demo/users/api/UserDto.java | 12 ---- .../example/demo/users/model/UserEntity.java | 12 +--- .../demo/users/service/UserService.java | 1 - .../com/example/demo/GameServiceTests.java | 57 +++++++++++-------- .../com/example/demo/OrderServiceTest.java | 52 +++++++++++++++++ .../com/example/demo/TypeServiceTests.java | 22 +++---- .../com/example/demo/UserServiceTest.java | 48 ++++++++++++++++ 13 files changed, 181 insertions(+), 97 deletions(-) create mode 100644 src/test/java/com/example/demo/OrderServiceTest.java create mode 100644 src/test/java/com/example/demo/UserServiceTest.java diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index 85779ac..5f863c5 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -60,9 +60,11 @@ public class DemoApplication implements CommandLineRunner { games.add(game1); games.add(game2); - final var order = orderService.create(new OrderEntity(null,games)); + userService.create(new UserEntity(null, "login1", "email@mail.com", "qwerty123")); + userService.create(new UserEntity(null, "login2", "email@gmail.com", "qwerty1234")); - userService.create(new UserEntity(null, order, "login1", "email@mail.com", "qwerty123")); + orderService.create(new OrderEntity(null, 1L,games)); + orderService.create(new OrderEntity(null, 2L,games)); } public static void main(String[] args) { diff --git a/src/main/java/com/example/demo/orders/api/OrderController.java b/src/main/java/com/example/demo/orders/api/OrderController.java index a8f5834..b0c12be 100644 --- a/src/main/java/com/example/demo/orders/api/OrderController.java +++ b/src/main/java/com/example/demo/orders/api/OrderController.java @@ -5,7 +5,6 @@ import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -18,6 +17,7 @@ import com.example.demo.games.model.GameEntity; import com.example.demo.games.service.GameService; import com.example.demo.orders.model.OrderEntity; import com.example.demo.orders.service.OrderService; +import com.example.demo.users.service.UserService; import jakarta.validation.Valid; @@ -27,11 +27,13 @@ public class OrderController { private final GameService gameService; private final ModelMapper modelMapper; private final OrderService orderService; + //private final UserService userService; - public OrderController(GameService gameService, ModelMapper modelMapper, OrderService orderService){ + public OrderController(GameService gameService, ModelMapper modelMapper, OrderService orderService, UserService userService){ this.gameService = gameService; this.modelMapper = modelMapper; this.orderService = orderService; + //this.userService = userService; } private OrderDto toDto(OrderEntity entity){ @@ -41,6 +43,7 @@ public class OrderController { for(var game : entity.getGames()){ sum += game.getPrice(); } + dto.setUserId(entity.getUserId()); dto.setSum(sum); dto.setGames(entity.getGames().stream().map(GameEntity::getId).toList()); return dto; @@ -56,8 +59,8 @@ public class OrderController { } @GetMapping - public List getAll(@RequestParam(name = "games", defaultValue = "") List games){ - return orderService.getAll(games).stream().map(this::toDto).toList(); + public List getAll(@RequestParam(name = "userId", defaultValue = "") Long userId){ + return orderService.getAll(userId).stream().map(this::toDto).toList(); } @GetMapping("/{id}") @@ -70,11 +73,6 @@ public class OrderController { return toDto(orderService.create(toEntity(dto))); } - @PutMapping("/{id}") - public OrderDto update(@PathVariable(name = "id") Long id, @RequestBody OrderDto dto){ - return toDto(orderService.update(id, toEntity(dto))); - } - @DeleteMapping("/{id}") public OrderDto delete(@PathVariable(name = "id") Long id){ return toDto(orderService.delete(id)); diff --git a/src/main/java/com/example/demo/orders/api/OrderDto.java b/src/main/java/com/example/demo/orders/api/OrderDto.java index 44f93a3..57c444f 100644 --- a/src/main/java/com/example/demo/orders/api/OrderDto.java +++ b/src/main/java/com/example/demo/orders/api/OrderDto.java @@ -14,6 +14,9 @@ public class OrderDto { @Min(1) private double sum; @NotNull + @Min(1) + private Long userId; + @NotNull private final List games = new ArrayList<>(); @JsonProperty(access = JsonProperty.Access.READ_ONLY) @@ -39,4 +42,12 @@ public class OrderDto { this.games.clear(); this.games.addAll(games); } + + public Long getUserId(){ + return userId; + } + + public void setUserId(Long userId){ + this.userId = userId; + } } 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 b0ae692..407e0fe 100644 --- a/src/main/java/com/example/demo/orders/model/OrderEntity.java +++ b/src/main/java/com/example/demo/orders/model/OrderEntity.java @@ -11,14 +11,16 @@ import com.example.demo.games.model.GameEntity; public class OrderEntity extends BaseEntity{ private double sum; + private Long userId; private final List games = new ArrayList<>(); public OrderEntity(){ super(); } - public OrderEntity(Long id, List games){ + public OrderEntity(Long id, Long userId, List games){ super(id); + this.userId = userId; this.games.clear(); this.games.addAll(games); } @@ -32,10 +34,16 @@ public class OrderEntity extends BaseEntity{ return sum; } + public Long getUserId(){ + return userId; + } + public void setUserId(Long userId){ + this.userId = userId; + } + public List getGames(){ return games; } - public void setGames(GameEntity game){ this.games.add(game); } 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 6cf17ab..c58b75e 100644 --- a/src/main/java/com/example/demo/orders/service/OrderService.java +++ b/src/main/java/com/example/demo/orders/service/OrderService.java @@ -3,32 +3,24 @@ package com.example.demo.orders.service; import org.springframework.stereotype.Service; import java.util.List; -import java.util.ArrayList; import java.util.Objects; import java.util.Optional; import com.example.demo.core.error.NotFoundException; -import com.example.demo.games.service.GameService; import com.example.demo.orders.model.OrderEntity; import com.example.demo.orders.repository.OrderRepository; @Service public class OrderService { private final OrderRepository repository; - private final GameService gameService; - public OrderService(OrderRepository repository, GameService gameService){ + public OrderService(OrderRepository repository){ this.repository = repository; - this.gameService = gameService; } - public List getAll(List games){ - var gamesEnt = new ArrayList<>(); - for(var gameId : games){ - gamesEnt.add(gameService.get(gameId)); - } - if(!Objects.equals(gamesEnt.size(), 0)){ - return repository.getAll().stream().filter(game -> game.getGames().containsAll(gamesEnt)).toList(); + public List getAll(Long userId){ + if(!Objects.equals(userId, null)){ + return repository.getAll().stream().filter(user -> user.getId().equals(userId)).toList(); } return repository.getAll(); } @@ -41,15 +33,6 @@ public class OrderService { return repository.create(entity); } - public OrderEntity update(Long id, OrderEntity entity){ - final OrderEntity existEntity = get(id); - var games = entity.getGames(); - for(var game : games){ - existEntity.setGames(game); - } - return repository.update(existEntity); - } - public OrderEntity delete(Long id){ final OrderEntity exisEntity = get(id); return repository.delete(exisEntity); diff --git a/src/main/java/com/example/demo/users/api/UserController.java b/src/main/java/com/example/demo/users/api/UserController.java index e9e23ca..9b9c8e2 100644 --- a/src/main/java/com/example/demo/users/api/UserController.java +++ b/src/main/java/com/example/demo/users/api/UserController.java @@ -22,13 +22,11 @@ import jakarta.validation.Valid; @RestController @RequestMapping(Constants.API_URL+"/user") public class UserController { - private final OrderService orderService; private final ModelMapper modelMapper; private final UserService userService; public UserController(OrderService orderService, ModelMapper modelMapper, UserService userService){ this.modelMapper = modelMapper; - this.orderService = orderService; this.userService = userService; } @@ -38,13 +36,11 @@ public class UserController { dto.setLogin(entity.getLogin()); dto.setEmail(entity.getEmail()); dto.setPassword(entity.getPassword()); - dto.setOrderId(entity.getOrder().getId()); return dto; } private UserEntity toEntity(UserDto dto){ final UserEntity entity = modelMapper.map(dto, UserEntity.class); - entity.setOrder(orderService.get(dto.getOrderId())); return entity; } diff --git a/src/main/java/com/example/demo/users/api/UserDto.java b/src/main/java/com/example/demo/users/api/UserDto.java index f79067b..4de59c1 100644 --- a/src/main/java/com/example/demo/users/api/UserDto.java +++ b/src/main/java/com/example/demo/users/api/UserDto.java @@ -2,9 +2,7 @@ package com.example.demo.users.api; import com.fasterxml.jackson.annotation.JsonProperty; -import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotNull; public class UserDto { private Long id; @@ -14,9 +12,6 @@ public class UserDto { private String email; @NotBlank private String password; - @NotNull - @Min(1) - private Long orderId; @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId(){ @@ -46,11 +41,4 @@ public class UserDto { public void setPassword(String password){ this.password = password; } - - public Long getOrderId(){ - return orderId; - } - public void setOrderId(Long orderId){ - this.orderId = orderId; - } } diff --git a/src/main/java/com/example/demo/users/model/UserEntity.java b/src/main/java/com/example/demo/users/model/UserEntity.java index 560b0d2..b9e2135 100644 --- a/src/main/java/com/example/demo/users/model/UserEntity.java +++ b/src/main/java/com/example/demo/users/model/UserEntity.java @@ -1,24 +1,21 @@ package com.example.demo.users.model; import com.example.demo.core.model.BaseEntity; -import com.example.demo.orders.model.OrderEntity; public class UserEntity extends BaseEntity{ private String login; private String email; private String password; - private OrderEntity order; public UserEntity(){ super(); } - public UserEntity(Long id, OrderEntity order, String login, String email, String password){ + public UserEntity(Long id, String login, String email, String password){ super(id); this.login = login; this.email = email; this.password = password; - this.order = order; } public String getLogin(){ @@ -41,11 +38,4 @@ public class UserEntity extends BaseEntity{ public void setPassword(String password){ this.password = password; } - - public OrderEntity getOrder(){ - return order; - } - public void setOrder(OrderEntity order){ - this.order = order; - } } diff --git a/src/main/java/com/example/demo/users/service/UserService.java b/src/main/java/com/example/demo/users/service/UserService.java index f539565..749fc6d 100644 --- a/src/main/java/com/example/demo/users/service/UserService.java +++ b/src/main/java/com/example/demo/users/service/UserService.java @@ -34,7 +34,6 @@ public class UserService { existEntity.setLogin(entity.getLogin()); existEntity.setEmail(entity.getEmail()); existEntity.setPassword(entity.getPassword()); - existEntity.setOrder(entity.getOrder()); return repository.update(existEntity); } diff --git a/src/test/java/com/example/demo/GameServiceTests.java b/src/test/java/com/example/demo/GameServiceTests.java index a74d7c9..a31f472 100644 --- a/src/test/java/com/example/demo/GameServiceTests.java +++ b/src/test/java/com/example/demo/GameServiceTests.java @@ -1,5 +1,8 @@ package com.example.demo; +import java.util.ArrayList; +import java.util.List; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; import org.junit.jupiter.api.Order; @@ -11,6 +14,7 @@ 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; @SpringBootTest @@ -18,36 +22,41 @@ import com.example.demo.games.service.GameService; class GameServiceTests { @Autowired private GameService gameService; + List list = new ArrayList(); + List list2 = new ArrayList(); @Test void getTest(){ Assertions.assertThrows(NotFoundException.class, () -> gameService.get(0L)); } - // @Test - // @Order(1) - // void createTest(){ - // gameService.create(new GameEntity(null, null, "Game", 210.0, "cool game", null)); - // gameService.create(new GameEntity(null, null, "VideoGame", 2100.0, "very cool game", null)); - // Assertions.assertEquals(2, gameService.getAll(0L,0L).size()); - // } + @Test + @Order(1) + void createTest(){ + + list.add(new GenreEntity(null, "приключения")); + list.add(new GenreEntity(null, "Симулятор")); + gameService.create(new GameEntity(null, null, "Game", 210.0, "cool game", list)); + gameService.create(new GameEntity(null, null, "VideoGame", 2100.0, "very cool game", list)); + Assertions.assertEquals(4, gameService.getAll(0L,list2).size()); + } - // @Test - // @Order(2) - // void updateTest(){ - // final GameEntity newGame = new GameEntity(null, null, "Programm", 1500.0, "cool not game", null); - // final GameEntity updGame = gameService.update(2L, newGame); - // Assertions.assertEquals(2, gameService.getAll(0L, 0L).size()); - // Assertions.assertEquals(updGame, gameService.get(2L)); - // Assertions.assertEquals(newGame.getName(), updGame.getName()); - // Assertions.assertEquals(newGame.getDescription(), updGame.getDescription()); - // Assertions.assertEquals(newGame.getPrice(), updGame.getPrice()); - // } + @Test + @Order(2) + void updateTest(){ + final GameEntity newGame = new GameEntity(null, null, "Programm", 1500.0, "cool not game", list); + final GameEntity updGame = gameService.update(2L, newGame); + Assertions.assertEquals(4, gameService.getAll(0L, list2).size()); + Assertions.assertEquals(updGame, gameService.get(2L)); + Assertions.assertEquals(newGame.getName(), updGame.getName()); + Assertions.assertEquals(newGame.getDescription(), updGame.getDescription()); + Assertions.assertEquals(newGame.getPrice(), updGame.getPrice()); + } - // @Test - // @Order(3) - // void deleteTest(){ - // gameService.delete(2L); - // Assertions.assertEquals(1L, gameService.getAll(0L,0L).size()); - // } + @Test + @Order(3) + void deleteTest(){ + gameService.delete(2L); + Assertions.assertEquals(3L, gameService.getAll(0L,list2).size()); + } } 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..3d7fda5 --- /dev/null +++ b/src/test/java/com/example/demo/OrderServiceTest.java @@ -0,0 +1,52 @@ +package com.example.demo; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Assertions; +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.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +public class OrderServiceTest { + @Autowired + private OrderService orderService; + @Autowired + private GameService gameService; + + @Test + void getTest(){ + Assertions.assertThrows(NotFoundException.class, () -> orderService.get(0L)); + } + + @Test + @Order(1) + void createTest(){ + final List games = new ArrayList(); + games.add(gameService.get(1L)); + orderService.create(new OrderEntity(null, 1L,games)); + final var last = orderService.create(new OrderEntity(null, 2L,games)); + Assertions.assertEquals(4, orderService.getAll(null).size()); + Assertions.assertEquals(last, orderService.get(4L)); + } + + @Test + @Order(2) + void deleteTest(){ + orderService.delete(2L); + Assertions.assertEquals(3, orderService.getAll(null).size()); + final OrderEntity last = orderService.get(1L); + Assertions.assertEquals(1L, last.getId()); + } +} diff --git a/src/test/java/com/example/demo/TypeServiceTests.java b/src/test/java/com/example/demo/TypeServiceTests.java index 38014c2..e0a3e39 100644 --- a/src/test/java/com/example/demo/TypeServiceTests.java +++ b/src/test/java/com/example/demo/TypeServiceTests.java @@ -29,30 +29,30 @@ class TypeServiceTests { typeService.create(new TypeEntity(null, "Игра")); typeService.create(new TypeEntity(null, "Программа")); final TypeEntity last = typeService.create(new TypeEntity(null, "Игра2")); - Assertions.assertEquals(3, typeService.getAll().size()); - Assertions.assertEquals(last, typeService.get(3L)); + Assertions.assertEquals(5, typeService.getAll().size()); + Assertions.assertEquals(last, typeService.get(5L)); } @Test @Order(2) void updateTest(){ final String test = "TEST"; - final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test)); - Assertions.assertEquals(3, typeService.getAll().size()); - Assertions.assertEquals(newEntity, typeService.get(3L)); + final TypeEntity newEntity = typeService.update(5L, new TypeEntity(1L, test)); + Assertions.assertEquals(5, typeService.getAll().size()); + Assertions.assertEquals(newEntity, typeService.get(5L)); Assertions.assertEquals(test, newEntity.getName()); } @Test @Order(3) void deleteTest(){ - typeService.delete(3L); - Assertions.assertEquals(2, typeService.getAll().size()); - final TypeEntity last = typeService.get(2L); - Assertions.assertEquals(2L, last.getId()); + typeService.delete(5L); + Assertions.assertEquals(4, typeService.getAll().size()); + final TypeEntity last = typeService.get(4L); + Assertions.assertEquals(4L, last.getId()); final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Игра")); - Assertions.assertEquals(3, typeService.getAll().size()); - Assertions.assertEquals(4L, newEntity.getId()); + Assertions.assertEquals(5, typeService.getAll().size()); + Assertions.assertEquals(6L, newEntity.getId()); } } diff --git a/src/test/java/com/example/demo/UserServiceTest.java b/src/test/java/com/example/demo/UserServiceTest.java new file mode 100644 index 0000000..c58f269 --- /dev/null +++ b/src/test/java/com/example/demo/UserServiceTest.java @@ -0,0 +1,48 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +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) +public class UserServiceTest { + @Autowired + UserService userService; + + @Test + void getTest(){ + Assertions.assertThrows(NotFoundException.class, () -> userService.get(0L)); + } + + @Test + @Order(1) + void createTest(){ + userService.create(new UserEntity(null, "login5", "gmail", "qwert123")); + final UserEntity last = userService.create(new UserEntity(null, "login6", "mail", "qwer123")); + Assertions.assertEquals(4, userService.getAll().size()); + Assertions.assertEquals(last, userService.get(4L)); + } + + @Test + @Order(2) + void updateTest(){ + userService.update(4L, new UserEntity(null,"login","email", "qwe123")); + Assertions.assertEquals(4, userService.getAll().size()); + } + + @Test + @Order(3) + void deleteTest(){ + userService.delete(2L); + Assertions.assertEquals(3, userService.getAll().size()); + } +}