Готовая 3 лаба
This commit is contained in:
parent
108665ee17
commit
39680b73cf
@ -14,6 +14,9 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'com.h2database:h2:2.1.210'
|
||||
implementation 'org.springframework.data:spring-data-jpa:3.0.3'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
113
src/main/java/ru/ulstu/is/myapp/models/Customer.java
Normal file
113
src/main/java/ru/ulstu/is/myapp/models/Customer.java
Normal file
@ -0,0 +1,113 @@
|
||||
package ru.ulstu.is.myapp.models;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "table_customer")
|
||||
public class Customer {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column()
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String address;
|
||||
private String email;
|
||||
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
|
||||
private List<Purchase> purchases = new ArrayList<>();
|
||||
|
||||
public Customer() {
|
||||
}
|
||||
|
||||
|
||||
public Customer(String firstName, String lastName, String address, String email) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.address = address;
|
||||
this.email = email;
|
||||
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
public String setAddress(String address) {
|
||||
return address;
|
||||
}
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
public String setEmail(String email) {
|
||||
return email;
|
||||
}
|
||||
public List<Purchase> getPurchases() {
|
||||
return purchases;
|
||||
}
|
||||
|
||||
public void setPurchases(List<Purchase> purchases) {
|
||||
this.purchases = purchases;
|
||||
}
|
||||
public void addNewPurchase(Purchase purchase) {
|
||||
purchases.add(purchase);
|
||||
purchase.setCustomer(this);
|
||||
}
|
||||
|
||||
public void deletePurchase(Purchase purchase) {
|
||||
purchases.remove(purchase);
|
||||
purchase.deleteCompany();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@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 int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer{" +
|
||||
"id=" + id +
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
", address='" + address + '\'' +
|
||||
", email='" + email + '\'' +
|
||||
", purchases=" + purchases +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
99
src/main/java/ru/ulstu/is/myapp/models/Product.java
Normal file
99
src/main/java/ru/ulstu/is/myapp/models/Product.java
Normal file
@ -0,0 +1,99 @@
|
||||
package ru.ulstu.is.myapp.models;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Product {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column()
|
||||
private String name;
|
||||
private Double price;
|
||||
private String category;
|
||||
|
||||
@ManyToMany(mappedBy = "products", cascade = {CascadeType.REMOVE}, fetch = FetchType.EAGER)
|
||||
private List<Purchase> purchases = new ArrayList<>();
|
||||
|
||||
public Product(){
|
||||
|
||||
}
|
||||
public Product(String name, Double price, String category){
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
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 String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Product product = (Product) o;
|
||||
return Objects.equals(id, product.id);
|
||||
}
|
||||
public List<Purchase> getPurchases() {
|
||||
return purchases;
|
||||
}
|
||||
|
||||
public void setPurchases(List<Purchase> employees) {
|
||||
this.purchases = employees;
|
||||
}
|
||||
|
||||
public void addNewPurchase(Purchase purchase) {
|
||||
purchases.add(purchase);
|
||||
|
||||
}
|
||||
public void deletePurchase(Purchase purchase) {
|
||||
purchases.remove(purchase);
|
||||
if (purchase.getProducts().contains(this)) {
|
||||
purchase.removeProducts(this);
|
||||
}
|
||||
}
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Product{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", price='" + price + '\'' +
|
||||
", category='" + category + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
101
src/main/java/ru/ulstu/is/myapp/models/Purchase.java
Normal file
101
src/main/java/ru/ulstu/is/myapp/models/Purchase.java
Normal file
@ -0,0 +1,101 @@
|
||||
package ru.ulstu.is.myapp.models;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@Entity
|
||||
public class Purchase {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date purchase_date;
|
||||
private double price;
|
||||
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE)
|
||||
private Customer customer;
|
||||
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||
private Set<Product> products = new HashSet<>();
|
||||
public Purchase(){
|
||||
|
||||
}
|
||||
public Purchase(Date purchase_date, Double price){
|
||||
|
||||
this.purchase_date = purchase_date;
|
||||
this.price = price;
|
||||
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return purchase_date;
|
||||
}
|
||||
|
||||
public void setDate(Date purchase_date) {
|
||||
this.purchase_date = purchase_date;
|
||||
}
|
||||
//
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Customer getCustomer() {
|
||||
return customer;
|
||||
}
|
||||
public List<Product> getProducts() {
|
||||
return products.stream().toList();
|
||||
}
|
||||
public void addNewProduct(Product p) {
|
||||
products.add(p);
|
||||
if (!p.getPurchases().contains(this)) {
|
||||
p.addNewPurchase(this);
|
||||
}
|
||||
|
||||
}
|
||||
public void removeProducts(Product product) {
|
||||
products.remove(product);
|
||||
if (product.getPurchases().contains(this)) {
|
||||
product.deletePurchase(this);
|
||||
}
|
||||
}
|
||||
public void setCustomer(Customer customer) {
|
||||
this.customer = customer;
|
||||
|
||||
}
|
||||
public void deleteCompany() {
|
||||
if (customer.getPurchases().contains(this)) {
|
||||
customer.deletePurchase(this);
|
||||
}
|
||||
this.customer = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Purchase p = (Purchase) o;
|
||||
return Objects.equals(id, p.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Purchase{" +
|
||||
"Id=" + id +
|
||||
", purchase_date='" + purchase_date + '\'' +
|
||||
", price=" + price +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
83
src/main/java/ru/ulstu/is/myapp/service/CustomerService.java
Normal file
83
src/main/java/ru/ulstu/is/myapp/service/CustomerService.java
Normal file
@ -0,0 +1,83 @@
|
||||
package ru.ulstu.is.myapp.service;
|
||||
|
||||
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 ru.ulstu.is.myapp.models.Customer;
|
||||
import ru.ulstu.is.myapp.models.Purchase;
|
||||
|
||||
import java.util.List;
|
||||
@Service
|
||||
public class CustomerService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Customer addCustomer(String firstName, String lastName, String address, String email) {
|
||||
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) || !StringUtils.hasText(address) || !StringUtils.hasText(email) ) {
|
||||
throw new IllegalArgumentException("Пустое поле!");
|
||||
}
|
||||
final Customer customer = new Customer(firstName, lastName, address, email);
|
||||
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("Company with id [%s] is not found", id));
|
||||
}
|
||||
return customer;
|
||||
}
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Customer> findAllCustomers() {
|
||||
return em.createQuery("select c from Customer c", Customer.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Customer updateCustomer(Long id, String firstName, String lastName, String address, String email) {
|
||||
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) || !StringUtils.hasText(address) || !StringUtils.hasText(email)) {
|
||||
throw new IllegalArgumentException("Пустое поле");
|
||||
}
|
||||
final Customer currentCustomer = findCustomer(id);
|
||||
currentCustomer.setFirstName(firstName);
|
||||
currentCustomer.setLastName(lastName);
|
||||
currentCustomer.setEmail(email);
|
||||
currentCustomer.setAddress(address);
|
||||
|
||||
return em.merge(currentCustomer);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Customer deleteCustomer(Long id) {
|
||||
final Customer currentCustomer = findCustomer(id);
|
||||
em.remove(currentCustomer);
|
||||
return currentCustomer;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllCustomer() {
|
||||
em.createQuery("delete from Customer").executeUpdate();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addNewPurchase(Long id, Purchase purchase) {
|
||||
Customer currentCustomer = findCustomer(id);
|
||||
currentCustomer.addNewPurchase(purchase);
|
||||
em.merge(currentCustomer);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deletePurchase(Long id, Purchase purchase) {
|
||||
Customer currentCustomer = findCustomer(id);
|
||||
currentCustomer.deletePurchase(purchase);
|
||||
em.merge(purchase);
|
||||
}
|
||||
}
|
67
src/main/java/ru/ulstu/is/myapp/service/ProductService.java
Normal file
67
src/main/java/ru/ulstu/is/myapp/service/ProductService.java
Normal file
@ -0,0 +1,67 @@
|
||||
package ru.ulstu.is.myapp.service;
|
||||
|
||||
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 ru.ulstu.is.myapp.models.Product;
|
||||
|
||||
import java.util.List;
|
||||
@Service
|
||||
public class ProductService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Product addProduct(String name, Double price, String category) {
|
||||
if (!StringUtils.hasText(name)||!StringUtils.hasText(price.toString())||!StringUtils.hasText(category)) {
|
||||
throw new IllegalArgumentException("Employee's data is null or empty");
|
||||
}
|
||||
final Product product = new Product(name, price, category);
|
||||
em.persist(product);
|
||||
return product;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Product findProduct(Long id) {
|
||||
final Product product = em.find(Product.class, id);
|
||||
if (product == null) {
|
||||
throw new EntityNotFoundException(String.format("Product with id [%s] is not found", id));
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Product> findAllProducts() {
|
||||
return em.createQuery("select p from Product p", Product.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Product updateProduct(Long id, String name, Double price, String category) {
|
||||
if (!StringUtils.hasText(name)||!StringUtils.hasText(price.toString())||!StringUtils.hasText(category)) {
|
||||
throw new IllegalArgumentException("Пустое поле");
|
||||
}
|
||||
final Product currentProduct = findProduct(id);
|
||||
currentProduct.setName(name);
|
||||
currentProduct.setPrice(price);
|
||||
currentProduct.setCategory(category);
|
||||
|
||||
|
||||
return em.merge(currentProduct);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Product deleteProduct(Long id) {
|
||||
final Product p = findProduct(id);
|
||||
em.remove(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllProducts() {
|
||||
em.createQuery("delete from Product").executeUpdate();
|
||||
}
|
||||
}
|
111
src/main/java/ru/ulstu/is/myapp/service/PurchaseService.java
Normal file
111
src/main/java/ru/ulstu/is/myapp/service/PurchaseService.java
Normal file
@ -0,0 +1,111 @@
|
||||
package ru.ulstu.is.myapp.service;
|
||||
|
||||
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 ru.ulstu.is.myapp.models.Customer;
|
||||
import ru.ulstu.is.myapp.models.Product;
|
||||
import ru.ulstu.is.myapp.models.Purchase;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class PurchaseService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Purchase addPurchase(Date purchase_date, Double price) {
|
||||
if (!StringUtils.hasText(purchase_date.toString()) ||!StringUtils.hasText(price.toString())) {
|
||||
throw new IllegalArgumentException("Пустое поле");
|
||||
}
|
||||
final Purchase purchase = new Purchase(purchase_date, price);
|
||||
em.persist(purchase);
|
||||
return purchase;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Purchase findPurchase (Long id) {
|
||||
final Purchase purchase = em.find(Purchase.class, id);
|
||||
if (purchase == null) {
|
||||
throw new EntityNotFoundException(String.format("Purchase with id [%s] is not found", id));
|
||||
}
|
||||
return purchase ;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Purchase> findAllPurchases() {
|
||||
return em.createQuery("select p from Purchase p", Purchase.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Purchase updatePurchase(Long id, Date purchase_date, Double price) {
|
||||
if (!StringUtils.hasText(purchase_date.toString()) ||!StringUtils.hasText(price.toString())) {
|
||||
throw new IllegalArgumentException("Пустое поле");
|
||||
}
|
||||
final Purchase currentPurchase = findPurchase(id);
|
||||
currentPurchase.setDate(purchase_date);
|
||||
currentPurchase.setPrice(price);
|
||||
|
||||
return em.merge(currentPurchase);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Purchase deletePurchase(Long id) {
|
||||
final Purchase purchase = findPurchase(id);
|
||||
em.remove(purchase);
|
||||
return purchase;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllPurchases() {
|
||||
em.createQuery("delete from Purchase").executeUpdate();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addCustomer(Long id, Customer c) {
|
||||
final Purchase purchase = findPurchase(id);
|
||||
purchase.setCustomer(c);
|
||||
em.merge(purchase);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteCustomer(Long id) {
|
||||
final Purchase purchase = findPurchase(id);
|
||||
purchase.deleteCompany();
|
||||
em.merge(purchase);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addProduct(Long id, Product p) {
|
||||
Purchase pur = findPurchase(id);
|
||||
pur.addNewProduct(p);
|
||||
em.merge(pur);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteProduct(Long id, Product p) {
|
||||
Purchase pur = findPurchase(id);
|
||||
pur.removeProducts(p);
|
||||
em.merge(pur);
|
||||
em.flush();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Purchase> getPurchasesByProducts(Product p) {
|
||||
List<Purchase> purchases = em.createQuery("select p from Purchase pur INNER JOIN pur.products p WHERE p.id=:product",
|
||||
Purchase.class)
|
||||
.setParameter("product", p.getId())
|
||||
.getResultList();
|
||||
|
||||
for (Purchase pur : purchases) {
|
||||
System.out.println(pur);
|
||||
}
|
||||
return purchases;
|
||||
}
|
||||
}
|
118
src/test/java/ru/ulstu/is/myapp/CustomerServiceTests.java
Normal file
118
src/test/java/ru/ulstu/is/myapp/CustomerServiceTests.java
Normal file
@ -0,0 +1,118 @@
|
||||
package ru.ulstu.is.myapp;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
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;
|
||||
import ru.ulstu.is.myapp.models.Customer;
|
||||
import ru.ulstu.is.myapp.models.Purchase;
|
||||
import ru.ulstu.is.myapp.service.CustomerService;
|
||||
import ru.ulstu.is.myapp.service.PurchaseService;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@SpringBootTest
|
||||
public class CustomerServiceTests {
|
||||
@Autowired
|
||||
private CustomerService customerService;
|
||||
|
||||
@Autowired
|
||||
private PurchaseService purchaseService;
|
||||
|
||||
|
||||
@Test
|
||||
public void testDeleteAllCustomers() {
|
||||
customerService.deleteAllCustomer();
|
||||
int n = 3;
|
||||
for (int i = 0; i < n; i++) {
|
||||
String name = "Customer" + i;
|
||||
customerService.addCustomer(name, name, "address", "email"+i);
|
||||
}
|
||||
customerService.deleteAllCustomer();
|
||||
Assertions.assertEquals(customerService.findAllCustomers().size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteCustomer() {
|
||||
customerService.deleteAllCustomer();
|
||||
String name = "Customer1";
|
||||
Customer c = customerService.addCustomer(name, name, "address", "email");
|
||||
customerService.deleteCustomer(c.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> {customerService.findCustomer(c.getId());});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddCustomer() {
|
||||
customerService.deleteAllCustomer();
|
||||
String name = "Company1";
|
||||
Customer c = customerService.addCustomer(name, name, "address", "email");
|
||||
Assertions.assertNotNull(customerService.findCustomer(c.getId()));
|
||||
customerService.deleteAllCustomer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateCustomer() {
|
||||
customerService.deleteAllCustomer();
|
||||
String name = "Customer1";
|
||||
Customer c = customerService.addCustomer(name, name, "address", "email");
|
||||
String name2 = "Customer2";
|
||||
customerService.updateCustomer(c.getId(), name2, name2, "fdgd", "effsd");
|
||||
Assertions.assertNotNull(customerService.findCustomer(c.getId()));
|
||||
customerService.deleteAllCustomer();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testFindAllCustomers() {
|
||||
customerService.deleteAllCustomer();
|
||||
|
||||
int n = 3;
|
||||
for (int i = 0; i < n; i++) {
|
||||
String name = "customer" + i;
|
||||
customerService.addCustomer(name, name, "address", "email2");
|
||||
}
|
||||
|
||||
Assertions.assertEquals(customerService.findAllCustomers().size(), n);
|
||||
customerService.deleteAllCustomer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddPurchase() {
|
||||
|
||||
purchaseService.deleteAllPurchases();
|
||||
customerService.deleteAllCustomer();
|
||||
|
||||
|
||||
final String name = "Customer";
|
||||
Customer c = customerService.addCustomer(name, name, "address", "email");
|
||||
Purchase newPurchase = purchaseService.addPurchase(new Date(1212121212121L), 3456.00);
|
||||
customerService.addNewPurchase(c.getId(), newPurchase);
|
||||
|
||||
Assertions.assertTrue(customerService.findCustomer(c.getId()).getPurchases().contains(newPurchase));
|
||||
|
||||
|
||||
purchaseService.deleteAllPurchases();
|
||||
customerService.deleteAllCustomer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePurchase() {
|
||||
purchaseService.deleteAllPurchases();
|
||||
customerService.deleteAllCustomer();
|
||||
|
||||
|
||||
final String name = "Company";
|
||||
Customer c = customerService.addCustomer(name, name, "address", "email");
|
||||
Purchase newPurchase = purchaseService.addPurchase(new Date(1212121212121L), 3567.00);
|
||||
|
||||
customerService.addNewPurchase(c.getId(), newPurchase);
|
||||
Assertions.assertTrue(customerService.findCustomer(c.getId()).getPurchases().contains(newPurchase));
|
||||
|
||||
customerService.deletePurchase(c.getId(), newPurchase);
|
||||
Assertions.assertFalse(customerService.findCustomer(c.getId()).getPurchases().contains(newPurchase));
|
||||
|
||||
purchaseService.deleteAllPurchases();
|
||||
purchaseService.deleteAllPurchases();
|
||||
|
||||
}
|
||||
}
|
72
src/test/java/ru/ulstu/is/myapp/ProductTests.java
Normal file
72
src/test/java/ru/ulstu/is/myapp/ProductTests.java
Normal file
@ -0,0 +1,72 @@
|
||||
package ru.ulstu.is.myapp;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
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;
|
||||
import ru.ulstu.is.myapp.models.Product;
|
||||
import ru.ulstu.is.myapp.models.Purchase;
|
||||
import ru.ulstu.is.myapp.service.ProductService;
|
||||
import ru.ulstu.is.myapp.service.PurchaseService;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@SpringBootTest
|
||||
public class ProductTests {
|
||||
@Autowired
|
||||
private ProductService productService;
|
||||
|
||||
@Test
|
||||
public void testAddCustomer() {
|
||||
productService.deleteAllProducts();
|
||||
Product p = productService.addProduct("dgdf", 4567.00, "gfgf");
|
||||
Assertions.assertNotNull(productService.findProduct(p.getId()));
|
||||
productService.deleteAllProducts();
|
||||
}
|
||||
@Test
|
||||
public void testUpdatePurchase() {
|
||||
productService.deleteAllProducts();
|
||||
Product p = productService.addProduct("dgdf", 4567.00, "gfgf");
|
||||
productService.updateProduct(p.getId(),"dgdf", 4567.00, "gfgf");
|
||||
Assertions.assertNotNull(productService.findProduct(p.getId()));
|
||||
productService.deleteAllProducts();
|
||||
}
|
||||
@Test
|
||||
public void testDeleteCustomer() {
|
||||
productService.deleteAllProducts();
|
||||
Product p = productService.addProduct("dgdf", 4567.00, "gfgf");
|
||||
productService.deleteProduct(p.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> {productService.findProduct(p.getId());});
|
||||
}
|
||||
@Test
|
||||
void testPurchaseRead() {
|
||||
productService.deleteAllProducts();
|
||||
final Product product = productService.addProduct("dgdf", 4567.00, "gfgf");
|
||||
final Product findProduct= productService.findProduct(product.getId());
|
||||
Assertions.assertEquals(product, findProduct);
|
||||
}
|
||||
@Test
|
||||
void testPurchaseReadNotFound() {
|
||||
|
||||
productService.deleteAllProducts();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPurchaseReadAll() {
|
||||
productService.deleteAllProducts();
|
||||
productService.addProduct("dgdf", 4567.00, "gfgf");
|
||||
productService.addProduct("dgdf", 4567.00, "gfgf");
|
||||
final List<Product> purchases = productService.findAllProducts();
|
||||
|
||||
Assertions.assertEquals(purchases.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPurchaseReadAllEmpty() {
|
||||
productService.deleteAllProducts();
|
||||
final List<Product> products = productService.findAllProducts();
|
||||
Assertions.assertEquals(products.size(), 0);
|
||||
}
|
||||
}
|
78
src/test/java/ru/ulstu/is/myapp/PurchaseTests.java
Normal file
78
src/test/java/ru/ulstu/is/myapp/PurchaseTests.java
Normal file
@ -0,0 +1,78 @@
|
||||
package ru.ulstu.is.myapp;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
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;
|
||||
import ru.ulstu.is.myapp.models.Customer;
|
||||
import ru.ulstu.is.myapp.models.Purchase;
|
||||
import ru.ulstu.is.myapp.service.CustomerService;
|
||||
import ru.ulstu.is.myapp.service.PurchaseService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootTest
|
||||
public class PurchaseTests {
|
||||
private static final Logger log = LoggerFactory.getLogger(PurchaseTests.class);
|
||||
|
||||
@Autowired
|
||||
private PurchaseService purchaseService;
|
||||
@Test
|
||||
public void testAddCustomer() {
|
||||
purchaseService.deleteAllPurchases();
|
||||
Purchase p = purchaseService.addPurchase(new Date(1212121212121L), 4356.00);
|
||||
Assertions.assertNotNull(purchaseService.findPurchase(p.getId()));
|
||||
purchaseService.deleteAllPurchases();
|
||||
}
|
||||
@Test
|
||||
public void testUpdatePurchase() {
|
||||
purchaseService.deleteAllPurchases();
|
||||
Purchase p = purchaseService.addPurchase(new Date(1212121212121L), 4356.00);
|
||||
purchaseService.updatePurchase(p.getId(), new Date(1212121212121L), 4356.00);
|
||||
Assertions.assertNotNull(purchaseService.findPurchase(p.getId()));
|
||||
purchaseService.deleteAllPurchases();
|
||||
}
|
||||
@Test
|
||||
public void testDeleteCustomer() {
|
||||
purchaseService.deleteAllPurchases();
|
||||
Purchase p = purchaseService.addPurchase(new Date(1212121212121L), 4356.00);
|
||||
purchaseService.deletePurchase(p.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> {purchaseService.findPurchase(p.getId());});
|
||||
}
|
||||
@Test
|
||||
void testPurchaseRead() {
|
||||
purchaseService.deleteAllPurchases();
|
||||
final Purchase purchase = purchaseService.addPurchase(new Date(1212121212121L), 4356.00);
|
||||
final Purchase findPurchase = purchaseService.findPurchase(purchase.getId());
|
||||
Assertions.assertEquals(purchase, findPurchase);
|
||||
}
|
||||
@Test
|
||||
void testPurchaseReadNotFound() {
|
||||
|
||||
purchaseService.deleteAllPurchases();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> purchaseService.findPurchase(1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPurchaseReadAll() {
|
||||
purchaseService.deleteAllPurchases();
|
||||
purchaseService.addPurchase(new Date(1212121212121L), 4356.00);
|
||||
purchaseService.addPurchase(new Date(1212124212221L), 4356.00);
|
||||
final List<Purchase> purchases = purchaseService.findAllPurchases();
|
||||
log.info(purchases.toString());
|
||||
Assertions.assertEquals(purchases.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPurchaseReadAllEmpty() {
|
||||
purchaseService.deleteAllPurchases();
|
||||
final List<Purchase> students = purchaseService.findAllPurchases();
|
||||
Assertions.assertEquals(students.size(), 0);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user