From bd0a3ef8cf4b910aaecaaaf6ecde2d03423318d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D1=8F=D1=87=D0=B5=D1=81=D0=BB=D0=B0=D0=B2=20=D0=98?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Mon, 25 Mar 2024 13:40:49 +0400 Subject: [PATCH] test --- demo/build.gradle | 1 + .../com/example/demo/DemoApplication.java | 2 +- .../demo/orders/api/OrderController.java | 74 ++++++++++++++++ .../com/example/demo/orders/api/OrderDto.java | 52 ++++++++++++ .../demo/orders/model/OrderEntity.java | 66 +++++++++++++++ .../orders/repository/OrderRepository.java | 10 +++ .../demo/orders/service/OrderService.java | 48 +++++++++++ .../demo/product/api/ProductController.java | 70 ++++++++++++++++ .../example/demo/product/api/ProductDto.java | 52 ++++++++++++ .../demo/product/model/ProductEntity.java | 65 ++++++++++++++ .../product/repository/ProductRepository.java | 10 +++ .../demo/product/service/ProductService.java | 51 +++++++++++ .../demo/products/api/ProductController.java | 4 +- .../demo/types/api/TypeController.java | 2 +- .../demo/users/api/UserController.java | 2 +- .../com/example/demo/OrderServiceTests.java | 84 +++++++++++++++++++ .../com/example/demo/ProductServiceTests.java | 16 ++-- .../example/demo/ProductsServiceTests.java | 75 +++++++++++++++++ .../com/example/demo/TypeServiceTests.java | 12 +-- 19 files changed, 677 insertions(+), 19 deletions(-) create mode 100644 demo/src/main/java/com/example/demo/orders/api/OrderController.java create mode 100644 demo/src/main/java/com/example/demo/orders/api/OrderDto.java create mode 100644 demo/src/main/java/com/example/demo/orders/model/OrderEntity.java create mode 100644 demo/src/main/java/com/example/demo/orders/repository/OrderRepository.java create mode 100644 demo/src/main/java/com/example/demo/orders/service/OrderService.java create mode 100644 demo/src/main/java/com/example/demo/product/api/ProductController.java create mode 100644 demo/src/main/java/com/example/demo/product/api/ProductDto.java create mode 100644 demo/src/main/java/com/example/demo/product/model/ProductEntity.java create mode 100644 demo/src/main/java/com/example/demo/product/repository/ProductRepository.java create mode 100644 demo/src/main/java/com/example/demo/product/service/ProductService.java create mode 100644 demo/src/test/java/com/example/demo/OrderServiceTests.java create mode 100644 demo/src/test/java/com/example/demo/ProductsServiceTests.java diff --git a/demo/build.gradle b/demo/build.gradle index b7867b9..92a39ba 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -17,6 +17,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0' implementation 'org.modelmapper:modelmapper:3.2.0' diff --git a/demo/src/main/java/com/example/demo/DemoApplication.java b/demo/src/main/java/com/example/demo/DemoApplication.java index 41109e7..51ed527 100644 --- a/demo/src/main/java/com/example/demo/DemoApplication.java +++ b/demo/src/main/java/com/example/demo/DemoApplication.java @@ -37,7 +37,7 @@ public class DemoApplication implements CommandLineRunner { final var type2 = typeService.create(new TypeEntity(null, "Пепперони")); final var type3 = typeService.create(new TypeEntity(null, "Сырная")); - log.info("Create default items values"); + log.info("Create default products values"); productService.create(new ProductEntity(null, type1, 399.00, 20)); productService.create(new ProductEntity(null, type1, 499.00, 3)); productService.create(new ProductEntity(null, type2, 450.50, 30)); diff --git a/demo/src/main/java/com/example/demo/orders/api/OrderController.java b/demo/src/main/java/com/example/demo/orders/api/OrderController.java new file mode 100644 index 0000000..1a8ec32 --- /dev/null +++ b/demo/src/main/java/com/example/demo/orders/api/OrderController.java @@ -0,0 +1,74 @@ +package com.example.demo.orders.api; + +import java.util.List; + +import org.modelmapper.ModelMapper; +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; +import org.springframework.web.bind.annotation.RestController; + +import com.example.demo.core.configuration.Constants; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; +import com.example.demo.product.service.ProductService; +import com.example.demo.users.service.UserService; + +import jakarta.validation.Valid; + +@RestController +@RequestMapping(Constants.API_URL + "/order") +public class OrderController { + private final UserService userService; + private final ProductService productService; + private final OrderService orderService; + private final ModelMapper modelMapper; + + public OrderController(UserService userService, ProductService productService, OrderService orderService, ModelMapper modelMapper) { + this.userService = userService; + this.productService = productService; + this.orderService = orderService; + this.modelMapper = modelMapper; + } + + private OrderDto toDto(OrderEntity entity) { + return modelMapper.map(entity, OrderDto.class); + } + + private OrderEntity toEntity(OrderDto dto) { + final OrderEntity entity = modelMapper.map(dto, OrderEntity.class); + entity.setUser(userService.get(dto.getUserId())); + entity.setProduct(productService.get(dto.getProductId())); + return entity; + } + + @GetMapping + public List getAll(@RequestParam(name = "userId", defaultValue = "0") Long productId) { + return orderService.getAll(productId).stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public OrderDto get(@PathVariable(name = "id") Long id) { + return toDto(orderService.get(id)); + } + + @PostMapping + public OrderDto create(@RequestBody @Valid OrderDto dto) { + return toDto(orderService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public OrderDto update(@PathVariable(name = "id") Long id, @RequestBody @Valid 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/demo/src/main/java/com/example/demo/orders/api/OrderDto.java b/demo/src/main/java/com/example/demo/orders/api/OrderDto.java new file mode 100644 index 0000000..96a4ac5 --- /dev/null +++ b/demo/src/main/java/com/example/demo/orders/api/OrderDto.java @@ -0,0 +1,52 @@ +package com.example.demo.orders.api; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; + +public class OrderDto { + private Long id; + @NotNull + @Min(1) + private Long userId; + @NotNull + @Min(1) + private Long productId; + @NotNull + @Min(1) + private Integer count; + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getUserId() { + return userId; + } + + public void setUserId(Long userId) { + this.userId = userId; + } + + public Long getProductId() { + return productId; + } + + public void setProductId(Long productId) { + this.productId = productId; + } + + public Integer getCount() { + return count; + } + + public void setPrice(Integer count) { + this.count = count; + } +} diff --git a/demo/src/main/java/com/example/demo/orders/model/OrderEntity.java b/demo/src/main/java/com/example/demo/orders/model/OrderEntity.java new file mode 100644 index 0000000..055f280 --- /dev/null +++ b/demo/src/main/java/com/example/demo/orders/model/OrderEntity.java @@ -0,0 +1,66 @@ +package com.example.demo.orders.model; + +import java.util.Objects; + +import com.example.demo.core.model.BaseEntity; +import com.example.demo.product.model.ProductEntity; +import com.example.demo.users.model.UserEntity; + +public class OrderEntity extends BaseEntity { + private UserEntity user; + private ProductEntity product; + private Integer count; + + public OrderEntity() { + super(); + } + + public OrderEntity(Long id, UserEntity user, ProductEntity product, Integer count) { + super(id); + this.user = user; + this.product = product; + this.count = count; + } + + public UserEntity getUser() { + return user; + } + + public void setUser(UserEntity user) { + this.user = user; + } + + public ProductEntity getProduct() { + return product; + } + + public void setProduct(ProductEntity product) { + this.product = product; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } + + @Override + public int hashCode() { + return Objects.hash(id, user, product, count); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + final OrderEntity other = (OrderEntity) obj; + return Objects.equals(other.getId(), id) + && Objects.equals(other.getUser(), user) + && Objects.equals(other.getProduct(), product) + && Objects.equals(other.getCount(), count); + } +} diff --git a/demo/src/main/java/com/example/demo/orders/repository/OrderRepository.java b/demo/src/main/java/com/example/demo/orders/repository/OrderRepository.java new file mode 100644 index 0000000..ae96b81 --- /dev/null +++ b/demo/src/main/java/com/example/demo/orders/repository/OrderRepository.java @@ -0,0 +1,10 @@ +package com.example.demo.orders.repository; + +import org.springframework.stereotype.Repository; + +import com.example.demo.core.repository.MapRepository; +import com.example.demo.orders.model.OrderEntity; + +@Repository +public class OrderRepository extends MapRepository { +} diff --git a/demo/src/main/java/com/example/demo/orders/service/OrderService.java b/demo/src/main/java/com/example/demo/orders/service/OrderService.java new file mode 100644 index 0000000..74af30a --- /dev/null +++ b/demo/src/main/java/com/example/demo/orders/service/OrderService.java @@ -0,0 +1,48 @@ +package com.example.demo.orders.service; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.orders.model.OrderEntity; +import com.example.demo.orders.repository.OrderRepository; + +public class OrderService { + private final OrderRepository repository; + + public OrderService(OrderRepository repository) { + this.repository = repository; + } + + public List getAll(Long userId) { + if (Objects.equals(userId, 0L)) { + return repository.getAll(); + } + return repository.getAll().stream() + .filter(product -> product.getUser().getId().equals(userId)) + .toList(); + } + + public OrderEntity get(Long id) { + return Optional.ofNullable(repository.get(id)) + .orElseThrow(() -> new NotFoundException(id)); + } + + public OrderEntity create(OrderEntity entity) { + return repository.create(entity); + } + + public OrderEntity update(Long id, OrderEntity entity) { + final OrderEntity existsEntity = get(id); + existsEntity.setUser(entity.getUser()); + existsEntity.setProduct(entity.getProduct()); + existsEntity.setCount(entity.getCount()); + return repository.update(existsEntity); + } + + public OrderEntity delete(Long id) { + final OrderEntity existsEntity = get(id); + return repository.delete(existsEntity); + } +} diff --git a/demo/src/main/java/com/example/demo/product/api/ProductController.java b/demo/src/main/java/com/example/demo/product/api/ProductController.java new file mode 100644 index 0000000..5671be9 --- /dev/null +++ b/demo/src/main/java/com/example/demo/product/api/ProductController.java @@ -0,0 +1,70 @@ +package com.example.demo.product.api; + +import java.util.List; + +import org.modelmapper.ModelMapper; +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; +import org.springframework.web.bind.annotation.RestController; + +import com.example.demo.core.configuration.Constants; +import com.example.demo.product.model.ProductEntity; +import com.example.demo.product.service.ProductService; +import com.example.demo.types.service.TypeService; + +import jakarta.validation.Valid; + +@RestController +@RequestMapping(Constants.API_URL + "/product") +public class ProductController { + private final ProductService productService; + private final TypeService typeService; + private final ModelMapper modelMapper; + + public ProductController(ProductService productService, TypeService typeService, ModelMapper modelMapper) { + this.productService = productService; + this.typeService = typeService; + this.modelMapper = modelMapper; + } + + private ProductDto toDto(ProductEntity entity) { + return modelMapper.map(entity, ProductDto.class); + } + + private ProductEntity toEntity(ProductDto dto) { + final ProductEntity entity = modelMapper.map(dto, ProductEntity.class); + entity.setType(typeService.get(dto.getTypeId())); + return entity; + } + + @GetMapping + public List getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) { + return productService.getAll(typeId).stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public ProductDto get(@PathVariable(name = "id") Long id) { + return toDto(productService.get(id)); + } + + @PostMapping + public ProductDto create(@RequestBody @Valid ProductDto dto) { + return toDto(productService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public ProductDto update(@PathVariable(name = "id") Long id, @RequestBody @Valid ProductDto dto) { + return toDto(productService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public ProductDto delete(@PathVariable(name = "id") Long id) { + return toDto(productService.delete(id)); + } +} diff --git a/demo/src/main/java/com/example/demo/product/api/ProductDto.java b/demo/src/main/java/com/example/demo/product/api/ProductDto.java new file mode 100644 index 0000000..69c0168 --- /dev/null +++ b/demo/src/main/java/com/example/demo/product/api/ProductDto.java @@ -0,0 +1,52 @@ +package com.example.demo.product.api; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; + +public class ProductDto { + private Long id; + @NotNull + @Min(1) + private String name; + @NotNull + @Min(1) + private Long typeId; + @NotNull + @Min(1) + private Double price; + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getTypeId() { + return typeId; + } + + public void setTypeId(Long typeId) { + this.typeId = typeId; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } +} diff --git a/demo/src/main/java/com/example/demo/product/model/ProductEntity.java b/demo/src/main/java/com/example/demo/product/model/ProductEntity.java new file mode 100644 index 0000000..3dec1be --- /dev/null +++ b/demo/src/main/java/com/example/demo/product/model/ProductEntity.java @@ -0,0 +1,65 @@ +package com.example.demo.product.model; + +import java.util.Objects; + +import com.example.demo.core.model.BaseEntity; +import com.example.demo.types.model.TypeEntity; + +public class ProductEntity extends BaseEntity { + private String name; + private TypeEntity type; + private Double price; + + public ProductEntity() { + super(); + } + + public ProductEntity(Long id, String name, TypeEntity type, Double price) { + super(id); + this.name = name; + this.type = type; + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public TypeEntity getType() { + return type; + } + + public void setType(TypeEntity type) { + this.type = type; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + @Override + public int hashCode() { + return Objects.hash(id, name, type, price); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + final ProductEntity other = (ProductEntity) obj; + return Objects.equals(other.getId(), id) + && Objects.equals(other.getName(), name) + && Objects.equals(other.getType(), type) + && Objects.equals(other.getPrice(), price); + } +} diff --git a/demo/src/main/java/com/example/demo/product/repository/ProductRepository.java b/demo/src/main/java/com/example/demo/product/repository/ProductRepository.java new file mode 100644 index 0000000..cbc371e --- /dev/null +++ b/demo/src/main/java/com/example/demo/product/repository/ProductRepository.java @@ -0,0 +1,10 @@ +package com.example.demo.product.repository; + +import org.springframework.stereotype.Repository; + +import com.example.demo.core.repository.MapRepository; +import com.example.demo.product.model.ProductEntity; + +@Repository +public class ProductRepository extends MapRepository { +} diff --git a/demo/src/main/java/com/example/demo/product/service/ProductService.java b/demo/src/main/java/com/example/demo/product/service/ProductService.java new file mode 100644 index 0000000..93a50fb --- /dev/null +++ b/demo/src/main/java/com/example/demo/product/service/ProductService.java @@ -0,0 +1,51 @@ +package com.example.demo.product.service; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import org.springframework.stereotype.Service; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.product.model.ProductEntity; +import com.example.demo.product.repository.ProductRepository; + +@Service +public class ProductService { + private final ProductRepository repository; + + public ProductService(ProductRepository repository) { + this.repository = repository; + } + + public List getAll(Long typeId) { + if (Objects.equals(typeId, 0L)) { + return repository.getAll(); + } + return repository.getAll().stream() + .filter(product -> product.getType().getId().equals(typeId)) + .toList(); + } + + public ProductEntity get(Long id) { + return Optional.ofNullable(repository.get(id)) + .orElseThrow(() -> new NotFoundException(id)); + } + + public ProductEntity create(ProductEntity entity) { + return repository.create(entity); + } + + public ProductEntity update(Long id, ProductEntity entity) { + final ProductEntity existsEntity = get(id); + existsEntity.setName(entity.getName()); + existsEntity.setType(entity.getType()); + existsEntity.setPrice(entity.getPrice()); + return repository.update(existsEntity); + } + + public ProductEntity delete(Long id) { + final ProductEntity existsEntity = get(id); + return repository.delete(existsEntity); + } +} diff --git a/demo/src/main/java/com/example/demo/products/api/ProductController.java b/demo/src/main/java/com/example/demo/products/api/ProductController.java index 834e18a..e9a1cc5 100644 --- a/demo/src/main/java/com/example/demo/products/api/ProductController.java +++ b/demo/src/main/java/com/example/demo/products/api/ProductController.java @@ -21,7 +21,7 @@ import com.example.demo.types.service.TypeService; import jakarta.validation.Valid; @RestController -@RequestMapping(Constants.API_URL + "/product") +@RequestMapping(Constants.API_URL + "/products") public class ProductController { private final ProductService productService; private final TypeService typeService; @@ -59,7 +59,7 @@ public class ProductController { } @PutMapping("/{id}") - public ProductDto update(@PathVariable(name = "id") Long id, @RequestBody ProductDto dto) { + public ProductDto update(@PathVariable(name = "id") Long id, @RequestBody @Valid ProductDto dto) { return toDto(productService.update(id, toEntity(dto))); } diff --git a/demo/src/main/java/com/example/demo/types/api/TypeController.java b/demo/src/main/java/com/example/demo/types/api/TypeController.java index 81f07cf..d9785d8 100644 --- a/demo/src/main/java/com/example/demo/types/api/TypeController.java +++ b/demo/src/main/java/com/example/demo/types/api/TypeController.java @@ -53,7 +53,7 @@ public class TypeController { } @PutMapping("/{id}") - public TypeDto update(@PathVariable(name = "id") Long id, @RequestBody TypeDto dto) { + public TypeDto update(@PathVariable(name = "id") Long id, @RequestBody @Valid TypeDto dto) { return toDto(typeService.update(id, toEntity(dto))); } diff --git a/demo/src/main/java/com/example/demo/users/api/UserController.java b/demo/src/main/java/com/example/demo/users/api/UserController.java index 1d00e35..17e1bd2 100644 --- a/demo/src/main/java/com/example/demo/users/api/UserController.java +++ b/demo/src/main/java/com/example/demo/users/api/UserController.java @@ -53,7 +53,7 @@ public class UserController { } @PutMapping("/{id}") - public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto) { + public UserDto update(@PathVariable(name = "id") Long id, @RequestBody @Valid UserDto dto) { return toDto(userService.update(id, toEntity(dto))); } diff --git a/demo/src/test/java/com/example/demo/OrderServiceTests.java b/demo/src/test/java/com/example/demo/OrderServiceTests.java new file mode 100644 index 0000000..9c6cc3c --- /dev/null +++ b/demo/src/test/java/com/example/demo/OrderServiceTests.java @@ -0,0 +1,84 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +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.orders.model.OrderEntity; +import com.example.demo.orders.service.OrderService; +import com.example.demo.product.model.ProductEntity; +import com.example.demo.product.service.ProductService; +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) +public class OrderServiceTests { + @Autowired + private OrderService orderService; + + @Autowired + private UserService userService; + + @Autowired + private TypeService typeService; + + @Autowired + private ProductService productService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> orderService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + final UserEntity one = userService.create(new UserEntity(null, "test", "test", "test")); + final TypeEntity two = typeService.create(new TypeEntity(null, "test")); + final ProductEntity test = productService.create(new ProductEntity(null, "test", two ,100.00)); + + orderService.create(new OrderEntity(null, one, test, 5)); + orderService.create(new OrderEntity(null, one, test, 6)); + + final OrderEntity last = orderService.create(new OrderEntity(null, one, test, 7)); + + Assertions.assertEquals(3, orderService.getAll(0L).size()); + Assertions.assertEquals(last, orderService.get(6L)); + } + + @Test + @Order(2) + void updateTest() { + final UserEntity one = userService.create(new UserEntity(null, "test", "test", "test")); + final TypeEntity two = typeService.create(new TypeEntity(null, "test")); + final ProductEntity test = productService.create(new ProductEntity(null, "test", two ,100.00)); + + + Assertions.assertEquals(6, orderService.getAll().size()); + Assertions.assertEquals(newEntity, orderService.get(3L)); + Assertions.assertEquals(test, newEntity.getName()); + Assertions.assertNotEquals(oldName, newEntity.getName()); + } + + @Test + @Order(3) + void deleteTest() { + orderService.delete(3L); + Assertions.assertEquals(5, orderService.getAll().size()); + final TypeEntity last = orderService.get(2L); + Assertions.assertEquals(2L, last.getId()); + + final TypeEntity newEntity = orderService.create(new TypeEntity(null, "Сырная")); + Assertions.assertEquals(6, orderService.getAll().size()); + Assertions.assertEquals(7L, newEntity.getId()); + } +} diff --git a/demo/src/test/java/com/example/demo/ProductServiceTests.java b/demo/src/test/java/com/example/demo/ProductServiceTests.java index 4630554..277238d 100644 --- a/demo/src/test/java/com/example/demo/ProductServiceTests.java +++ b/demo/src/test/java/com/example/demo/ProductServiceTests.java @@ -9,14 +9,14 @@ 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.products.model.ProductEntity; -import com.example.demo.products.service.ProductService; +import com.example.demo.product.model.ProductEntity; +import com.example.demo.product.service.ProductService; import com.example.demo.types.model.TypeEntity; import com.example.demo.types.service.TypeService; @SpringBootTest @TestMethodOrder(OrderAnnotation.class) -class ProductServiceTests { +class ProductsServiceTests { @Autowired private ProductService productService; @@ -33,10 +33,10 @@ class ProductServiceTests { void createTest() { final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони")); - productService.create(new ProductEntity(null, type, 399.00, 20)); - productService.create(new ProductEntity(null, type, 599.00, 3)); + productService.create(new ProductEntity(null, "test", type, 399.00)); + productService.create(new ProductEntity(null, "test1", type, 599.00)); - final ProductEntity last = productService.create(new ProductEntity(null, type, 1500.00, 6)); + final ProductEntity last = productService.create(new ProductEntity(null, "test2", type, 1500.0)); Assertions.assertEquals(3, productService.getAll(0L).size()); Assertions.assertEquals(last, productService.get(3L)); @@ -50,7 +50,7 @@ class ProductServiceTests { final ProductEntity entity = productService.get(3L); final Double oldPrice = entity.getPrice(); - final ProductEntity newEntity = productService.update(3L, new ProductEntity(null, type, 200.00, 6)); + final ProductEntity newEntity = productService.update(3L, new ProductEntity(null, "test4", type, 200.00)); Assertions.assertEquals(3, productService.getAll(0L).size()); Assertions.assertEquals(newEntity, productService.get(3L)); @@ -67,7 +67,7 @@ class ProductServiceTests { Assertions.assertThrows(NotFoundException.class, () -> productService.get(3L)); final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони")); - final ProductEntity newEntity = productService.create(new ProductEntity(null, type, 499.00, 10)); + final ProductEntity newEntity = productService.create(new ProductEntity(null, "test5", type, 499.00)); Assertions.assertEquals(3, productService.getAll(0L).size()); Assertions.assertEquals(4L, newEntity.getId()); diff --git a/demo/src/test/java/com/example/demo/ProductsServiceTests.java b/demo/src/test/java/com/example/demo/ProductsServiceTests.java new file mode 100644 index 0000000..a3660f5 --- /dev/null +++ b/demo/src/test/java/com/example/demo/ProductsServiceTests.java @@ -0,0 +1,75 @@ +package com.example.demo; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +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.products.model.ProductEntity; +import com.example.demo.products.service.ProductService; +import com.example.demo.types.model.TypeEntity; +import com.example.demo.types.service.TypeService; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class ProductsServiceTests { + @Autowired + private ProductService productService; + + @Autowired + private TypeService typeService; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> productService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони")); + + productService.create(new ProductEntity(null, type, 399.00, 20)); + productService.create(new ProductEntity(null, type, 599.00, 3)); + + final ProductEntity last = productService.create(new ProductEntity(null, type, 1500.00, 6)); + + Assertions.assertEquals(3, productService.getAll(0L).size()); + Assertions.assertEquals(last, productService.get(3L)); + } + + @Test + @Order(2) + void updateTest() { + final TypeEntity type = typeService.create(new TypeEntity(null, "Мясная")); + + final ProductEntity entity = productService.get(3L); + final Double oldPrice = entity.getPrice(); + + final ProductEntity newEntity = productService.update(3L, new ProductEntity(null, type, 200.00, 6)); + + Assertions.assertEquals(3, productService.getAll(0L).size()); + Assertions.assertEquals(newEntity, productService.get(3L)); + Assertions.assertEquals(200.00, newEntity.getPrice()); + Assertions.assertNotEquals(oldPrice, newEntity.getPrice()); + } + + @Test + @Order(3) + void deleteTest() { + productService.delete(3L); + + Assertions.assertEquals(2, productService.getAll(0L).size()); + Assertions.assertThrows(NotFoundException.class, () -> productService.get(3L)); + + final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони")); + final ProductEntity newEntity = productService.create(new ProductEntity(null, type, 499.00, 10)); + + Assertions.assertEquals(3, productService.getAll(0L).size()); + Assertions.assertEquals(4L, newEntity.getId()); + } +} \ No newline at end of file diff --git a/demo/src/test/java/com/example/demo/TypeServiceTests.java b/demo/src/test/java/com/example/demo/TypeServiceTests.java index 07f7076..68bced0 100644 --- a/demo/src/test/java/com/example/demo/TypeServiceTests.java +++ b/demo/src/test/java/com/example/demo/TypeServiceTests.java @@ -29,8 +29,8 @@ class TypeServiceTests { typeService.create(new TypeEntity(null, "Пепперони")); typeService.create(new TypeEntity(null, "Мясная")); final TypeEntity last = typeService.create(new TypeEntity(null, "Сырная")); - Assertions.assertEquals(3, typeService.getAll().size()); - Assertions.assertEquals(last, typeService.get(3L)); + Assertions.assertEquals(6, typeService.getAll().size()); + Assertions.assertEquals(last, typeService.get(6L)); } @Test @@ -40,7 +40,7 @@ class TypeServiceTests { final TypeEntity entity = typeService.get(3L); final String oldName = entity.getName(); final TypeEntity newEntity = typeService.update(3L, new TypeEntity(1L, test)); - Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(6, typeService.getAll().size()); Assertions.assertEquals(newEntity, typeService.get(3L)); Assertions.assertEquals(test, newEntity.getName()); Assertions.assertNotEquals(oldName, newEntity.getName()); @@ -50,12 +50,12 @@ class TypeServiceTests { @Order(3) void deleteTest() { typeService.delete(3L); - Assertions.assertEquals(2, typeService.getAll().size()); + Assertions.assertEquals(5, typeService.getAll().size()); final TypeEntity last = typeService.get(2L); Assertions.assertEquals(2L, last.getId()); final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Сырная")); - Assertions.assertEquals(3, typeService.getAll().size()); - Assertions.assertEquals(4L, newEntity.getId()); + Assertions.assertEquals(6, typeService.getAll().size()); + Assertions.assertEquals(7L, newEntity.getId()); } }