|
| 1 | +package com.Rootin.domain.til.service; |
| 2 | + |
| 3 | +import com.Rootin.domain.ai.repository.AiResultTilRepository; |
| 4 | +import com.Rootin.domain.garden.entity.Pot; |
| 5 | +import com.Rootin.domain.garden.repository.PotRepository; |
| 6 | +import com.Rootin.domain.garden.repository.WateringLogRepository; |
| 7 | +import com.Rootin.domain.garden.service.ExperienceService; |
| 8 | +import com.Rootin.domain.til.entity.Til; |
| 9 | +import com.Rootin.domain.til.repository.TagRepository; |
| 10 | +import com.Rootin.domain.til.repository.TilRepository; |
| 11 | +import com.Rootin.domain.user.entity.User; |
| 12 | +import com.Rootin.domain.user.repository.UserRepository; |
| 13 | +import com.Rootin.global.exception.CustomException; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.DisplayName; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 18 | +import org.mockito.InjectMocks; |
| 19 | +import org.mockito.Mock; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | +import org.springframework.http.HttpStatus; |
| 22 | +import org.springframework.test.util.ReflectionTestUtils; |
| 23 | + |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; |
| 27 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 28 | +import static org.mockito.BDDMockito.given; |
| 29 | +import static org.mockito.Mockito.*; |
| 30 | + |
| 31 | +@ExtendWith(MockitoExtension.class) |
| 32 | +class TilServiceTest { |
| 33 | + |
| 34 | + @InjectMocks |
| 35 | + private TilService tilService; |
| 36 | + |
| 37 | + @Mock |
| 38 | + private TilRepository tilRepository; |
| 39 | + |
| 40 | + @Mock |
| 41 | + private TagRepository tagRepository; |
| 42 | + |
| 43 | + @Mock |
| 44 | + private UserRepository userRepository; |
| 45 | + |
| 46 | + @Mock |
| 47 | + private PotRepository potRepository; |
| 48 | + |
| 49 | + @Mock |
| 50 | + private ExperienceService experienceService; |
| 51 | + |
| 52 | + @Mock |
| 53 | + private AiResultTilRepository aiResultTilRepository; |
| 54 | + |
| 55 | + @Mock |
| 56 | + private WateringLogRepository wateringLogRepository; |
| 57 | + |
| 58 | + private User owner; |
| 59 | + private Til til; |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setUp() { |
| 63 | + owner = new User(); |
| 64 | + ReflectionTestUtils.setField(owner, "id", 1L); |
| 65 | + |
| 66 | + Pot pot = Pot.builder() |
| 67 | + .userId(1L) |
| 68 | + .title("테스트 화분") |
| 69 | + .level(1) |
| 70 | + .totalExp(0) |
| 71 | + .build(); |
| 72 | + ReflectionTestUtils.setField(pot, "id", 10L); |
| 73 | + |
| 74 | + til = new Til(); |
| 75 | + ReflectionTestUtils.setField(til, "id", 100L); |
| 76 | + ReflectionTestUtils.setField(til, "user", owner); |
| 77 | + ReflectionTestUtils.setField(til, "pot", pot); |
| 78 | + } |
| 79 | + |
| 80 | + // ─── delete() ──────────────────────────────────────────────────── |
| 81 | + |
| 82 | + @Test |
| 83 | + @DisplayName("정상 삭제 — aiResultTil·wateringLog 정리 후 til 삭제, 삭제 순서 보장") |
| 84 | + void delete_success_removesRelatedRecordsInOrder() { |
| 85 | + given(tilRepository.findById(100L)).willReturn(Optional.of(til)); |
| 86 | + |
| 87 | + tilService.delete(100L, 1L); |
| 88 | + |
| 89 | + // aiResultTil → wateringLog → til 순서 검증 |
| 90 | + var inOrder = inOrder(aiResultTilRepository, wateringLogRepository, tilRepository); |
| 91 | + inOrder.verify(aiResultTilRepository).deleteByTilId(100L); |
| 92 | + inOrder.verify(wateringLogRepository).deleteByPostId(100L); |
| 93 | + inOrder.verify(tilRepository).delete(til); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @DisplayName("타인 TIL 삭제 시도 → 403") |
| 98 | + void delete_forbidden_when_not_owner() { |
| 99 | + given(tilRepository.findById(100L)).willReturn(Optional.of(til)); |
| 100 | + |
| 101 | + assertThatThrownBy(() -> tilService.delete(100L, 2L)) |
| 102 | + .isInstanceOf(CustomException.class) |
| 103 | + .satisfies(e -> assertThat(((CustomException) e).getStatus()).isEqualTo(HttpStatus.FORBIDDEN)); |
| 104 | + |
| 105 | + verifyNoInteractions(aiResultTilRepository, wateringLogRepository); |
| 106 | + verify(tilRepository, never()).delete(any()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @DisplayName("존재하지 않는 tilId 삭제 → 404") |
| 111 | + void delete_notFound_when_til_not_exists() { |
| 112 | + given(tilRepository.findById(999L)).willReturn(Optional.empty()); |
| 113 | + |
| 114 | + assertThatThrownBy(() -> tilService.delete(999L, 1L)) |
| 115 | + .isInstanceOf(CustomException.class) |
| 116 | + .satisfies(e -> assertThat(((CustomException) e).getStatus()).isEqualTo(HttpStatus.NOT_FOUND)); |
| 117 | + |
| 118 | + verifyNoInteractions(aiResultTilRepository, wateringLogRepository); |
| 119 | + verify(tilRepository, never()).delete(any()); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments