|
| 1 | +package com.codeit.team5.mopl.binarycontent.service; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.ArgumentMatchers.eq; |
| 7 | +import static org.mockito.Mockito.never; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import com.codeit.team5.mopl.binarycontent.dto.UploadedBinaryContent; |
| 12 | +import com.codeit.team5.mopl.binarycontent.event.BinaryContentDeleteEvent; |
| 13 | +import com.codeit.team5.mopl.binarycontent.storage.StorageDirectory; |
| 14 | +import com.codeit.team5.mopl.global.dto.FileRequest; |
| 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.context.ApplicationEventPublisher; |
| 22 | + |
| 23 | +@ExtendWith(MockitoExtension.class) |
| 24 | +class UploadWithRollbackTest { |
| 25 | + |
| 26 | + @Mock |
| 27 | + private BinaryContentService binaryContentService; |
| 28 | + |
| 29 | + @Mock |
| 30 | + private ApplicationEventPublisher eventPublisher; |
| 31 | + |
| 32 | + @InjectMocks |
| 33 | + private UploadWithRollback uploadWithRollback; |
| 34 | + |
| 35 | + private final FileRequest image = new FileRequest(new byte[]{1, 2, 3}, "file.jpg"); |
| 36 | + private final UploadedBinaryContent uploaded = |
| 37 | + new UploadedBinaryContent("profiles/key.jpg", "http://localhost/profiles/key.jpg"); |
| 38 | + |
| 39 | + @Test |
| 40 | + @DisplayName("이미지가 없으면 업로드 없이 후속 작업 실행 성공") |
| 41 | + void execute_noImage_skipsUpload() { |
| 42 | + // when |
| 43 | + String result = uploadWithRollback.execute( |
| 44 | + StorageDirectory.PROFILE, null, u -> u == null ? "null" : "not-null"); |
| 45 | + |
| 46 | + // then |
| 47 | + assertThat(result).isEqualTo("null"); |
| 48 | + verify(binaryContentService, never()).uploadToStorage(any(), any()); |
| 49 | + verify(eventPublisher, never()).publishEvent(any()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + @DisplayName("업로드와 후속 작업 성공 시 이벤트 미발행 성공") |
| 54 | + void execute_success_noEvent() { |
| 55 | + // given |
| 56 | + when(binaryContentService.uploadToStorage(eq(StorageDirectory.PROFILE), any())).thenReturn(uploaded); |
| 57 | + |
| 58 | + // when |
| 59 | + String result = uploadWithRollback.execute( |
| 60 | + StorageDirectory.PROFILE, image, u -> "done:" + u.key()); |
| 61 | + |
| 62 | + // then |
| 63 | + assertThat(result).isEqualTo("done:profiles/key.jpg"); |
| 64 | + verify(eventPublisher, never()).publishEvent(any()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @DisplayName("후속 작업 실패 시 삭제 이벤트 발행 후 예외 전파 성공") |
| 69 | + void execute_persistFails_publishesDeleteEvent() { |
| 70 | + // given |
| 71 | + when(binaryContentService.uploadToStorage(eq(StorageDirectory.PROFILE), any())).thenReturn(uploaded); |
| 72 | + |
| 73 | + // when & then |
| 74 | + assertThatThrownBy(() -> uploadWithRollback.execute( |
| 75 | + StorageDirectory.PROFILE, image, u -> { |
| 76 | + throw new RuntimeException("persist fail"); |
| 77 | + })) |
| 78 | + .isInstanceOf(RuntimeException.class); |
| 79 | + |
| 80 | + verify(eventPublisher).publishEvent(new BinaryContentDeleteEvent(uploaded)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + @DisplayName("업로드 실패 시 이벤트 미발행하고 예외 전파 성공") |
| 85 | + void execute_uploadFails_noEvent() { |
| 86 | + // given |
| 87 | + when(binaryContentService.uploadToStorage(eq(StorageDirectory.PROFILE), any())) |
| 88 | + .thenThrow(new RuntimeException("upload fail")); |
| 89 | + |
| 90 | + // when & then |
| 91 | + assertThatThrownBy(() -> uploadWithRollback.execute( |
| 92 | + StorageDirectory.PROFILE, image, u -> "never")) |
| 93 | + .isInstanceOf(RuntimeException.class); |
| 94 | + |
| 95 | + verify(eventPublisher, never()).publishEvent(any()); |
| 96 | + } |
| 97 | +} |
0 commit comments