|
| 1 | +package com.joaov1ct0r.restful_api_users_java.modules.posts.services; |
| 2 | + |
| 3 | +import com.joaov1ct0r.restful_api_users_java.modules.domain.entities.ErrorLogEntity; |
| 4 | +import com.joaov1ct0r.restful_api_users_java.modules.domain.exceptions.BadRequestException; |
| 5 | +import com.joaov1ct0r.restful_api_users_java.modules.domain.exceptions.ForbiddenException; |
| 6 | +import com.joaov1ct0r.restful_api_users_java.modules.domain.repositories.ErrorLogsRepository; |
| 7 | +import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.PostDTO; |
| 8 | +import com.joaov1ct0r.restful_api_users_java.modules.posts.dtos.UpdatePostDTO; |
| 9 | +import com.joaov1ct0r.restful_api_users_java.modules.posts.entities.PostEntity; |
| 10 | +import com.joaov1ct0r.restful_api_users_java.modules.posts.repositories.PostRepository; |
| 11 | +import com.joaov1ct0r.restful_api_users_java.modules.users.entities.UserEntity; |
| 12 | +import com.joaov1ct0r.restful_api_users_java.modules.users.repositories.UserRepository; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 17 | +import org.mockito.InjectMocks; |
| 18 | +import org.mockito.Mock; |
| 19 | +import org.mockito.Mockito; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 22 | +import org.mockito.quality.Strictness; |
| 23 | +import java.time.LocalDateTime; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.UUID; |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | +import static org.mockito.Mockito.any; |
| 29 | + |
| 30 | +@ExtendWith(MockitoExtension.class) |
| 31 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 32 | +public class UpdatePostServiceTest { |
| 33 | + @InjectMocks |
| 34 | + private UpdatePostService sut; |
| 35 | + |
| 36 | + @Mock |
| 37 | + private PostRepository postRepository; |
| 38 | + |
| 39 | + @Mock |
| 40 | + private UserRepository userRepository; |
| 41 | + |
| 42 | + @Mock |
| 43 | + private ErrorLogsRepository errorLogsRepository; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + public void beforeEachSetUp() { |
| 47 | + Mockito.reset(this.postRepository); |
| 48 | + Mockito.reset(this.userRepository); |
| 49 | + Mockito.reset(this.errorLogsRepository); |
| 50 | + |
| 51 | + when( |
| 52 | + this.errorLogsRepository.save( |
| 53 | + any() |
| 54 | + ) |
| 55 | + ).thenReturn( |
| 56 | + new ErrorLogEntity( |
| 57 | + UUID.randomUUID(), |
| 58 | + UUID.randomUUID(), |
| 59 | + LocalDateTime.now(), |
| 60 | + 400, |
| 61 | + "any_description" |
| 62 | + ) |
| 63 | + ); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @DisplayName("Should not be able to update a post if user is not found") |
| 69 | + public void shouldNotBeAbleToUpdateAPostIfUserIsNotFound() { |
| 70 | + UUID userId = UUID.randomUUID(); |
| 71 | + UpdatePostDTO updatePostDTO = new UpdatePostDTO(); |
| 72 | + |
| 73 | + when(this.userRepository.findById(any())).thenReturn(Optional.empty()); |
| 74 | + |
| 75 | + try { |
| 76 | + this.sut.execute(updatePostDTO, userId); |
| 77 | + } catch(Exception e) { |
| 78 | + assertThat(e).isInstanceOf(BadRequestException.class); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + @DisplayName("Should not be able to update a post if a post is not found") |
| 84 | + public void shouldNotBeAbleToUpdateAPostIfPostIfNotFound() { |
| 85 | + UUID userId = UUID.randomUUID(); |
| 86 | + UpdatePostDTO updatePostDTO = new UpdatePostDTO(UUID.randomUUID().toString(), "any_content"); |
| 87 | + |
| 88 | + when(this.userRepository.findById(any())).thenReturn( |
| 89 | + Optional.of( |
| 90 | + new UserEntity( |
| 91 | + userId, |
| 92 | + "any_name", |
| 93 | + |
| 94 | + "any_username", |
| 95 | + "any_password", |
| 96 | + "any_photo_url", |
| 97 | + LocalDateTime.now(), |
| 98 | + null |
| 99 | + ) |
| 100 | + ) |
| 101 | + ); |
| 102 | + |
| 103 | + when(this.postRepository.findById(any())).thenReturn(Optional.empty()); |
| 104 | + |
| 105 | + try { |
| 106 | + this.sut.execute(updatePostDTO, userId); |
| 107 | + } catch(Exception e) { |
| 108 | + assertThat(e).isInstanceOf(BadRequestException.class); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @DisplayName("Should not be able to update a post if user is not owner of post") |
| 114 | + public void shouldNotBeAbleToUpdateAPostIfUserIsNotOwnerOfPost() { |
| 115 | + UUID userId = UUID.randomUUID(); |
| 116 | + UUID postId = UUID.randomUUID(); |
| 117 | + UpdatePostDTO updatePostDTO = new UpdatePostDTO(postId.toString(), "any_content"); |
| 118 | + |
| 119 | + when(this.userRepository.findById(any())).thenReturn( |
| 120 | + Optional.of( |
| 121 | + new UserEntity( |
| 122 | + userId, |
| 123 | + "any_name", |
| 124 | + |
| 125 | + "any_username", |
| 126 | + "any_password", |
| 127 | + "any_photo_url", |
| 128 | + LocalDateTime.now(), |
| 129 | + null |
| 130 | + ) |
| 131 | + ) |
| 132 | + ); |
| 133 | + |
| 134 | + when(this.postRepository.findById(any())).thenReturn(Optional.of( |
| 135 | + new PostEntity( |
| 136 | + postId, |
| 137 | + "any_other_content", |
| 138 | + LocalDateTime.now(), |
| 139 | + null, |
| 140 | + UUID.randomUUID() |
| 141 | + ) |
| 142 | + )); |
| 143 | + |
| 144 | + try { |
| 145 | + this.sut.execute(updatePostDTO, userId); |
| 146 | + } catch (Exception e) { |
| 147 | + assertThat(e).isInstanceOf(ForbiddenException.class); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + @DisplayName("Should be able to update a post") |
| 153 | + public void shouldBeAbleToUpdateAPost() { |
| 154 | + UUID userId = UUID.randomUUID(); |
| 155 | + UUID postId = UUID.randomUUID(); |
| 156 | + UpdatePostDTO updatePostDTO = new UpdatePostDTO(postId.toString(), "any_content"); |
| 157 | + |
| 158 | + when(this.userRepository.findById(any())).thenReturn( |
| 159 | + Optional.of( |
| 160 | + new UserEntity( |
| 161 | + userId, |
| 162 | + "any_name", |
| 163 | + |
| 164 | + "any_username", |
| 165 | + "any_password", |
| 166 | + "any_photo_url", |
| 167 | + LocalDateTime.now(), |
| 168 | + null |
| 169 | + ) |
| 170 | + ) |
| 171 | + ); |
| 172 | + |
| 173 | + when(this.postRepository.findById(any())).thenReturn(Optional.of( |
| 174 | + new PostEntity( |
| 175 | + postId, |
| 176 | + "any_other_content", |
| 177 | + LocalDateTime.now(), |
| 178 | + null, |
| 179 | + userId |
| 180 | + ) |
| 181 | + )); |
| 182 | + |
| 183 | + when(this.postRepository.save(any())).thenReturn( |
| 184 | + new PostEntity( |
| 185 | + postId, |
| 186 | + updatePostDTO.getContent(), |
| 187 | + LocalDateTime.now(), |
| 188 | + LocalDateTime.now(), |
| 189 | + userId |
| 190 | + ) |
| 191 | + ); |
| 192 | + |
| 193 | + PostDTO updatedPost = this.sut.execute(updatePostDTO, userId); |
| 194 | + |
| 195 | + assertThat(updatedPost.getUserWhoCreatedId()).isEqualTo(userId); |
| 196 | + } |
| 197 | +} |
0 commit comments