diff --git a/src/main/java/com/example/demo/itemOrders/service/OrdersService.java b/src/main/java/com/example/demo/itemOrders/service/OrdersService.java index 142a051..b3ed978 100644 --- a/src/main/java/com/example/demo/itemOrders/service/OrdersService.java +++ b/src/main/java/com/example/demo/itemOrders/service/OrdersService.java @@ -18,6 +18,7 @@ public class OrdersService { this.repository = repository; } + /* */ public List getAll(Long productId) { if (Objects.equals(productId, 0L)) { return repository.getAll(); @@ -40,6 +41,7 @@ public class OrdersService { existsEntity.setDate(entity.getDate()); return repository.update(existsEntity); } + public OrdersEntity delete(Long id) { final OrdersEntity existsEntity = get(id); return repository.delete(existsEntity); diff --git a/src/main/java/com/example/demo/itemProducts/service/ProductsService.java b/src/main/java/com/example/demo/itemProducts/service/ProductsService.java index 759dd5b..20ec9a7 100644 --- a/src/main/java/com/example/demo/itemProducts/service/ProductsService.java +++ b/src/main/java/com/example/demo/itemProducts/service/ProductsService.java @@ -1,7 +1,6 @@ package com.example.demo.itemProducts.service; import java.util.List; -import java.util.Objects; import java.util.Optional; import org.springframework.stereotype.Service; @@ -17,14 +16,16 @@ public class ProductsService { public ProductsService(ProductsRepository repository) { this.repository = repository; } -/* - public List getAll(Long typeId) { - if (Objects.equals(typeId, 0L)) { - return repository.getAll(); - } - return repository.getAll().stream().filter(item -> item.getType().getId().equals(typeId)).toList(); - } -*/ + + /* + * public List getAll(Long typeId) { + * if (Objects.equals(typeId, 0L)) { + * return repository.getAll(); + * } + * return repository.getAll().stream().filter(item -> + * item.getType().getId().equals(typeId)).toList(); + * } + */ public List getAll() { return repository.getAll(); } @@ -45,7 +46,6 @@ public class ProductsService { return repository.update(existsEntity); } - public ProductsEntity delete(Long id) { final ProductsEntity existsEntity = get(id); return repository.delete(existsEntity); diff --git a/src/test/java/com/example/demo/OrdersServiceTests.java b/src/test/java/com/example/demo/OrdersServiceTests.java new file mode 100644 index 0000000..7b1ecdd --- /dev/null +++ b/src/test/java/com/example/demo/OrdersServiceTests.java @@ -0,0 +1,84 @@ +package com.example.demo; + +import java.text.SimpleDateFormat; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.itemCategories.model.CategoriesEntity; +import com.example.demo.itemCategories.service.CategoriesService; +import com.example.demo.itemOrders.service.OrdersService; +import com.example.demo.itemProducts.model.ProductsEntity; +import com.example.demo.itemProducts.service.ProductsService; +import com.example.demo.itemOrders.model.OrdersEntity; + +@SpringBootTest +@TestMethodOrder(OrderAnnotation.class) +class OrdersServiceTests { + @Autowired + private OrdersService ordersService; + @Autowired + private CategoriesService categoriesService; + private CategoriesEntity category1; + private CategoriesEntity category2; + @Autowired + private ProductsService productsService; + private ProductsEntity product1; + private ProductsEntity product2; + private ProductsEntity product3; + + @Test + void getTest() { + Assertions.assertThrows(NotFoundException.class, () -> ordersService.get(0L)); + } + + @Test + @Order(1) + void createTest() { + + category1 = categoriesService.create(new CategoriesEntity(null, "Телефон")); + category2 = categoriesService.create(new CategoriesEntity(null, "Игровая приставка")); + + product1 = productsService.create(new ProductsEntity(null, category1, "Lenovo IDEA PAD 13", 15232.00)); + product2 = productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00)); + product3 = productsService.create(new ProductsEntity(null, category2, "Iphone 13", 150000.00)); + + ordersService.create(new OrdersEntity(null, product1, 3, new SimpleDateFormat("09/04/2024"))); + ordersService.create(new OrdersEntity(null, product2, 2, new SimpleDateFormat("09/04/2024"))); + ordersService.create(new OrdersEntity(null, product3, 1, new SimpleDateFormat("07/04/2024"))); + final OrdersEntity last = ordersService + .create(new OrdersEntity(null, product1, 4, new SimpleDateFormat("07/04/2024"))); + Assertions.assertEquals(4, ordersService.getAll(0L).size()); + Assertions.assertEquals(0, ordersService.getAll(-1L).size()); + Assertions.assertEquals(last, ordersService.get(last.getId())); + } + + @Test + @Order(2) + void updateTest() { + final Integer newCount = 5; + final OrdersEntity entity = ordersService.get(1L); + final Integer oldCount = entity.getCount(); + final OrdersEntity newEntity = ordersService.update(1L, + new OrdersEntity(null, product1, newCount, new SimpleDateFormat("09/04/2024"))); + Assertions.assertEquals(4, ordersService.getAll(0L).size()); + Assertions.assertEquals(newEntity, ordersService.get(1L)); + Assertions.assertEquals(newCount, newEntity.getCount()); + Assertions.assertNotEquals(oldCount, newEntity.getCount()); + } + + @Test + @Order(3) + void deleteTest() { + ordersService.delete(3L); + Assertions.assertEquals(3, ordersService.getAll(0L).size()); + final OrdersEntity last = ordersService.get(2L); + Assertions.assertEquals(2L, last.getId()); + } +} diff --git a/src/test/java/com/example/demo/ProductsServiceTests.java b/src/test/java/com/example/demo/ProductsServiceTests.java index e429b58..8414abf 100644 --- a/src/test/java/com/example/demo/ProductsServiceTests.java +++ b/src/test/java/com/example/demo/ProductsServiceTests.java @@ -19,9 +19,11 @@ import com.example.demo.itemProducts.model.ProductsEntity; class ProductsServiceTests { @Autowired private ProductsService productsService; + @Autowired private CategoriesService categoriesService; - CategoriesEntity category1 = categoriesService.create(new CategoriesEntity(null, "Телефон")); - CategoriesEntity category2 = categoriesService.create(new CategoriesEntity(null, "Игровая приставка")); + private CategoriesEntity category1; + private CategoriesEntity category2; + @Test void getTest() { Assertions.assertThrows(NotFoundException.class, () -> productsService.get(0L)); @@ -30,12 +32,11 @@ class ProductsServiceTests { @Test @Order(1) void createTest() { - final CategoriesEntity category1 = categoriesService.create(new CategoriesEntity(null, "Ноутбук")); - final CategoriesEntity category2 = categoriesService.create(new CategoriesEntity(null, "Телефон")); - + category1 = categoriesService.create(new CategoriesEntity(null, "Телефон")); + category2 = categoriesService.create(new CategoriesEntity(null, "Игровая приставка")); productsService.create(new ProductsEntity(null, category1, "Lenovo IDEA PAD 13", 15232.00)); productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00)); - final ProductsEntity last = productsService.create(new ProductsEntity (null, category2, "Iphone 13", 150000.00)); + final ProductsEntity last = productsService.create(new ProductsEntity(null, category2, "Iphone 13", 150000.00)); Assertions.assertEquals(3, productsService.getAll().size()); Assertions.assertEquals(last, productsService.get(3L)); @@ -47,7 +48,8 @@ class ProductsServiceTests { final Double newPrice = 20000.00; final ProductsEntity entity = productsService.get(1L); final Double oldPrice = entity.getPrice(); - final ProductsEntity newEntity = productsService.update(1L, new ProductsEntity(1L,category1, "Lenovo IDEA PAD 13", newPrice)); + final ProductsEntity newEntity = productsService.update(1L, + new ProductsEntity(1L, category1, "Lenovo IDEA PAD 13", newPrice)); Assertions.assertEquals(3, productsService.getAll().size()); Assertions.assertEquals(newEntity, productsService.get(1L)); Assertions.assertEquals(newPrice, newEntity.getPrice()); diff --git a/src/test/java/com/example/demo/Test.java b/src/test/java/com/example/demo/Test.java deleted file mode 100644 index 7873aae..0000000 --- a/src/test/java/com/example/demo/Test.java +++ /dev/null @@ -1,148 +0,0 @@ -package com.example.demo; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; -import org.junit.jupiter.api.Order; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestMethodOrder; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; - -import com.example.demo.core.error.NotFoundException; -import com.example.demo.itemCategories.model.CategoriesEntity; -import com.example.demo.itemCategories.service.CategoriesService; -import com.example.demo.itemProducts.model.ProductsEntity; -import com.example.demo.itemProducts.service.ProductsService; -import com.example.demo.itemUsers.service.UsersService; -import com.example.demo.itemUsers.model.UsersEntity; - -@SpringBootTest -@TestMethodOrder(OrderAnnotation.class) -class UsersServiceTests { - @Autowired - private UsersService usersService; - private CategoriesService categoriesService; - private ProductsService productsService; - - @Test - void getUsers() { - Assertions.assertThrows(NotFoundException.class, () -> usersService.get(0L)); - } - - @Test - @Order(1) - void createUsers() { - usersService.create(new UsersEntity(null, "Natalia", "1234")); - final UsersEntity last = usersService.create(new UsersEntity(null, "Revengel", "4567")); - Assertions.assertEquals(2, usersService.getAll().size()); - Assertions.assertEquals(last, usersService.get(2L)); - } - - @Test - @Order(2) - void updateUsers() { - final String newPassword = "0000"; - final UsersEntity entity = usersService.get(2L); - final String login = entity.getLogin(); - final String oldPassword = entity.getPassword(); - final UsersEntity newEntity = usersService.update(2L, new UsersEntity(1L, login, newPassword)); - Assertions.assertEquals(2, usersService.getAll().size()); - Assertions.assertEquals(newEntity, usersService.get(2L)); - Assertions.assertEquals(newPassword, newEntity.getPassword()); - Assertions.assertNotEquals(oldPassword, newEntity.getPassword()); - } - - @Test - @Order(3) - void deleteUsers() { - usersService.delete(2L); - Assertions.assertEquals(1, usersService.getAll().size()); - final UsersEntity last = usersService.get(1L); - Assertions.assertEquals(1L, last.getId()); - } - - - - @Test - @Order(4) - void getCategories() { - Assertions.assertThrows(NotFoundException.class, () -> categoriesService.get(0L)); - } - - @Test - @Order(5) - void createCategories() { - CategoriesEntity category1 = categoriesService.create(new CategoriesEntity(null, "Телефон")); - CategoriesEntity category2 = categoriesService.create(new CategoriesEntity(null, "Игровая приставка")); - final CategoriesEntity last = categoriesService.create(new CategoriesEntity(null, "Ноутбук")); - Assertions.assertEquals(3, categoriesService.getAll().size()); - Assertions.assertEquals(last, categoriesService.get(3L)); - } - - @Test - @Order(6) - void updateCategories() { - final String test = "TEST"; - final CategoriesEntity entity = categoriesService.get(3L); - final String oldName = entity.getName(); - final CategoriesEntity newEntity = categoriesService.update(3L, new CategoriesEntity(1L, test)); - Assertions.assertEquals(3, categoriesService.getAll().size()); - Assertions.assertEquals(newEntity, categoriesService.get(3L)); - Assertions.assertEquals(test, newEntity.getName()); - Assertions.assertNotEquals(oldName, newEntity.getName()); - } - - @Test - @Order(7) - void deleteCategories() { - categoriesService.delete(3L); - Assertions.assertEquals(2, categoriesService.getAll().size()); - final CategoriesEntity last = categoriesService.get(2L); - Assertions.assertEquals(2L, last.getId()); - - final CategoriesEntity newEntity = categoriesService.create(new CategoriesEntity(null, "Игровая приставка")); - Assertions.assertEquals(3, categoriesService.getAll().size()); - Assertions.assertEquals(4L, newEntity.getId()); - } - - - - @Test - @Order(8) - void getTest() { - Assertions.assertThrows(NotFoundException.class, () -> productsService.get(0L)); - } - - @Test - @Order(9) - void createTest() { - productsService.create(new ProductsEntity(null, category1, "Lenovo IDEA PAD 13", 15232.00)); - productsService.create(new ProductsEntity(null, category1, "Acer", 20300.00)); - final ProductsEntity last = productsService.create(new ProductsEntity (null, category2, "Iphone 13", 150000.00)); - - Assertions.assertEquals(3, productsService.getAll().size()); - Assertions.assertEquals(last, productsService.get(3L)); - } - - @Test - @Order(10) - void updateTest() { - final Double newPrice = 20000.00; - final ProductsEntity entity = productsService.get(1L); - final Double oldPrice = entity.getPrice(); - final ProductsEntity newEntity = productsService.update(1L, new ProductsEntity(1L,category1, "Lenovo IDEA PAD 13", newPrice)); - Assertions.assertEquals(3, productsService.getAll().size()); - Assertions.assertEquals(newEntity, productsService.get(1L)); - Assertions.assertEquals(newPrice, newEntity.getPrice()); - Assertions.assertNotEquals(oldPrice, newEntity.getPrice()); - } - - @Test - @Order(11) - void deleteTest() { - productsService.delete(3L); - Assertions.assertEquals(2, productsService.getAll().size()); - final ProductsEntity last = productsService.get(2L); - Assertions.assertEquals(2L, last.getId()); - } -} diff --git a/src/test/java/com/example/demo/UsersServiceTests.java b/src/test/java/com/example/demo/UsersServiceTests.java index 7152610..70bb3c5 100644 --- a/src/test/java/com/example/demo/UsersServiceTests.java +++ b/src/test/java/com/example/demo/UsersServiceTests.java @@ -49,9 +49,9 @@ class UsersServiceTests { @Test @Order(3) void deleteTest() { - usersService.delete(1L); + usersService.delete(2L); Assertions.assertEquals(1, usersService.getAll().size()); - final UsersEntity last = usersService.get(0L); + final UsersEntity last = usersService.get(1L); Assertions.assertEquals(1L, last.getId()); } }