From 99bfffc09913ccdfb7945df5ecdb51ef58c006ca 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: Wed, 27 Mar 2024 20:13:25 +0400 Subject: [PATCH] lab2 --- demo/build.gradle | 2 +- .../com/example/demo/DemoApplication.java | 20 ++--- .../demo/orders/api/OrderController.java | 7 +- .../com/example/demo/orders/api/OrderDto.java | 20 ++++- .../demo/orders/service/OrderService.java | 2 + .../example/demo/product/api/ProductDto.java | 4 +- .../demo/products/api/ProductController.java | 70 ----------------- .../example/demo/products/api/ProductDto.java | 57 -------------- .../demo/products/model/ProductEntity.java | 65 ---------------- .../repository/ProductRepository.java | 10 --- .../demo/products/service/ProductService.java | 51 ------------- .../demo/speaker/api/SpeakerController.java | 23 ------ .../configuration/SpeakerConfiguration.java | 27 ------- .../example/demo/speaker/domain/Speaker.java | 5 -- .../demo/speaker/domain/SpeakerDeu.java | 28 ------- .../demo/speaker/domain/SpeakerEng.java | 26 ------- .../demo/speaker/domain/SpeakerRus.java | 21 ------ .../demo/speaker/service/SpeakerService.java | 20 ----- .../com/example/demo/users/api/UserDto.java | 12 +-- .../com/example/demo/OrderServiceTests.java | 6 +- .../example/demo/ProductsServiceTests.java | 75 ------------------- .../com/example/demo/SpeakerSrviceTests.java | 38 ---------- .../com/example/demo/UserServiceTests.java | 12 +-- 23 files changed, 53 insertions(+), 548 deletions(-) delete mode 100644 demo/src/main/java/com/example/demo/products/api/ProductController.java delete mode 100644 demo/src/main/java/com/example/demo/products/api/ProductDto.java delete mode 100644 demo/src/main/java/com/example/demo/products/model/ProductEntity.java delete mode 100644 demo/src/main/java/com/example/demo/products/repository/ProductRepository.java delete mode 100644 demo/src/main/java/com/example/demo/products/service/ProductService.java delete mode 100644 demo/src/main/java/com/example/demo/speaker/api/SpeakerController.java delete mode 100644 demo/src/main/java/com/example/demo/speaker/configuration/SpeakerConfiguration.java delete mode 100644 demo/src/main/java/com/example/demo/speaker/domain/Speaker.java delete mode 100644 demo/src/main/java/com/example/demo/speaker/domain/SpeakerDeu.java delete mode 100644 demo/src/main/java/com/example/demo/speaker/domain/SpeakerEng.java delete mode 100644 demo/src/main/java/com/example/demo/speaker/domain/SpeakerRus.java delete mode 100644 demo/src/main/java/com/example/demo/speaker/service/SpeakerService.java delete mode 100644 demo/src/test/java/com/example/demo/ProductsServiceTests.java delete mode 100644 demo/src/test/java/com/example/demo/SpeakerSrviceTests.java diff --git a/demo/build.gradle b/demo/build.gradle index 92a39ba..14c3827 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -1,6 +1,6 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.2.3' + id 'org.springframework.boot' version '3.2.4' id 'io.spring.dependency-management' version '1.1.4' } diff --git a/demo/src/main/java/com/example/demo/DemoApplication.java b/demo/src/main/java/com/example/demo/DemoApplication.java index 51ed527..73c5fba 100644 --- a/demo/src/main/java/com/example/demo/DemoApplication.java +++ b/demo/src/main/java/com/example/demo/DemoApplication.java @@ -8,8 +8,8 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -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; @@ -37,14 +37,14 @@ 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 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)); - 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)); + log.info("Create default product values"); + productService.create(new ProductEntity(null, "test", type1, 399.00)); + productService.create(new ProductEntity(null, "test", type1, 499.00)); + productService.create(new ProductEntity(null, "test", type2, 450.50)); + productService.create(new ProductEntity(null, "test", type2, 900.50)); + productService.create(new ProductEntity(null, "test", type2, 600.00)); + productService.create(new ProductEntity(null, "test", type3, 750.00)); + productService.create(new ProductEntity(null, "test", type3, 670.00)); } } } 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 index 1a8ec32..1040314 100644 --- a/demo/src/main/java/com/example/demo/orders/api/OrderController.java +++ b/demo/src/main/java/com/example/demo/orders/api/OrderController.java @@ -37,7 +37,12 @@ public class OrderController { } private OrderDto toDto(OrderEntity entity) { - return modelMapper.map(entity, OrderDto.class); + OrderDto dto = new OrderDto(productService); + dto.setId(entity.getId()); + dto.setUserId(entity.getUser().getId()); + dto.setProductId(entity.getProduct().getId()); + dto.setCount(entity.getCount()); + return dto; } private OrderEntity toEntity(OrderDto dto) { 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 index 96a4ac5..2631fdd 100644 --- a/demo/src/main/java/com/example/demo/orders/api/OrderDto.java +++ b/demo/src/main/java/com/example/demo/orders/api/OrderDto.java @@ -1,5 +1,7 @@ package com.example.demo.orders.api; +import com.example.demo.product.model.ProductEntity; +import com.example.demo.product.service.ProductService; import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.Min; @@ -17,6 +19,16 @@ public class OrderDto { @Min(1) private Integer count; + transient private final ProductService productService; + + public OrderDto() { + this.productService = null; + } + + public OrderDto(ProductService productService) { + this.productService = productService; + } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId() { return id; @@ -46,7 +58,13 @@ public class OrderDto { return count; } - public void setPrice(Integer count) { + public void setCount(Integer count) { this.count = count; } + + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public Double getSum() { + ProductEntity product = productService.get(productId); + return product.getPrice() * count; + } } 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 index 74af30a..e3ab454 100644 --- a/demo/src/main/java/com/example/demo/orders/service/OrderService.java +++ b/demo/src/main/java/com/example/demo/orders/service/OrderService.java @@ -4,10 +4,12 @@ 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.orders.model.OrderEntity; import com.example.demo.orders.repository.OrderRepository; +@Service public class OrderService { private final OrderRepository repository; 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 index 69c0168..6e8496e 100644 --- a/demo/src/main/java/com/example/demo/product/api/ProductDto.java +++ b/demo/src/main/java/com/example/demo/product/api/ProductDto.java @@ -3,12 +3,12 @@ package com.example.demo.product.api; import com.fasterxml.jackson.annotation.JsonProperty; import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; public class ProductDto { private Long id; - @NotNull - @Min(1) + @NotBlank private String name; @NotNull @Min(1) 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 deleted file mode 100644 index e9a1cc5..0000000 --- a/demo/src/main/java/com/example/demo/products/api/ProductController.java +++ /dev/null @@ -1,70 +0,0 @@ -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 + "/products") -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/products/api/ProductDto.java b/demo/src/main/java/com/example/demo/products/api/ProductDto.java deleted file mode 100644 index 6eb5e00..0000000 --- a/demo/src/main/java/com/example/demo/products/api/ProductDto.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.example.demo.products.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 Long typeId; - @NotNull - @Min(1) - private Double price; - @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 getTypeId() { - return typeId; - } - - public void setTypeId(Long typeId) { - this.typeId = typeId; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price; - } - - public Integer getCount() { - return count; - } - - public void setCount(Integer count) { - this.count = count; - } - - @JsonProperty(access = JsonProperty.Access.READ_ONLY) - public Double getSum() { - return price * count; - } -} diff --git a/demo/src/main/java/com/example/demo/products/model/ProductEntity.java b/demo/src/main/java/com/example/demo/products/model/ProductEntity.java deleted file mode 100644 index 092c7c4..0000000 --- a/demo/src/main/java/com/example/demo/products/model/ProductEntity.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.example.demo.products.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 TypeEntity type; - private Double price; - private Integer count; - - public ProductEntity() { - super(); - } - - public ProductEntity(Long id, TypeEntity type, Double price, Integer count) { - super(id); - this.type = type; - this.price = price; - this.count = count; - } - - 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; - } - - public Integer getCount() { - return count; - } - - public void setCount(Integer count) { - this.count = count; - } - - @Override - public int hashCode() { - return Objects.hash(id, type, price, count); - } - - @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.getType(), type) - && Objects.equals(other.getPrice(), price) - && 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 deleted file mode 100644 index 2dbfa07..0000000 --- a/demo/src/main/java/com/example/demo/products/repository/ProductRepository.java +++ /dev/null @@ -1,10 +0,0 @@ -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/products/service/ProductService.java b/demo/src/main/java/com/example/demo/products/service/ProductService.java deleted file mode 100644 index 43ec012..0000000 --- a/demo/src/main/java/com/example/demo/products/service/ProductService.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.example.demo.products.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.products.model.ProductEntity; -import com.example.demo.products.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.setType(entity.getType()); - existsEntity.setPrice(entity.getPrice()); - existsEntity.setCount(entity.getCount()); - 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/speaker/api/SpeakerController.java b/demo/src/main/java/com/example/demo/speaker/api/SpeakerController.java deleted file mode 100644 index 293296d..0000000 --- a/demo/src/main/java/com/example/demo/speaker/api/SpeakerController.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.example.demo.speaker.api; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import com.example.demo.speaker.service.SpeakerService; - -@RestController -public class SpeakerController { - private final SpeakerService speakerService; - - public SpeakerController(SpeakerService speakerService) { - this.speakerService = speakerService; - } - - @GetMapping - public String hello( - @RequestParam(value = "name", defaultValue = "Мир") String name, - @RequestParam(value = "lang", defaultValue = "ru") String lang) { - return speakerService.say(name, lang); - } -} diff --git a/demo/src/main/java/com/example/demo/speaker/configuration/SpeakerConfiguration.java b/demo/src/main/java/com/example/demo/speaker/configuration/SpeakerConfiguration.java deleted file mode 100644 index 0ef7cda..0000000 --- a/demo/src/main/java/com/example/demo/speaker/configuration/SpeakerConfiguration.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.example.demo.speaker.configuration; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.example.demo.speaker.domain.Speaker; -import com.example.demo.speaker.domain.SpeakerEng; -import com.example.demo.speaker.domain.SpeakerRus; - -@Configuration -public class SpeakerConfiguration { - private final Logger log = LoggerFactory.getLogger(SpeakerConfiguration.class); - - @Bean(value = "ru", initMethod = "init", destroyMethod = "destroy") - public SpeakerRus createRusSpeaker() { - log.info("Call createRusSpeaker()"); - return new SpeakerRus(); - } - - @Bean(value = "en") - public Speaker createEngSpeaker() { - log.info("Call createEngSpeaker()"); - return new SpeakerEng(); - } -} diff --git a/demo/src/main/java/com/example/demo/speaker/domain/Speaker.java b/demo/src/main/java/com/example/demo/speaker/domain/Speaker.java deleted file mode 100644 index 27a0a29..0000000 --- a/demo/src/main/java/com/example/demo/speaker/domain/Speaker.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.example.demo.speaker.domain; - -public interface Speaker { - String say(); -} diff --git a/demo/src/main/java/com/example/demo/speaker/domain/SpeakerDeu.java b/demo/src/main/java/com/example/demo/speaker/domain/SpeakerDeu.java deleted file mode 100644 index 5909864..0000000 --- a/demo/src/main/java/com/example/demo/speaker/domain/SpeakerDeu.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.example.demo.speaker.domain; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -import jakarta.annotation.PostConstruct; -import jakarta.annotation.PreDestroy; - -@Component(value = "de") -public class SpeakerDeu implements Speaker { - private final Logger log = LoggerFactory.getLogger(SpeakerDeu.class); - - @Override - public String say() { - return "Hallo"; - } - - @PostConstruct - public void init() { - log.info("SpeakerDeu.init()"); - } - - @PreDestroy - public void destroy() { - log.info("SpeakerDeu.destroy()"); - } -} diff --git a/demo/src/main/java/com/example/demo/speaker/domain/SpeakerEng.java b/demo/src/main/java/com/example/demo/speaker/domain/SpeakerEng.java deleted file mode 100644 index a310403..0000000 --- a/demo/src/main/java/com/example/demo/speaker/domain/SpeakerEng.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.example.demo.speaker.domain; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.InitializingBean; - -public class SpeakerEng implements Speaker, InitializingBean, DisposableBean { - private final Logger log = LoggerFactory.getLogger(SpeakerEng.class); - - @Override - public String say() { - return "Hello"; - } - - @Override - public void afterPropertiesSet() { - log.info("SpeakerEng.afterPropertiesSet()"); - } - - @Override - public void destroy() { - log.info("SpeakerEng.destroy()"); - - } -} diff --git a/demo/src/main/java/com/example/demo/speaker/domain/SpeakerRus.java b/demo/src/main/java/com/example/demo/speaker/domain/SpeakerRus.java deleted file mode 100644 index d6ed18e..0000000 --- a/demo/src/main/java/com/example/demo/speaker/domain/SpeakerRus.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.demo.speaker.domain; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SpeakerRus implements Speaker { - private final Logger log = LoggerFactory.getLogger(SpeakerRus.class); - - @Override - public String say() { - return "Привет"; - } - - public void init() { - log.info("SpeakerRus.init()"); - } - - public void destroy() { - log.info("SpeakerRus.destroy()"); - } -} diff --git a/demo/src/main/java/com/example/demo/speaker/service/SpeakerService.java b/demo/src/main/java/com/example/demo/speaker/service/SpeakerService.java deleted file mode 100644 index db81ba4..0000000 --- a/demo/src/main/java/com/example/demo/speaker/service/SpeakerService.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.example.demo.speaker.service; - -import org.springframework.context.ApplicationContext; -import org.springframework.stereotype.Service; - -import com.example.demo.speaker.domain.Speaker; - -@Service -public class SpeakerService { - private final ApplicationContext applicationContext; - - public SpeakerService(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } - - public String say(String name, String lang) { - final Speaker speaker = (Speaker) applicationContext.getBean(lang); - return String.format("%s, %s!", speaker.say(), name); - } -} diff --git a/demo/src/main/java/com/example/demo/users/api/UserDto.java b/demo/src/main/java/com/example/demo/users/api/UserDto.java index d75d2b0..83218dd 100644 --- a/demo/src/main/java/com/example/demo/users/api/UserDto.java +++ b/demo/src/main/java/com/example/demo/users/api/UserDto.java @@ -2,19 +2,15 @@ package com.example.demo.users.api; import com.fasterxml.jackson.annotation.JsonProperty; -import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.NotBlank; public class UserDto { private Long id; - @NotNull - @Min(1) + @NotBlank private String name; - @NotNull - @Min(1) + @NotBlank private String email; - @NotNull - @Min(1) + @NotBlank private String password; @JsonProperty(access = JsonProperty.Access.READ_ONLY) diff --git a/demo/src/test/java/com/example/demo/OrderServiceTests.java b/demo/src/test/java/com/example/demo/OrderServiceTests.java index 112db63..3106034 100644 --- a/demo/src/test/java/com/example/demo/OrderServiceTests.java +++ b/demo/src/test/java/com/example/demo/OrderServiceTests.java @@ -52,7 +52,7 @@ public class OrderServiceTests { final OrderEntity last = orderService.create(new OrderEntity(null, one, test, 7)); Assertions.assertEquals(3, orderService.getAll(0L).size()); - Assertions.assertEquals(last, orderService.get(6L)); + Assertions.assertEquals(last, orderService.get(3L)); } @Test @@ -87,7 +87,7 @@ public class OrderServiceTests { final OrderEntity newOrder = orderService.create(new OrderEntity(null, one, test, 15)); - Assertions.assertEquals(6, orderService.getAll(0L).size()); - Assertions.assertEquals(7L, newOrder.getId()); + Assertions.assertEquals(3, orderService.getAll(0L).size()); + Assertions.assertEquals(4L, newOrder.getId()); } } diff --git a/demo/src/test/java/com/example/demo/ProductsServiceTests.java b/demo/src/test/java/com/example/demo/ProductsServiceTests.java deleted file mode 100644 index a3660f5..0000000 --- a/demo/src/test/java/com/example/demo/ProductsServiceTests.java +++ /dev/null @@ -1,75 +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.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/SpeakerSrviceTests.java b/demo/src/test/java/com/example/demo/SpeakerSrviceTests.java deleted file mode 100644 index d8624d3..0000000 --- a/demo/src/test/java/com/example/demo/SpeakerSrviceTests.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.example.demo; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import com.example.demo.speaker.service.SpeakerService; - -@SpringBootTest -class SpeakerSrviceTests { - @Autowired - SpeakerService speakerService; - - @Test - void testSpeakerRus() { - final String res = speakerService.say("Мир", "ru"); - Assertions.assertEquals("Привет, Мир!", res); - } - - @Test - void testSpeakerEng() { - final String res = speakerService.say("World", "en"); - Assertions.assertEquals("Hello, World!", res); - } - - @Test - void testSpeakerDeu() { - final String res = speakerService.say("Welt", "de"); - Assertions.assertEquals("Hallo, Welt!", res); - } - - @Test - void testSpeakerErrorWired() { - Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> speakerService.say("Мир", "rus")); - } -} diff --git a/demo/src/test/java/com/example/demo/UserServiceTests.java b/demo/src/test/java/com/example/demo/UserServiceTests.java index 3940e4c..16e411c 100644 --- a/demo/src/test/java/com/example/demo/UserServiceTests.java +++ b/demo/src/test/java/com/example/demo/UserServiceTests.java @@ -29,8 +29,8 @@ class UserServiceTests { userService.create(new UserEntity(null, "User 1", "test@test", "qwerty")); userService.create(new UserEntity(null, "User 2", "test1@test1", "12345678")); final UserEntity last = userService.create(new UserEntity(null, "User 3", "test2@test2", "mrgurgnugrn")); - Assertions.assertEquals(3, userService.getAll().size()); - Assertions.assertEquals(last, userService.get(3L)); + Assertions.assertEquals(5, userService.getAll().size()); + Assertions.assertEquals(last, userService.get(5L)); } @Test @@ -42,7 +42,7 @@ class UserServiceTests { final UserEntity entity = userService.get(3L); final String oldName = entity.getName(); final UserEntity newEntity = userService.update(3L, new UserEntity(1L, test, email, password)); - Assertions.assertEquals(3, userService.getAll().size()); + Assertions.assertEquals(5, userService.getAll().size()); Assertions.assertEquals(newEntity, userService.get(3L)); Assertions.assertEquals(test, newEntity.getName()); Assertions.assertNotEquals(oldName, newEntity.getName()); @@ -52,12 +52,12 @@ class UserServiceTests { @Order(3) void deleteTest() { userService.delete(3L); - Assertions.assertEquals(2, userService.getAll().size()); + Assertions.assertEquals(4, userService.getAll().size()); final UserEntity last = userService.get(2L); Assertions.assertEquals(2L, last.getId()); final UserEntity newEntity = userService.create(new UserEntity(null, "TEST", "ortrigr@rbirirrgi", "rgkrgimrgirg")); - Assertions.assertEquals(3, userService.getAll().size()); - Assertions.assertEquals(4L, newEntity.getId()); + Assertions.assertEquals(5, userService.getAll().size()); + Assertions.assertEquals(6L, newEntity.getId()); } }