diff --git a/demo/src/main/java/com/example/demo/DemoApplication.java b/demo/src/main/java/com/example/demo/DemoApplication.java index 97917f3..41109e7 100644 --- a/demo/src/main/java/com/example/demo/DemoApplication.java +++ b/demo/src/main/java/com/example/demo/DemoApplication.java @@ -8,10 +8,8 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import com.example.demo.items.model.ItemEntity; -import com.example.demo.items.service.ItemService; -import com.example.demo.stocks.model.StockEntity; -import com.example.demo.stocks.service.StockService; +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; @@ -20,13 +18,11 @@ public class DemoApplication implements CommandLineRunner { private final Logger log = LoggerFactory.getLogger(DemoApplication.class); private final TypeService typeService; - private final ItemService itemService; - private final StockService stockService; + private final ProductService productService; - public DemoApplication(TypeService typeService, ItemService itemService, StockService stockService) { + public DemoApplication(TypeService typeService, ProductService productService) { this.typeService = typeService; - this.itemService = itemService; - this.stockService = stockService; + this.productService = productService; } public static void main(String[] args) { @@ -41,18 +37,14 @@ public class DemoApplication implements CommandLineRunner { final var type2 = typeService.create(new TypeEntity(null, "Пепперони")); final var type3 = typeService.create(new TypeEntity(null, "Сырная")); - final var stock1 = stockService.create(new StockEntity(null, "без акции", 0)); - final var stock2 = stockService.create(new StockEntity(null, "50% до вечера", 50)); - final var stock3 = stockService.create(new StockEntity(null, "День рождения", 25)); - log.info("Create default items values"); - itemService.create(new ItemEntity(null, type1, 399.00, 20, stock1)); - itemService.create(new ItemEntity(null, type1, 499.00, 3, stock2)); - itemService.create(new ItemEntity(null, type2, 450.50, 30, stock3)); - itemService.create(new ItemEntity(null, type2, 900.50, 10, stock1)); - itemService.create(new ItemEntity(null, type2, 600.00, 6, stock2)); - itemService.create(new ItemEntity(null, type3, 750.00, 6, stock3)); - itemService.create(new ItemEntity(null, type3, 670.00, 3, stock1)); + 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)); + productService.create(new ProductEntity(null, type2, 900.50, 10)); + productService.create(new ProductEntity(null, type2, 600.00, 6)); + productService.create(new ProductEntity(null, type3, 750.00, 6)); + productService.create(new ProductEntity(null, type3, 670.00, 3)); } } } diff --git a/demo/src/main/java/com/example/demo/items/api/ItemController.java b/demo/src/main/java/com/example/demo/items/api/ItemController.java deleted file mode 100644 index 6f3182d..0000000 --- a/demo/src/main/java/com/example/demo/items/api/ItemController.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.example.demo.items.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.items.model.ItemEntity; -import com.example.demo.items.service.ItemService; -import com.example.demo.stocks.service.StockService; -import com.example.demo.types.service.TypeService; - -import jakarta.validation.Valid; - -@RestController -@RequestMapping(Constants.API_URL + "/item") -public class ItemController { - private final ItemService itemService; - private final TypeService typeService; - private final StockService stockService; - private final ModelMapper modelMapper; - - public ItemController(ItemService itemService, TypeService typeService, StockService stockService, ModelMapper modelMapper) { - this.itemService = itemService; - this.typeService = typeService; - this.stockService = stockService; - this.modelMapper = modelMapper; - } - - private ItemDto toDto(ItemEntity entity) { - return modelMapper.map(entity, ItemDto.class); - } - - private ItemEntity toEntity(ItemDto dto) { - final ItemEntity entity = modelMapper.map(dto, ItemEntity.class); - entity.setType(typeService.get(dto.getTypeId())); - entity.setStock(stockService.get(dto.getStockId())); - return entity; - } - - @GetMapping - public List getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) { - return itemService.getAll(typeId).stream().map(this::toDto).toList(); - } - - @GetMapping("/{id}") - public ItemDto get(@PathVariable(name = "id") Long id) { - return toDto(itemService.get(id)); - } - - @PostMapping - public ItemDto create(@RequestBody @Valid ItemDto dto) { - return toDto(itemService.create(toEntity(dto))); - } - - @PutMapping("/{id}") - public ItemDto update(@PathVariable(name = "id") Long id, @RequestBody ItemDto dto) { - return toDto(itemService.update(id, toEntity(dto))); - } - - @DeleteMapping("/{id}") - public ItemDto delete(@PathVariable(name = "id") Long id) { - return toDto(itemService.delete(id)); - } -} diff --git a/demo/src/main/java/com/example/demo/items/repository/ItemRepository.java b/demo/src/main/java/com/example/demo/items/repository/ItemRepository.java deleted file mode 100644 index 6488c26..0000000 --- a/demo/src/main/java/com/example/demo/items/repository/ItemRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.example.demo.items.repository; - -import org.springframework.stereotype.Repository; - -import com.example.demo.core.repository.MapRepository; -import com.example.demo.items.model.ItemEntity; - -@Repository -public class ItemRepository extends MapRepository { -} 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 new file mode 100644 index 0000000..834e18a --- /dev/null +++ b/demo/src/main/java/com/example/demo/products/api/ProductController.java @@ -0,0 +1,70 @@ +package com.example.demo.products.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.products.model.ProductEntity; +import com.example.demo.products.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 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/items/api/ItemDto.java b/demo/src/main/java/com/example/demo/products/api/ProductDto.java similarity index 62% rename from demo/src/main/java/com/example/demo/items/api/ItemDto.java rename to demo/src/main/java/com/example/demo/products/api/ProductDto.java index 791aa8a..6eb5e00 100644 --- a/demo/src/main/java/com/example/demo/items/api/ItemDto.java +++ b/demo/src/main/java/com/example/demo/products/api/ProductDto.java @@ -1,14 +1,14 @@ -package com.example.demo.items.api; +package com.example.demo.products.api; -import com.example.demo.stocks.service.StockService; import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; -public class ItemDto { +public class ProductDto { private Long id; @NotNull + @Min(1) private Long typeId; @NotNull @Min(1) @@ -16,9 +16,6 @@ public class ItemDto { @NotNull @Min(1) private Integer count; - @NotNull - private Long stockId; - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId() { @@ -53,19 +50,8 @@ public class ItemDto { this.count = count; } - public Long getStockId() { - return stockId; - } - - public void setStockId(Long stockId) { - this.stockId = stockId; - } - @JsonProperty(access = JsonProperty.Access.READ_ONLY) - public Double getSum(StockService stockService) { - Integer stockValue = stockService.getStockValueById(stockId); - - double multiplier = (stockValue != null && stockValue != 0) ? stockValue / 100.0 : 1.0; - return price * count * multiplier; + public Double getSum() { + return price * count; } } diff --git a/demo/src/main/java/com/example/demo/items/model/ItemEntity.java b/demo/src/main/java/com/example/demo/products/model/ProductEntity.java similarity index 62% rename from demo/src/main/java/com/example/demo/items/model/ItemEntity.java rename to demo/src/main/java/com/example/demo/products/model/ProductEntity.java index 9a364c9..092c7c4 100644 --- a/demo/src/main/java/com/example/demo/items/model/ItemEntity.java +++ b/demo/src/main/java/com/example/demo/products/model/ProductEntity.java @@ -1,27 +1,24 @@ -package com.example.demo.items.model; +package com.example.demo.products.model; import java.util.Objects; import com.example.demo.core.model.BaseEntity; -import com.example.demo.stocks.model.StockEntity; import com.example.demo.types.model.TypeEntity; -public class ItemEntity extends BaseEntity { +public class ProductEntity extends BaseEntity { private TypeEntity type; private Double price; private Integer count; - private StockEntity stock; - public ItemEntity() { + public ProductEntity() { super(); } - public ItemEntity(Long id, TypeEntity type, Double price, Integer count, StockEntity stock) { + public ProductEntity(Long id, TypeEntity type, Double price, Integer count) { super(id); this.type = type; this.price = price; this.count = count; - this.stock = stock; } public TypeEntity getType() { @@ -48,17 +45,9 @@ public class ItemEntity extends BaseEntity { this.count = count; } - public StockEntity getStock() { - return stock; - } - - public void setStock(StockEntity stock) { - this.stock = stock; - } - @Override public int hashCode() { - return Objects.hash(id, type, price, count, stock); + return Objects.hash(id, type, price, count); } @Override @@ -67,11 +56,10 @@ public class ItemEntity extends BaseEntity { return true; if (obj == null || getClass() != obj.getClass()) return false; - final ItemEntity other = (ItemEntity) obj; + final ProductEntity other = (ProductEntity) obj; return Objects.equals(other.getId(), id) && Objects.equals(other.getType(), type) && Objects.equals(other.getPrice(), price) - && Objects.equals(other.getCount(), count) - && Objects.equals(other.getStock(), stock); + && Objects.equals(other.getCount(), count); } } diff --git a/demo/src/main/java/com/example/demo/products/repository/ProductRepository.java b/demo/src/main/java/com/example/demo/products/repository/ProductRepository.java new file mode 100644 index 0000000..2dbfa07 --- /dev/null +++ b/demo/src/main/java/com/example/demo/products/repository/ProductRepository.java @@ -0,0 +1,10 @@ +package com.example.demo.products.repository; + +import org.springframework.stereotype.Repository; + +import com.example.demo.core.repository.MapRepository; +import com.example.demo.products.model.ProductEntity; + +@Repository +public class ProductRepository extends MapRepository { +} diff --git a/demo/src/main/java/com/example/demo/items/service/ItemService.java b/demo/src/main/java/com/example/demo/products/service/ProductService.java similarity index 53% rename from demo/src/main/java/com/example/demo/items/service/ItemService.java rename to demo/src/main/java/com/example/demo/products/service/ProductService.java index a58395d..43ec012 100644 --- a/demo/src/main/java/com/example/demo/items/service/ItemService.java +++ b/demo/src/main/java/com/example/demo/products/service/ProductService.java @@ -1,4 +1,4 @@ -package com.example.demo.items.service; +package com.example.demo.products.service; import java.util.List; import java.util.Objects; @@ -7,46 +7,45 @@ import java.util.Optional; import org.springframework.stereotype.Service; import com.example.demo.core.error.NotFoundException; -import com.example.demo.items.model.ItemEntity; -import com.example.demo.items.repository.ItemRepository; +import com.example.demo.products.model.ProductEntity; +import com.example.demo.products.repository.ProductRepository; @Service -public class ItemService { - private final ItemRepository repository; +public class ProductService { + private final ProductRepository repository; - public ItemService(ItemRepository repository) { + public ProductService(ProductRepository repository) { this.repository = repository; } - public List getAll(Long typeId) { + public List getAll(Long typeId) { if (Objects.equals(typeId, 0L)) { return repository.getAll(); } return repository.getAll().stream() - .filter(item -> item.getType().getId().equals(typeId)) + .filter(product -> product.getType().getId().equals(typeId)) .toList(); } - public ItemEntity get(Long id) { + public ProductEntity get(Long id) { return Optional.ofNullable(repository.get(id)) .orElseThrow(() -> new NotFoundException(id)); } - public ItemEntity create(ItemEntity entity) { + public ProductEntity create(ProductEntity entity) { return repository.create(entity); } - public ItemEntity update(Long id, ItemEntity entity) { - final ItemEntity existsEntity = get(id); + public ProductEntity update(Long id, ProductEntity entity) { + final ProductEntity existsEntity = get(id); existsEntity.setType(entity.getType()); existsEntity.setPrice(entity.getPrice()); existsEntity.setCount(entity.getCount()); - existsEntity.setStock(entity.getStock()); return repository.update(existsEntity); } - public ItemEntity delete(Long id) { - final ItemEntity existsEntity = get(id); + public ProductEntity delete(Long id) { + final ProductEntity existsEntity = get(id); return repository.delete(existsEntity); } } diff --git a/demo/src/main/java/com/example/demo/stocks/api/StockController.java b/demo/src/main/java/com/example/demo/stocks/api/StockController.java deleted file mode 100644 index fca0cf6..0000000 --- a/demo/src/main/java/com/example/demo/stocks/api/StockController.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.example.demo.stocks.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.RestController; - -import com.example.demo.core.configuration.Constants; -import com.example.demo.stocks.model.StockEntity; -import com.example.demo.stocks.service.StockService; -import jakarta.validation.Valid; - -@RestController -@RequestMapping(Constants.API_URL + "/stock") -public class StockController { - private final StockService stockService; - private final ModelMapper modelMapper; - - public StockController(StockService stockService, ModelMapper modelMapper) { - this.stockService = stockService; - this.modelMapper = modelMapper; - } - - private StockDto toDto(StockEntity entity) { - return modelMapper.map(entity, StockDto.class); - } - - private StockEntity toEntity(StockDto dto) { - return modelMapper.map(dto, StockEntity.class); - } - - @GetMapping - public List getAll() { - return stockService.getAll().stream().map(this::toDto).toList(); - } - - @GetMapping("/{id}") - public StockDto get(@PathVariable(name = "id") Long id) { - return toDto(stockService.get(id)); - } - - @PostMapping - public StockDto create(@RequestBody @Valid StockDto dto) { - return toDto(stockService.create(toEntity(dto))); - } - - @PutMapping("/{id}") - public StockDto update(@PathVariable(name = "id") Long id, @RequestBody StockDto dto) { - return toDto(stockService.update(id, toEntity(dto))); - } - - @DeleteMapping("/{id}") - public StockDto delete(@PathVariable(name = "id") Long id) { - return toDto(stockService.delete(id)); - } -} diff --git a/demo/src/main/java/com/example/demo/stocks/api/StockDto.java b/demo/src/main/java/com/example/demo/stocks/api/StockDto.java deleted file mode 100644 index 0a8ed11..0000000 --- a/demo/src/main/java/com/example/demo/stocks/api/StockDto.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.example.demo.stocks.api; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import jakarta.validation.constraints.NotBlank; - -public class StockDto { - private Long id; - @NotBlank - private String name; - @NotBlank - private Integer value; - - @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 Integer getValue() { - return value; - } - - public void setValue(Integer value) { - this.value = value; - } -} diff --git a/demo/src/main/java/com/example/demo/stocks/model/StockEntity.java b/demo/src/main/java/com/example/demo/stocks/model/StockEntity.java deleted file mode 100644 index 11d6988..0000000 --- a/demo/src/main/java/com/example/demo/stocks/model/StockEntity.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.example.demo.stocks.model; - -import java.util.Objects; - -import com.example.demo.core.model.BaseEntity; - -public class StockEntity extends BaseEntity { - private String name; - private Integer value; - - public StockEntity() { - super(); - } - - public StockEntity(Long id, String name, Integer value) { - super(id); - this.name = name; - this.value = value; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Integer getValue() { - return value; - } - - public void setValue(Integer value) { - this.value = value; - } - - @Override - public int hashCode() { - return Objects.hash(id, name, value); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null || getClass() != obj.getClass()) - return false; - final StockEntity other = (StockEntity) obj; - return Objects.equals(other.getId(), id) - && Objects.equals(other.getName(), name) - && Objects.equals(other.getValue(), value); - } -} diff --git a/demo/src/main/java/com/example/demo/stocks/repository/StockRepository.java b/demo/src/main/java/com/example/demo/stocks/repository/StockRepository.java deleted file mode 100644 index 5ff13bb..0000000 --- a/demo/src/main/java/com/example/demo/stocks/repository/StockRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.example.demo.stocks.repository; - -import org.springframework.stereotype.Repository; - -import com.example.demo.core.repository.MapRepository; -import com.example.demo.stocks.model.StockEntity; - -@Repository -public class StockRepository extends MapRepository { -} diff --git a/demo/src/main/java/com/example/demo/stocks/service/StockService.java b/demo/src/main/java/com/example/demo/stocks/service/StockService.java deleted file mode 100644 index a880cd0..0000000 --- a/demo/src/main/java/com/example/demo/stocks/service/StockService.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.example.demo.stocks.service; - -import java.util.List; -import java.util.Optional; - -import org.springframework.stereotype.Service; - -import com.example.demo.core.error.NotFoundException; -import com.example.demo.stocks.model.StockEntity; -import com.example.demo.stocks.repository.StockRepository; - -@Service -public class StockService { - private final StockRepository repository; - - public StockService(StockRepository repository) { - this.repository = repository; - } - - public Integer getStockValueById(Long stockId) { - StockEntity stockEntity = repository.get(stockId); - return (stockEntity != null) ? stockEntity.getValue() : null; - } - - public List getAll() { - return repository.getAll(); - } - - public StockEntity get(Long id) { - return Optional.ofNullable(repository.get(id)) - .orElseThrow(() -> new NotFoundException(id)); - } - - public StockEntity create(StockEntity entity) { - return repository.create(entity); - } - - public StockEntity update(Long id, StockEntity entity) { - final StockEntity existsEntity = get(id); - existsEntity.setName(entity.getName()); - existsEntity.setValue(entity.getValue()); - return repository.update(existsEntity); - } - - public StockEntity delete(Long id) { - final StockEntity existsEntity = get(id); - return repository.delete(existsEntity); - } -} diff --git a/demo/src/test/java/com/example/demo/ItemServiceTests.java b/demo/src/test/java/com/example/demo/ItemServiceTests.java deleted file mode 100644 index 844fdc3..0000000 --- a/demo/src/test/java/com/example/demo/ItemServiceTests.java +++ /dev/null @@ -1,83 +0,0 @@ -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.items.model.ItemEntity; -import com.example.demo.items.service.ItemService; -import com.example.demo.stocks.model.StockEntity; -import com.example.demo.stocks.service.StockService; -import com.example.demo.types.model.TypeEntity; -import com.example.demo.types.service.TypeService; - -@SpringBootTest -@TestMethodOrder(OrderAnnotation.class) -class ItemServiceTests { - @Autowired - private ItemService itemService; - - @Autowired - private TypeService typeService; - - @Autowired - private StockService stockService; - - @Test - void getTest() { - Assertions.assertThrows(NotFoundException.class, () -> itemService.get(0L)); - } - - @Test - @Order(1) - void createTest() { - final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони")); - final StockEntity stock = stockService.create(new StockEntity(null, "Две по цене одной", 50)); - - itemService.create(new ItemEntity(null, type, 399.00, 20, stock)); - itemService.create(new ItemEntity(null, type, 599.00, 3, stock)); - - final ItemEntity last = itemService.create(new ItemEntity(null, type, 1500.00, 6, stock)); - - Assertions.assertEquals(3, itemService.getAll(0L).size()); - Assertions.assertEquals(last, itemService.get(3L)); - } - - @Test - @Order(2) - void updateTest() { - final TypeEntity type = typeService.create(new TypeEntity(null, "Мясная")); - final StockEntity stock = stockService.create(new StockEntity(null, "День рождения", 25)); - - final ItemEntity entity = itemService.get(3L); - final Double oldPrice = entity.getPrice(); - - final ItemEntity newEntity = itemService.update(3L, new ItemEntity(null, type, 200.00, 6, stock)); - - Assertions.assertEquals(3, itemService.getAll(0L).size()); - Assertions.assertEquals(newEntity, itemService.get(3L)); - Assertions.assertEquals(200.00, newEntity.getPrice()); - Assertions.assertNotEquals(oldPrice, newEntity.getPrice()); - } - - @Test - @Order(3) - void deleteTest() { - itemService.delete(3L); - - Assertions.assertEquals(2, itemService.getAll(0L).size()); - Assertions.assertThrows(NotFoundException.class, () -> itemService.get(3L)); - - final TypeEntity type = typeService.create(new TypeEntity(null, "Пепперони")); - final StockEntity stock = stockService.create(new StockEntity(null, "День рождения", 25)); - final ItemEntity newEntity = itemService.create(new ItemEntity(null, type, 499.00, 10, stock)); - - Assertions.assertEquals(3, itemService.getAll(0L).size()); - Assertions.assertEquals(4L, newEntity.getId()); - } -} \ No newline at end of file diff --git a/demo/src/test/java/com/example/demo/ProductServiceTests.java b/demo/src/test/java/com/example/demo/ProductServiceTests.java new file mode 100644 index 0000000..4630554 --- /dev/null +++ b/demo/src/test/java/com/example/demo/ProductServiceTests.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 ProductServiceTests { + @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/StockServiceTests.java b/demo/src/test/java/com/example/demo/StockServiceTests.java deleted file mode 100644 index ea7b72c..0000000 --- a/demo/src/test/java/com/example/demo/StockServiceTests.java +++ /dev/null @@ -1,62 +0,0 @@ -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.stocks.model.StockEntity; -import com.example.demo.stocks.service.StockService; - -@SpringBootTest -@TestMethodOrder(OrderAnnotation.class) -class StockServiceTests { - @Autowired - private StockService stockService; - - @Test - void getTest() { - Assertions.assertThrows(NotFoundException.class, () -> stockService.get(0L)); - } - - @Test - @Order(1) - void createTest() { - stockService.create(new StockEntity(null, "Stock 1", 50)); - stockService.create(new StockEntity(null, "Stock 2", 0)); - final StockEntity last = stockService.create(new StockEntity(null, "Stock 3", 25)); - Assertions.assertEquals(3, stockService.getAll().size()); - Assertions.assertEquals(last, stockService.get(3L)); - } - - @Test - @Order(2) - void updateTest() { - final String test = "TEST"; - final Integer valueTest = 15; - final StockEntity entity = stockService.get(3L); - final String oldName = entity.getName(); - final StockEntity newEntity = stockService.update(3L, new StockEntity(1L, test, valueTest)); - Assertions.assertEquals(3, stockService.getAll().size()); - Assertions.assertEquals(newEntity, stockService.get(3L)); - Assertions.assertEquals(test, newEntity.getName()); - Assertions.assertNotEquals(oldName, newEntity.getName()); - } - - @Test - @Order(3) - void deleteTest() { - stockService.delete(3L); - Assertions.assertEquals(2, stockService.getAll().size()); - final StockEntity last = stockService.get(2L); - Assertions.assertEquals(2L, last.getId()); - - final StockEntity newEntity = stockService.create(new StockEntity(null, "TEST", 15)); - Assertions.assertEquals(3, stockService.getAll().size()); - Assertions.assertEquals(4L, newEntity.getId()); - } -} diff --git a/demo/src/test/java/com/example/demo/TypeServiceTests.java b/demo/src/test/java/com/example/demo/TypeServiceTests.java index 68bced0..07f7076 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(6, typeService.getAll().size()); - Assertions.assertEquals(last, typeService.get(6L)); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(last, typeService.get(3L)); } @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(6, typeService.getAll().size()); + Assertions.assertEquals(3, 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(5, typeService.getAll().size()); + Assertions.assertEquals(2, typeService.getAll().size()); final TypeEntity last = typeService.get(2L); Assertions.assertEquals(2L, last.getId()); final TypeEntity newEntity = typeService.create(new TypeEntity(null, "Сырная")); - Assertions.assertEquals(6, typeService.getAll().size()); - Assertions.assertEquals(7L, newEntity.getId()); + Assertions.assertEquals(3, typeService.getAll().size()); + Assertions.assertEquals(4L, newEntity.getId()); } }