Skip to content

Commit 96b4d5a

Browse files
committed
test(VolunteerQueryService): 봉사자 기본/상세 정보 조회 테스트, 닉네임 조회 테스트 추가
- beforeEach 를 transactional로 변경
1 parent 8749fc1 commit 96b4d5a

File tree

1 file changed

+109
-17
lines changed

1 file changed

+109
-17
lines changed

src/test/java/com/somemore/volunteer/service/VolunteerQueryServiceTest.java

Lines changed: 109 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
import com.somemore.auth.oauth.OAuthProvider;
55
import com.somemore.global.exception.BadRequestException;
66
import com.somemore.volunteer.domain.Volunteer;
7+
import com.somemore.volunteer.domain.VolunteerDetail;
8+
import com.somemore.volunteer.dto.request.VolunteerRegisterRequestDto;
9+
import com.somemore.volunteer.dto.response.VolunteerResponseDto;
10+
import com.somemore.volunteer.repository.VolunteerDetailRepository;
711
import com.somemore.volunteer.repository.VolunteerRepository;
8-
import org.junit.jupiter.api.AfterEach;
912
import org.junit.jupiter.api.DisplayName;
1013
import org.junit.jupiter.api.Test;
1114
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.transaction.annotation.Transactional;
1216

1317
import java.util.UUID;
1418

1519
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_VOLUNTEER;
20+
import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_VOLUNTEER_DETAIL;
21+
import static com.somemore.volunteer.domain.Volunteer.createDefault;
1622
import static org.assertj.core.api.Assertions.assertThat;
1723
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1824

25+
@Transactional
1926
class VolunteerQueryServiceTest extends IntegrationTestSupport {
2027

2128
@Autowired
@@ -24,18 +31,18 @@ class VolunteerQueryServiceTest extends IntegrationTestSupport {
2431
@Autowired
2532
private VolunteerRepository volunteerRepository;
2633

27-
@AfterEach
28-
void tearDown() {
29-
volunteerRepository.deleteAllInBatch();
30-
}
34+
@Autowired
35+
private VolunteerDetailRepository volunteerDetailRepository;
36+
37+
final String oAuthId = "example-oauth-id";
38+
final OAuthProvider oAuthProvider = OAuthProvider.NAVER;
39+
3140

3241
@DisplayName("존재하는 OAuth ID로 봉사자 ID를 조회한다")
3342
@Test
34-
void findVolunteerId() {
43+
void getVolunteerIdByOAuthId() {
3544
// given
36-
String oAuthId = "example-oauth-id";
37-
Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId);
38-
45+
Volunteer volunteer = createDefault(oAuthProvider, oAuthId);
3946
volunteerRepository.save(volunteer);
4047

4148
// when
@@ -49,10 +56,7 @@ void findVolunteerId() {
4956

5057
@DisplayName("존재하지 않는 OAuth ID로 조회 시 예외를 던진다")
5158
@Test
52-
void throwExceptionWhenVolunteerNotFound() {
53-
// given
54-
String oAuthId = "non-existing-oauth-id";
55-
59+
void getVolunteerIdByNonExistOAuthId() {
5660
// when
5761
// then
5862
assertThatThrownBy(() -> volunteerQueryService.getVolunteerIdByOAuthId(oAuthId))
@@ -63,11 +67,8 @@ void throwExceptionWhenVolunteerNotFound() {
6367
@DisplayName("봉사자의 id로 nickname을 조회한다.")
6468
@Test
6569
void getNicknameById() {
66-
6770
//given
68-
String oAuthId = "example-oauth-id";
69-
Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId);
70-
71+
Volunteer volunteer = createDefault(oAuthProvider, oAuthId);
7172
volunteerRepository.save(volunteer);
7273

7374
//when
@@ -76,4 +77,95 @@ void getNicknameById() {
7677
//then
7778
assertThat(nickname).isEqualTo(volunteer.getNickname());
7879
}
80+
81+
@DisplayName("존재하지 않는 봉사자 ID로 닉네임 조회 시 예외를 던진다")
82+
@Test
83+
void throwExceptionWhenNicknameNotFound() {
84+
// given
85+
UUID volunteerId = UUID.randomUUID();
86+
87+
// when
88+
// then
89+
assertThatThrownBy(() -> volunteerQueryService.getNicknameById(volunteerId))
90+
.isInstanceOf(BadRequestException.class)
91+
.hasMessage(NOT_EXISTS_VOLUNTEER.getMessage());
92+
}
93+
94+
@DisplayName("내 프로필 조회 성공")
95+
@Test
96+
void getMyProfile() {
97+
// given
98+
Volunteer volunteer = createDefault(oAuthProvider, oAuthId);
99+
volunteerRepository.save(volunteer);
100+
UUID volunteerId = volunteer.getId();
101+
102+
VolunteerDetail volunteerDetail = createVolunteerDetail(volunteerId);
103+
volunteerDetailRepository.save(volunteerDetail);
104+
105+
// when
106+
VolunteerResponseDto response = volunteerQueryService.getMyProfile(volunteerId);
107+
108+
// then
109+
assertThat(response).isNotNull();
110+
assertThat(response.volunteerId()).isEqualTo(volunteerId.toString());
111+
assertThat(response.nickname()).isEqualTo(volunteer.getNickname());
112+
}
113+
114+
@DisplayName("봉사자 프로필 조회 성공")
115+
@Test
116+
void getVolunteerProfile() {
117+
// given
118+
Volunteer volunteer = createDefault(oAuthProvider, oAuthId);
119+
volunteerRepository.save(volunteer);
120+
UUID volunteerId = volunteer.getId();
121+
122+
VolunteerDetail volunteerDetail = createVolunteerDetail(volunteerId);
123+
volunteerDetailRepository.save(volunteerDetail);
124+
125+
// when
126+
VolunteerResponseDto response = volunteerQueryService.getVolunteerProfile(volunteerId);
127+
128+
// then
129+
assertThat(response).isNotNull();
130+
assertThat(response.volunteerId()).isEqualTo(volunteerId.toString());
131+
assertThat(response.nickname()).isEqualTo(volunteer.getNickname());
132+
assertThat(response.volunteerDetailResponseDto()).isNull();
133+
}
134+
135+
@DisplayName("권한이 없는 기관의 봉사자 상세 프로필 조회 실패")
136+
@Test
137+
void getVolunteerDetailedProfile() {
138+
// given
139+
UUID centerId = UUID.randomUUID();
140+
141+
Volunteer volunteer = createDefault(oAuthProvider, oAuthId);
142+
volunteerRepository.save(volunteer);
143+
UUID volunteerId = volunteer.getId();
144+
145+
VolunteerDetail volunteerDetail = createVolunteerDetail(volunteerId);
146+
volunteerDetailRepository.save(volunteerDetail);
147+
148+
// when
149+
// then
150+
assertThatThrownBy(() -> volunteerQueryService.getVolunteerDetailedProfile(volunteerId, centerId))
151+
.isInstanceOf(BadRequestException.class)
152+
.hasMessage(UNAUTHORIZED_VOLUNTEER_DETAIL.getMessage());
153+
}
154+
155+
private static VolunteerDetail createVolunteerDetail(UUID volunteerId) {
156+
157+
VolunteerRegisterRequestDto volunteerRegisterRequestDto =
158+
new VolunteerRegisterRequestDto(
159+
OAuthProvider.NAVER,
160+
"example-oauth-id",
161+
"making",
162+
163+
"male",
164+
"06-08",
165+
"1998",
166+
"010-1234-5678"
167+
);
168+
169+
return VolunteerDetail.of(volunteerRegisterRequestDto, volunteerId);
170+
}
79171
}

0 commit comments

Comments
 (0)