From 220c3a1bd5d68b9940888b02594578d169fa1ce8 Mon Sep 17 00:00:00 2001
From: superpollo2 <laura.tascon@udea.edu.co>
Date: Thu, 6 Jun 2024 21:01:38 -0500
Subject: [PATCH] add test for loan and return loan

---
 .../com/consola/lis/service/LoanService.java  |   8 +-
 .../consola/lis/service/LoanServiceTest.java  | 143 ++++++++++++++++++
 .../lis/service/ReturnLoanServiceTest.java    |  55 +++++++
 .../consola/lis/service/UserServiceTest.java  |  48 +++++-
 4 files changed, 248 insertions(+), 6 deletions(-)
 create mode 100644 src/test/java/com/consola/lis/service/LoanServiceTest.java
 create mode 100644 src/test/java/com/consola/lis/service/ReturnLoanServiceTest.java

diff --git a/src/main/java/com/consola/lis/service/LoanService.java b/src/main/java/com/consola/lis/service/LoanService.java
index 2d13eff..3c0695e 100644
--- a/src/main/java/com/consola/lis/service/LoanService.java
+++ b/src/main/java/com/consola/lis/service/LoanService.java
@@ -40,7 +40,7 @@ public Loan createLoan(LoanDTO loanRequest) {
         return loanRepository.save(loan);
     }
 
-    private void validateLoanRequest(LoanDTO loanRequest) {
+    public void validateLoanRequest(LoanDTO loanRequest) {
         if (!inventoryItemService.existItem(loanRequest.getItemId())) {
             throw new AlreadyExistsException("409", HttpStatus.CONFLICT, "Item does not exist in inventory");
         }
@@ -56,17 +56,17 @@ private void validateLoanRequest(LoanDTO loanRequest) {
         }
     }
 
-    private void updateItemStateAndTotal(InventoryItem item, int quantity) {
+    public void updateItemStateAndTotal(InventoryItem item, int quantity) {
         inventoryItemService.changeStateNoQuantizableItem(item, ItemState.LENDED);
 
-        if (Boolean.TRUE.equals(item.getCategory().getQuantizable()) && item.getTotal() - quantity == 0) {
+        if (item.getCategory() != null &&Boolean.TRUE.equals(item.getCategory().getQuantizable()) && item.getTotal() - quantity == 0) {
             inventoryItemService.updateInventoryItemState(item.getItemId(), ItemState.OUT_OF_STOCK);
         }
 
         inventoryItemService.updateInventoryItemTotal(item.getItemId(), -quantity);
     }
 
-    private Loan buildLoanFromRequest(LoanDTO loanRequest) {
+    public Loan buildLoanFromRequest(LoanDTO loanRequest) {
         if (loanRequest.getLoanType() == null) {
             loanRequest.setLoanType(LoanType.GENERAL);
         }
diff --git a/src/test/java/com/consola/lis/service/LoanServiceTest.java b/src/test/java/com/consola/lis/service/LoanServiceTest.java
new file mode 100644
index 0000000..fcdaf33
--- /dev/null
+++ b/src/test/java/com/consola/lis/service/LoanServiceTest.java
@@ -0,0 +1,143 @@
+package com.consola.lis.service;
+
+import com.consola.lis.dto.LoanDTO;
+import com.consola.lis.model.entity.InventoryItem;
+import com.consola.lis.model.entity.Loan;
+import com.consola.lis.model.repository.InventoryItemRepository;
+import com.consola.lis.model.repository.LoanRepository;
+import com.consola.lis.util.exception.IllegalParameterInRequest;
+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.data.domain.Page;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class LoanServiceTest {
+
+    @Mock
+    LoanRepository loanRepository;
+
+    @Mock
+    InventoryItemService inventoryItemService;
+
+    @Mock
+    InventoryItemRepository inventoryItemRepository;
+
+    @InjectMocks
+    LoanService loanService;
+
+    @BeforeEach
+    void setUp() {
+        MockitoAnnotations.openMocks(this);
+    }
+
+
+    @Test
+    void testCreateLoan_ItemNotLendable() {
+        LoanDTO loanRequest = new LoanDTO();
+        loanRequest.setItemId(String.valueOf(1));
+        loanRequest.setQuantity(1);
+
+        InventoryItem item = new InventoryItem();
+        item.setLendable(false);
+
+        when(inventoryItemService.existItem(loanRequest.getItemId())).thenReturn(true);
+        when(inventoryItemService.findInventoryItem(loanRequest.getItemId())).thenReturn(item);
+
+        assertThrows(IllegalParameterInRequest.class, () -> loanService.createLoan(loanRequest));
+    }
+
+    @Test
+    void testCreateLoan_QuantityGreaterThanStock() {
+        LoanDTO loanRequest = new LoanDTO();
+        loanRequest.setItemId(String.valueOf(1));
+        loanRequest.setQuantity(3);
+
+        InventoryItem item = new InventoryItem();
+        item.setTotal(2);
+        item.setLendable(true);
+
+        when(inventoryItemService.existItem(loanRequest.getItemId())).thenReturn(true);
+        when(inventoryItemService.findInventoryItem(loanRequest.getItemId())).thenReturn(item);
+
+        assertThrows(IllegalParameterInRequest.class, () -> loanService.createLoan(loanRequest));
+    }
+
+
+    // ... existing tests ...
+
+    @Test
+    void testDeleteLoan_LoanExists() {
+        int loanId = 1;
+        when(loanRepository.existsById(loanId)).thenReturn(true);
+
+        loanService.deleteLoan(loanId);
+
+        verify(loanRepository, times(1)).deleteById(loanId);
+    }
+
+    @Test
+    void testGetOneLoan_LoanExists() {
+        int loanId = 1;
+        Loan loan = new Loan();
+        when(loanRepository.findById(loanId)).thenReturn(Optional.of(loan));
+
+        Loan result = loanService.getOneLoan(loanId);
+
+        assertEquals(loan, result);
+        verify(loanRepository, times(1)).findById(loanId);
+    }
+
+    @Test
+    void testExistLoan_LoanExists() {
+        int loanId = 1;
+        when(loanRepository.existsById(loanId)).thenReturn(false);
+
+        boolean result = loanService.existLoan(loanId);
+
+        assertTrue(result);
+        verify(loanRepository, times(1)).existsById(loanId);
+    }
+
+    @Test
+    void testGetAllLoans() {
+        List<Loan> loans = new ArrayList<>();
+        when(loanRepository.findAll()).thenReturn(loans);
+
+        List<Loan> result = loanService.getAllLoans();
+
+        assertEquals(loans, result);
+        verify(loanRepository, times(1)).findAll();
+    }
+
+    @Test
+    void testGetAllLoansMapper() {
+        Page<Loan> page = mock(Page.class);
+        when(loanRepository.findAllLoansByState(any(), any())).thenReturn(page);
+
+        Map<String, Object> result = loanService.getAllLoansMapper(any(), any());
+
+        assertNotNull(result);
+        verify(loanRepository, times(1)).findAllLoansByState(any(), any());
+    }
+
+    @Test
+    void testUpdateReturnLoanState() {
+        int loanId = 1;
+        Loan loan = new Loan();
+        when(loanRepository.findById(loanId)).thenReturn(Optional.of(loan));
+
+        loanService.updateReturnLoanState(loanId, any());
+
+        verify(loanRepository, times(1)).save(loan);
+    }
+}
diff --git a/src/test/java/com/consola/lis/service/ReturnLoanServiceTest.java b/src/test/java/com/consola/lis/service/ReturnLoanServiceTest.java
new file mode 100644
index 0000000..0c6b4a5
--- /dev/null
+++ b/src/test/java/com/consola/lis/service/ReturnLoanServiceTest.java
@@ -0,0 +1,55 @@
+package com.consola.lis.service;
+
+import com.consola.lis.dto.ReturnLoanDTO;
+import com.consola.lis.model.repository.LoanRepository;
+import com.consola.lis.model.repository.ReturnLoanRepository;
+import com.consola.lis.util.exception.NotExistingException;
+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 ReturnLoanServiceTest {
+
+    @Mock
+    ReturnLoanRepository returnLoanRepository;
+
+    @Mock
+    LoanRepository loanRepository;
+
+    @Mock
+    LoanService loanService;
+
+    @Mock
+    InventoryItemService inventoryItemService;
+
+    @InjectMocks
+    ReturnLoanService returnLoanService;
+
+    @BeforeEach
+    void setUp() {
+        MockitoAnnotations.openMocks(this);
+    }
+
+
+    @Test
+    void createReturnLoan_WhenLoanDoesNotExist() {
+        ReturnLoanDTO returnLoanRequest = new ReturnLoanDTO();
+        returnLoanRequest.setLoanId(1);
+
+        when(loanService.existLoan(returnLoanRequest.getLoanId())).thenReturn(true);
+
+        assertThrows(NotExistingException.class, () -> returnLoanService.createReturnLoan(returnLoanRequest));
+    }
+
+    @Test
+    void getAllReturnsLoans_ReturnsAllLoans() {
+        returnLoanService.getAllReturnsLoans();
+
+        verify(returnLoanRepository, times(1)).findAll();
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/com/consola/lis/service/UserServiceTest.java b/src/test/java/com/consola/lis/service/UserServiceTest.java
index 5f313a8..18fe22e 100644
--- a/src/test/java/com/consola/lis/service/UserServiceTest.java
+++ b/src/test/java/com/consola/lis/service/UserServiceTest.java
@@ -1,5 +1,6 @@
 package com.consola.lis.service;
 
+import com.consola.lis.dto.UserLisDTO;
 import com.consola.lis.model.entity.UserLis;
 import com.consola.lis.model.repository.UserLisRepository;
 import com.consola.lis.util.exception.NotExistingException;
@@ -9,6 +10,7 @@
 import org.mockito.Mock;
 import org.junit.jupiter.api.Test;
 import org.mockito.MockitoAnnotations;
+import org.springframework.http.ResponseEntity;
 import org.springframework.security.crypto.password.PasswordEncoder;
 import org.springframework.web.client.RestTemplate;
 
@@ -52,7 +54,6 @@ void testGetUser_UserNotExists() {
     }
 
 
-
     @Test
     void testDeleteUser_UserExist() {
         String username = "existingUser";
@@ -74,5 +75,48 @@ void testDeleteUser_UserNotExist() {
 
     }
 
+    @Test
+    void testCheckExistUser_UserExistsInDatabase() {
+        String username = "existingUser";
+        when(userRepository.existsByUsername(username)).thenReturn(true);
+
+        Boolean result = userService.checkExistUser(username);
+
+        assertTrue(result);
+        verify(userRepository, times(1)).existsByUsername(username);
+        verify(restTemplate, never()).getForEntity(anyString(), any(), Optional.ofNullable(any()));
+    }
+
+    @Test
+    void testCheckExistUser_UserExistsInLdap() {
+        String username = "ldapUser";
+        when(userRepository.existsByUsername(username)).thenReturn(false);
+        UserLisDTO userLisDTO = new UserLisDTO();
+        userLisDTO.setIdUser("1");
+        userLisDTO.setUsername(username);
+        ResponseEntity<UserLisDTO> responseEntity = ResponseEntity.ok(userLisDTO);
+        when(restTemplate.getForEntity(anyString(), eq(UserLisDTO.class), eq(username))).thenReturn(responseEntity);
+
+        Boolean result = userService.checkExistUser(username);
+
+        assertTrue(result);
+        verify(userRepository, times(1)).existsByUsername(username);
+        verify(restTemplate, times(1)).getForEntity(anyString(), eq(UserLisDTO.class), eq(username));
+        verify(userRepository, times(1)).save(any(UserLis.class));
+    }
+
+    @Test
+    void testCheckExistUser_UserDoesNotExist() {
+        String username = "nonExistingUser";
+        when(userRepository.existsByUsername(username)).thenReturn(false);
+        when(restTemplate.getForEntity(anyString(), eq(UserLisDTO.class), eq(username))).thenReturn(ResponseEntity.ok(null));
+
+        Boolean result = userService.checkExistUser(username);
+
+        assertFalse(result);
+        verify(userRepository, times(1)).existsByUsername(username);
+        verify(restTemplate, times(1)).getForEntity(anyString(), eq(UserLisDTO.class), eq(username));
+        verify(userRepository, never()).save(any(UserLis.class));
+    }
+}
 
-}
\ No newline at end of file