Skip to content

Commit

Permalink
Merge pull request #56 from Integradorl-UdeA/test
Browse files Browse the repository at this point in the history
add test for loan and return loan
  • Loading branch information
superpollo2 authored Jun 7, 2024
2 parents 07d97df + 220c3a1 commit 9ec2bed
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/consola/lis/service/LoanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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);
}
Expand Down
143 changes: 143 additions & 0 deletions src/test/java/com/consola/lis/service/LoanServiceTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
55 changes: 55 additions & 0 deletions src/test/java/com/consola/lis/service/ReturnLoanServiceTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
48 changes: 46 additions & 2 deletions src/test/java/com/consola/lis/service/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -52,7 +54,6 @@ void testGetUser_UserNotExists() {
}



@Test
void testDeleteUser_UserExist() {
String username = "existingUser";
Expand All @@ -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));
}
}

}

0 comments on commit 9ec2bed

Please sign in to comment.