Вроде работает

This commit is contained in:
Inohara 2023-03-21 00:01:18 +04:00
parent ba48211be0
commit ca7afdf94e
28 changed files with 480 additions and 427 deletions

View File

@ -15,7 +15,10 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.postgresql:postgresql:'
implementation 'com.h2database:h2:2.1.210' 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'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }

View File

@ -2,15 +2,11 @@ package com.example.demo;
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.*;
@SpringBootApplication @SpringBootApplication
public class DemoApplication { public class DemoApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args); SpringApplication.run(DemoApplication.class, args);
} }
} }

View File

@ -1,45 +0,0 @@
package com.example.demo.method.controller;
import com.example.demo.method.service.MethodService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@CrossOrigin
public class MethodController {
private final MethodService methodService;
public MethodController(MethodService methodService) {
this.methodService = methodService;
}
@GetMapping("/summa")
public String qw(@RequestParam(value = "first", defaultValue = "1") Object first,
@RequestParam(value = "second", defaultValue = "1") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Sum(first,second,type);
}
@GetMapping("/min")
public String Ras(@RequestParam(value = "first", defaultValue = "1") Object first,
@RequestParam(value = "second", defaultValue = "1") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Min(first,second,type);
}
@GetMapping("/multi")
public String Pros(@RequestParam(value = "first", defaultValue = "1") Object first,
@RequestParam(value = "second", defaultValue = "1") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Mult(first,second,type);
}
@GetMapping("/div")
public String Del(@RequestParam(value = "first", defaultValue = "1") Object first,
@RequestParam(value = "second", defaultValue = "1") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Div(first,second,type);
}
}

View File

@ -1,11 +0,0 @@
package com.example.demo.method.domain;
public interface IMethod<T> {
T Sum(T first, T second);
T Mult(T first, T second);
T Min(T first, T second);
T Div(T first, T second);
}

View File

@ -1,23 +0,0 @@
package com.example.demo.method.domain;
import org.springframework.stereotype.Component;
@Component(value="int")
public class MethodInt implements IMethod<Integer>{
public Integer Sum(Integer first, Integer second) {
return first + second;
}
public Integer Mult(Integer first, Integer second) {
return first * second;
}
public Integer Min(Integer first, Integer second) {
return first - second;
}
public Integer Div(Integer first, Integer second) {
if (second == 0) second = 1;
return first/second;
}
}

View File

@ -1,26 +0,0 @@
package com.example.demo.method.domain;
import org.springframework.stereotype.Component;
@Component(value="str")
public class MethodString implements IMethod<String>{
@Override
public String Sum(String first, String second) {
return first + "+" + second;
}
@Override
public String Mult(String first, String second) {
return first + "*" + second;
}
@Override
public String Min(String first, String second) {
return first + "-" + second;
}
@Override
public String Div(String first, String second) {
return first + "/" + second;
}
}

View File

@ -1,48 +0,0 @@
package com.example.demo.method.service;
import com.example.demo.method.domain.IMethod;
import com.example.demo.method.domain.MethodString;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class MethodService {
private final ApplicationContext applicationContext;
public MethodService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public String Sum(Object first, Object second, String type) {
final IMethod operation = (IMethod) applicationContext.getBean(type);
if (operation instanceof MethodString)
return String.format("%s", operation.Sum(first,second));
else
return String.format("%s", operation.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
public String Min(Object first, Object second, String type) {
final IMethod operation = (IMethod) applicationContext.getBean(type);
if (operation instanceof MethodString)
return String.format("%s", operation.Min(first,second));
else
return String.format("%s", operation.Min(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
public String Mult(Object first, Object second, String type) {
final IMethod operation = (IMethod) applicationContext.getBean(type);
if (operation instanceof MethodString)
return String.format("%s", operation.Mult(first,second));
else
return String.format("%s", operation.Mult(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
public String Div(Object first, Object second, String type) {
final IMethod operation = (IMethod) applicationContext.getBean(type);
if (operation instanceof MethodString)
return String.format("%s", operation.Div(first,second));
else
return String.format("%s", operation.Div(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
}
}

View File

@ -1,21 +0,0 @@
package com.example.demo.speaker.configuration;
import com.example.demo.speaker.domain.Speaker;
import com.example.demo.speaker.domain.SpeakerEng;
import com.example.demo.speaker.domain.SpeakerRus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpeakerConfiguration {
@Bean(value = "ru")
public SpeakerRus createRusSpeaker(){
return new SpeakerRus();
}
@Bean(value = "en")
public Speaker createEngSpeaker(){
return new SpeakerEng();
}
}

View File

@ -1,44 +0,0 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.service.SpeakerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SpeakerController {
private final SpeakerService speakerService;
public SpeakerController(SpeakerService speakerService){
this.speakerService = speakerService;
}
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "мир") String name,
@RequestParam(defaultValue = "ru") String lang){
return speakerService.say(name, lang);
}
@GetMapping("/sum")
public @ResponseBody Integer sum(@RequestParam(required = false, defaultValue = "0") int val1,
@RequestParam(required = false, defaultValue = "0") int val2){
return val1 + val2;
}
@GetMapping("/toLowerCase")
public @ResponseBody String toLowerCase(@RequestParam(defaultValue = "TO LOW CASE") String value) {
return value.toLowerCase();
}
@GetMapping("/minus")
public @ResponseBody Integer minus(@RequestParam(required = false, defaultValue = "0") int val1,
@RequestParam(required = false, defaultValue = "0") int val2){
return val1 - val2;
}
@GetMapping("/join")
public @ResponseBody String join(@RequestParam(required = false, defaultValue = "0") String str1,
@RequestParam(required = false, defaultValue = "0") String str2){
return String.join("", str1, str2);
}
}

View File

@ -1,5 +0,0 @@
package com.example.demo.speaker.domain;
public interface Speaker {
String say();
}

View File

@ -1,11 +0,0 @@
package com.example.demo.speaker.domain;
import org.springframework.stereotype.Component;
@Component(value = "de")
public class SpeakerDeu implements Speaker{
@Override
public String say(){
return "Hallo";
}
}

View File

@ -1,8 +0,0 @@
package com.example.demo.speaker.domain;
public class SpeakerEng implements Speaker{
@Override
public String say() {
return "Hello";
}
}

View File

@ -1,11 +0,0 @@
package com.example.demo.speaker.domain;
import org.springframework.stereotype.Component;
public class SpeakerRus implements Speaker{
@Override
public String say(){
return "Привет";
}
}

View File

@ -1,19 +0,0 @@
package com.example.demo.speaker.service;
import com.example.demo.speaker.domain.Speaker;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class SpeakerService {
private final ApplicationContext applicationContext;
public SpeakerService(ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
public String say(String name, String lang){
final Speaker speaker = (Speaker) applicationContext.getBean(lang);
return String.format("%s %s!", speaker.say(), name);
}
}

View File

@ -28,7 +28,7 @@ public class ProductController {
} }
@PostMapping("/") @PostMapping("/")
public Product createStudent(@RequestParam() String name, public Product createProduct(@RequestParam() String name,
@RequestParam() float cost) { @RequestParam() float cost) {
return productService.addProduct(name, cost); return productService.addProduct(name, cost);
} }

View File

@ -0,0 +1,43 @@
package com.example.demo.supply.controllers;
import com.example.demo.supply.models.Supplier;
import com.example.demo.supply.services.SupplierService;
import org.springframework.web.bind.annotation.*;
import java.util.List;
public class SupplierController {
private final SupplierService supplierService;
public SupplierController(SupplierService supplierService) {
this.supplierService = supplierService;
}
@GetMapping("/{id}")
public Supplier getSupplier(@PathVariable Long id) {
return supplierService.findSupplier(id);
}
@GetMapping("/")
public List<Supplier> getSupplier() {
return supplierService.findAllSuppliers();
}
@PostMapping("/")
public Supplier createSupplier(@RequestParam() String name,
@RequestParam() int license) {
return supplierService.addSupplier(name, license);
}
@PatchMapping("/{id}")
public Supplier updateSupplier(@PathVariable Long id,
@RequestParam() String name,
@RequestParam() int license) {
return supplierService.updateSupplier(id, name, license);
}
@DeleteMapping("/{id}")
public Supplier deleteSupplier(@PathVariable Long id) {
return supplierService.deleteSupplier(id);
}
}

View File

@ -1,69 +0,0 @@
package com.example.demo.supply.models;
import jakarta.persistence.*;
import java.util.List;
import java.util.Objects;
@Entity
public class Maker {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String director;
private List<Product> products;
public Maker(){}
public Maker( String name, String director) {
this.name = name;
this.director = director;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Maker maker = (Maker) o;
return Objects.equals(id, maker.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", director='" + director + '\'' +
'}';
}
}

View File

@ -0,0 +1,93 @@
package com.example.demo.supply.models;
import jakarta.persistence.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Entity
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
@Temporal(TemporalType.DATE)
private Date dateOfOrder;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "supplier_fk")
private Supplier supplier;
@ManyToMany(fetch = FetchType.EAGER, cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
@JoinTable(joinColumns = @JoinColumn(name = "order_fk"),
inverseJoinColumns = @JoinColumn(name = "product_fk"))
private List<Product> products;
public Long getId(){
return id;
}
public Orders(Date dateOfOrder) {
this.dateOfOrder = dateOfOrder;
products = new ArrayList<>();
}
public Orders() {
}
public Date getDateOfOrder() {
return dateOfOrder;
}
public void setDateOfOrder(Date dateOfOrder) {
this.dateOfOrder = dateOfOrder;
}
public Supplier getSupplier() {
return supplier;
}
public void setSupplier(Supplier supplier) {
this.supplier = supplier;
supplier.getOrders().add(this);
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public void addProduct(Product product){
if(!products.contains(product)){
products.add(product);
product.getOrders().add(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Orders order = (Orders) o;
if(!Objects.equals(id, order.id)) return false;
if(!Objects.equals(dateOfOrder.toString(), order.dateOfOrder.toString())) return false;
if(!Objects.equals(supplier, order.supplier)) return false;
// if(!Objects.equals(products.size(), order.products.size())) return false;
return true;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", date='" + dateOfOrder + '\'' +
'}';
}
}

View File

@ -1,8 +1,9 @@
package com.example.demo.supply.models; package com.example.demo.supply.models;
import jakarta.persistence.*; import jakarta.persistence.*;
import org.hibernate.annotations.AnyDiscriminatorValue;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@ -14,35 +15,42 @@ public class Product {
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
@Column(nullable = false) @Column(nullable = false)
private float cost; private double cost;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "products")
private List<Orders> orders;
public Product(){} public Product(){}
public Product(String name, float cost){ public Product(String name, double cost){
this.name = name; this.name = name;
this.cost = cost; this.cost = cost;
orders = new ArrayList<>();
} }
public long getId() { public long getId() {
return id; return id;
} }
public String getName() { public String getName() {
return name; return name;
} }
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public double getCost() {
public float getCost() {
return cost; return cost;
} }
public void setCost(float cost) { public void setCost(float cost) {
this.cost = cost; this.cost = cost;
} }
public List<Orders> getOrders() {
return orders;
}
public void setOrders(List<Orders> orders) {
this.orders = orders;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -2,6 +2,8 @@ package com.example.demo.supply.models;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@ -15,13 +17,18 @@ public class Supplier {
@Column(nullable = false) @Column(nullable = false)
private int license; private int license;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "supplier", cascade = CascadeType.REMOVE)
private List<Orders> orders;
public Supplier(){} public Supplier(){}
public Supplier( String name, int license) { public Supplier( String name, int license) {
this.name = name; this.name = name;
this.license = license; this.license = license;
orders = new ArrayList<>();
} }
public Long getId() { return id; }
public String getName() { public String getName() {
return name; return name;
} }
@ -38,6 +45,13 @@ public class Supplier {
this.license = license; this.license = license;
} }
public List<Orders> getOrders() {
return orders;
}
public void setOrders(List<Orders> orders) {
this.orders = orders;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {

View File

@ -0,0 +1,77 @@
package com.example.demo.supply.services;
import com.example.demo.supply.models.Orders;
import com.example.demo.supply.models.Product;
import com.example.demo.supply.models.Supplier;
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.sql.Date;
import java.util.List;
@Service
public class OrderService {
@PersistenceContext
private EntityManager em;
@Transactional
public Orders addOrder(Supplier supplier){
if (supplier == null || em.find(Supplier.class, supplier.getId()) == null) {
throw new IllegalArgumentException("invalid supplier");
}
final Orders order = new Orders(new Date(System.currentTimeMillis()));
order.setSupplier(supplier);
em.persist(order);
em.merge(supplier);
return order;
}
@Transactional
public Orders addProduct(Long id, Product product) {
if (product == null) {
throw new IllegalArgumentException("empty product");
}
final Orders currentOrder = findOrder(id);
currentOrder.addProduct(product);
product.getOrders().add(currentOrder);
em.merge(product);
return em.merge(currentOrder);
}
@Transactional(readOnly = true)
public Orders findOrder(Long id) {
final Orders order = em.find(Orders.class, id);
if (order == null) {
throw new EntityNotFoundException(String.format("Order with id [%s] is not found", id));
}
return order;
}
@Transactional(readOnly = true)
public List<Orders> findAllOrders() {
return em.createQuery("select o from Orders o", Orders.class).getResultList();
}
@Transactional
public Orders updateOrder(Long id, Date date) {
final Orders currentOrder = findOrder(id);
currentOrder.setDateOfOrder(date);
return em.merge(currentOrder);
}
@Transactional
public Orders deleteOrder(Long id) {
final Orders currentOrder = findOrder(id);
em.remove(currentOrder);
return currentOrder;
}
@Transactional
public void deleteAll(){
em.createQuery("delete from Orders ").executeUpdate();
}
}

View File

@ -16,7 +16,7 @@ public class ProductService {
private EntityManager em; private EntityManager em;
@Transactional @Transactional
public Product addProduct(String name, float cost){ public Product addProduct(String name, double cost){
if(!StringUtils.hasText(name) || cost <= 0) if(!StringUtils.hasText(name) || cost <= 0)
throw new IllegalArgumentException("Invalid name and/or cost"); throw new IllegalArgumentException("Invalid name and/or cost");
@ -56,4 +56,9 @@ public class ProductService {
em.remove(currentProduct); em.remove(currentProduct);
return currentProduct; return currentProduct;
} }
@Transactional
public void deleteAll(){
em.createQuery("delete from Product").executeUpdate();
}
} }

View File

@ -0,0 +1,64 @@
package com.example.demo.supply.services;
import com.example.demo.supply.models.Supplier;
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.List;
@Service
public class SupplierService {
@PersistenceContext
private EntityManager em;
@Transactional
public Supplier addSupplier(String name, int license){
if(!StringUtils.hasText(name) || license < 0)
throw new IllegalArgumentException("Invalid name and/or license");
final Supplier supplier = new Supplier(name, license);
em.persist(supplier);
return supplier;
}
@Transactional(readOnly = true)
public Supplier findSupplier(Long id) {
final Supplier supplier = em.find(Supplier.class, id);
if (supplier == null) {
throw new EntityNotFoundException(String.format("Supplier with id [%s] is not found", id));
}
return supplier;
}
@Transactional(readOnly = true)
public List<Supplier> findAllSuppliers() {
return em.createQuery("select s from Supplier s", Supplier.class).getResultList();
}
@Transactional
public Supplier updateSupplier(Long id, String name, int license) {
if (!StringUtils.hasText(name) || license < 0)
throw new IllegalArgumentException("Invalid name and/or license");
final Supplier currentSupplier = findSupplier(id);
currentSupplier.setName(name);
currentSupplier.setLicense(license);
return em.merge(currentSupplier);
}
@Transactional
public Supplier deleteSupplier(Long id) {
final Supplier currentSupplier = findSupplier(id);
em.remove(currentSupplier);
return currentSupplier;
}
@Transactional
public void deleteAll(){
em.createQuery("delete from Supplier ").executeUpdate();
}
}

View File

@ -1 +1,9 @@
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

View File

@ -1,71 +0,0 @@
package com.example.demo;
import com.example.demo.method.service.MethodService;
import com.example.demo.speaker.service.SpeakerService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired
MethodService methodService;
@Test
void testSumInt() {
final String res = methodService.Sum(12, 12, "int");
Assertions.assertEquals(24, Integer.parseInt(res));
}
@Test
void testMinInt() {
final String res = methodService.Min(12, 12, "int");
Assertions.assertEquals(0, Integer.parseInt(res));
}
@Test
void testMultInt() {
final String res = methodService.Mult(2, 3, "int");
Assertions.assertEquals(6, Integer.parseInt(res));
}
@Test
void testDivInt() {
final String res = methodService.Div(12, 6, "int");
Assertions.assertEquals(2, Integer.parseInt(res));
}
@Test
void testSumStr() {
final String res = methodService.Sum("12", "12", "str");
Assertions.assertEquals("12+12", res);
}
@Test
void testMinStr() {
final String res = methodService.Min("12", "12", "str");
Assertions.assertEquals("12-12", res);
}
@Test
void testMultStr() {
final String res = methodService.Mult("12", "12", "str");
Assertions.assertEquals("12*12", res);
}
@Test
void testDivStr() {
final String res = methodService.Div("12", "12", "str");
Assertions.assertEquals("12/12", res);
}
@Test
void testNoSuchBean() {
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> methodService.Min("12", "12", "string"));
}
@Test
void testNumFormatError() {
Assertions.assertThrows(NumberFormatException.class, () -> methodService.Min("12dd", 12, "int"));
}
}

View File

@ -0,0 +1,44 @@
package com.example.demo;
import com.example.demo.supply.models.Orders;
import com.example.demo.supply.models.Product;
import com.example.demo.supply.models.Supplier;
import com.example.demo.supply.services.OrderService;
import com.example.demo.supply.services.ProductService;
import com.example.demo.supply.services.SupplierService;
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 java.util.List;
@SpringBootTest
public class Tests {
@Autowired
private ProductService productService;
@Autowired
private OrderService orderService;
@Autowired
private SupplierService supplierService;
@Test
public void testOrder(){
productService.deleteAll();
orderService.deleteAll();
supplierService.deleteAll();
final Product product1 = productService.addProduct("Huawei Band 3", 2000.13);
final Product product2 = productService.addProduct("Samsung A2", 22000.56);
final Supplier supplier = supplierService.addSupplier("SuperSupplier", 325453);
Orders order = orderService.addOrder(supplierService.findSupplier(supplier.getId()));
order = orderService.addProduct(order.getId(), product1);
order = orderService.addProduct(order.getId(), product2);
// List<Product> products1 = order.getProducts();
final Orders order2 = orderService.findOrder(order.getId());
// List<Product> products2 = order2.getProducts();
Assertions.assertEquals(order, order2);
}
}

View File

@ -0,0 +1,56 @@
package com.example.demo;
import com.example.demo.supply.models.Product;
import com.example.demo.supply.services.ProductService;
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 java.util.List;
@SpringBootTest
public class TestsProduct {
@Autowired
private ProductService productService;
@Test
void testProduct(){
productService.deleteAll();
final Product product = productService.addProduct("Huawei Band 3", 2000.13);
Assertions.assertNotNull(product.getId());
}
@Test
void testProductRead(){
productService.deleteAll();
final Product product = productService.addProduct("Huawei Band 3", 2000.13);
final Product findProduct = productService.findProduct(product.getId());
Assertions.assertEquals(product, findProduct);
}
@Test
void testProductReadNotFound(){
productService.deleteAll();
Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(-1L));
}
@Test
void testProductReadAll(){
productService.deleteAll();
productService.addProduct("Samsung A3", 22000.4);
productService.addProduct("Huawei Band 3", 2000.13);
final List<Product> products = productService.findAllProducts();
Assertions.assertEquals(products.size(), 2);
}
@Test
void testProductReadAllEmpty(){
productService.deleteAll();
final List<Product> products = productService.findAllProducts();
Assertions.assertEquals(products.size(), 0);
}
}

View File

@ -0,0 +1,54 @@
package com.example.demo;
import com.example.demo.supply.models.Supplier;
import com.example.demo.supply.services.SupplierService;
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 java.util.List;
@SpringBootTest
public class TestsSupplier {
@Autowired
private SupplierService supplierService;
@Test
void testSupplier(){
supplierService.deleteAll();
final Supplier supplier = supplierService.addSupplier("SuperSup", 359342);
Assertions.assertNotNull(supplier.getId());
}
@Test
void testSupplierRead(){
supplierService.deleteAll();
final Supplier supplier = supplierService.addSupplier("Huawei", 4357695);
final Supplier findSupplier = supplierService.findSupplier(supplier.getId());
Assertions.assertEquals(supplier, findSupplier);
}
@Test
void testSupplierReadNotFound(){
supplierService.deleteAll();
Assertions.assertThrows(EntityNotFoundException.class, () -> supplierService.findSupplier(-1L));
}
@Test
void testSupplierReadAll(){
supplierService.deleteAll();
supplierService.addSupplier("Samsung", 3485456);
supplierService.addSupplier("Huawei", 45736964);
final List<Supplier> suppliers = supplierService.findAllSuppliers();
Assertions.assertEquals(suppliers.size(), 2);
}
@Test
void testSupplierReadAllEmpty(){
supplierService.deleteAll();
final List<Supplier> suppliers = supplierService.findAllSuppliers();
Assertions.assertEquals(suppliers.size(), 0);
}
}