|
| 1 | +package com.outfitlab.project.infrastructure.repositories; |
| 2 | + |
| 3 | +import com.outfitlab.project.domain.exceptions.FavoritesException; |
| 4 | +import com.outfitlab.project.domain.exceptions.UserCombinationFavoriteNotFoundException; |
| 5 | +import com.outfitlab.project.domain.exceptions.UserNotFoundException; |
| 6 | +import com.outfitlab.project.domain.model.UserCombinationFavoriteModel; |
| 7 | +import com.outfitlab.project.domain.model.dto.PageDTO; |
| 8 | +import com.outfitlab.project.infrastructure.model.UserCombinationFavoriteEntity; |
| 9 | +import com.outfitlab.project.infrastructure.model.UserEntity; |
| 10 | +import com.outfitlab.project.infrastructure.repositories.interfaces.UserCombinationFavoriteJpaRepository; |
| 11 | +import com.outfitlab.project.infrastructure.repositories.interfaces.UserJpaRepository; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.mockito.InjectMocks; |
| 14 | +import org.mockito.Mock; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest; |
| 16 | +import org.springframework.data.domain.Page; |
| 17 | +import org.springframework.data.domain.PageImpl; |
| 18 | +import org.springframework.data.domain.PageRequest; |
| 19 | +import org.springframework.test.context.ActiveProfiles; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.*; |
| 24 | + |
| 25 | +import static org.mockito.Mockito.*; |
| 26 | + |
| 27 | +@SpringBootTest |
| 28 | +@ActiveProfiles("test") |
| 29 | +class UserCombinationFavoriteRepositoryImplTest { |
| 30 | + @Mock |
| 31 | + private UserCombinationFavoriteJpaRepository favoriteJpaRepository; |
| 32 | + |
| 33 | + @Mock |
| 34 | + private UserJpaRepository userJpaRepository; |
| 35 | + |
| 36 | + @InjectMocks |
| 37 | + private UserCombinationFavoriteRepositoryImpl repository; |
| 38 | + |
| 39 | + private static final String EMAIL = "[email protected]"; |
| 40 | + private static final String URL = "https://combination.com/1"; |
| 41 | + |
| 42 | + @Test |
| 43 | + void shouldReturnFavoriteWhenExists() throws Exception { |
| 44 | + UserCombinationFavoriteEntity entity = givenFavoriteEntity(); |
| 45 | + givenFavoriteExists(URL, EMAIL, entity); |
| 46 | + |
| 47 | + UserCombinationFavoriteModel result = whenFindByCombinationAndEmail(); |
| 48 | + |
| 49 | + thenFavoriteShouldMatch(result, URL); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void shouldThrowExceptionWhenFavoriteDoesNotExist() { |
| 54 | + givenFavoriteDoesNotExist(URL, EMAIL); |
| 55 | + |
| 56 | + assertThatThrownBy(() -> whenFindByCombinationAndEmail()) |
| 57 | + .isInstanceOf(UserCombinationFavoriteNotFoundException.class) |
| 58 | + .hasMessageContaining(URL); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void shouldAddToFavoritesWhenUserExists() throws Exception { |
| 63 | + UserEntity user = givenUser(); |
| 64 | + givenUserExists(user); |
| 65 | + UserCombinationFavoriteEntity saved = givenSavedFavorite(user); |
| 66 | + |
| 67 | + givenFavoriteSaved(saved); |
| 68 | + |
| 69 | + UserCombinationFavoriteEntity result = whenAddToFavorites(); |
| 70 | + |
| 71 | + assertThat(result.getCombinationUrl()).isEqualTo(URL); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void shouldThrowExceptionWhenAddingFavoriteForNonExistingUser() { |
| 76 | + givenUserDoesNotExist(); |
| 77 | + |
| 78 | + assertThatThrownBy(() -> whenAddToFavorites()) |
| 79 | + .isInstanceOf(UserNotFoundException.class) |
| 80 | + .hasMessageContaining(EMAIL); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void shouldDeleteFavoriteWhenUserExists() throws Exception { |
| 85 | + UserEntity user = givenUser(); |
| 86 | + givenUserExists(user); |
| 87 | + |
| 88 | + UserCombinationFavoriteEntity favorite = givenFavoriteEntity(); |
| 89 | + givenFavoriteExists(URL, EMAIL, favorite); |
| 90 | + |
| 91 | + whenDeleteFavorite(); |
| 92 | + |
| 93 | + verify(favoriteJpaRepository).delete(favorite); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void shouldThrowExceptionWhenDeletingFavoriteWithNonExistingUser() { |
| 98 | + givenUserDoesNotExist(); |
| 99 | + |
| 100 | + assertThatThrownBy(this::whenDeleteFavorite) |
| 101 | + .isInstanceOf(UserNotFoundException.class); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void shouldReturnPageDTOWhenUserHasFavorites() throws Exception { |
| 106 | + UserEntity user = givenUser(); |
| 107 | + givenUserExists(user); |
| 108 | + |
| 109 | + Page<UserCombinationFavoriteEntity> page = givenFavoritePage(List.of(givenFavoriteEntity())); |
| 110 | + |
| 111 | + givenFavoritePageExists(page); |
| 112 | + |
| 113 | + PageDTO<UserCombinationFavoriteModel> result = whenGetFavoritesPage(); |
| 114 | + |
| 115 | + assertThat(result.getContent()).hasSize(1); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void shouldThrowExceptionWhenUserHasNoFavorites() throws Exception { |
| 120 | + UserEntity user = givenUser(); |
| 121 | + givenUserExists(user); |
| 122 | + |
| 123 | + givenFavoritePageExists(givenFavoritePage(List.of())); // empty page |
| 124 | + |
| 125 | + assertThatThrownBy(this::whenGetFavoritesPage) |
| 126 | + .isInstanceOf(FavoritesException.class); |
| 127 | + } |
| 128 | + |
| 129 | + private UserCombinationFavoriteEntity givenFavoriteEntity() { |
| 130 | + return new UserCombinationFavoriteEntity(new UserEntity(), URL); |
| 131 | + } |
| 132 | + |
| 133 | + private UserCombinationFavoriteEntity givenSavedFavorite(UserEntity user) { |
| 134 | + return new UserCombinationFavoriteEntity(user, URL); |
| 135 | + } |
| 136 | + |
| 137 | + private UserEntity givenUser() { |
| 138 | + UserEntity user = new UserEntity(); |
| 139 | + user.setEmail(EMAIL); |
| 140 | + return user; |
| 141 | + } |
| 142 | + |
| 143 | + private void givenUserExists(UserEntity user) { |
| 144 | + when(userJpaRepository.findByEmail(EMAIL)).thenReturn(user); |
| 145 | + } |
| 146 | + |
| 147 | + private void givenUserDoesNotExist() { |
| 148 | + when(userJpaRepository.findByEmail(EMAIL)).thenReturn(null); |
| 149 | + } |
| 150 | + |
| 151 | + private void givenFavoriteExists(String url, String email, UserCombinationFavoriteEntity entity) { |
| 152 | + when(favoriteJpaRepository.findBycombinationUrlAndUser_Email(url, email)) |
| 153 | + .thenReturn(entity); |
| 154 | + } |
| 155 | + |
| 156 | + private void givenFavoriteDoesNotExist(String url, String email) { |
| 157 | + when(favoriteJpaRepository.findBycombinationUrlAndUser_Email(url, email)) |
| 158 | + .thenReturn(null); |
| 159 | + } |
| 160 | + |
| 161 | + private void givenFavoriteSaved(UserCombinationFavoriteEntity saved) { |
| 162 | + when(favoriteJpaRepository.save(any())).thenReturn(saved); |
| 163 | + } |
| 164 | + |
| 165 | + private Page<UserCombinationFavoriteEntity> givenFavoritePage(List<UserCombinationFavoriteEntity> list) { |
| 166 | + return new PageImpl<>(list, PageRequest.of(0, 10), list.size()); |
| 167 | + } |
| 168 | + |
| 169 | + private void givenFavoritePageExists(Page<UserCombinationFavoriteEntity> page) { |
| 170 | + when(favoriteJpaRepository.findByUser_Email(eq(EMAIL), any(PageRequest.class))) |
| 171 | + .thenReturn(page); |
| 172 | + } |
| 173 | + |
| 174 | + private UserCombinationFavoriteModel whenFindByCombinationAndEmail() throws Exception { |
| 175 | + return repository.findByCombinationUrlAndUserEmail(URL, EMAIL); |
| 176 | + } |
| 177 | + |
| 178 | + private UserCombinationFavoriteEntity whenAddToFavorites() throws Exception { |
| 179 | + return repository.addToFavorite(URL, EMAIL); |
| 180 | + } |
| 181 | + |
| 182 | + private void whenDeleteFavorite() throws Exception { |
| 183 | + repository.deleteFromFavorites(URL, EMAIL); |
| 184 | + } |
| 185 | + |
| 186 | + private PageDTO<UserCombinationFavoriteModel> whenGetFavoritesPage() throws Exception { |
| 187 | + return repository.getCombinationFavoritesForUserByEmail(EMAIL, 0); |
| 188 | + } |
| 189 | + |
| 190 | + private void thenFavoriteShouldMatch(UserCombinationFavoriteModel model, String url) { |
| 191 | + assertThat(model.getCombinationUrl()).isEqualTo(url); |
| 192 | + } |
| 193 | +} |
0 commit comments