|
| 1 | +package com.semosan.api.domain.community.post.service; |
| 2 | + |
| 3 | +import com.semosan.api.common.exception.GeneralException; |
| 4 | +import com.semosan.api.common.status.ErrorStatus; |
| 5 | +import com.semosan.api.domain.community.post.entity.RecordPost; |
| 6 | +import com.semosan.api.domain.community.post.repository.RecordPostRepository; |
| 7 | +import com.semosan.api.domain.hiking.repository.HikingMemberRepository; |
| 8 | +import com.semosan.api.domain.hiking.repository.HikingRecordRepository; |
| 9 | +import com.semosan.api.domain.user.entity.User; |
| 10 | +import com.semosan.api.domain.user.enums.user.DeviceType; |
| 11 | +import com.semosan.api.domain.user.service.UserReader; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.mockito.InjectMocks; |
| 15 | +import org.mockito.Mock; |
| 16 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 17 | +import org.springframework.test.util.ReflectionTestUtils; |
| 18 | + |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +@ExtendWith(MockitoExtension.class) |
| 26 | +class RecordPostServiceTest { |
| 27 | + |
| 28 | + @Mock |
| 29 | + private RecordPostRepository recordPostRepository; |
| 30 | + |
| 31 | + @Mock |
| 32 | + private HikingRecordRepository hikingRecordRepository; |
| 33 | + |
| 34 | + @Mock |
| 35 | + private HikingMemberRepository hikingMemberRepository; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private UserReader userReader; |
| 39 | + |
| 40 | + @InjectMocks |
| 41 | + private RecordPostService recordPostService; |
| 42 | + |
| 43 | + @Test |
| 44 | + void deleteSoftDeletesWhenRequesterOwnsPost() { |
| 45 | + RecordPost post = recordPost(10L, user(2L, "author")); |
| 46 | + |
| 47 | + when(recordPostRepository.findById(10L)).thenReturn(Optional.of(post)); |
| 48 | + |
| 49 | + recordPostService.delete(10L, 2L); |
| 50 | + |
| 51 | + assertThat(post.isDeleted()).isTrue(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void deleteThrowsWhenRequesterDoesNotOwnPost() { |
| 56 | + RecordPost post = recordPost(10L, user(2L, "author")); |
| 57 | + |
| 58 | + when(recordPostRepository.findById(10L)).thenReturn(Optional.of(post)); |
| 59 | + |
| 60 | + assertThatThrownBy(() -> recordPostService.delete(10L, 3L)) |
| 61 | + .isInstanceOf(GeneralException.class) |
| 62 | + .extracting("errorStatus") |
| 63 | + .isEqualTo(ErrorStatus.POST_FORBIDDEN); |
| 64 | + assertThat(post.isDeleted()).isFalse(); |
| 65 | + } |
| 66 | + |
| 67 | + private User user(Long id, String nickname) { |
| 68 | + User user = User.createTestUser(nickname, DeviceType.IOS); |
| 69 | + ReflectionTestUtils.setField(user, "id", id); |
| 70 | + ReflectionTestUtils.setField(user, "nickname", nickname); |
| 71 | + return user; |
| 72 | + } |
| 73 | + |
| 74 | + private RecordPost recordPost(Long id, User author) { |
| 75 | + RecordPost post = RecordPost.create(author, "본문", null); |
| 76 | + ReflectionTestUtils.setField(post, "id", id); |
| 77 | + return post; |
| 78 | + } |
| 79 | +} |
0 commit comments