This commit is contained in:
revengel66 2024-04-09 20:11:20 +03:00
parent b9859be707
commit 7369280716
6 changed files with 107 additions and 167 deletions

View File

@ -18,6 +18,7 @@ public class OrdersService {
this.repository = repository; this.repository = repository;
} }
/* */
public List<OrdersEntity> getAll(Long productId) { public List<OrdersEntity> getAll(Long productId) {
if (Objects.equals(productId, 0L)) { if (Objects.equals(productId, 0L)) {
return repository.getAll(); return repository.getAll();
@ -40,6 +41,7 @@ public class OrdersService {
existsEntity.setDate(entity.getDate()); existsEntity.setDate(entity.getDate());
return repository.update(existsEntity); return repository.update(existsEntity);
} }
public OrdersEntity delete(Long id) { public OrdersEntity delete(Long id) {
final OrdersEntity existsEntity = get(id); final OrdersEntity existsEntity = get(id);
return repository.delete(existsEntity); return repository.delete(existsEntity);

View File

@ -1,7 +1,6 @@
package com.example.demo.itemProducts.service; package com.example.demo.itemProducts.service;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -17,13 +16,15 @@ public class ProductsService {
public ProductsService(ProductsRepository repository) { public ProductsService(ProductsRepository repository) {
this.repository = repository; this.repository = repository;
} }
/* /*
public List<ProductsEntity> getAll(Long typeId) { * public List<ProductsEntity> getAll(Long typeId) {
if (Objects.equals(typeId, 0L)) { * if (Objects.equals(typeId, 0L)) {
return repository.getAll(); * return repository.getAll();
} * }
return repository.getAll().stream().filter(item -> item.getType().getId().equals(typeId)).toList(); * return repository.getAll().stream().filter(item ->
} * item.getType().getId().equals(typeId)).toList();
* }
*/ */
public List<ProductsEntity> getAll() { public List<ProductsEntity> getAll() {
return repository.getAll(); return repository.getAll();
@ -45,7 +46,6 @@ public class ProductsService {
return repository.update(existsEntity); return repository.update(existsEntity);
} }
public ProductsEntity delete(Long id) { public ProductsEntity delete(Long id) {
final ProductsEntity existsEntity = get(id); final ProductsEntity existsEntity = get(id);
return repository.delete(existsEntity); return repository.delete(existsEntity);

View File

@ -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());
}
}

View File

@ -19,9 +19,11 @@ import com.example.demo.itemProducts.model.ProductsEntity;
class ProductsServiceTests { class ProductsServiceTests {
@Autowired @Autowired
private ProductsService productsService; private ProductsService productsService;
@Autowired
private CategoriesService categoriesService; private CategoriesService categoriesService;
CategoriesEntity category1 = categoriesService.create(new CategoriesEntity(null, "Телефон")); private CategoriesEntity category1;
CategoriesEntity category2 = categoriesService.create(new CategoriesEntity(null, "Игровая приставка")); private CategoriesEntity category2;
@Test @Test
void getTest() { void getTest() {
Assertions.assertThrows(NotFoundException.class, () -> productsService.get(0L)); Assertions.assertThrows(NotFoundException.class, () -> productsService.get(0L));
@ -30,9 +32,8 @@ class ProductsServiceTests {
@Test @Test
@Order(1) @Order(1)
void createTest() { void createTest() {
final CategoriesEntity category1 = categoriesService.create(new CategoriesEntity(null, "Ноутбук")); category1 = categoriesService.create(new CategoriesEntity(null, "Телефон"));
final CategoriesEntity category2 = 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, "Lenovo IDEA PAD 13", 15232.00));
productsService.create(new ProductsEntity(null, category1, "Acer", 20300.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));
@ -47,7 +48,8 @@ class ProductsServiceTests {
final Double newPrice = 20000.00; final Double newPrice = 20000.00;
final ProductsEntity entity = productsService.get(1L); final ProductsEntity entity = productsService.get(1L);
final Double oldPrice = entity.getPrice(); 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(3, productsService.getAll().size());
Assertions.assertEquals(newEntity, productsService.get(1L)); Assertions.assertEquals(newEntity, productsService.get(1L));
Assertions.assertEquals(newPrice, newEntity.getPrice()); Assertions.assertEquals(newPrice, newEntity.getPrice());

View File

@ -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());
}
}

View File

@ -49,9 +49,9 @@ class UsersServiceTests {
@Test @Test
@Order(3) @Order(3)
void deleteTest() { void deleteTest() {
usersService.delete(1L); usersService.delete(2L);
Assertions.assertEquals(1, usersService.getAll().size()); Assertions.assertEquals(1, usersService.getAll().size());
final UsersEntity last = usersService.get(0L); final UsersEntity last = usersService.get(1L);
Assertions.assertEquals(1L, last.getId()); Assertions.assertEquals(1L, last.getId());
} }
} }