Ну вроде что-то работает

This commit is contained in:
dasha 2023-03-15 23:52:50 +04:00
parent 13226e0fa9
commit ba29a321f0
17 changed files with 242 additions and 574 deletions

Binary file not shown.

View File

@ -2,42 +2,12 @@ package com.labwork1.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}
/*@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/plus")
public String plus(@RequestParam(required = false, defaultValue = "0") double first,
@RequestParam(required = false, defaultValue = "0") double second) {
return Double.toString(first + second);
}
@GetMapping("/minus")
public String minus(@RequestParam(required = false, defaultValue = "0") double first,
@RequestParam(required = false, defaultValue = "0") double second) {
return Double.toString(first - second);
}
@GetMapping("/mult")
public String mult(@RequestParam(required = false, defaultValue = "1") double first,
@RequestParam(required = false, defaultValue = "1") double second) {
return Double.toString(first * second);
}
@GetMapping("/div")
public String div(@RequestParam(required = false, defaultValue = "1") double first,
@RequestParam(required = false, defaultValue = "1") double second) {
if(second == 0)
{
return null;
}
return Double.toString(first/second);
}*/
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}
}

View File

@ -1,44 +0,0 @@
package com.labwork1.app.calc.controller;
import com.labwork1.app.calc.service.CalcService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CalcController {
private final CalcService calcService;
public CalcController(CalcService calcService) {
this.calcService = calcService;
}
@GetMapping("/plus")
public String plus(@RequestParam(required = false, defaultValue = "0") Object first,
@RequestParam(required = false, defaultValue = "0") Object second,
@RequestParam(required = false, defaultValue = "num") String type) {
return calcService.plus((Object) first, (Object) second, type);
}
@GetMapping("/minus")
public String minus(@RequestParam(required = false, defaultValue = "0") Object first,
@RequestParam(required = false, defaultValue = "0") Object second,
@RequestParam(required = false, defaultValue = "num") String type) {
return calcService.minus((Object) first, (Object) second, type);
}
@GetMapping("/mult")
public String mult(@RequestParam(required = false, defaultValue = "1") Object first,
@RequestParam(required = false, defaultValue = "1") Object second,
@RequestParam(required = false, defaultValue = "num") String type) {
return calcService.mult((Object) first, (Object) second, type);
}
@GetMapping("/div")
public String div(@RequestParam(required = false, defaultValue = "1") Object first,
@RequestParam(required = false, defaultValue = "1") Object second,
@RequestParam(required = false, defaultValue = "num") String type) {
if(Integer.parseInt(second.toString()) == 0)
{
return null;
}
return calcService.div((Object) first, (Object) second, type);
}
}

View File

@ -1,46 +0,0 @@
package com.labwork1.app.calc.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component(value = "num")
public class CalcNum implements ICalc<Integer>, InitializingBean, DisposableBean {
private final Logger log = LoggerFactory.getLogger(CalcNum.class);
@Override
public void afterPropertiesSet() {
log.info("CalcNum.afterPropertiesSet()");
}
@Override
public void destroy() {
log.info("CalcNum.destroy()");
}
@Override
public Integer Plus(Integer first, Integer second) {
return first + second;
}
@Override
public Integer Mult(Integer first, Integer second) {
return first * second;
}
@Override
public Integer Minus(Integer first, Integer second) {
return first - second;
}
@Override
public Integer Div(Integer first, Integer second) {
if (second == 0){
return null;
}else{
return first / second;
}
}
}

View File

@ -1,46 +0,0 @@
package com.labwork1.app.calc.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component(value = "str")
public class CalcStr implements ICalc<String>, InitializingBean, DisposableBean {
private final Logger log = LoggerFactory.getLogger(CalcStr.class);
@Override
public void afterPropertiesSet() {
log.info("CalcStr.afterPropertiesSet()");
}
@Override
public void destroy() {
log.info("CalcStr.destroy()");
}
@Override
public String Plus(String first, String second) {
return first.concat(second);
}
@Override
public String Mult(String first, String second) {
String temp = first;
for (int i = 0; i < Integer.parseInt(second) - 1; i++) {
temp = Plus(temp, first);
}
return temp;
}
@Override
public String Minus(String first, String second) {
return first.replaceFirst(second, "");
}
@Override
public String Div(String first, String second) {
return first.replaceAll(second, "");
}
}

View File

@ -1,11 +0,0 @@
package com.labwork1.app.calc.domain;
public interface ICalc<T> {
T Plus(T first, T second);
T Mult(T first, T second);
T Minus(T first, T second);
T Div(T first, T second);
}

View File

@ -1,52 +0,0 @@
package com.labwork1.app.calc.service;
import com.labwork1.app.calc.domain.CalcNum;
import com.labwork1.app.calc.domain.CalcStr;
import com.labwork1.app.calc.domain.ICalc;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class CalcService {
private final ApplicationContext applicationContext;
public CalcService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public String plus(Object first, Object second, String type) {
final ICalc temp = (ICalc) applicationContext.getBean(type);
if (temp instanceof CalcStr) {
return String.format("%s", temp.Plus(first, second));
}else{
return String.format("%s", temp.Plus(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}
public String minus(Object first, Object second, String type) {
final ICalc temp = (ICalc) applicationContext.getBean(type);
if (temp instanceof CalcStr) {
return String.format("%s", temp.Minus(first, second));
}else{
return String.format("%s", temp.Minus(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}
public String mult(Object first, Object second, String type) {
final ICalc temp = (ICalc) applicationContext.getBean(type);
if (temp instanceof CalcStr) {
return String.format("%s", temp.Mult(first, second));
}else{
return String.format("%s", temp.Mult(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}
public String div(Object first, Object second, String type) {
final ICalc temp = (ICalc) applicationContext.getBean(type);
if (temp instanceof CalcStr) {
return String.format("%s", temp.Div(first, second));
}else{
return String.format("%s", temp.Div(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
}
}

View File

@ -1,45 +0,0 @@
package com.labwork1.app.student.controller;
import com.labwork1.app.student.model.Customer;
import com.labwork1.app.student.service.CustomerService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/customer")
public class CustomerController {
private final CustomerService customerService;
public CustomerController(CustomerService studentService) {
this.customerService = studentService;
}
@GetMapping("/{id}")
public Customer getCustomer(@PathVariable Long id) {
return customerService.findCustomer(id);
}
@GetMapping("/")
public List<Customer> gesCustomers() {
return customerService.findAllCustomers();
}
@PostMapping("/")
public Customer createCustomer(@RequestParam("login") String login,
@RequestParam("password") String password) {
return customerService.addCustomer(login, password);
}
@PatchMapping("/{id}")
public Customer updateStudent(@PathVariable Long id,
@RequestParam("login") String login,
@RequestParam("password") String password) {
return customerService.updateCustomer(id, login, password);
}
@DeleteMapping("/{id}")
public Customer deleteCustomer(@PathVariable Long id) {
return customerService.deleteCustomer(id);
}
}

View File

@ -15,20 +15,44 @@ public class Customer {
private String login;
@Column
private String password;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.REMOVE)
private List<Order> orders;
public Customer() {
}
public Customer(String firstName, String password) {
this.login = firstName;
public Customer(String login, String password) {
this.login = login;
this.password = password;
this.orders = new ArrayList<>();
}
public List<Order> getOrders() {
return orders;
public void addOrder(Order order) {
orders.add(order);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return Objects.equals(id, customer.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Customer {" +
"id=" + id +
", login='" + login + '\'' +
", password='" + password + '\'' +
'}';
}
public Long getId() {
return id;
}
@ -49,25 +73,11 @@ public class Customer {
this.password = password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Customer customer = (Customer) o;
return Objects.equals(id, customer.id);
public List<Order> getOrders() {
return orders;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", login='" + login + '\'' +
", password='" + password + '\'' +
'}';
public void setOrders(List<Order> orders) {
this.orders = orders;
}
}

View File

@ -2,8 +2,7 @@ package com.labwork1.app.student.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.sql.Date;
import java.util.List;
import java.util.Objects;
@ -17,30 +16,49 @@ public class Order {
@Temporal(TemporalType.DATE)
private Date dateOfPurchase;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="customer_fk")
@JoinColumn(name = "customer_fk")
private Customer customer;
@ManyToMany(mappedBy = "orders")
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "order_ticket",
joinColumns = @JoinColumn(name = "order_fk"),
inverseJoinColumns = @JoinColumn(name = "ticket_fk"))
private List<Ticket> tickets;
public Order(Customer customer, Date dateOfPurchase) {
public Order() {
}
public Order(Customer customer, Date dateOfPurchase, List<Ticket> tickets) {
this.customer = customer;
this.dateOfPurchase = dateOfPurchase;
this.tickets = new ArrayList<>();
this.tickets = tickets;
}
public void addTicket(Ticket ticket) {
if (tickets == null){
tickets = new ArrayList<>();
}
this.tickets.add(ticket);
tickets.add(ticket);
if (ticket.getOrders() == null) {
ticket.setOrder(this);
}
}
public Order() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order order = (Order) o;
return Objects.equals(id, order.id);
}
public void setId(Long id) {
this.id = id;
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Order {" +
"id=" + id +
", date='" + dateOfPurchase.toString() + '\'' +
", customer='" + customer.toString() + '\'';
}
public Long getId() {
@ -70,26 +88,4 @@ public class Order {
public void setTickets(List<Ticket> tickets) {
this.tickets = tickets;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order customer = (Order) o;
return Objects.equals(id, customer.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "OrderItem={" +
"id=" + id +
", date='" + dateOfPurchase.toString() + '\'' +
", customer='" + customer.toString() + '\'' +
", tickets='" + tickets.toString() + '\'' +
'}';
}
}

View File

@ -2,8 +2,7 @@ package com.labwork1.app.student.model;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
@ -18,37 +17,46 @@ public class Ticket {
private Double price;
@Column
@Temporal(TemporalType.TIMESTAMP)
private Date date;
@ManyToMany
@JoinTable(name = "tickets_orders",
joinColumns = @JoinColumn(name = "ticket_fk"),
inverseJoinColumns = @JoinColumn(name = "order_fk"))
private Timestamp timestamp;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "tickets")
private List<Order> orders;
public Ticket(String name, Double price, Date date) {
public Ticket() {
}
public Ticket(String name, Double price, Timestamp timestamp) {
this.name = name;
this.price = price;
this.date = date;
this.orders = new ArrayList<>();
this.timestamp = timestamp;
}
public void setOrder(Order order) {
if (orders == null){
orders = new ArrayList<>();
}
this.orders.add(order);
if (!order.getTickets().contains(this)) {
order.getTickets().add(this);
}
}
public Ticket() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Ticket ticket = (Ticket) o;
return Objects.equals(id, ticket.id);
}
public List<Order> getOrders() {
return orders;
@Override
public int hashCode() {
return Objects.hash(id);
}
public void setId(Long id) {
this.id = id;
@Override
public String toString() {
return "Ticket {" +
"id=" + id +
", name='" + name + '\'' +
", price='" + price + '\'' +
", timestamp='" + timestamp.toString() + '\'' +
'}';
}
public Long getId() {
@ -63,41 +71,24 @@ public class Ticket {
this.name = name;
}
public double getPrice() {
public Double getPrice() {
return price;
}
public void setPrice(double price) {
public void setPrice(Double price) {
this.price = price;
}
public Date getDate() {
return date;
public Timestamp getTimestamp() {
return timestamp;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Ticket customer = (Ticket) o;
return Objects.equals(id, customer.id);
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
@Override
public int hashCode() {
return Objects.hash(id);
public List<Order> getOrders() {
return orders;
}
@Override
public String toString() {
return "Ticket={" +
"id=" + id +
", name='" + name + '\'' +
", price='" + price + '\'' +
", date='" + date.toString() + '\'' +
'}';
}
}

View File

@ -8,6 +8,7 @@ import org.springframework.util.StringUtils;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import java.util.List;
@Service
@ -36,7 +37,7 @@ public class CustomerService {
@Transactional(readOnly = true)
public List<Customer> findAllCustomers() {
return em.createQuery("select s from Customer s", Customer.class)
return em.createQuery("select c from Customer c", Customer.class)
.getResultList();
}
@ -45,10 +46,10 @@ public class CustomerService {
if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Customer login/password is null or empty");
}
final Customer currentStudent = findCustomer(id);
currentStudent.setLogin(login);
currentStudent.setPassword(password);
return em.merge(currentStudent);
final Customer currentCustomer = findCustomer(id);
currentCustomer.setLogin(login);
currentCustomer.setPassword(password);
return em.merge(currentCustomer);
}
@Transactional

View File

@ -9,7 +9,7 @@ import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.sql.Date;
import java.util.List;
@Service
@ -18,51 +18,63 @@ public class OrderService {
private EntityManager em;
@Transactional
public Order addOrder(Customer customer, Date dateOfPurchase) {
if (customer==null || dateOfPurchase==null ) {
public Order addOrder(Customer customer, List<Ticket> tickets) {
if (customer == null || tickets.size() == 0) {
throw new IllegalArgumentException("addOrder empty fields");
}
final Order order = new Order(customer, dateOfPurchase);
final Order order = new Order(customer,
new Date(System.currentTimeMillis()), tickets);
em.persist(order);
return order;
}
@Transactional
public Order addTicket(Long id, Ticket ticket) {
if (ticket == null) {
throw new IllegalArgumentException("addOrder empty fields");
}
final Order currentOrder = findOrder(id);
currentOrder.addTicket(ticket);
return em.merge(currentOrder);
}
@Transactional(readOnly = true)
public Order findOrder(Long id) {
final Order order = em.find(Order.class, id);
if (order == null) {
throw new EntityNotFoundException(String.format("OrderItem with id [%s] is not found", id));
throw new EntityNotFoundException(String.format("Order with id [%s] is not found", id));
}
return order;
}
@Transactional(readOnly = true)
public List<Order> findAllOrders() {
return em.createQuery("select s from Order s", Order.class)
return em.createQuery("select o from Order o", Order.class)
.getResultList();
}
@Transactional
public Order updateOrder(Long id, Date dateOfPurchase, List<Ticket> tickets) {
if (id==null || dateOfPurchase==null || tickets==null) {
if (dateOfPurchase == null || tickets == null) {
throw new IllegalArgumentException("updateOrder empty fields");
}
final Order currentStudent = findOrder(id);
currentStudent.setDateOfPurchase(dateOfPurchase);
currentStudent.setTickets(tickets);
return em.merge(currentStudent);
if (tickets.size() == 0)
return deleteOrder(id);
final Order currentOrder = findOrder(id);
currentOrder.setDateOfPurchase(dateOfPurchase);
currentOrder.setTickets(tickets);
return em.merge(currentOrder);
}
@Transactional
public Order deleteOrder(Long id) {
final Order currentCustomer = findOrder(id);
em.remove(currentCustomer);
return currentCustomer;
final Order currentOrder = findOrder(id);
em.remove(currentOrder);
return currentOrder;
}
@Transactional
public void deleteAllOrders() {
em.createQuery("delete from Order").executeUpdate();
}
}

View File

@ -1,13 +1,15 @@
package com.labwork1.app.student.service;
import com.labwork1.app.student.model.Order;
import com.labwork1.app.student.model.Ticket;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.sql.Timestamp;
import java.util.List;
@Service
@ -16,8 +18,8 @@ public class TicketService {
private EntityManager em;
@Transactional
public Ticket addTicket(String name, Double price, Date date) {
if (name==null|| name.equals("") || price==null || price<=0 || date==null) {
public Ticket addTicket(String name, Double price, Timestamp date) {
if (!StringUtils.hasText(name) || price <= 0 || date == null) {
throw new IllegalArgumentException("addTicket empty fields");
}
final Ticket ticket = new Ticket(name, price, date);
@ -36,26 +38,26 @@ public class TicketService {
@Transactional(readOnly = true)
public List<Ticket> findAllTickets() {
return em.createQuery("select s from Ticket s", Ticket.class)
return em.createQuery("select t from Ticket t", Ticket.class)
.getResultList();
}
@Transactional
public Ticket updateTicket(Long id, String name, Double price) {
if (id==null || name==null || name.equals("") || price == null || price==0) {
if (!StringUtils.hasText(name) || price <= 0) {
throw new IllegalArgumentException("updateTicket empty fields");
}
final Ticket currentStudent = findTicket(id);
currentStudent.setName(name);
currentStudent.setPrice(price);
return em.merge(currentStudent);
final Ticket currentTicket = findTicket(id);
currentTicket.setName(name);
currentTicket.setPrice(price);
return em.merge(currentTicket);
}
@Transactional
public Ticket deleteTicket(Long id) {
final Ticket currentCustomer = findTicket(id);
em.remove(currentCustomer);
return currentCustomer;
final Ticket currentTicket = findTicket(id);
em.remove(currentTicket);
return currentTicket;
}
@Transactional

View File

@ -1,5 +1,4 @@
spring.main.banner-mode=off
#server.port=8080
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa

View File

@ -1,56 +0,0 @@
package com.labwork1.app;
import com.labwork1.app.calc.service.CalcService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AppApplicationTests {
@Autowired
CalcService calcService;
@Test
void testPlusNum() {
final String res = calcService.plus(10, 10, "num");
Assertions.assertEquals(20, Integer.parseInt(res));
}
@Test
void testMinusNum() {
final String res = calcService.minus(5, 2, "num");
Assertions.assertEquals(3, Integer.parseInt(res));
}
@Test
void testMultNum() {
final String res = calcService.mult(10, 10, "num");
Assertions.assertEquals(100, Integer.parseInt(res));
}
@Test
void testDivNum() {
final String res = calcService.div(20, 10, "num");
Assertions.assertEquals(2, Integer.parseInt(res));
}
@Test
void testPlusStr() {
final String res = calcService.plus("10", "10", "str");
Assertions.assertEquals("1010", res);
}
@Test
void testMinusStr() {
final String res = calcService.minus("5252", "2", "str");
Assertions.assertEquals("552", res);
}
@Test
void testMultStr() {
final String res = calcService.mult("5", "3", "str");
Assertions.assertEquals("555", res);
}
@Test
void testDivStr() {
final String res = calcService.div("5252", "2", "str");
Assertions.assertEquals("55", res);
}
}

View File

@ -14,13 +14,13 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
public class JpaCustomerTests {
private static final Logger log = LoggerFactory.getLogger(JpaCustomerTests.class);
@Autowired
private CustomerService customerService;
@Autowired
@ -28,122 +28,109 @@ public class JpaCustomerTests {
@Autowired
private OrderService orderService;
@Test
void testCustomerCreate() {
customerService.deleteAllCustomers();
final Customer customer = customerService.addCustomer("Иван", "Иванов");
log.info(customer.toString()+"testCustomerCreate");
Assertions.assertNotNull(customer.getId());
}
@Test
void testCustomerRead() {
customerService.deleteAllCustomers();
final Customer customer = customerService.addCustomer("Иван", "Иванов");
log.info(customer.toString()+"testCustomerRead1");
final Customer findCustomer = customerService.findCustomer(customer.getId());
log.info(findCustomer.toString()+"testCustomerRead2");
Assertions.assertEquals(customer, findCustomer);
}
@Test
void testCustomerReadNotFound() {
customerService.deleteAllCustomers();
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(-1L));
log.info("testCustomerReadNotFound");
}
@Test
void testCustomerReadAll() {
customerService.deleteAllCustomers();
customerService.addCustomer("Иван", "Иванов");
customerService.addCustomer("Петр", "Петров");
final List<Customer> customers = customerService.findAllCustomers();
log.info(customers.toString()+"testCustomerReadAll");
Assertions.assertEquals(customers.size(), 2);
}
@Test
void testCustomerReadAllEmpty() {
customerService.deleteAllCustomers();
final List<Customer> customers = customerService.findAllCustomers();
log.info(customers.toString()+"testCustomerReadAllEmpty");
Assertions.assertEquals(customers.size(), 0);
}
@Test
void testTicketCreate() {
ticketService.deleteAllTickets();
final Ticket ticket = ticketService.addTicket("Первый", 100.0, new Date());
log.info(ticket.toString()+"testTicketCreate");
Assertions.assertNotNull(ticket.getId());
}
@Test
void testTicketRead() {
ticketService.deleteAllTickets();
final Ticket ticket = ticketService.addTicket("Первый", 100.0, new Date());
log.info(ticket.toString()+"testTicketRead1");
final Ticket findTicket = ticketService.findTicket(ticket.getId());
log.info(findTicket.toString()+"testTicketRead2");
Assertions.assertEquals(ticket, findTicket);
}
@Test
void testTicketReadNotFound() {
ticketService.deleteAllTickets();
Assertions.assertThrows(EntityNotFoundException.class, () -> ticketService.findTicket(-1L));
log.info("testTicketReadNotFound");
}
@Test
void testTicketReadAll() {
ticketService.deleteAllTickets();
ticketService.addTicket("Первый", 100.0, new Date());
ticketService.addTicket("Второй", 100.0, new Date());
final List<Ticket> tickets = ticketService.findAllTickets();
log.info(tickets.toString()+"testTicketReadAll");
Assertions.assertEquals(tickets.size(), 2);
}
@Test
void testTicketReadAllEmpty() {
ticketService.deleteAllTickets();
final List<Ticket> tickets = ticketService.findAllTickets();
log.info(tickets.toString()+"testTicketReadAllEmpty");
Assertions.assertEquals(tickets.size(), 0);
}
@Test
void testOrder() {
customerService.deleteAllCustomers();
orderService.deleteAllOrders();
ticketService.deleteAllTickets();
orderService.deleteAllOrders();
customerService.deleteAllCustomers();
final Ticket ticket1 = ticketService.addTicket("раз", 300.0, new Date());
final Ticket ticket2 = ticketService.addTicket("два", 200.0, new Date());
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 Customer customer2 = customerService.addCustomer("Петр", "Иванов");
final Customer customer1 = customerService.addCustomer("Родион", "Иванов");
final Order order = orderService.addOrder(customer1, new Date());
/*order.addTicket(ticket1);
order.addTicket(ticket2);
log.info(order.toString());
final OrderItem order1 = orderService.findOrder(order.getId());
Assertions.assertEquals(true, order1.getTickets().contains(ticket1));
final Order order = orderService
.addOrder(customerService.findCustomer(customer1.getId()), tickets);
final Order order1 = orderService.findOrder(order.getId());
Assertions.assertEquals(order, order1);
Assertions.assertEquals(order1.getTickets().size(), 2);
Assertions.assertEquals(customerService
.findCustomer(customer1.getId()).getOrders().size(), 1);
orderService.deleteAllOrders();
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L));
final OrderItem order2 = orderService.addOrder(customer2, new Date());
order2.addTicket(ticket1);
order2.addTicket(ticket2);
log.info(order2.toString());*/
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);
orderService.addTicket(order2.getId(), ticket1);
orderService.addTicket(order2.getId(), ticket2);
Assertions.assertEquals(orderService
.findOrder(order2.getId()).getTickets().size(), 3);
customerService.deleteCustomer(customer2.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order2.getId()));
Assertions.assertEquals(orderService.findAllOrders().size(), 0);
}
/*@Test()
void testCustomer() {
customerService.deleteAllCustomers();
final Customer customer = customerService.addCustomer("Иван", "Иванов");
log.info(customer.toString() + "testCustomerCreate");
Assertions.assertNotNull(customer.getId());
customerService.deleteAllCustomers();
final Customer customer1 = customerService.addCustomer("Иван", "Иванов");
log.info(customer1.toString() + "testCustomerRead1");
final Customer findCustomer = customerService.findCustomer(customer1.getId());
log.info(findCustomer.toString() + "testCustomerRead2");
Assertions.assertEquals(customer1, findCustomer);
customerService.deleteAllCustomers();
Assertions.assertThrows(EntityNotFoundException.class, () -> customerService.findCustomer(-1L));
log.info("testCustomerReadNotFound");
customerService.deleteAllCustomers();
customerService.addCustomer("Иван", "Иванов");
customerService.addCustomer("Петр", "Петров");
final List<Customer> customers1 = customerService.findAllCustomers();
log.info(customers1.toString() + "testCustomerReadAll");
Assertions.assertEquals(customers1.size(), 2);
customerService.deleteAllCustomers();
final List<Customer> customers2 = customerService.findAllCustomers();
log.info(customers2.toString() + "testCustomerReadAllEmpty");
Assertions.assertEquals(customers2.size(), 0);
}
@Test
void testTicket() {
ticketService.deleteAllTickets();
final Ticket ticket1 = ticketService.addTicket("Ледниковый период", 100.0, new Timestamp(System.currentTimeMillis()));
log.info(ticket1.toString() + "testTicketCreate");
Assertions.assertNotNull(ticket1.getId());
ticketService.deleteAllTickets();
final Ticket ticket2 = ticketService.addTicket("Выживший", 100.0, new Timestamp(System.currentTimeMillis()));
log.info(ticket2.toString() + "testTicketRead1");
final Ticket findTicket = ticketService.findTicket(ticket2.getId());
log.info(findTicket.toString() + "testTicketRead2");
Assertions.assertEquals(ticket2, findTicket);
ticketService.deleteAllTickets();
Assertions.assertThrows(EntityNotFoundException.class, () -> ticketService.findTicket(-1L));
log.info("testTicketReadNotFound");
ticketService.deleteAllTickets();
ticketService.addTicket("1+1", 100.0, new Timestamp(System.currentTimeMillis()));
ticketService.addTicket("Титаник", 100.0, new Timestamp(System.currentTimeMillis()));
final List<Ticket> tickets1 = ticketService.findAllTickets();
log.info(tickets1.toString() + "testTicketReadAll");
Assertions.assertEquals(tickets1.size(), 2);
ticketService.deleteAllTickets();
final List<Ticket> tickets2 = ticketService.findAllTickets();
log.info(tickets2.toString() + "testTicketReadAllEmpty");
Assertions.assertEquals(tickets2.size(), 0);
}*/
}