Некоторые тесты всяких штук
This commit is contained in:
parent
0a40a6e2d1
commit
a7c19cd750
@ -3,6 +3,7 @@ package com.labwork1.app.student.model;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -27,13 +28,14 @@ public class Order {
|
||||
public Order() {
|
||||
}
|
||||
|
||||
public Order(Customer customer, Date dateOfPurchase, List<Ticket> tickets) {
|
||||
public Order(Customer customer, Date dateOfPurchase) {
|
||||
this.customer = customer;
|
||||
this.dateOfPurchase = dateOfPurchase;
|
||||
this.tickets = tickets;
|
||||
}
|
||||
|
||||
public void addTicket(Ticket ticket) {
|
||||
if (tickets == null)
|
||||
tickets = new ArrayList<>();
|
||||
tickets.add(ticket);
|
||||
if (ticket.getOrders() == null) {
|
||||
ticket.setOrder(this);
|
||||
|
@ -18,12 +18,12 @@ public class OrderService {
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Order addOrder(Customer customer, List<Ticket> tickets) {
|
||||
if (customer == null || tickets.size() == 0) {
|
||||
public Order addOrder(Customer customer) {
|
||||
if (customer == null) {
|
||||
throw new IllegalArgumentException("addOrder empty fields");
|
||||
}
|
||||
final Order order = new Order(customer,
|
||||
new Date(System.currentTimeMillis()), tickets);
|
||||
new Date(System.currentTimeMillis()));
|
||||
em.persist(order);
|
||||
return order;
|
||||
}
|
||||
@ -73,6 +73,19 @@ public class OrderService {
|
||||
return currentOrder;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteTicketInOrder(Long id, Long ticket_id) {
|
||||
// final Order currentOrder = findOrder(id);
|
||||
// em.remove(currentOrder);
|
||||
// return currentOrder;
|
||||
final Order currentReader = findOrder(id);
|
||||
final Ticket currentManga = em.find(Ticket.class, ticket_id);
|
||||
currentReader.getTickets().remove(currentManga);
|
||||
currentManga.getOrders().remove(currentReader);
|
||||
em.merge(currentReader);
|
||||
em.merge(currentManga);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllOrders() {
|
||||
em.createQuery("delete from Order").executeUpdate();
|
||||
|
@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
@ -36,16 +35,12 @@ public class JpaCustomerTests {
|
||||
|
||||
final Ticket ticket1 = ticketService.addTicket("Меню", 300.0, new Timestamp(System.currentTimeMillis()));
|
||||
final Ticket ticket2 = ticketService.addTicket("Аватар", 200.0, new Timestamp(System.currentTimeMillis()));
|
||||
List<Ticket> tickets = new ArrayList<>();
|
||||
tickets.add(ticket1);
|
||||
tickets.add(ticket2);
|
||||
|
||||
final Customer customer1 = customerService.addCustomer("Родион", "Иванов");
|
||||
|
||||
final Order order = orderService
|
||||
.addOrder(customerService.findCustomer(customer1.getId()), tickets);
|
||||
final Order order1 = orderService.findOrder(order.getId());
|
||||
Assertions.assertEquals(order, order1);
|
||||
final Order order0 = orderService.addOrder(customerService.findCustomer(customer1.getId()));
|
||||
final Order order1 = orderService.findOrder(order0.getId());
|
||||
Assertions.assertEquals(order0, order1);
|
||||
|
||||
Assertions.assertEquals(customerService
|
||||
.findCustomer(customer1.getId()).getOrders().size(), 1);
|
||||
@ -56,16 +51,17 @@ public class JpaCustomerTests {
|
||||
final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов");
|
||||
|
||||
final Ticket ticket3 = ticketService.addTicket("Чебурашка", 300.0, new Timestamp(System.currentTimeMillis()));
|
||||
tickets.clear();
|
||||
tickets.add(ticket3);
|
||||
|
||||
final Order order2 = orderService
|
||||
.addOrder(customerService.findCustomer(customer2.getId()), tickets);
|
||||
.addOrder(customerService.findCustomer(customer2.getId()));
|
||||
|
||||
orderService.addTicket(order2.getId(), ticket1);
|
||||
orderService.addTicket(order2.getId(), ticket2);
|
||||
|
||||
orderService.deleteTicketInOrder(order2.getId(), ticket2.getId());
|
||||
|
||||
Assertions.assertEquals(orderService
|
||||
.findOrder(order2.getId()).getTickets().size(), 3);
|
||||
.findOrder(order2.getId()).getTickets().size(), 2);
|
||||
|
||||
customerService.deleteCustomer(customer2.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId()));
|
||||
@ -133,5 +129,7 @@ public class JpaCustomerTests {
|
||||
final List<Ticket> tickets2 = ticketService.findAllTickets();
|
||||
log.info(tickets2.toString() + "testTicketReadAllEmpty");
|
||||
Assertions.assertEquals(tickets2.size(), 0);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user