From d304afbd9b2af51b512ffd397a7c2a7b17eaf4b2 Mon Sep 17 00:00:00 2001 From: Inohara Date: Mon, 3 Apr 2023 22:04:24 +0400 Subject: [PATCH] =?UTF-8?q?@=5F@=20=D1=82=D1=80=D0=B5=D1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/supply/services/OrderService.java | 22 ++----- .../demo/supply/services/SupplierService.java | 2 +- .../src/test/java/com/example/demo/Tests.java | 13 ++-- .../java/com/example/demo/TestsProduct.java | 56 ----------------- .../java/com/example/demo/TestsSupplier.java | 60 ------------------- 5 files changed, 13 insertions(+), 140 deletions(-) delete mode 100644 demo/src/test/java/com/example/demo/TestsProduct.java delete mode 100644 demo/src/test/java/com/example/demo/TestsSupplier.java diff --git a/demo/src/main/java/com/example/demo/supply/services/OrderService.java b/demo/src/main/java/com/example/demo/supply/services/OrderService.java index 92892bf..1c7c255 100644 --- a/demo/src/main/java/com/example/demo/supply/services/OrderService.java +++ b/demo/src/main/java/com/example/demo/supply/services/OrderService.java @@ -13,7 +13,9 @@ import org.springframework.transaction.annotation.Transactional; import java.sql.Date; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; @Service public class OrderService { @@ -31,29 +33,13 @@ public class OrderService { return order; } - //поставщики, у которых есть заказ на конкретный товар или несколько товаров @Transactional public List suppliers(List products){ - List result = new ArrayList<>(); - List orders = findAllOrders(); - for(Orders order : orders){ - int k = 0; - for(Product product : products){ - if(order.getProducts().contains(product)) k++; - } - if(k == products.size()) - result.add(order.getSupplier()); - } - return result; + return em.createQuery("SELECT distinct o.supplier FROM Orders o join Product p where p in (:products) ", Supplier.class) + .setParameter("products", products).getResultList(); } -// @Transactional -// public List suppliers(List products){ -// return em.createQuery("SELECT o.supplier FROM Orders o WHERE o.products = :products ", Supplier.class) -// .setParameter("products", products).getResultList(); -// } - @Transactional public Orders addProduct(Long id, Product product) { if (product == null) { diff --git a/demo/src/main/java/com/example/demo/supply/services/SupplierService.java b/demo/src/main/java/com/example/demo/supply/services/SupplierService.java index 64fc53f..1b05130 100644 --- a/demo/src/main/java/com/example/demo/supply/services/SupplierService.java +++ b/demo/src/main/java/com/example/demo/supply/services/SupplierService.java @@ -59,6 +59,6 @@ public class SupplierService { @Transactional public void deleteAll(){ - em.createQuery("delete from Supplier ").executeUpdate(); + em.createQuery("delete from Supplier").executeUpdate(); } } diff --git a/demo/src/test/java/com/example/demo/Tests.java b/demo/src/test/java/com/example/demo/Tests.java index 6f89a34..9f51114 100644 --- a/demo/src/test/java/com/example/demo/Tests.java +++ b/demo/src/test/java/com/example/demo/Tests.java @@ -25,8 +25,8 @@ public class Tests { @Test public void testOrder(){ - productService.deleteAll(); orderService.deleteAll(); + productService.deleteAll(); supplierService.deleteAll(); final Product product1 = productService.addProduct("Huawei Band 3", 2000.13); @@ -38,22 +38,25 @@ public class Tests { order = orderService.addProduct(order.getId(), product2); final Orders order2 = orderService.findOrder(order.getId()); Assertions.assertEquals(order, order2); - productService.deleteAll(); orderService.deleteAll(); + productService.deleteAll(); supplierService.deleteAll(); } @Test public void test(){ - productService.deleteAll(); orderService.deleteAll(); + productService.deleteAll(); supplierService.deleteAll(); -/ + + + final Product product1 = productService.addProduct("Huawei Band 3", 2000.13); final Product product2 = productService.addProduct("Samsung A2", 22000.56); final Product product3 = productService.addProduct("Redmond f3", 2000.13); final Product product4 = productService.addProduct("Asus red9", 22000.56); + final Supplier supplier1 = supplierService.addSupplier("SuperSupplier1", 325453); final Supplier supplier2 = supplierService.addSupplier("SuperSupplier2", 545455); final Supplier supplier3 = supplierService.addSupplier("SuperSupplier3", 122122); @@ -74,8 +77,8 @@ public class Tests { List suppliers = orderService.suppliers(products); Assertions.assertEquals(suppliers.size(), 2); - productService.deleteAll(); orderService.deleteAll(); + productService.deleteAll(); supplierService.deleteAll(); } } diff --git a/demo/src/test/java/com/example/demo/TestsProduct.java b/demo/src/test/java/com/example/demo/TestsProduct.java deleted file mode 100644 index 40f107c..0000000 --- a/demo/src/test/java/com/example/demo/TestsProduct.java +++ /dev/null @@ -1,56 +0,0 @@ -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 products = productService.findAllProducts(); - Assertions.assertEquals(products.size(), 2); - } - - @Test - void testProductReadAllEmpty(){ - productService.deleteAll(); - final List products = productService.findAllProducts(); - Assertions.assertEquals(products.size(), 0); - } -} diff --git a/demo/src/test/java/com/example/demo/TestsSupplier.java b/demo/src/test/java/com/example/demo/TestsSupplier.java deleted file mode 100644 index ac7a165..0000000 --- a/demo/src/test/java/com/example/demo/TestsSupplier.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.example.demo; - -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 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; - @Autowired - private OrderService orderService; - @Autowired - private ProductService productService; - - @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 suppliers = supplierService.findAllSuppliers(); - Assertions.assertEquals(suppliers.size(), 2); - } - - @Test - void testSupplierReadAllEmpty(){ - supplierService.deleteAll(); - final List suppliers = supplierService.findAllSuppliers(); - Assertions.assertEquals(suppliers.size(), 0); - } -}