ниче не работает
This commit is contained in:
parent
cfcae77ad5
commit
13226e0fa9
@ -14,6 +14,11 @@ repositories {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
|
implementation 'com.h2database:h2:2.1.210'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||||
|
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
data.mv.db
Normal file
BIN
data.mv.db
Normal file
Binary file not shown.
@ -0,0 +1,45 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
73
src/main/java/com/labwork1/app/student/model/Customer.java
Normal file
73
src/main/java/com/labwork1/app/student/model/Customer.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package com.labwork1.app.student.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Customer {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
@Column
|
||||||
|
private String login;
|
||||||
|
@Column
|
||||||
|
private String password;
|
||||||
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||||
|
private List<Order> orders;
|
||||||
|
|
||||||
|
public Customer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer(String firstName, String password) {
|
||||||
|
this.login = firstName;
|
||||||
|
this.password = password;
|
||||||
|
this.orders = new ArrayList<>();
|
||||||
|
}
|
||||||
|
public List<Order> getOrders() {
|
||||||
|
return orders;
|
||||||
|
}
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLogin() {
|
||||||
|
return login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogin(String login) {
|
||||||
|
this.login = login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Customer{" +
|
||||||
|
"id=" + id +
|
||||||
|
", login='" + login + '\'' +
|
||||||
|
", password='" + password + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
95
src/main/java/com/labwork1/app/student/model/Order.java
Normal file
95
src/main/java/com/labwork1/app/student/model/Order.java
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
package com.labwork1.app.student.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "tab_order")
|
||||||
|
public class Order {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
@Column
|
||||||
|
@Temporal(TemporalType.DATE)
|
||||||
|
private Date dateOfPurchase;
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
@JoinColumn(name="customer_fk")
|
||||||
|
private Customer customer;
|
||||||
|
@ManyToMany(mappedBy = "orders")
|
||||||
|
private List<Ticket> tickets;
|
||||||
|
public Order(Customer customer, Date dateOfPurchase) {
|
||||||
|
this.customer = customer;
|
||||||
|
this.dateOfPurchase = dateOfPurchase;
|
||||||
|
this.tickets = new ArrayList<>();
|
||||||
|
}
|
||||||
|
public void addTicket(Ticket ticket) {
|
||||||
|
if (tickets == null){
|
||||||
|
tickets = new ArrayList<>();
|
||||||
|
}
|
||||||
|
this.tickets.add(ticket);
|
||||||
|
if (ticket.getOrders() == null) {
|
||||||
|
ticket.setOrder(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public Order() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDateOfPurchase() {
|
||||||
|
return dateOfPurchase;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDateOfPurchase(Date dateOfPurchase) {
|
||||||
|
this.dateOfPurchase = dateOfPurchase;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer getCustomer() {
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomer(Customer customer) {
|
||||||
|
this.customer = customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Ticket> getTickets() {
|
||||||
|
return tickets;
|
||||||
|
}
|
||||||
|
|
||||||
|
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() + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
103
src/main/java/com/labwork1/app/student/model/Ticket.java
Normal file
103
src/main/java/com/labwork1/app/student/model/Ticket.java
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
package com.labwork1.app.student.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Ticket {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
@Column
|
||||||
|
private String name;
|
||||||
|
@Column
|
||||||
|
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 List<Order> orders;
|
||||||
|
public Ticket(String name, Double price, Date date) {
|
||||||
|
this.name = name;
|
||||||
|
this.price = price;
|
||||||
|
this.date = date;
|
||||||
|
this.orders = new ArrayList<>();
|
||||||
|
}
|
||||||
|
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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Order> getOrders() {
|
||||||
|
return orders;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrice(double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Ticket={" +
|
||||||
|
"id=" + id +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
", price='" + price + '\'' +
|
||||||
|
", date='" + date.toString() + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
package com.labwork1.app.student.service;
|
||||||
|
|
||||||
|
import com.labwork1.app.student.model.Customer;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CustomerService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer addCustomer(String login, String password) {
|
||||||
|
if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) {
|
||||||
|
throw new IllegalArgumentException("Customer login/password is null or empty");
|
||||||
|
}
|
||||||
|
final Customer customer = new Customer(login, password);
|
||||||
|
em.persist(customer);
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Customer findCustomer(Long id) {
|
||||||
|
final Customer customer = em.find(Customer.class, id);
|
||||||
|
if (customer == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Customer with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Customer> findAllCustomers() {
|
||||||
|
return em.createQuery("select s from Customer s", Customer.class)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer updateCustomer(Long id, String login, String password) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer deleteCustomer(Long id) {
|
||||||
|
final Customer currentCustomer = findCustomer(id);
|
||||||
|
em.remove(currentCustomer);
|
||||||
|
return currentCustomer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllCustomers() {
|
||||||
|
em.createQuery("delete from Customer").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.labwork1.app.student.service;
|
||||||
|
|
||||||
|
import com.labwork1.app.student.model.Customer;
|
||||||
|
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 java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class OrderService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Order addOrder(Customer customer, Date dateOfPurchase) {
|
||||||
|
if (customer==null || dateOfPurchase==null ) {
|
||||||
|
throw new IllegalArgumentException("addOrder empty fields");
|
||||||
|
}
|
||||||
|
final Order order = new Order(customer, dateOfPurchase);
|
||||||
|
em.persist(order);
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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));
|
||||||
|
}
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Order> findAllOrders() {
|
||||||
|
return em.createQuery("select s from Order s", Order.class)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Order updateOrder(Long id, Date dateOfPurchase, List<Ticket> tickets) {
|
||||||
|
if (id==null || 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Order deleteOrder(Long id) {
|
||||||
|
final Order currentCustomer = findOrder(id);
|
||||||
|
em.remove(currentCustomer);
|
||||||
|
return currentCustomer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllOrders() {
|
||||||
|
em.createQuery("delete from Order").executeUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
package com.labwork1.app.student.service;
|
||||||
|
|
||||||
|
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 java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class TicketService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Ticket addTicket(String name, Double price, Date date) {
|
||||||
|
if (name==null|| name.equals("") || price==null || price<=0 || date==null) {
|
||||||
|
throw new IllegalArgumentException("addTicket empty fields");
|
||||||
|
}
|
||||||
|
final Ticket ticket = new Ticket(name, price, date);
|
||||||
|
em.persist(ticket);
|
||||||
|
return ticket;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Ticket findTicket(Long id) {
|
||||||
|
final Ticket ticket = em.find(Ticket.class, id);
|
||||||
|
if (ticket == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Ticket with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return ticket;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<Ticket> findAllTickets() {
|
||||||
|
return em.createQuery("select s from Ticket s", Ticket.class)
|
||||||
|
.getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Ticket updateTicket(Long id, String name, Double price) {
|
||||||
|
if (id==null || name==null || name.equals("") || price == null || price==0) {
|
||||||
|
throw new IllegalArgumentException("updateTicket empty fields");
|
||||||
|
}
|
||||||
|
final Ticket currentStudent = findTicket(id);
|
||||||
|
currentStudent.setName(name);
|
||||||
|
currentStudent.setPrice(price);
|
||||||
|
return em.merge(currentStudent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Ticket deleteTicket(Long id) {
|
||||||
|
final Ticket currentCustomer = findTicket(id);
|
||||||
|
em.remove(currentCustomer);
|
||||||
|
return currentCustomer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllTickets() {
|
||||||
|
em.createQuery("delete from Ticket ").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,11 @@
|
|||||||
|
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
|
||||||
|
spring.datasource.password=password
|
||||||
|
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.h2.console.enabled=true
|
||||||
|
spring.h2.console.settings.trace=false
|
||||||
|
spring.h2.console.settings.web-allow-others=false
|
149
src/test/java/com/labwork1/app/JpaCustomerTests.java
Normal file
149
src/test/java/com/labwork1/app/JpaCustomerTests.java
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
package com.labwork1.app;
|
||||||
|
|
||||||
|
import com.labwork1.app.student.model.Customer;
|
||||||
|
import com.labwork1.app.student.model.Order;
|
||||||
|
import com.labwork1.app.student.model.Ticket;
|
||||||
|
import com.labwork1.app.student.service.CustomerService;
|
||||||
|
import com.labwork1.app.student.service.OrderService;
|
||||||
|
import com.labwork1.app.student.service.TicketService;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
public class JpaCustomerTests {
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(JpaCustomerTests.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CustomerService customerService;
|
||||||
|
@Autowired
|
||||||
|
private TicketService ticketService;
|
||||||
|
@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();
|
||||||
|
|
||||||
|
final Ticket ticket1 = ticketService.addTicket("раз", 300.0, new Date());
|
||||||
|
final Ticket ticket2 = ticketService.addTicket("два", 200.0, new Date());
|
||||||
|
|
||||||
|
final Customer customer1 = customerService.addCustomer("Иван", "Иванов");
|
||||||
|
final Customer customer2 = 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));
|
||||||
|
Assertions.assertEquals(order, order1);
|
||||||
|
|
||||||
|
Assertions.assertEquals(order1.getTickets().size(), 2);
|
||||||
|
|
||||||
|
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());*/
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
6
src/test/resources/application.properties
Normal file
6
src/test/resources/application.properties
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
spring.datasource.url=jdbc:h2:mem:testdb
|
||||||
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=password
|
||||||
|
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||||
|
spring.jpa.hibernate.ddl-auto=create-drop
|
Loading…
Reference in New Issue
Block a user