Skip to content

Commit 0854249

Browse files
committed
test(VolunteerStatusChangeEvent): 봉사상태변경이벤트 발생 검증 테스트 추가
- 이전 상태와 현 상태가 동일하다면 이벤트가 발생하지 않음.
1 parent 652033b commit 0854249

File tree

2 files changed

+167
-23
lines changed

2 files changed

+167
-23
lines changed

src/test/java/com/somemore/volunteerapply/service/ApproveVolunteerApplyServiceTest.java

Lines changed: 83 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
package 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-
123
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.global.common.event.ServerEventPublisher;
135
import com.somemore.global.exception.BadRequestException;
146
import com.somemore.global.exception.ExceptionMessage;
157
import com.somemore.recruitboard.domain.RecruitBoard;
168
import com.somemore.recruitboard.repository.RecruitBoardRepository;
9+
import com.somemore.recruitboard.usecase.query.RecruitBoardQueryUseCase;
1710
import com.somemore.volunteerapply.domain.VolunteerApply;
11+
import com.somemore.volunteerapply.event.VolunteerApplyStatusChangeEvent;
1812
import com.somemore.volunteerapply.repository.VolunteerApplyRepository;
19-
import java.util.UUID;
13+
import org.junit.jupiter.api.BeforeEach;
2014
import org.junit.jupiter.api.DisplayName;
2115
import org.junit.jupiter.api.Test;
16+
import org.mockito.ArgumentCaptor;
2217
import org.springframework.beans.factory.annotation.Autowired;
2318
import 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
2637
class ApproveVolunteerApplyServiceTest extends IntegrationTestSupport {
2738

28-
@Autowired
2939
private ApproveVolunteerApplyService approveVolunteerApplyService;
3040

3141
@Autowired
@@ -34,6 +44,22 @@ class ApproveVolunteerApplyServiceTest extends IntegrationTestSupport {
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+
approveVolunteerApplyService = new ApproveVolunteerApplyService(
57+
volunteerApplyRepository,
58+
recruitBoardQueryUseCase,
59+
serverEventPublisher
60+
);
61+
}
62+
3763
@DisplayName("봉사 지원을 승인할 수 있다.")
3864
@Test
3965
void approve() {
@@ -96,6 +122,52 @@ void approveWithAlreadyCompletedRecruit() {
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+
approveVolunteerApplyService.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+
approveVolunteerApplyService.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
}

src/test/java/com/somemore/volunteerapply/service/RejectVolunteerApplyServiceTest.java

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
11
package 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.REJECTED;
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-
123
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.global.common.event.ServerEventPublisher;
135
import com.somemore.global.exception.BadRequestException;
146
import com.somemore.global.exception.ExceptionMessage;
157
import com.somemore.recruitboard.domain.RecruitBoard;
168
import com.somemore.recruitboard.repository.RecruitBoardRepository;
9+
import com.somemore.recruitboard.usecase.query.RecruitBoardQueryUseCase;
1710
import com.somemore.volunteerapply.domain.VolunteerApply;
11+
import com.somemore.volunteerapply.event.VolunteerApplyStatusChangeEvent;
1812
import com.somemore.volunteerapply.repository.VolunteerApplyRepository;
19-
import java.util.UUID;
13+
import org.junit.jupiter.api.BeforeEach;
2014
import org.junit.jupiter.api.DisplayName;
2115
import org.junit.jupiter.api.Test;
16+
import org.mockito.ArgumentCaptor;
2217
import org.springframework.beans.factory.annotation.Autowired;
2318
import 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.REJECTED;
28+
import static com.somemore.volunteerapply.domain.ApplyStatus.WAITING;
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
31+
import static org.mockito.ArgumentMatchers.any;
32+
import static org.mockito.Mockito.mock;
33+
import static org.mockito.Mockito.never;
34+
import static org.mockito.Mockito.times;
35+
import static org.mockito.Mockito.verify;
36+
2537
@Transactional
2638
class RejectVolunteerApplyServiceTest extends IntegrationTestSupport {
2739

28-
@Autowired
2940
private RejectVolunteerApplyService rejectVolunteerApplyService;
3041

3142
@Autowired
@@ -34,6 +45,22 @@ class RejectVolunteerApplyServiceTest extends IntegrationTestSupport {
3445
@Autowired
3546
private RecruitBoardRepository recruitBoardRepository;
3647

48+
@Autowired
49+
private RecruitBoardQueryUseCase recruitBoardQueryUseCase;
50+
51+
52+
ServerEventPublisher serverEventPublisher;
53+
54+
@BeforeEach
55+
void setUp() {
56+
serverEventPublisher = mock(ServerEventPublisher.class);
57+
rejectVolunteerApplyService = new RejectVolunteerApplyService(
58+
volunteerApplyRepository,
59+
recruitBoardQueryUseCase,
60+
serverEventPublisher
61+
);
62+
}
63+
3764
@DisplayName("봉사 지원을 거절할 수 있다.")
3865
@Test
3966
void reject() {
@@ -96,6 +123,52 @@ void rejectWithAlreadyCompletedRecruit() {
96123
.hasMessage(ExceptionMessage.RECRUIT_BOARD_ALREADY_COMPLETED.getMessage());
97124
}
98125

126+
@DisplayName("지원 상태가 변경되지 않은 경우 이벤트 퍼블리셔가 호출되지 않는다.")
127+
@Test
128+
void approveWithSameStatusDoesNotPublishEvent() {
129+
// given
130+
UUID centerId = UUID.randomUUID();
131+
132+
RecruitBoard board = createRecruitBoard(centerId);
133+
recruitBoardRepository.save(board);
134+
135+
VolunteerApply apply = createApply(board.getId());
136+
apply.changeStatus(REJECTED);
137+
volunteerApplyRepository.save(apply);
138+
139+
// when
140+
rejectVolunteerApplyService.reject(apply.getId(), centerId);
141+
142+
// then
143+
verify(serverEventPublisher, never()).publish(any());
144+
VolunteerApply approvedApply = volunteerApplyRepository.findById(apply.getId()).orElseThrow();
145+
assertThat(approvedApply.getStatus()).isEqualTo(REJECTED);
146+
}
147+
148+
@DisplayName("지원 상태가 변경된 경우 이벤트 퍼블리셔가 호출된다.")
149+
@Test
150+
void approveWithDifferentStatusPublishesEvent() {
151+
// given
152+
UUID centerId = UUID.randomUUID();
153+
154+
RecruitBoard board = createRecruitBoard(centerId);
155+
recruitBoardRepository.save(board);
156+
157+
VolunteerApply apply = createApply(board.getId());
158+
volunteerApplyRepository.save(apply);
159+
160+
// when
161+
rejectVolunteerApplyService.reject(apply.getId(), centerId);
162+
163+
// then
164+
ArgumentCaptor<VolunteerApplyStatusChangeEvent> eventCaptor = ArgumentCaptor.forClass(VolunteerApplyStatusChangeEvent.class);
165+
verify(serverEventPublisher, times(1)).publish(eventCaptor.capture());
166+
167+
VolunteerApplyStatusChangeEvent capturedEvent = eventCaptor.getValue();
168+
assertThat(capturedEvent).isNotNull();
169+
assertThat(capturedEvent.getVolunteerApplyId()).isEqualTo(apply.getId());
170+
}
171+
99172
private VolunteerApply createApply(Long recruitBoardId) {
100173
return VolunteerApply.builder()
101174
.volunteerId(UUID.randomUUID())

0 commit comments

Comments
 (0)