SHARE
TWEET

card

a guest Feb 17th, 2020 137 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.team16.virtualwallet.services;
  2.  
  3. import com.team16.virtualwallet.exceptions.DuplicateEntityException;
  4. import com.team16.virtualwallet.exceptions.EntityNotFoundException;
  5. import com.team16.virtualwallet.models.Card;
  6. import com.team16.virtualwallet.models.DTOs.CardDTO;
  7. import com.team16.virtualwallet.models.User;
  8. import com.team16.virtualwallet.models.mappers.CardMapper;
  9. import com.team16.virtualwallet.repositories.CardRepository;
  10. import com.team16.virtualwallet.repositories.UserRepository;
  11. import com.team16.virtualwallet.services.contracts.UserService;
  12. import org.junit.Assert;
  13. import org.junit.Test;
  14. import org.junit.jupiter.api.Assertions;
  15. import org.junit.runner.RunWith;
  16. import org.mockito.InjectMocks;
  17. import org.mockito.Mock;
  18. import org.mockito.Mockito;
  19. import org.mockito.junit.MockitoJUnitRunner;
  20.  
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Optional;
  24.  
  25. import static com.team16.virtualwallet.Factory.*;
  26. import static org.mockito.ArgumentMatchers.*;
  27. import static org.mockito.Mockito.*;
  28.  
  29. @RunWith(MockitoJUnitRunner.class)
  30. public class CardServiceImplTests {
  31.  
  32.     @Mock
  33.     CardRepository mockCardRepository;
  34.  
  35.     @Mock
  36.     UserRepository mockUserRepository;
  37.  
  38.  
  39.     @Mock
  40.     UserService mockUserService;
  41.  
  42.     @Mock
  43.     CardMapper mockCardMapper;
  44.  
  45.  
  46.     @InjectMocks
  47.     CardServiceImpl mockCardService;
  48.  
  49.     @Test(expected = EntityNotFoundException.class)
  50.     public void findByIdAndOwnerShouldThrow_WhenCardDoestNotExist() {
  51.         //Act
  52.         mockCardService.findByIdAndOwner(anyInt(), any(User.class));
  53.     }
  54.  
  55.  
  56.     @Test
  57.     public void createCardShould_CallRepository() {
  58.         CardDTO cardDto = createCardDto();
  59.         User user = createUser();
  60.  
  61.  
  62.         when(mockUserRepository.findByUsernameAndEnabledIsTrue(cardDto.getCardOwner())).thenReturn(Optional.of(user));
  63.  
  64.         mockCardService.create(cardDto);
  65.  
  66.         verify(mockCardRepository, times(1)).save(mockCardMapper.mapToCard(cardDto));
  67.     }
  68.  
  69.     @Test
  70.     public void updateCardShould_CallRepository() {
  71.         Card card = createCard();
  72.         CardDTO cardDto = createCardDto();
  73.  
  74.         when(mockCardRepository.findById(anyInt())).thenReturn(card);
  75.  
  76.         mockCardService.update(anyInt(), card.getCardNumber(), cardDto);
  77.  
  78.         verify(mockCardRepository, times(1)).save(card);
  79.     }
  80.  
  81.     @Test
  82.     public void findByIdShould_CallRepository() {
  83.         //Arrange
  84.         Card card = createCard();
  85.  
  86.         Mockito.when(mockCardRepository.findById(anyInt())).thenReturn(card);
  87.  
  88.         //Act
  89.         mockCardService.findById(anyInt());
  90.  
  91.         //Assert
  92.         Mockito.verify(mockCardRepository,
  93.                 times(1)).findById(anyInt());
  94.     }
  95.  
  96.  
  97.     @Test
  98.     public void findAllByCardOwnerShould_CallRepository() {
  99.         //Arrange
  100.         User user = createUser();
  101.  
  102.         Mockito.when(mockCardRepository.findAllByCardOwnerAndDeletedIsFalse(user)).thenReturn(user.getUserCards());
  103.  
  104.         //Act
  105.         mockCardService.findAllByOwner(user);
  106.  
  107.         //Assert
  108.         Mockito.verify(mockCardRepository,
  109.                 times(1)).findAllByCardOwnerAndDeletedIsFalse(user);
  110.     }
  111.  
  112.     @Test(expected = EntityNotFoundException.class)
  113.     public void findByCardNumberAndCardNumberShouldThrow_EntityNotFoundException() {
  114.         //Arrange
  115.         Card card = createCard();
  116.         User cardOwner = card.getCardOwner();
  117.  
  118.  
  119.         when(mockCardService.findByCardNumberAndCardOwner(card.getCardNumber(), card.getCardOwner().getUsername())).thenReturn(card);
  120.         //Act
  121.         mockCardService.findByCardNumberAndCardOwner(card.getCardNumber(), card.getCardOwner().getUsername());
  122.         //Assert
  123.         verify(mockCardRepository, timeout(1)).findByCardNumberAndCardOwnerAndDeletedIsFalse(card.getCardNumber(), card.getCardOwner());
  124.     }
  125.  
  126.     @Test(expected = EntityNotFoundException.class)
  127.     public void deleteCardOwnerAndCardNumberShould_CallRepository() {
  128.         //Arrange
  129.         Card card = createCard();
  130.         User owner = createUser();
  131.         owner.getUserCards().add(card);
  132.         card.setCardOwner(owner);
  133.  
  134.  
  135.         when(mockUserRepository.findByUsernameAndEnabledIsTrue(anyString())).thenReturn(Optional.of(owner));
  136.         when(mockCardRepository.findByCardNumberAndCardOwnerAndDeletedIsFalse(card.getCardNumber(),owner))
  137.                 .thenReturn(Optional.of(card));
  138.         //Act
  139.         mockCardService.deleteCardByCardOwnerAndCardNumber(card.getCardNumber(), owner.getUsername());
  140.         //Assert
  141.         verify(mockCardRepository, times(1)).save(card);
  142.     }
  143.  
  144. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top