11package com .somemore .volunteerapply .service ;
22
3- import static com .somemore .common .fixture .RecruitBoardFixture .createCompletedRecruitBoard ;
4- import static com .somemore .common .fixture .RecruitBoardFixture .createRecruitBoard ;
5- import static com .somemore .global .exception .ExceptionMessage .UNAUTHORIZED_RECRUIT_BOARD ;
6- import static com .somemore .recruitboard .domain .VolunteerCategory .OTHER ;
7- import static com .somemore .volunteerapply .domain .ApplyStatus .APPROVED ;
8- import static com .somemore .volunteerapply .domain .ApplyStatus .WAITING ;
9- import static org .assertj .core .api .Assertions .assertThat ;
10- import static org .assertj .core .api .Assertions .assertThatThrownBy ;
11-
123import com .somemore .IntegrationTestSupport ;
4+ import com .somemore .global .common .event .ServerEventPublisher ;
135import com .somemore .global .exception .BadRequestException ;
146import com .somemore .global .exception .ExceptionMessage ;
157import com .somemore .recruitboard .domain .RecruitBoard ;
168import com .somemore .recruitboard .repository .RecruitBoardRepository ;
9+ import com .somemore .recruitboard .usecase .query .RecruitBoardQueryUseCase ;
1710import com .somemore .volunteerapply .domain .VolunteerApply ;
11+ import com .somemore .volunteerapply .event .VolunteerApplyStatusChangeEvent ;
1812import com .somemore .volunteerapply .repository .VolunteerApplyRepository ;
19- import java . util . UUID ;
13+ import org . junit . jupiter . api . BeforeEach ;
2014import org .junit .jupiter .api .DisplayName ;
2115import org .junit .jupiter .api .Test ;
16+ import org .mockito .ArgumentCaptor ;
2217import org .springframework .beans .factory .annotation .Autowired ;
2318import org .springframework .transaction .annotation .Transactional ;
2419
20+ import java .util .UUID ;
21+
22+ import static com .somemore .common .fixture .RecruitBoardFixture .createCompletedRecruitBoard ;
23+ import static com .somemore .common .fixture .RecruitBoardFixture .createRecruitBoard ;
24+ import static com .somemore .global .exception .ExceptionMessage .UNAUTHORIZED_RECRUIT_BOARD ;
25+ import static com .somemore .recruitboard .domain .VolunteerCategory .OTHER ;
26+ import static com .somemore .volunteerapply .domain .ApplyStatus .APPROVED ;
27+ import static com .somemore .volunteerapply .domain .ApplyStatus .WAITING ;
28+ import static org .assertj .core .api .Assertions .assertThat ;
29+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
30+ import static org .mockito .ArgumentMatchers .any ;
31+ import static org .mockito .Mockito .mock ;
32+ import static org .mockito .Mockito .never ;
33+ import static org .mockito .Mockito .times ;
34+ import static org .mockito .Mockito .verify ;
35+
2536@ Transactional
26- class ApproveVolunteerApplyServiceTest extends IntegrationTestSupport {
37+ class ApproveVolunteerApplyStatusChangeServiceTest extends IntegrationTestSupport {
2738
28- @ Autowired
29- private ApproveVolunteerApplyService approveVolunteerApplyService ;
39+ private VolunteerApplyStatusChangeService volunteerApplyStatusChangeService ;
3040
3141 @ Autowired
3242 private VolunteerApplyRepository volunteerApplyRepository ;
3343
3444 @ Autowired
3545 private RecruitBoardRepository recruitBoardRepository ;
3646
47+ @ Autowired
48+ private RecruitBoardQueryUseCase recruitBoardQueryUseCase ;
49+
50+
51+ ServerEventPublisher serverEventPublisher ;
52+
53+ @ BeforeEach
54+ void setUp () {
55+ serverEventPublisher = mock (ServerEventPublisher .class );
56+ volunteerApplyStatusChangeService = new VolunteerApplyStatusChangeService (
57+ volunteerApplyRepository ,
58+ recruitBoardQueryUseCase ,
59+ serverEventPublisher
60+ );
61+ }
62+
3763 @ DisplayName ("봉사 지원을 승인할 수 있다." )
3864 @ Test
3965 void approve () {
@@ -47,7 +73,7 @@ void approve() {
4773 volunteerApplyRepository .save (apply );
4874
4975 // when
50- approveVolunteerApplyService .approve (apply .getId (), centerId );
76+ volunteerApplyStatusChangeService .approve (apply .getId (), centerId );
5177
5278 // then
5379 VolunteerApply approve = volunteerApplyRepository .findById (apply .getId ()).orElseThrow ();
@@ -71,7 +97,7 @@ void approveWithWrongCenter() {
7197 // when
7298 // then
7399 assertThatThrownBy (
74- () -> approveVolunteerApplyService .approve (id , wrongCenterId )
100+ () -> volunteerApplyStatusChangeService .approve (id , wrongCenterId )
75101 ).isInstanceOf (BadRequestException .class )
76102 .hasMessage (UNAUTHORIZED_RECRUIT_BOARD .getMessage ());
77103 }
@@ -91,11 +117,57 @@ void approveWithAlreadyCompletedRecruit() {
91117 // when
92118 // then
93119 assertThatThrownBy (
94- () -> approveVolunteerApplyService .approve (id , centerId )
120+ () -> volunteerApplyStatusChangeService .approve (id , centerId )
95121 ).isInstanceOf (BadRequestException .class )
96122 .hasMessage (ExceptionMessage .RECRUIT_BOARD_ALREADY_COMPLETED .getMessage ());
97123 }
98124
125+ @ DisplayName ("지원 상태가 변경되지 않은 경우 이벤트 퍼블리셔가 호출되지 않는다." )
126+ @ Test
127+ void approveWithSameStatusDoesNotPublishEvent () {
128+ // given
129+ UUID centerId = UUID .randomUUID ();
130+
131+ RecruitBoard board = createRecruitBoard (centerId );
132+ recruitBoardRepository .save (board );
133+
134+ VolunteerApply apply = createApply (board .getId ());
135+ apply .changeStatus (APPROVED );
136+ volunteerApplyRepository .save (apply );
137+
138+ // when
139+ volunteerApplyStatusChangeService .approve (apply .getId (), centerId );
140+
141+ // then
142+ verify (serverEventPublisher , never ()).publish (any ());
143+ VolunteerApply approvedApply = volunteerApplyRepository .findById (apply .getId ()).orElseThrow ();
144+ assertThat (approvedApply .getStatus ()).isEqualTo (APPROVED );
145+ }
146+
147+ @ DisplayName ("지원 상태가 변경된 경우 이벤트 퍼블리셔가 호출된다." )
148+ @ Test
149+ void approveWithDifferentStatusPublishesEvent () {
150+ // given
151+ UUID centerId = UUID .randomUUID ();
152+
153+ RecruitBoard board = createRecruitBoard (centerId );
154+ recruitBoardRepository .save (board );
155+
156+ VolunteerApply apply = createApply (board .getId ());
157+ volunteerApplyRepository .save (apply );
158+
159+ // when
160+ volunteerApplyStatusChangeService .approve (apply .getId (), centerId );
161+
162+ // then
163+ ArgumentCaptor <VolunteerApplyStatusChangeEvent > eventCaptor = ArgumentCaptor .forClass (VolunteerApplyStatusChangeEvent .class );
164+ verify (serverEventPublisher , times (1 )).publish (eventCaptor .capture ());
165+
166+ VolunteerApplyStatusChangeEvent capturedEvent = eventCaptor .getValue ();
167+ assertThat (capturedEvent ).isNotNull ();
168+ assertThat (capturedEvent .getVolunteerApplyId ()).isEqualTo (apply .getId ());
169+ }
170+
99171 private VolunteerApply createApply (Long recruitBoardId ) {
100172 return VolunteerApply .builder ()
101173 .volunteerId (UUID .randomUUID ())
@@ -104,5 +176,4 @@ private VolunteerApply createApply(Long recruitBoardId) {
104176 .attended (false )
105177 .build ();
106178 }
107-
108179}
0 commit comments