end lab 2

This commit is contained in:
revengel66 2024-05-30 14:30:01 +03:00
parent 0e8ee04e57
commit 018a0c3c4d
56 changed files with 4762 additions and 244 deletions

Binary file not shown.

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"java.dependency.packagePresentation": "hierarchical"
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
package com.example.demo;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
@ -10,6 +10,7 @@ import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.demo.orders.api.OrderProductDto;
import com.example.demo.orders.model.OrdersEntity;
import com.example.demo.orders.service.OrdersService;
import com.example.demo.products.model.ProductsEntity;
@ -43,7 +44,7 @@ public class DemoApplication implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
if (args.length > 0 && Objects.equals("--populate", args[0])) {
//if (args.length > 0 && Objects.equals("--populate", args[0])) {
log.info("Создание пользователей");
final var user1 = userService.create(new UsersEntity("Anton", "1234"));
@ -60,12 +61,12 @@ public class DemoApplication implements CommandLineRunner {
log.info("Создание заказов");
final var orders = List.of(
new OrdersEntity(product1, 3, new Date()),
new OrdersEntity(product2, 2, new Date()),
new OrdersEntity(product3, 1, new Date()),
new OrdersEntity(product1, 4, new Date()));
orders.forEach(order -> ordersService.create(user1.getId(), order));
}
ordersService.create(user1.getId(), new OrdersEntity( new Date()), Arrays.asList(
new OrderProductDto(product1.getId(), 3),
new OrderProductDto(product2.getId(), 1),
new OrderProductDto(product3.getId(), 1)
));
//}
}
}

View File

@ -0,0 +1,83 @@
package com.example.demo.orderproducts.model;
import java.util.Objects;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.orders.model.OrdersEntity;
import com.example.demo.products.model.ProductsEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
@Entity
@Table(name = "orders_products")
public class OrderProductsEntity extends BaseEntity {
@ManyToOne
@JoinColumn(name = "order_id")
private OrdersEntity order;
@ManyToOne
@JoinColumn(name = "product_id")
private ProductsEntity product;
@Column(nullable = false)
private Integer count;
public OrderProductsEntity() {
}
public OrderProductsEntity(OrdersEntity order, ProductsEntity product, Integer count) {
this.order = order;
this.product = product;
this.count = count;
}
public OrdersEntity getOrder() {
return order;
}
public void setOrder(OrdersEntity order) {
this.order = order;
if (!order.getOrderProducts().contains(this)) {
order.getOrderProducts().add(this);
}
}
public ProductsEntity getProduct() {
return product;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
@Override
public int hashCode() {
return Objects.hash(id, order.getId(), product.getId(), count);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
OrderProductsEntity other = (OrderProductsEntity) obj;
return Objects.equals(id, other.id)
&& Objects.equals(order.getId(), other.order.getId())
&& Objects.equals(product.getId(), other.product.getId())
&& count == other.count;
}
}

View File

@ -0,0 +1,52 @@
package com.example.demo.orderproducts.model;
import java.util.Objects;
import java.util.Optional;
public class OrderProductsId {
private Long orderId;
private Long productId;
public OrderProductsId() {
}
public OrderProductsId(Long orderId, Long productId) {
this.orderId = orderId;
this.productId = productId;
}
public Long getOrderId() {
return orderId;
}
public void setOrderId(Long orderId) {
this.orderId = orderId;
}
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
@Override
public int hashCode() {
return Objects.hash(
Optional.ofNullable(orderId).orElse(0L),
Optional.ofNullable(productId).orElse(0L));
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
OrderProductsId other = (OrderProductsId) obj;
return Objects.equals(orderId, other.orderId)
&& Objects.equals(productId, other.productId);
}
}

View File

@ -0,0 +1,32 @@
package com.example.demo.orders.api;
import jakarta.validation.constraints.NotNull;
public class OrderProductDto {
private Long productId;
@NotNull
private Integer count;
public OrderProductDto(Long productId, Integer count) {
this.productId = productId;
this.count = count;
}
public OrderProductDto() {
}
public Long getProductId() {
return productId;
}
public void setProductId(Long id) {
this.productId = id;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}

View File

@ -1,10 +1,8 @@
package com.example.demo.orders.api;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.Hibernate;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
@ -19,8 +17,8 @@ import org.springframework.web.bind.annotation.RestController;
import com.example.demo.core.configuration.Constants;
import com.example.demo.orders.model.OrdersEntity;
import com.example.demo.orders.service.OrdersService;
import com.example.demo.products.api.ProductsDto;
import com.example.demo.products.model.ProductsEntity;
import com.example.demo.products.service.ProductsService;
import jakarta.validation.Valid;
@ -28,42 +26,30 @@ import jakarta.validation.Valid;
@RequestMapping(Constants.API_URL + "/user/{user}/order")
public class OrdersController {
private final OrdersService ordersService;
private final ProductsService productsService;
private final ModelMapper modelMapper;
private SimpleDateFormat formater = new SimpleDateFormat("dd.MM.yyyy");
public OrdersController(OrdersService ordersService, ProductsService productsService, ModelMapper modelMapper) {
public OrdersController(OrdersService ordersService, ModelMapper modelMapper) {
this.ordersService = ordersService;
this.productsService = productsService;
this.modelMapper = modelMapper;
}
public Date StringConvertDate(String date) {
try {
return formater.parse(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public String DateConvertString(Date date) {
return formater.format(date);
}
private OrdersDto toDto(OrdersEntity entity) {
// String dateEntity = DateConvertString(entity.getDate());
// entity.setDate(dateEntity);
return modelMapper.map(entity, OrdersDto.class);
Hibernate.initialize(entity.getOrderProducts());
final OrdersDto dto = modelMapper.map(entity, OrdersDto.class);
dto.setSum(ordersService.getSum(entity.getUser().getId(), entity.getId()));
dto.setCount(ordersService.getFullCount(entity.getUser().getId(), entity.getId()));
return dto;
}
private OrdersEntity toEntity(OrdersDto dto) {
final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class);
entity.setProduct(productsService.get(dto.getProductId()));
return entity;
}
private ProductsDto toProductDto(ProductsEntity entity) {
return modelMapper.map(entity, ProductsDto.class);
}
@GetMapping
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId,
@RequestParam(name = "productId", defaultValue = "0") ProductsEntity product) {
@ -77,17 +63,24 @@ public class OrdersController {
@PostMapping
public OrdersDto create(@PathVariable(name = "user") Long userId, @RequestBody @Valid OrdersDto dto) {
return toDto(ordersService.create(userId, toEntity(dto)));
return toDto(ordersService.create(userId, toEntity(dto), dto.getOrderProducts()));
}
@PutMapping("/{id}")
public OrdersDto update(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id,
@RequestBody OrdersDto dto) {
return toDto(ordersService.update(userId, id, toEntity(dto)));
return toDto(ordersService.update(userId, id, toEntity(dto), dto.getOrderProducts()));
}
@DeleteMapping("/{id}")
public OrdersDto delete(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id) {
return toDto(ordersService.delete(userId, id));
}
@GetMapping("/{id}/product")
public List<ProductsDto> getOrderProducts(
@PathVariable(name = "user") Long userId,
@PathVariable(name = "id") Long id) {
return ordersService.getOrderProducts(userId, id).stream().map(this::toProductDto).toList();
}
}

View File

@ -1,23 +1,30 @@
package com.example.demo.orders.api;
import java.util.List;
import java.util.Date;
import com.example.demo.products.model.ProductsEntity;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
public class OrdersDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@NotNull
private ProductsEntity product;
@NotNull
@Min(1)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Double sum;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Integer count;
@NotNull
private Date date;
private List<OrderProductDto> orderProducts;
public List<OrderProductDto> getOrderProducts() {
return orderProducts;
}
public void setOrderProducts(List<OrderProductDto> orderProducts) {
this.orderProducts = orderProducts;
}
public Long getId() {
return id;
@ -27,22 +34,6 @@ public class OrdersDto {
this.id = id;
}
public ProductsEntity getProduct() {
return product;
}
public void setProduct(ProductsEntity product) {
this.product = product;
}
public Long getProductId() {
return product.getId();
}
public void setProductId(Long productId) {
productId = product.getId();
}
public Integer getCount() {
return count;
}
@ -59,9 +50,12 @@ public class OrdersDto {
this.date = date;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Double getSum() {
return count * product.getPrice();
return sum;
}
public void setSum(Double sum) {
this.sum = sum;
}
}

View File

@ -3,55 +3,45 @@ package com.example.demo.orders.model;
import java.util.Date;
import java.util.Objects;
import java.util.Set;
import java.util.HashSet;
import com.example.demo.core.model.BaseEntity;
import com.example.demo.products.model.ProductsEntity;
import com.example.demo.orderproducts.model.OrderProductsEntity;
import com.example.demo.users.model.UsersEntity;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import jakarta.persistence.Column;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;
@Entity
@Table(name = "orders")
public class OrdersEntity extends BaseEntity {
@ManyToOne
@JoinColumn(name = "productId", nullable = false)
private ProductsEntity product;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy("id ASC")
private Set<OrderProductsEntity> orderProducts = new HashSet<>();
@ManyToOne
@JoinColumn(name = "userId", nullable = false)
private UsersEntity user;
@Column(nullable = false)
private Integer count;
private Double sum;
@Temporal(TemporalType.DATE)
private Date date;
public OrdersEntity() {
}
public OrdersEntity(ProductsEntity product, Integer count, Date date) {
this.product = product;
this.count = count;
this.sum = getSum();
public OrdersEntity(Date date) {
this.date = date;
}
public ProductsEntity getProduct() {
return product;
}
public void setProduct(ProductsEntity product) {
this.product = product;
}
public UsersEntity getUser() {
return user;
}
@ -63,17 +53,27 @@ public class OrdersEntity extends BaseEntity {
}
}
public Double getSum() {
sum = count * product.getPrice();
return sum;
public Set<OrderProductsEntity> getOrderProducts() {
return orderProducts;
}
public Integer getCount() {
return count;
public void setOrdersProducts(Set<OrderProductsEntity> set) {
this.orderProducts = set;
}
public void setCount(Integer count) {
this.count = count;
public void addProduct(OrderProductsEntity orderProduct) {
if (orderProduct.getOrder() != this) {
orderProduct.setOrder(this);
}
orderProducts.add(orderProduct);
}
public void deleteProduct(OrderProductsEntity orderProduct) {
if (orderProduct.getOrder() != this) {
return;
}
orderProducts.remove(orderProduct);
}
public Date getDate() {
@ -84,9 +84,10 @@ public class OrdersEntity extends BaseEntity {
this.date = date;
}
@Override
public int hashCode() {
return Objects.hash(id, product, count);
return Objects.hash(id, date);
}
@Override
@ -97,9 +98,8 @@ public class OrdersEntity extends BaseEntity {
return false;
final OrdersEntity other = (OrdersEntity) obj;
return Objects.equals(other.getId(), id)
&& Objects.equals(other.getProduct(), product)
&& Objects.equals(other.getUser().getId(), user.getId())
&& Objects.equals(other.getCount(), count)
&& Objects.equals(other.getOrderProducts(), orderProducts)
&& Objects.equals(other.getDate(), date);
}
}

View File

@ -1,13 +1,18 @@
package com.example.demo.orders.service;
import java.util.List;
import java.util.ArrayList;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.core.error.NotFoundException;
import com.example.demo.orderproducts.model.OrderProductsEntity;
import com.example.demo.orders.api.OrderProductDto;
import com.example.demo.orders.model.OrdersEntity;
import com.example.demo.orders.repository.OrdersRepository;
import com.example.demo.products.model.ProductsEntity;
import com.example.demo.products.service.ProductsService;
import com.example.demo.users.model.UsersEntity;
import com.example.demo.users.service.UsersService;
@ -15,10 +20,12 @@ import com.example.demo.users.service.UsersService;
public class OrdersService {
private final OrdersRepository repository;
private final UsersService userService;
private final ProductsService productService;
public OrdersService(OrdersRepository repository, UsersService userService) {
public OrdersService(OrdersRepository repository, UsersService userService, ProductsService productService) {
this.repository = repository;
this.userService = userService;
this.productService = productService;
}
@Transactional(readOnly = true)
@ -35,22 +42,37 @@ public class OrdersService {
}
@Transactional
public OrdersEntity create(long userId, OrdersEntity entity) {
public OrdersEntity create(long userId, OrdersEntity entity,List<OrderProductDto> orderProductList) {
if (entity == null) {
throw new IllegalArgumentException("Entity is null");
}
final UsersEntity existsUser = userService.get(userId);
entity.setUser(existsUser);
entity = repository.save(entity);
List<OrderProductsEntity> orderProductListEntities = new ArrayList<OrderProductsEntity>();
for (OrderProductDto element : orderProductList) {
OrderProductsEntity orderProductEntity = new OrderProductsEntity(entity, productService.get(element.getProductId()), element.getCount());
orderProductEntity.setOrder(entity);
orderProductListEntities.add(orderProductEntity);
entity.addProduct(orderProductEntity);
}
return repository.save(entity);
}
@Transactional
public OrdersEntity update(long userId, Long id, OrdersEntity entity) {
public OrdersEntity update(long userId, Long id, OrdersEntity entity, List<OrderProductDto> orderProductList) {
userService.get(userId);
final OrdersEntity existsEntity = get(userId, id);
existsEntity.setProduct(entity.getProduct());
existsEntity.setCount(entity.getCount());
existsEntity.setDate(entity.getDate());
existsEntity.getOrderProducts().clear();
for (OrderProductDto dto : orderProductList) {
OrderProductsEntity orderProductEntity = new OrderProductsEntity(existsEntity, productService.get(dto.getProductId()), dto.getCount());
existsEntity.addProduct(orderProductEntity);
}
return repository.save(existsEntity);
}
@ -62,10 +84,22 @@ public class OrdersService {
return existsEntity;
}
// @Transactional(readOnly = true)
// public List<OrdersGrouped> getTotal(long userId) {
// //userService.get(userId);
// return repository.getOrdersTotalByUser(userId);
// }
@Transactional(readOnly = true)
public List<ProductsEntity> getOrderProducts(long userId, long id) {
userService.get(userId);
return get(userId, id).getOrderProducts().stream().map(orderProduct -> orderProduct.getProduct()).toList();
}
@Transactional(readOnly = true)
public Double getSum(long userId, long id) {
userService.get(userId);
return get(userId, id).getOrderProducts().stream().mapToDouble(orderProduct -> orderProduct.getCount() * orderProduct.getProduct().getPrice()).sum();
}
@Transactional(readOnly = true)
public Integer getFullCount(long userId, long id) {
userService.get(userId);
return get(userId, id).getOrderProducts().stream().mapToInt(OrderProductsEntity::getCount).sum();
}
}

View File

@ -0,0 +1,114 @@
package com.example.demo;
import java.util.Date;
import java.util.Arrays;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.orders.api.OrderProductDto;
import com.example.demo.orders.model.OrdersEntity;
import com.example.demo.orders.service.OrdersService;
import com.example.demo.products.model.ProductsEntity;
import com.example.demo.products.service.ProductsService;
import com.example.demo.types.model.TypesEntity;
import com.example.demo.types.service.TypesService;
import com.example.demo.users.model.UsersEntity;
import com.example.demo.users.service.UsersService;
import jakarta.transaction.Transactional;
@SpringBootTest
class OrdersServiceTests {
@Autowired
private TypesService typeService;
@Autowired
private ProductsService productService;
@Autowired
private UsersService userService;
@Autowired
private OrdersService orderService;
private OrdersEntity order;
private UsersEntity user;
private ProductsEntity product1;
private ProductsEntity product2;
private ProductsEntity product3;
@BeforeEach
void createData() {
removeData();
final var type = typeService.create(new TypesEntity("Телефон"));
product1 = productService.create(new ProductsEntity(type,"Nokia 2000", 399.00));
product2 = productService.create(new ProductsEntity(type,"Nokia 2002", 499.00));
product3 = productService.create(new ProductsEntity(type,"Nokia 2007", 450.50));
user = userService.create(new UsersEntity("Antosha","qwerty123"));
order = orderService.create(user.getId(), new OrdersEntity(new Date()), Arrays.asList(
new OrderProductDto(product1.getId(), 3),
new OrderProductDto(product2.getId(), 1),
new OrderProductDto(product3.getId(), 2)));
orderService.create(user.getId(), new OrdersEntity(new Date()), Arrays.asList(
new OrderProductDto(product1.getId(), 5)));
orderService.create(user.getId(), new OrdersEntity(new Date()), Arrays.asList(
new OrderProductDto(product2.getId(), 1),
new OrderProductDto(product3.getId(), 1)));
}
@AfterEach
void removeData() {
userService.getAll().forEach(item -> userService.delete(item.getId()));
productService.getAll().forEach(item -> productService.delete(item.getId()));
typeService.getAll().forEach(item -> typeService.delete(item.getId()));
}
@Transactional
@Test
void createTest() {
Assertions.assertEquals(3, orderService.getAll(user.getId()).size());
Assertions.assertEquals(order, orderService.get(user.getId(), order.getId()));
}
@Transactional
@Test
void updateTest() {
final OrdersEntity newEntity = orderService.update(user.getId(), order.getId(), new OrdersEntity(new Date()), Arrays.asList(
new OrderProductDto(product1.getId(), 5)));
Assertions.assertEquals(3, orderService.getAll(user.getId()).size());
Assertions.assertEquals(newEntity, orderService.get(user.getId(), order.getId()));
}
@Test
void deleteTest() {
orderService.delete(user.getId(), order.getId());
Assertions.assertEquals(2, orderService.getAll(user.getId()).size());
final OrdersEntity newEntity = orderService.create(user.getId(), new OrdersEntity(new Date()), Arrays.asList(
new OrderProductDto(product1.getId(), 5)));
Assertions.assertEquals(3, orderService.getAll(user.getId()).size());
Assertions.assertNotEquals(order.getId(), newEntity.getId());
}
@Test
void getSumTest() {
Double sum = orderService.getSum(user.getId(), order.getId());
Assertions.assertEquals(product1.getPrice() * 3 + product2.getPrice() + product3.getPrice() * 2, sum);
}
@Test
void getFullCountTest() {
Integer count = orderService.getFullCount(user.getId(), order.getId());
Assertions.assertEquals(6, count);
}
}

View File

@ -0,0 +1,61 @@
package com.example.demo;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.users.model.UsersEntity;
import com.example.demo.users.service.UsersService;
@SpringBootTest
public class UserServiceTests {
@Autowired
private UsersService userService;
private UsersEntity user;
@BeforeEach
void createData() {
removeData();
user = userService.create(new UsersEntity("Anton", "1234"));
userService.create(new UsersEntity("Daria", "4567"));
}
@AfterEach
void removeData() {
userService.getAll().forEach(item -> userService.delete(item.getId()));
}
@Test
void createTest() {
Assertions.assertEquals(2, userService.getAll().size());
Assertions.assertEquals(user, userService.get(user.getId()));
}
@Test
void updateTest() {
final String test = "TEST";
final String oldLogin = user.getLogin();
final UsersEntity newEntity = userService.update(user.getId(), new UsersEntity("Anton123", "1234"));
Assertions.assertEquals(2, userService.getAll().size());
Assertions.assertEquals(newEntity, userService.get(user.getId()));
Assertions.assertNotEquals(test, newEntity.getLogin());
Assertions.assertNotEquals(oldLogin, newEntity.getLogin());
}
@Test
void deleteTest() {
userService.delete(user.getId());
Assertions.assertEquals(1, userService.getAll().size());
final UsersEntity newEntity = userService.create(new UsersEntity("Anton123", "1234"));
Assertions.assertEquals(2, userService.getAll().size());
Assertions.assertNotEquals(user.getId(), newEntity.getId());
}
}

View File

@ -1,141 +0,0 @@
package com.example.demo;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
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.orders.model.OrdersEntity;
import com.example.demo.orders.service.OrdersService;
import com.example.demo.products.model.ProductsEntity;
import com.example.demo.products.service.ProductsService;
import com.example.demo.types.model.TypesEntity;
import com.example.demo.types.service.TypesService;
import com.example.demo.users.model.UsersEntity;
import com.example.demo.users.service.UsersService;
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class UsersOrdersServiceTests {
@Autowired
private UsersService usersService;
@Autowired
private OrdersService ordersService;
@Autowired
private ProductsService productsService;
@Autowired
private TypesService typesService;
private UsersEntity user1;
private UsersEntity user2;
private ProductsEntity product;
private TypesEntity type;
private OrdersEntity order;
private SimpleDateFormat formater = new SimpleDateFormat("dd.MM.yyyy");
public String DateConvertString(Date date) {
return formater.format(date);
}
@BeforeEach
void createData() {
removeData();
type = typesService.create(new TypesEntity("Телефон"));
product = productsService.create(new ProductsEntity(type, "Nokia 2000", 1000.00));
user1 = usersService.create(new UsersEntity("Anton", "1234"));
user2 = usersService.create(new UsersEntity("Daria", "4567"));
final var orders = List.of(
new OrdersEntity(product, 3, new Date()),
new OrdersEntity(product, 2, new Date()),
new OrdersEntity(product, 1, new Date()),
new OrdersEntity(product, 4, new Date()));
orders.forEach(order -> ordersService.create(user1.getId(), order));
final var orders2 = List.of(
new OrdersEntity(product, 1, new Date()),
new OrdersEntity(product, 1, new Date()),
new OrdersEntity(product, 1, new Date()),
new OrdersEntity(product, 1, new Date()),
new OrdersEntity(product, 1, new Date()));
orders2.forEach(order -> ordersService.create(user2.getId(), order));
// order = ordersService.get(user1.getId(), 1);
order = orders.get(0);
}
@AfterEach
void removeData() {
usersService.getAll().forEach(item -> usersService.delete(item.getId()));
productsService.getAll().forEach(item -> productsService.delete(item.getId()));
typesService.getAll().forEach(item -> typesService.delete(item.getId()));
}
@Test
void getOrderTest() {
Assertions.assertEquals(4, ordersService.getAll(user1.getId()).size());
Assertions.assertEquals(5, ordersService.getAll(user2.getId()).size());
}
@Test
void getUserTest() {
Assertions.assertEquals(2, usersService.getAll().size());
}
@Test
void updateOrderTest() {
final Integer newCount = 5;
final OrdersEntity entity = ordersService.get(user1.getId(), order.getId());
final Integer oldCount = entity.getCount();
final OrdersEntity newEntity = ordersService.update(user1.getId(), order.getId(),
new OrdersEntity(product, newCount, new Date()));
Assertions.assertEquals(4, ordersService.getAll(user1.getId()).size());
Assertions.assertEquals(newCount, newEntity.getCount());
Assertions.assertNotEquals(oldCount, newEntity.getCount());
}
@Test
void updateUserTest() {
final String newPassword = "0000";
final UsersEntity entity = usersService.get(user1.getId());
final String login = entity.getLogin();
final String oldPassword = entity.getPassword();
final UsersEntity newEntity = usersService.update(user1.getId(), new UsersEntity(login, newPassword));
Assertions.assertEquals(2, usersService.getAll().size());
Assertions.assertEquals(newEntity, usersService.get(newEntity.getId()));
Assertions.assertEquals(newPassword, newEntity.getPassword());
Assertions.assertNotEquals(oldPassword, newEntity.getPassword());
}
@Test
void deleteOrderTest() {
ordersService.delete(user1.getId(), order.getId());
Assertions.assertEquals(3, ordersService.getAll(user1.getId()).size());
final OrdersEntity newEntity = ordersService.create(user1.getId(), new OrdersEntity(product, 3, new Date()));
Assertions.assertEquals(4, ordersService.getAll(user1.getId()).size());
Assertions.assertNotEquals(order.getId(), newEntity.getId());
}
@Test
void deleteUserTest() {
usersService.delete(user1.getId());
Assertions.assertEquals(1, usersService.getAll().size());
final UsersEntity newEntity = usersService.create(new UsersEntity("Anton",
"1234"));
Assertions.assertEquals(2, usersService.getAll().size());
Assertions.assertNotEquals(user1.getId(), newEntity.getId());
}
}