Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into fix-tests-for-transactions
  • Loading branch information
amechkarov committed Mar 17, 2024
2 parents 822910a + 85e863f commit 7f44cfb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/test/java/com/telerikacademy/web/virtualwallet/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.telerikacademy.web.virtualwallet.models.*;
import com.telerikacademy.web.virtualwallet.models.wallets.Wallet;
import com.telerikacademy.web.virtualwallet.utils.UserRole;

import java.time.LocalDateTime;
import java.util.HashSet;
Expand All @@ -10,8 +11,6 @@ public class Helpers {

public static User createMockUser() {
var mockUser = new User();
Role role = new Role();
role.setRoleType("admin");
mockUser.setId(1);
mockUser.setUsername("MockUsername");
mockUser.setPassword("MockPassword");
Expand All @@ -20,7 +19,7 @@ public static User createMockUser() {
mockUser.setEmail("[email protected]");
mockUser.setPhoneNumber("123456789123");
mockUser.setUserRoles(new HashSet<>());
mockUser.getUserRoles().add(role);
mockUser.getUserRoles().add(createMockAdminRole());
mockUser.setUserCards(new HashSet<>());
mockUser.setWallets(new HashSet<>());
mockUser.getWallets().add(new Wallet());
Expand Down Expand Up @@ -58,6 +57,13 @@ public static Card createMockCard() {
return mockCard;
}

public static Role createMockAdminRole(){
var mockRole = new Role();
mockRole.setRoleId(1);
mockRole.setRoleType("admin");
return mockRole;
}

public static ProfilePhoto createMockProfilePhoto() {
var profilePhoto = new ProfilePhoto();
profilePhoto.setUser(createMockUser());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.telerikacademy.web.virtualwallet.services;

import com.cloudinary.Cloudinary;
import com.cloudinary.Uploader;
import com.cloudinary.utils.ObjectUtils;
import com.telerikacademy.web.virtualwallet.Helpers;
import com.telerikacademy.web.virtualwallet.config.CloudinaryConfig;
import com.telerikacademy.web.virtualwallet.exceptions.EntityDuplicateException;
import com.telerikacademy.web.virtualwallet.exceptions.EntityNotFoundException;
import com.telerikacademy.web.virtualwallet.filters.UserFilterOptions;
import com.telerikacademy.web.virtualwallet.models.Role;
import com.telerikacademy.web.virtualwallet.models.User;
import com.telerikacademy.web.virtualwallet.models.wallets.Wallet;
import com.telerikacademy.web.virtualwallet.repositories.contracts.ProfilePhotoRepository;
import com.telerikacademy.web.virtualwallet.repositories.contracts.UserRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -15,6 +21,8 @@
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.*;

import static com.telerikacademy.web.virtualwallet.Helpers.createMockUser;

@ExtendWith(MockitoExtension.class)
Expand All @@ -29,16 +37,25 @@ public class UserServiceImplTests {
@Mock
UserRepository mockUserRepository;

@Mock
private Cloudinary cloudinary;

@Mock
private Uploader uploader;

@Mock
private ProfilePhotoRepository profilePhotoRepository;

@Test
void getAll_Should_CallRepository(){
//Arrange, Act
Mockito.when(mockUserRepository.getAllUsersFiltered(new UserFilterOptions())).thenReturn(null);
User user = createMockUser();
mockUserService.getAll(new UserFilterOptions()
,user);
User testUser = createMockUser();
UserFilterOptions userFilterOptions = new UserFilterOptions("testUsername","[email protected]","1234567890");
Mockito.when(mockUserRepository.getAllUsersFiltered(userFilterOptions)).thenReturn(new ArrayList<>());
mockUserService.getAll(userFilterOptions,testUser);

//Assert
Mockito.verify(mockUserRepository,Mockito.times(1)).getAllUsersFiltered(new UserFilterOptions());
Mockito.verify(mockUserRepository,Mockito.times(1)).getAllUsersFiltered(userFilterOptions);
}

@Test
Expand Down Expand Up @@ -131,7 +148,7 @@ void create_Should_CallRepository_When_UserWithSameUsernameOrEmailDoesNotExist()
User user = createMockUser();
Mockito.when(mockUserRepository.getByField("username",user.getUsername())).thenThrow(EntityNotFoundException.class);
Mockito.when(mockUserRepository.getByField("email",user.getEmail())).thenThrow(EntityNotFoundException.class);

Mockito.when(cloudinary.uploader()).thenReturn(uploader);
//Act
mockUserService.create(user);

Expand All @@ -147,7 +164,7 @@ void create_Should_CallWalletService_When_UserWithSameUsernameOrEmailDoesNotExis
User user = createMockUser();
Mockito.when(mockUserRepository.getByField("username",user.getUsername())).thenThrow(EntityNotFoundException.class);
Mockito.when(mockUserRepository.getByField("email",user.getEmail())).thenThrow(EntityNotFoundException.class);

Mockito.when(cloudinary.uploader()).thenReturn(uploader);
//Act
mockUserService.create(user);

Expand Down

0 comments on commit 7f44cfb

Please sign in to comment.