1+ package io .crops .warmletter .domain .letter .service ;
2+
3+ import io .crops .warmletter .domain .letter .entity .Letter ;
4+ import io .crops .warmletter .domain .letter .enums .*;
5+ import io .crops .warmletter .domain .letter .repository .LetterRepository ;
6+ import io .crops .warmletter .domain .timeline .dto .request .NotificationRequest ;
7+ import io .crops .warmletter .domain .timeline .enums .AlarmType ;
8+ import org .junit .jupiter .api .DisplayName ;
9+ import org .junit .jupiter .api .Test ;
10+ import org .junit .jupiter .api .extension .ExtendWith ;
11+ import org .mockito .ArgumentCaptor ;
12+ import org .mockito .Captor ;
13+ import org .mockito .InjectMocks ;
14+ import org .mockito .Mock ;
15+ import org .mockito .junit .jupiter .MockitoExtension ;
16+ import org .springframework .context .ApplicationEventPublisher ;
17+ import org .springframework .test .util .ReflectionTestUtils ;
18+
19+ import static org .assertj .core .api .Assertions .assertThat ;
20+ import static org .mockito .ArgumentMatchers .any ;
21+ import static org .mockito .Mockito .*;
22+
23+ @ ExtendWith (MockitoExtension .class )
24+ class LetterProcessingServiceTest {
25+
26+ @ Mock
27+ private LetterRepository letterRepository ;
28+
29+ @ Mock
30+ private ApplicationEventPublisher notificationPublisher ;
31+
32+ @ InjectMocks
33+ private LetterProcessingService letterProcessingService ;
34+
35+ @ Captor
36+ private ArgumentCaptor <NotificationRequest > notificationCaptor ;
37+
38+ private final String ZIP_CODE = "12345" ;
39+ private final Long TEST_RECEIVER_ID = 100L ;
40+ private final Long TEST_WRITER_ID = 200L ;
41+ private final Long TEST_LETTER_ID = 300L ;
42+
43+ private Letter createTestLetter () {
44+ // Builder 패턴을 사용하여 Letter 객체 생성
45+ Letter letter = Letter .builder ()
46+ .writerId (TEST_WRITER_ID )
47+ .receiverId (TEST_RECEIVER_ID )
48+ .parentLetterId (null )
49+ .letterType (LetterType .DIRECT )
50+ .category (Category .ETC )
51+ .title ("테스트 편지" )
52+ .content ("테스트 내용입니다." )
53+ .status (Status .IN_DELIVERY )
54+ .fontType (FontType .DEFAULT )
55+ .paperType (PaperType .PAPER )
56+ .matchingId (null )
57+ .build ();
58+
59+ ReflectionTestUtils .setField (letter , "id" , TEST_LETTER_ID );
60+
61+ return letter ;
62+ }
63+
64+ @ Test
65+ @ DisplayName ("편지 배송 완료 처리 - 상태 업데이트 및 저장 검증" )
66+ void processDeliveryCompletion_ShouldUpdateStatusAndSave () {
67+ // Given
68+ Letter testLetter = createTestLetter ();
69+
70+ // When
71+ letterProcessingService .processDeliveryCompletion (testLetter , ZIP_CODE );
72+
73+ // Then
74+ assertThat (testLetter .getStatus ()).isEqualTo (Status .DELIVERED );
75+ verify (letterRepository , times (1 )).save (testLetter );
76+ }
77+
78+ @ Test
79+ @ DisplayName ("편지 배송 완료 처리 - 알림 전송 검증" )
80+ void processDeliveryCompletion_ShouldSendNotification () {
81+ // Given
82+ Letter testLetter = createTestLetter ();
83+
84+ // When
85+ letterProcessingService .processDeliveryCompletion (testLetter , ZIP_CODE );
86+
87+ // Then
88+ verify (notificationPublisher , times (1 )).publishEvent (notificationCaptor .capture ());
89+
90+ NotificationRequest capturedRequest = notificationCaptor .getValue ();
91+ assertThat (capturedRequest .getSenderZipCode ()).isEqualTo (ZIP_CODE );
92+ assertThat (capturedRequest .getReceiverId ()).isEqualTo (TEST_RECEIVER_ID );
93+ assertThat (capturedRequest .getAlarmType ()).isEqualTo (AlarmType .LETTER );
94+ assertThat (capturedRequest .getData ()).isEqualTo (TEST_LETTER_ID .toString ());
95+ }
96+
97+ @ Test
98+ @ DisplayName ("편지 배송 완료 처리 - 수신자 ID가 null인 경우 알림 미전송" )
99+ void processDeliveryCompletion_WhenReceiverIdIsNull_ShouldNotSendNotification () {
100+ // Given
101+ Letter testLetter = Letter .builder ()
102+ .writerId (TEST_WRITER_ID )
103+ .receiverId (null ) // 수신자 ID를 null로 설정
104+ .title ("테스트 편지" )
105+ .content ("테스트 내용입니다." )
106+ .status (Status .IN_DELIVERY )
107+ .build ();
108+ ReflectionTestUtils .setField (testLetter , "id" , TEST_LETTER_ID );
109+
110+ // When
111+ letterProcessingService .processDeliveryCompletion (testLetter , ZIP_CODE );
112+
113+ // Then
114+ verify (letterRepository , times (1 )).save (testLetter );
115+ verify (notificationPublisher , never ()).publishEvent (any ());
116+ }
117+
118+ @ Test
119+ @ DisplayName ("편지 배송 완료 처리 - 발신자 우편번호가 null인 경우 알림 미전송" )
120+ void processDeliveryCompletion_WhenSenderZipCodeIsNull_ShouldNotSendNotification () {
121+ // Given
122+ Letter testLetter = createTestLetter ();
123+
124+ // When
125+ letterProcessingService .processDeliveryCompletion (testLetter , null );
126+
127+ // Then
128+ verify (letterRepository , times (1 )).save (testLetter );
129+ verify (notificationPublisher , never ()).publishEvent (any ());
130+ }
131+ }
0 commit comments