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

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; package com.example.demo.orders.api;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import java.util.List;
import org.hibernate.Hibernate;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; 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.core.configuration.Constants;
import com.example.demo.orders.model.OrdersEntity; import com.example.demo.orders.model.OrdersEntity;
import com.example.demo.orders.service.OrdersService; 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.model.ProductsEntity;
import com.example.demo.products.service.ProductsService;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@ -28,42 +26,30 @@ import jakarta.validation.Valid;
@RequestMapping(Constants.API_URL + "/user/{user}/order") @RequestMapping(Constants.API_URL + "/user/{user}/order")
public class OrdersController { public class OrdersController {
private final OrdersService ordersService; private final OrdersService ordersService;
private final ProductsService productsService;
private final ModelMapper modelMapper; 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.ordersService = ordersService;
this.productsService = productsService;
this.modelMapper = modelMapper; 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) { private OrdersDto toDto(OrdersEntity entity) {
// String dateEntity = DateConvertString(entity.getDate()); Hibernate.initialize(entity.getOrderProducts());
// entity.setDate(dateEntity); final OrdersDto dto = modelMapper.map(entity, OrdersDto.class);
return 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) { private OrdersEntity toEntity(OrdersDto dto) {
final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class); final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class);
entity.setProduct(productsService.get(dto.getProductId()));
return entity; return entity;
} }
private ProductsDto toProductDto(ProductsEntity entity) {
return modelMapper.map(entity, ProductsDto.class);
}
@GetMapping @GetMapping
public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId, public List<OrdersDto> getAll(@PathVariable(name = "user") Long userId,
@RequestParam(name = "productId", defaultValue = "0") ProductsEntity product) { @RequestParam(name = "productId", defaultValue = "0") ProductsEntity product) {
@ -77,17 +63,24 @@ public class OrdersController {
@PostMapping @PostMapping
public OrdersDto create(@PathVariable(name = "user") Long userId, @RequestBody @Valid OrdersDto dto) { 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}") @PutMapping("/{id}")
public OrdersDto update(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id, public OrdersDto update(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id,
@RequestBody OrdersDto dto) { @RequestBody OrdersDto dto) {
return toDto(ordersService.update(userId, id, toEntity(dto))); return toDto(ordersService.update(userId, id, toEntity(dto), dto.getOrderProducts()));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public OrdersDto delete(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id) { public OrdersDto delete(@PathVariable(name = "user") Long userId, @PathVariable(name = "id") Long id) {
return toDto(ordersService.delete(userId, 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; package com.example.demo.orders.api;
import java.util.List;
import java.util.Date; import java.util.Date;
import com.example.demo.products.model.ProductsEntity;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
public class OrdersDto { public class OrdersDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY) @JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id; private Long id;
@NotNull @JsonProperty(access = JsonProperty.Access.READ_ONLY)
private ProductsEntity product; private Double sum;
@NotNull @JsonProperty(access = JsonProperty.Access.READ_ONLY)
@Min(1)
private Integer count; private Integer count;
@NotNull @NotNull
private Date date; 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() { public Long getId() {
return id; return id;
@ -27,22 +34,6 @@ public class OrdersDto {
this.id = id; 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() { public Integer getCount() {
return count; return count;
} }
@ -59,9 +50,12 @@ public class OrdersDto {
this.date = date; this.date = date;
} }
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Double getSum() { 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.Date;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import java.util.HashSet;
import com.example.demo.core.model.BaseEntity; 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 com.example.demo.users.model.UsersEntity;
import jakarta.persistence.Temporal; import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType; import jakarta.persistence.TemporalType;
import jakarta.persistence.Column; import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn; import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne; import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table; import jakarta.persistence.Table;
@Entity @Entity
@Table(name = "orders") @Table(name = "orders")
public class OrdersEntity extends BaseEntity { public class OrdersEntity extends BaseEntity {
@ManyToOne
@JoinColumn(name = "productId", nullable = false) @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
private ProductsEntity product; @OrderBy("id ASC")
private Set<OrderProductsEntity> orderProducts = new HashSet<>();
@ManyToOne @ManyToOne
@JoinColumn(name = "userId", nullable = false) @JoinColumn(name = "userId", nullable = false)
private UsersEntity user; private UsersEntity user;
@Column(nullable = false)
private Integer count;
private Double sum;
@Temporal(TemporalType.DATE) @Temporal(TemporalType.DATE)
private Date date; private Date date;
public OrdersEntity() { public OrdersEntity() {
} }
public OrdersEntity(ProductsEntity product, Integer count, Date date) { public OrdersEntity(Date date) {
this.product = product;
this.count = count;
this.sum = getSum();
this.date = date; this.date = date;
} }
public ProductsEntity getProduct() {
return product;
}
public void setProduct(ProductsEntity product) {
this.product = product;
}
public UsersEntity getUser() { public UsersEntity getUser() {
return user; return user;
} }
@ -63,17 +53,27 @@ public class OrdersEntity extends BaseEntity {
} }
} }
public Double getSum() {
sum = count * product.getPrice(); public Set<OrderProductsEntity> getOrderProducts() {
return sum; return orderProducts;
} }
public Integer getCount() { public void setOrdersProducts(Set<OrderProductsEntity> set) {
return count; this.orderProducts = set;
} }
public void setCount(Integer count) { public void addProduct(OrderProductsEntity orderProduct) {
this.count = count; 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() { public Date getDate() {
@ -84,9 +84,10 @@ public class OrdersEntity extends BaseEntity {
this.date = date; this.date = date;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, product, count); return Objects.hash(id, date);
} }
@Override @Override
@ -97,9 +98,8 @@ public class OrdersEntity extends BaseEntity {
return false; return false;
final OrdersEntity other = (OrdersEntity) obj; final OrdersEntity other = (OrdersEntity) obj;
return Objects.equals(other.getId(), id) return Objects.equals(other.getId(), id)
&& Objects.equals(other.getProduct(), product)
&& Objects.equals(other.getUser().getId(), user.getId()) && Objects.equals(other.getUser().getId(), user.getId())
&& Objects.equals(other.getCount(), count) && Objects.equals(other.getOrderProducts(), orderProducts)
&& Objects.equals(other.getDate(), date); && Objects.equals(other.getDate(), date);
} }
} }

View File

@ -1,13 +1,18 @@
package com.example.demo.orders.service; package com.example.demo.orders.service;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.example.demo.core.error.NotFoundException; 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.model.OrdersEntity;
import com.example.demo.orders.repository.OrdersRepository; 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.model.UsersEntity;
import com.example.demo.users.service.UsersService; import com.example.demo.users.service.UsersService;
@ -15,10 +20,12 @@ import com.example.demo.users.service.UsersService;
public class OrdersService { public class OrdersService {
private final OrdersRepository repository; private final OrdersRepository repository;
private final UsersService userService; 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.repository = repository;
this.userService = userService; this.userService = userService;
this.productService = productService;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
@ -35,22 +42,37 @@ public class OrdersService {
} }
@Transactional @Transactional
public OrdersEntity create(long userId, OrdersEntity entity) { public OrdersEntity create(long userId, OrdersEntity entity,List<OrderProductDto> orderProductList) {
if (entity == null) { if (entity == null) {
throw new IllegalArgumentException("Entity is null"); throw new IllegalArgumentException("Entity is null");
} }
final UsersEntity existsUser = userService.get(userId); final UsersEntity existsUser = userService.get(userId);
entity.setUser(existsUser); 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); return repository.save(entity);
} }
@Transactional @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); userService.get(userId);
final OrdersEntity existsEntity = get(userId, id); final OrdersEntity existsEntity = get(userId, id);
existsEntity.setProduct(entity.getProduct());
existsEntity.setCount(entity.getCount());
existsEntity.setDate(entity.getDate()); 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); return repository.save(existsEntity);
} }
@ -62,10 +84,22 @@ public class OrdersService {
return existsEntity; return existsEntity;
} }
// @Transactional(readOnly = true) @Transactional(readOnly = true)
// public List<OrdersGrouped> getTotal(long userId) { public List<ProductsEntity> getOrderProducts(long userId, long id) {
// //userService.get(userId); userService.get(userId);
// return repository.getOrdersTotalByUser(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());
}
}