Некоторые тесты всяких штук

This commit is contained in:
dasha 2023-03-16 15:12:45 +04:00
parent 0a40a6e2d1
commit a7c19cd750
3 changed files with 30 additions and 17 deletions

View File

@ -3,6 +3,7 @@ package com.labwork1.app.student.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.sql.Date; import java.sql.Date;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -27,13 +28,14 @@ public class Order {
public Order() { public Order() {
} }
public Order(Customer customer, Date dateOfPurchase, List<Ticket> tickets) { public Order(Customer customer, Date dateOfPurchase) {
this.customer = customer; this.customer = customer;
this.dateOfPurchase = dateOfPurchase; this.dateOfPurchase = dateOfPurchase;
this.tickets = tickets;
} }
public void addTicket(Ticket ticket) { public void addTicket(Ticket ticket) {
if (tickets == null)
tickets = new ArrayList<>();
tickets.add(ticket); tickets.add(ticket);
if (ticket.getOrders() == null) { if (ticket.getOrders() == null) {
ticket.setOrder(this); ticket.setOrder(this);

View File

@ -18,12 +18,12 @@ public class OrderService {
private EntityManager em; private EntityManager em;
@Transactional @Transactional
public Order addOrder(Customer customer, List<Ticket> tickets) { public Order addOrder(Customer customer) {
if (customer == null || tickets.size() == 0) { if (customer == null) {
throw new IllegalArgumentException("addOrder empty fields"); throw new IllegalArgumentException("addOrder empty fields");
} }
final Order order = new Order(customer, final Order order = new Order(customer,
new Date(System.currentTimeMillis()), tickets); new Date(System.currentTimeMillis()));
em.persist(order); em.persist(order);
return order; return order;
} }
@ -73,6 +73,19 @@ public class OrderService {
return currentOrder; 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 @Transactional
public void deleteAllOrders() { public void deleteAllOrders() {
em.createQuery("delete from Order").executeUpdate(); em.createQuery("delete from Order").executeUpdate();

View File

@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@SpringBootTest @SpringBootTest
@ -36,16 +35,12 @@ public class JpaCustomerTests {
final Ticket ticket1 = ticketService.addTicket("Меню", 300.0, new Timestamp(System.currentTimeMillis())); final Ticket ticket1 = ticketService.addTicket("Меню", 300.0, new Timestamp(System.currentTimeMillis()));
final Ticket ticket2 = ticketService.addTicket("Аватар", 200.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 Customer customer1 = customerService.addCustomer("Родион", "Иванов");
final Order order = orderService final Order order0 = orderService.addOrder(customerService.findCustomer(customer1.getId()));
.addOrder(customerService.findCustomer(customer1.getId()), tickets); final Order order1 = orderService.findOrder(order0.getId());
final Order order1 = orderService.findOrder(order.getId()); Assertions.assertEquals(order0, order1);
Assertions.assertEquals(order, order1);
Assertions.assertEquals(customerService Assertions.assertEquals(customerService
.findCustomer(customer1.getId()).getOrders().size(), 1); .findCustomer(customer1.getId()).getOrders().size(), 1);
@ -56,16 +51,17 @@ public class JpaCustomerTests {
final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов"); final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов");
final Ticket ticket3 = ticketService.addTicket("Чебурашка", 300.0, new Timestamp(System.currentTimeMillis())); final Ticket ticket3 = ticketService.addTicket("Чебурашка", 300.0, new Timestamp(System.currentTimeMillis()));
tickets.clear();
tickets.add(ticket3);
final Order order2 = orderService final Order order2 = orderService
.addOrder(customerService.findCustomer(customer2.getId()), tickets); .addOrder(customerService.findCustomer(customer2.getId()));
orderService.addTicket(order2.getId(), ticket1); orderService.addTicket(order2.getId(), ticket1);
orderService.addTicket(order2.getId(), ticket2); orderService.addTicket(order2.getId(), ticket2);
orderService.deleteTicketInOrder(order2.getId(), ticket2.getId());
Assertions.assertEquals(orderService Assertions.assertEquals(orderService
.findOrder(order2.getId()).getTickets().size(), 3); .findOrder(order2.getId()).getTickets().size(), 2);
customerService.deleteCustomer(customer2.getId()); customerService.deleteCustomer(customer2.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId())); Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(customer2.getId()));
@ -133,5 +129,7 @@ public class JpaCustomerTests {
final List<Ticket> tickets2 = ticketService.findAllTickets(); final List<Ticket> tickets2 = ticketService.findAllTickets();
log.info(tickets2.toString() + "testTicketReadAllEmpty"); log.info(tickets2.toString() + "testTicketReadAllEmpty");
Assertions.assertEquals(tickets2.size(), 0); Assertions.assertEquals(tickets2.size(), 0);
} }
} }