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

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,8 +2,6 @@ package com.labwork1.app;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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; import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication @SpringBootApplication
@ -12,32 +10,4 @@ public class AppApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(AppApplication.class, 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);
}*/
} }

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; private String login;
@Column @Column
private String password; 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; private List<Order> orders;
public Customer() { public Customer() {
} }
public Customer(String firstName, String password) { public Customer(String login, String password) {
this.login = firstName; this.login = login;
this.password = password; this.password = password;
this.orders = new ArrayList<>(); 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() { public Long getId() {
return id; return id;
} }
@ -49,25 +73,11 @@ public class Customer {
this.password = password; this.password = password;
} }
@Override public List<Order> getOrders() {
public boolean equals(Object o) { return orders;
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 void setOrders(List<Order> orders) {
public int hashCode() { this.orders = orders;
return Objects.hash(id);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", login='" + login + '\'' +
", password='" + password + '\'' +
'}';
} }
} }

View File

@ -2,8 +2,7 @@ package com.labwork1.app.student.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList; import java.sql.Date;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -17,30 +16,49 @@ public class Order {
@Temporal(TemporalType.DATE) @Temporal(TemporalType.DATE)
private Date dateOfPurchase; private Date dateOfPurchase;
@ManyToOne(fetch = FetchType.EAGER) @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="customer_fk") @JoinColumn(name = "customer_fk")
private Customer customer; 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; 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.customer = customer;
this.dateOfPurchase = dateOfPurchase; this.dateOfPurchase = dateOfPurchase;
this.tickets = new ArrayList<>(); this.tickets = tickets;
} }
public void addTicket(Ticket ticket) { public void addTicket(Ticket ticket) {
if (tickets == null){ tickets.add(ticket);
tickets = new ArrayList<>();
}
this.tickets.add(ticket);
if (ticket.getOrders() == null) { if (ticket.getOrders() == null) {
ticket.setOrder(this); 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) { @Override
this.id = id; public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Order {" +
"id=" + id +
", date='" + dateOfPurchase.toString() + '\'' +
", customer='" + customer.toString() + '\'';
} }
public Long getId() { public Long getId() {
@ -70,26 +88,4 @@ public class Order {
public void setTickets(List<Ticket> tickets) { public void setTickets(List<Ticket> tickets) {
this.tickets = 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 jakarta.persistence.*;
import java.util.ArrayList; import java.sql.Timestamp;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -18,37 +17,46 @@ public class Ticket {
private Double price; private Double price;
@Column @Column
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
private Date date; private Timestamp timestamp;
@ManyToMany @ManyToMany(fetch = FetchType.EAGER, mappedBy = "tickets")
@JoinTable(name = "tickets_orders",
joinColumns = @JoinColumn(name = "ticket_fk"),
inverseJoinColumns = @JoinColumn(name = "order_fk"))
private List<Order> orders; 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.name = name;
this.price = price; this.price = price;
this.date = date; this.timestamp = timestamp;
this.orders = new ArrayList<>();
} }
public void setOrder(Order order) { public void setOrder(Order order) {
if (orders == null){
orders = new ArrayList<>();
}
this.orders.add(order);
if (!order.getTickets().contains(this)) { if (!order.getTickets().contains(this)) {
order.getTickets().add(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() { @Override
return orders; public int hashCode() {
return Objects.hash(id);
} }
public void setId(Long id) { @Override
this.id = id; public String toString() {
return "Ticket {" +
"id=" + id +
", name='" + name + '\'' +
", price='" + price + '\'' +
", timestamp='" + timestamp.toString() + '\'' +
'}';
} }
public Long getId() { public Long getId() {
@ -63,41 +71,24 @@ public class Ticket {
this.name = name; this.name = name;
} }
public double getPrice() { public Double getPrice() {
return price; return price;
} }
public void setPrice(double price) { public void setPrice(Double price) {
this.price = price; this.price = price;
} }
public Date getDate() { public Timestamp getTimestamp() {
return date; return timestamp;
} }
public void setDate(Date date) { public void setTimestamp(Timestamp timestamp) {
this.date = date; this.timestamp = timestamp;
}
@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 List<Order> getOrders() {
public int hashCode() { return orders;
return Objects.hash(id);
} }
@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.EntityManager;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext; import jakarta.persistence.PersistenceContext;
import java.util.List; import java.util.List;
@Service @Service
@ -36,7 +37,7 @@ public class CustomerService {
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Customer> findAllCustomers() { 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(); .getResultList();
} }
@ -45,10 +46,10 @@ public class CustomerService {
if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) { if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) {
throw new IllegalArgumentException("Customer login/password is null or empty"); throw new IllegalArgumentException("Customer login/password is null or empty");
} }
final Customer currentStudent = findCustomer(id); final Customer currentCustomer = findCustomer(id);
currentStudent.setLogin(login); currentCustomer.setLogin(login);
currentStudent.setPassword(password); currentCustomer.setPassword(password);
return em.merge(currentStudent); return em.merge(currentCustomer);
} }
@Transactional @Transactional

View File

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

View File

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

View File

@ -1,5 +1,4 @@
spring.main.banner-mode=off spring.main.banner-mode=off
#server.port=8080
spring.datasource.url=jdbc:h2:file:./data spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa 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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date; import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@SpringBootTest @SpringBootTest
public class JpaCustomerTests { public class JpaCustomerTests {
private static final Logger log = LoggerFactory.getLogger(JpaCustomerTests.class); private static final Logger log = LoggerFactory.getLogger(JpaCustomerTests.class);
@Autowired @Autowired
private CustomerService customerService; private CustomerService customerService;
@Autowired @Autowired
@ -28,122 +28,109 @@ public class JpaCustomerTests {
@Autowired @Autowired
private OrderService orderService; 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 @Test
void testOrder() { void testOrder() {
customerService.deleteAllCustomers();
orderService.deleteAllOrders();
ticketService.deleteAllTickets(); ticketService.deleteAllTickets();
orderService.deleteAllOrders();
customerService.deleteAllCustomers();
final Ticket ticket1 = ticketService.addTicket("раз", 300.0, new Date()); final Ticket ticket1 = ticketService.addTicket("Меню", 300.0, new Timestamp(System.currentTimeMillis()));
final Ticket ticket2 = ticketService.addTicket("два", 200.0, new Date()); 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 Customer customer2 = customerService.addCustomer("Петр", "Иванов");
final Order order = orderService.addOrder(customer1, new Date()); final Order order = orderService
/*order.addTicket(ticket1); .addOrder(customerService.findCustomer(customer1.getId()), tickets);
order.addTicket(ticket2); final Order order1 = orderService.findOrder(order.getId());
log.info(order.toString());
final OrderItem order1 = orderService.findOrder(order.getId());
Assertions.assertEquals(true, order1.getTickets().contains(ticket1));
Assertions.assertEquals(order, order1); Assertions.assertEquals(order, order1);
Assertions.assertEquals(order1.getTickets().size(), 2); Assertions.assertEquals(customerService
.findCustomer(customer1.getId()).getOrders().size(), 1);
orderService.deleteAllOrders(); orderService.deleteAllOrders();
Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L)); Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L));
final OrderItem order2 = orderService.addOrder(customer2, new Date()); final Customer customer2 = customerService.addCustomer("Иннокентий", "Иванов");
order2.addTicket(ticket1);
order2.addTicket(ticket2);
log.info(order2.toString());*/
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);
}*/
} }