diff --git a/inventory-management/src/test/java/cz/muni/fi/pa165/controller/InventoryManagementAPITest.java b/inventory-management/src/test/java/cz/muni/fi/pa165/controller/InventoryManagementAPITest.java new file mode 100644 index 0000000000000000000000000000000000000000..b7a89a1d813c34638aa7d1b78e3286cc94c9f3c9 --- /dev/null +++ b/inventory-management/src/test/java/cz/muni/fi/pa165/controller/InventoryManagementAPITest.java @@ -0,0 +1,102 @@ +package cz.muni.fi.pa165.controller; + +import cz.muni.fi.pa165.Exceptions.ProductAlreadyExistsException; +import cz.muni.fi.pa165.facade.InventoryFacade; +import cz.muni.fi.pa165.model.DTO.GrapeDTO; +import cz.muni.fi.pa165.model.DTO.IngredientDTO; +import cz.muni.fi.pa165.model.DTO.ProductDTO; +import cz.muni.fi.pa165.model.DTO.TransactionDTO; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +class InventoryManagementAPITest { + + @Mock + private InventoryFacade inventoryFacade; + + @InjectMocks + private InventoryManagementAPI inventoryManagementAPI; + + private final ProductDTO testProduct = new ProductDTO("test", "test", 2); + private final GrapeDTO testGrape = new GrapeDTO(); + + private final IngredientDTO testIngredient = new IngredientDTO(); + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void getWineList() { + when(inventoryFacade.getWineList()).thenReturn(List.of(testProduct)); + + List<ProductDTO> result = inventoryManagementAPI.getWineList(); + + assertEquals(List.of(testProduct), result); + } + + @Test + void getWineById() { + when(inventoryFacade.getWineById(1L)).thenReturn(testProduct); + + ProductDTO result = inventoryManagementAPI.getWineById(1L); + + assertEquals(testProduct, result); + } + + @Test + void getWineByCode() { + when(inventoryFacade.getWineByCode("test-code")).thenReturn(testProduct); + + ProductDTO result = inventoryManagementAPI.getWineByCode("test-code"); + + assertEquals(testProduct, result); + } + + @Test + void getGrapeList() { + when(inventoryFacade.getGrapeList()).thenReturn(List.of(testGrape)); + + List<GrapeDTO> result = inventoryManagementAPI.getGrapeList(); + + assertEquals(List.of(testGrape), result); + } + + @Test + void getGrapeByCode() { + when(inventoryFacade.getGrapeByCode("test-code")).thenReturn(testGrape); + + GrapeDTO result = inventoryManagementAPI.getGrapeByCode("test-code"); + + assertEquals(testGrape, result); + } + + @Test + void getIngredientList() { + when(inventoryFacade.getIngredientList()).thenReturn(List.of(testIngredient)); + + List<IngredientDTO> result = inventoryManagementAPI.getIngredientList(); + + assertEquals(List.of(testIngredient), result); + } + + @Test + void getIngredientByName() { + when(inventoryFacade.getIngredientByName("test-name")).thenReturn(testIngredient); + + IngredientDTO result = inventoryManagementAPI.getIngredientByName("test-name"); + + assertEquals(testIngredient, result); + } +} \ No newline at end of file diff --git a/inventory-management/src/test/java/cz/muni/fi/pa165/facade/InventoryFacadeTest.java b/inventory-management/src/test/java/cz/muni/fi/pa165/facade/InventoryFacadeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..11ef8b64c74b74a1f456f27edca9e8e8697d90b6 --- /dev/null +++ b/inventory-management/src/test/java/cz/muni/fi/pa165/facade/InventoryFacadeTest.java @@ -0,0 +1,131 @@ +package cz.muni.fi.pa165.facade; + +import cz.muni.fi.pa165.model.DTO.GrapeDTO; +import cz.muni.fi.pa165.model.DTO.IngredientDTO; +import cz.muni.fi.pa165.model.DTO.ProductDTO; +import cz.muni.fi.pa165.model.DTO.TransactionDTO; +import cz.muni.fi.pa165.model.Grape; +import cz.muni.fi.pa165.model.Ingredient; +import cz.muni.fi.pa165.model.Product; +import cz.muni.fi.pa165.model.Transaction; +import cz.muni.fi.pa165.service.GrapeService; +import cz.muni.fi.pa165.service.IngredientService; +import cz.muni.fi.pa165.service.InventoryService; +import cz.muni.fi.pa165.service.Mapper; +import cz.muni.fi.pa165.service.ProductService; +import cz.muni.fi.pa165.service.TransactionService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +class InventoryFacadeTest { + + @Mock + private ProductService productService; + @Mock + private InventoryService inventoryService; + @Mock + private TransactionService transactionService; + @Mock + private GrapeService grapeService; + @Mock + private IngredientService ingredientService; + @Mock + private Mapper mapper; + @InjectMocks + private InventoryFacade inventoryFacade; + + private final ProductDTO testProductDTO = new ProductDTO(); + private final Product testProduct = new Product(); + + private final TransactionDTO testTransactionDTO = new TransactionDTO(); + private final Transaction testTransaction = new Transaction(); + private final GrapeDTO testGrapeDTO = new GrapeDTO(); + private final Grape testGrape = new Grape(); + private final IngredientDTO testIngredientDTO = new IngredientDTO(); + private final Ingredient testIngredient = new Ingredient(); + + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + when(mapper.mapProductDTOToProduct(testProductDTO)).thenReturn(testProduct); + when(mapper.mapProductToDTO(testProduct)).thenReturn(testProductDTO); + when(mapper.mapProductListToDTOList(List.of(testProduct))).thenReturn(List.of(testProductDTO)); + when(mapper.mapTransactionDtoToTransaction(testTransactionDTO)).thenReturn(testTransaction); + when(mapper.mapGrapeToDTO(testGrape)).thenReturn(testGrapeDTO); + when(mapper.mapGrapeDTOToGrape(testGrapeDTO)).thenReturn(testGrape); + when(mapper.mapIngredientDTOToIngredient(testIngredientDTO)).thenReturn(testIngredient); + when(mapper.mapIngredientToDTO(testIngredient)).thenReturn(testIngredientDTO); + } + + @Test + void getWineList() { + when(productService.getWineList()).thenReturn(List.of(testProduct)); + + List<ProductDTO> result = inventoryFacade.getWineList(); + + assertEquals(List.of(testProductDTO), result); + } + + @Test + void getWineById() { + when(productService.getWineById(1L)).thenReturn(testProduct); + + ProductDTO result = inventoryFacade.getWineById(1L); + + assertEquals(testProductDTO, result); + } + + @Test + void getWineByCode() { + when(productService.getProductByCode("test-code")).thenReturn(testProduct); + + ProductDTO result = inventoryFacade.getWineByCode("test-code"); + + assertEquals(testProductDTO, result); + } + + @Test + void getGrapeList() { + when(grapeService.getGrapeList()).thenReturn(List.of(testGrape)); + + List<GrapeDTO> result = inventoryFacade.getGrapeList(); + + assertEquals(List.of(testGrapeDTO), result); + } + + @Test + void getGrapeByCode() { + when(grapeService.getGrapeByCode("test-code")).thenReturn(testGrape); + + GrapeDTO result = inventoryFacade.getGrapeByCode("test-code"); + + assertEquals(testGrapeDTO, result); + } + + @Test + void getIngredientByName() { + when(ingredientService.getIngredientByName("test-name")).thenReturn(testIngredient); + + IngredientDTO result = inventoryFacade.getIngredientByName("test-name"); + + assertEquals(testIngredientDTO, result); + } + + @Test + void getIngredientList() { + when(ingredientService.getIngredientList()).thenReturn(List.of(testIngredient)); + + List<IngredientDTO> result = inventoryFacade.getIngredientList(); + + assertEquals(List.of(testIngredientDTO), result); + } +} \ No newline at end of file diff --git a/inventory-management/src/test/java/cz/muni/fi/pa165/service/CustomerServiceTest.java b/inventory-management/src/test/java/cz/muni/fi/pa165/service/CustomerServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..b1364e5039c3d77e2a78f76543a153b7fab54883 --- /dev/null +++ b/inventory-management/src/test/java/cz/muni/fi/pa165/service/CustomerServiceTest.java @@ -0,0 +1,35 @@ +package cz.muni.fi.pa165.service; + +import cz.muni.fi.pa165.model.Customer; +import cz.muni.fi.pa165.repository.CustomerRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; + +class CustomerServiceTest { + @Mock + private CustomerRepository customerRepository; + @InjectMocks + private CustomerService customerService; + + private final Customer testCustomer = new Customer(); + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void getCustomerByExternalId() { + when(customerRepository.findByExternalId(1L)).thenReturn(testCustomer); + + Customer result = customerService.getCustomerByExternalId(1L); + + assertEquals(testCustomer, result); + } +} \ No newline at end of file diff --git a/inventory-management/src/test/java/cz/muni/fi/pa165/service/GrapeServiceTest.java b/inventory-management/src/test/java/cz/muni/fi/pa165/service/GrapeServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6ef2c3eca273c2bb7f82614ad2e4e0fa7323f56b --- /dev/null +++ b/inventory-management/src/test/java/cz/muni/fi/pa165/service/GrapeServiceTest.java @@ -0,0 +1,95 @@ +package cz.muni.fi.pa165.service; + +import cz.muni.fi.pa165.model.Grape; +import cz.muni.fi.pa165.repository.GrapeRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class GrapeServiceTest { + + @Mock + private GrapeRepository grapeRepository; + + @InjectMocks + private GrapeService grapeService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testGetGrapeList() { + Grape grape1 = new Grape(); + Grape grape2 = new Grape(); + List<Grape> grapeList = new ArrayList<>(); + grapeList.add(grape1); + grapeList.add(grape2); + + when(grapeRepository.findAll()).thenReturn(grapeList); + + List<Grape> result = grapeService.getGrapeList(); + + verify(grapeRepository, times(1)).findAll(); + assertEquals(2, result.size()); + assertEquals(grape1, result.get(0)); + assertEquals(grape2, result.get(1)); + } + + @Test + public void testGetGrapeByCode() { + String grapeCode = "GRAPE001"; + Grape grape = new Grape(); + grape.setCode(grapeCode); + + when(grapeRepository.findByCode(grapeCode)).thenReturn(grape); + + Grape result = grapeService.getGrapeByCode(grapeCode); + + verify(grapeRepository, times(1)).findByCode(grapeCode); + assertEquals(grape, result); + } + + @Test + public void testRestockGrape() { + String grapeCode = "GRAPE001"; + Grape grape = new Grape(); + grape.setCode(grapeCode); + grape.setQuantityAvailable(10); + + Grape existingGrape = new Grape(); + existingGrape.setCode(grapeCode); + existingGrape.setQuantityAvailable(5); + + when(grapeRepository.findByCode(grapeCode)).thenReturn(existingGrape); + + grapeService.restockGrape(grape); + + verify(grapeRepository, times(1)).findByCode(grapeCode); + verify(grapeRepository, times(1)).save(existingGrape); + assertEquals(15, existingGrape.getQuantityAvailable()); // 5 (initial quantity) + 10 (restocked quantity) + } + + @Test + public void testCreateGrape_Success() { + String grapeCode = "GRAPE001"; + Grape grape = new Grape(); + grape.setCode(grapeCode); + + when(grapeRepository.findByCode(grapeCode)).thenReturn(null); + + grapeService.createGrape(grape); + + verify(grapeRepository, times(1)).findByCode(grapeCode); + verify(grapeRepository, times(1)).save(grape); + } +} \ No newline at end of file diff --git a/inventory-management/src/test/java/cz/muni/fi/pa165/service/IngredientServiceTest.java b/inventory-management/src/test/java/cz/muni/fi/pa165/service/IngredientServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f9c5b5ab0acd3b10e58054030d95cc7b5e6ef99a --- /dev/null +++ b/inventory-management/src/test/java/cz/muni/fi/pa165/service/IngredientServiceTest.java @@ -0,0 +1,76 @@ +package cz.muni.fi.pa165.service; + +import cz.muni.fi.pa165.model.Ingredient; +import cz.muni.fi.pa165.repository.IngredientRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class IngredientServiceTest { + + @Mock + private IngredientRepository ingredientRepository; + + @InjectMocks + private IngredientService ingredientService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testGetIngredientByName() { + Ingredient ingredient = new Ingredient(); + ingredient.setName("Salt"); + + when(ingredientRepository.findByName("Salt")).thenReturn(ingredient); + + Ingredient result = ingredientService.getIngredientByName("Salt"); + + verify(ingredientRepository, times(1)).findByName("Salt"); + assertEquals("Salt", result.getName()); + } + + @Test + public void testGetIngredientList() { + Ingredient salt = new Ingredient(); + salt.setName("Salt"); + + Ingredient sugar = new Ingredient(); + sugar.setName("Sugar"); + + List<Ingredient> ingredients = List.of(salt, sugar); + + when(ingredientRepository.findAll()).thenReturn(ingredients); + + List<Ingredient> result = ingredientService.getIngredientList(); + + verify(ingredientRepository, times(1)).findAll(); + assertEquals(2, result.size()); + assertEquals("Salt", result.get(0).getName()); + assertEquals("Sugar", result.get(1).getName()); + } + + @Test + public void testRestockIngredient() { + Ingredient salt = new Ingredient(); + salt.setName("Salt"); + salt.setQuantity(100); + + when(ingredientRepository.findByName("Salt")).thenReturn(salt); + + ingredientService.restockIngredient(salt); + + verify(ingredientRepository, times(1)).findByName("Salt"); + verify(ingredientRepository, times(1)).save(salt); + assertEquals(200, salt.getQuantity()); + } +} \ No newline at end of file diff --git a/inventory-management/src/test/java/cz/muni/fi/pa165/service/InventoryServiceTest.java b/inventory-management/src/test/java/cz/muni/fi/pa165/service/InventoryServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2fa31e91bac900adc9cdbfd6ee0869d0df9cc9a1 --- /dev/null +++ b/inventory-management/src/test/java/cz/muni/fi/pa165/service/InventoryServiceTest.java @@ -0,0 +1,76 @@ +package cz.muni.fi.pa165.service; + +import cz.muni.fi.pa165.Exceptions.ProductNotFoundException; +import cz.muni.fi.pa165.model.Enums; +import cz.muni.fi.pa165.model.InventoryItem; +import cz.muni.fi.pa165.model.Product; +import cz.muni.fi.pa165.model.Transaction; +import cz.muni.fi.pa165.repository.InventoryItemRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class InventoryServiceTest { + + @Mock + private InventoryItemRepository inventoryRepository; + + @Mock + private ProductService productService; + + @InjectMocks + private InventoryService inventoryService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testRestockWine_Success() throws ProductNotFoundException { + String productCode = "WINE001"; + int quantity = 10; + Product product = new Product(); + product.setProductCode(productCode); + + InventoryItem inventoryItem = new InventoryItem(product, 5); + + when(productService.getProductByCode(productCode)).thenReturn(product); + when(inventoryRepository.findByProductProductCode(productCode)).thenReturn(inventoryItem); + + inventoryService.restockWine(productCode, quantity); + + verify(inventoryRepository, times(1)).findByProductProductCode(productCode); + verify(inventoryRepository, times(1)).save(inventoryItem); + assertEquals(15, inventoryItem.getQuantity()); // 5 (initial quantity) + 10 (restocked quantity) + } + + @Test + public void testCommitTransaction_Sale() { + String productCode = "WINE001"; + int initialQuantity = 10; + int saleQuantity = 5; + Product product = new Product(); + product.setProductCode(productCode); + + InventoryItem inventoryItem = new InventoryItem(product, initialQuantity); + + Transaction saleTransaction = new Transaction(); + saleTransaction.setProduct(product); + saleTransaction.setTransactionType(Enums.TransactionType.Sale); + saleTransaction.setQuantity(saleQuantity); + + when(inventoryRepository.findByProductProductCode(productCode)).thenReturn(inventoryItem); + + inventoryService.commitTransaction(saleTransaction); + + verify(inventoryRepository, times(1)).findByProductProductCode(productCode); + verify(inventoryRepository, times(1)).save(inventoryItem); + assertEquals(initialQuantity - saleQuantity, inventoryItem.getQuantity()); // 10 (initial quantity) - 5 (sale quantity) + } +} \ No newline at end of file diff --git a/inventory-management/src/test/java/cz/muni/fi/pa165/service/ProductServiceTest.java b/inventory-management/src/test/java/cz/muni/fi/pa165/service/ProductServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6a4d185a86e88dd6ea5543e254e8ef00e8e13d7c --- /dev/null +++ b/inventory-management/src/test/java/cz/muni/fi/pa165/service/ProductServiceTest.java @@ -0,0 +1,109 @@ +package cz.muni.fi.pa165.service; + +import cz.muni.fi.pa165.Exceptions.ProductAlreadyExistsException; +import cz.muni.fi.pa165.model.Product; +import cz.muni.fi.pa165.repository.ProductRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.time.Year; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class ProductServiceTest { + + @Mock + private ProductRepository productRepository; + + @InjectMocks + private ProductService productService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testGetWineList() { + Product wine1 = new Product(); + Product wine2 = new Product(); + List<Product> wineList = new ArrayList<>(); + wineList.add(wine1); + wineList.add(wine2); + + when(productRepository.findAll()).thenReturn(wineList); + + List<Product> result = productService.getWineList(); + + verify(productRepository, times(1)).findAll(); + assertEquals(2, result.size()); + assertEquals(wine1, result.get(0)); + assertEquals(wine2, result.get(1)); + } + + @Test + public void testGetWineById() { + long wineId = 1L; + Product wine = new Product(); + wine.setId(wineId); + + when(productRepository.findById(wineId)).thenReturn(Optional.of(wine)); + + Product result = productService.getWineById(wineId); + + verify(productRepository, times(1)).findById(wineId); + assertEquals(wine, result); + } + + @Test + public void testAddWine_Success() throws ProductAlreadyExistsException { + String productCode = "WINE001"; + Product wine = new Product(); + wine.setProductCode(productCode); + + when(productRepository.findByProductCode(productCode)).thenReturn(null); + + productService.addWine(wine); + + verify(productRepository, times(1)).findByProductCode(productCode); + verify(productRepository, times(1)).save(wine); + assertEquals(Year.now().getValue(), wine.getVintageYear()); + } + + @Test + public void testAddWine_ProductAlreadyExists() { + String productCode = "WINE001"; + Product wine = new Product(); + wine.setProductCode(productCode); + + when(productRepository.findByProductCode(productCode)).thenReturn(wine); + + assertThrows(ProductAlreadyExistsException.class, () -> { + productService.addWine(wine); + }); + + verify(productRepository, times(1)).findByProductCode(productCode); + verify(productRepository, never()).save(wine); + } + + @Test + public void testGetProductByCode() { + String productCode = "WINE001"; + Product wine = new Product(); + wine.setProductCode(productCode); + + when(productRepository.findByProductCode(productCode)).thenReturn(wine); + + Product result = productService.getProductByCode(productCode); + + verify(productRepository, times(1)).findByProductCode(productCode); + assertEquals(wine, result); + } +} \ No newline at end of file diff --git a/inventory-management/src/test/java/cz/muni/fi/pa165/service/TransactionServiceTest.java b/inventory-management/src/test/java/cz/muni/fi/pa165/service/TransactionServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f1090b98d7cf31b163f54de55d58fa9f1d68a918 --- /dev/null +++ b/inventory-management/src/test/java/cz/muni/fi/pa165/service/TransactionServiceTest.java @@ -0,0 +1,44 @@ +package cz.muni.fi.pa165.service; + +import cz.muni.fi.pa165.model.Enums; +import cz.muni.fi.pa165.model.Transaction; +import cz.muni.fi.pa165.repository.TransactionRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +class TransactionServiceTest { + + @Mock + private InventoryService inventoryService; + + @Mock + private TransactionRepository transactionRepository; + + @InjectMocks + private TransactionService transactionService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testCreateTransaction() { + Transaction transaction = new Transaction(); + transaction.setTransactionType(Enums.TransactionType.Purchase); + transaction.setQuantity(10); + + transactionService.createTransaction(transaction); + + assertNotNull(transaction.getTimestamp()); + verify(transactionRepository, times(1)).save(transaction); + verify(inventoryService, times(1)).commitTransaction(transaction); + } +} \ No newline at end of file