11package com .example .solidconnection .siteuser .service ;
22
33import static com .example .solidconnection .common .exception .ErrorCode .CAN_NOT_CHANGE_NICKNAME_YET ;
4- import static com .example .solidconnection .common .exception .ErrorCode .PASSWORD_MISMATCH ;
54import static com .example .solidconnection .siteuser .service .MyPageService .MIN_DAYS_BETWEEN_NICKNAME_CHANGES ;
65import static com .example .solidconnection .siteuser .service .MyPageService .NICKNAME_LAST_CHANGE_DATE_FORMAT ;
76import static org .assertj .core .api .Assertions .assertThat ;
8- import static org .assertj .core .api .Assertions .assertThatThrownBy ;
97import static org .assertj .core .api .AssertionsForClassTypes .assertThatCode ;
10- import static org .junit .jupiter .api .Assertions .assertAll ;
118import static org .mockito .BDDMockito .any ;
129import static org .mockito .BDDMockito .eq ;
1310import static org .mockito .BDDMockito .given ;
1411import static org .mockito .BDDMockito .never ;
1512import static org .mockito .BDDMockito .then ;
1613
1714import com .example .solidconnection .common .exception .CustomException ;
15+ import com .example .solidconnection .location .country .domain .Country ;
16+ import com .example .solidconnection .location .country .domain .InterestedCountry ;
17+ import com .example .solidconnection .location .country .fixture .CountryFixture ;
18+ import com .example .solidconnection .location .country .repository .InterestedCountryRepository ;
19+ import com .example .solidconnection .mentor .fixture .MentorFixture ;
1820import com .example .solidconnection .s3 .domain .ImgType ;
1921import com .example .solidconnection .s3 .dto .UploadedFileUrlResponse ;
2022import com .example .solidconnection .s3 .service .S3Service ;
2123import com .example .solidconnection .siteuser .domain .AuthType ;
2224import com .example .solidconnection .siteuser .domain .Role ;
2325import com .example .solidconnection .siteuser .domain .SiteUser ;
2426import com .example .solidconnection .siteuser .dto .MyPageResponse ;
25- import com .example .solidconnection .siteuser .dto .PasswordUpdateRequest ;
2627import com .example .solidconnection .siteuser .fixture .SiteUserFixture ;
2728import com .example .solidconnection .siteuser .fixture .SiteUserFixtureBuilder ;
2829import com .example .solidconnection .siteuser .repository .SiteUserRepository ;
2930import com .example .solidconnection .support .TestContainerSpringBootTest ;
3031import com .example .solidconnection .university .domain .LikedUnivApplyInfo ;
32+ import com .example .solidconnection .university .domain .University ;
3133import com .example .solidconnection .university .fixture .UnivApplyInfoFixture ;
3234import com .example .solidconnection .university .repository .LikedUnivApplyInfoRepository ;
3335import java .time .LocalDateTime ;
36+ import org .junit .jupiter .api .Assertions ;
3437import org .junit .jupiter .api .BeforeEach ;
3538import org .junit .jupiter .api .DisplayName ;
3639import org .junit .jupiter .api .Nested ;
@@ -56,9 +59,18 @@ class MyPageServiceTest {
5659 @ Autowired
5760 private LikedUnivApplyInfoRepository likedUnivApplyInfoRepository ;
5861
62+ @ Autowired
63+ private InterestedCountryRepository interestedCountryRepository ;
64+
5965 @ Autowired
6066 private SiteUserFixture siteUserFixture ;
6167
68+ @ Autowired
69+ private MentorFixture mentorFixture ;
70+
71+ @ Autowired
72+ private CountryFixture countryFixture ;
73+
6274 @ Autowired
6375 private UnivApplyInfoFixture univApplyInfoFixture ;
6476
@@ -76,25 +88,94 @@ void setUp() {
7688 }
7789
7890 @ Test
79- void 마이페이지_정보를_조회한다 () {
91+ void 멘티의_마이페이지_정보를_조회한다 () {
8092 // given
8193 int likedUnivApplyInfoCount = createLikedUnivApplyInfos (user );
94+ Country country = countryFixture .미국 ();
95+ InterestedCountry interestedCountry = new InterestedCountry (user , country );
96+ interestedCountryRepository .save (interestedCountry );
8297
8398 // when
8499 MyPageResponse response = myPageService .getMyPageInfo (user .getId ());
85100
86101 // then
87- assertAll (
102+ Assertions . assertAll (
88103 () -> assertThat (response .nickname ()).isEqualTo (user .getNickname ()),
89104 () -> assertThat (response .profileImageUrl ()).isEqualTo (user .getProfileImageUrl ()),
90105 () -> assertThat (response .role ()).isEqualTo (user .getRole ()),
91106 () -> assertThat (response .email ()).isEqualTo (user .getEmail ()),
92107 // () -> assertThat(response.likedPostCount()).isEqualTo(user.getLikedPostList().size()),
93108 // todo : 좋아요한 게시물 수 반환 기능 추가와 함께 수정요망
94- () -> assertThat (response .likedUnivApplyInfoCount ()).isEqualTo (likedUnivApplyInfoCount )
109+ () -> assertThat (response .likedUnivApplyInfoCount ()).isEqualTo (likedUnivApplyInfoCount ),
110+ () -> assertThat (response .interestedCountries ().get (0 )).isEqualTo (country .getKoreanName ()),
111+ () -> assertThat (response .attendedUniversity ()).isNull ()
95112 );
96113 }
97114
115+ @ Test
116+ void 멘토의_마이페이지_정보를_조회한다 () {
117+ // given
118+ SiteUser mentorUser = siteUserFixture .멘토 (1 , "mentor" );
119+ University university = univApplyInfoFixture .괌대학_A_지원_정보 ().getUniversity ();
120+ mentorFixture .멘토 (mentorUser .getId (), university .getId ());
121+ int likedUnivApplyInfoCount = createLikedUnivApplyInfos (mentorUser );
122+
123+ // when
124+ MyPageResponse response = myPageService .getMyPageInfo (mentorUser .getId ());
125+
126+ // then
127+ Assertions .assertAll (
128+ () -> assertThat (response .nickname ()).isEqualTo (mentorUser .getNickname ()),
129+ () -> assertThat (response .profileImageUrl ()).isEqualTo (mentorUser .getProfileImageUrl ()),
130+ () -> assertThat (response .role ()).isEqualTo (mentorUser .getRole ()),
131+ () -> assertThat (response .email ()).isEqualTo (mentorUser .getEmail ()),
132+ // () -> assertThat(response.likedPostCount()).isEqualTo(user.getLikedPostList().size()),
133+ // todo : 좋아요한 게시물 수 반환 기능 추가와 함께 수정요망
134+ () -> assertThat (response .likedUnivApplyInfoCount ()).isEqualTo (likedUnivApplyInfoCount ),
135+ () -> assertThat (response .attendedUniversity ()).isEqualTo (university .getKoreanName ()),
136+ () -> assertThat (response .interestedCountries ()).isNull ()
137+ );
138+ }
139+
140+ private int createLikedUnivApplyInfos (SiteUser testUser ) {
141+ LikedUnivApplyInfo likedUnivApplyInfo1 = new LikedUnivApplyInfo (null , univApplyInfoFixture .괌대학_A_지원_정보 ().getId (), testUser .getId ());
142+ LikedUnivApplyInfo likedUnivApplyInfo2 = new LikedUnivApplyInfo (null , univApplyInfoFixture .메이지대학_지원_정보 ().getId (), testUser .getId ());
143+ LikedUnivApplyInfo likedUnivApplyInfo3 = new LikedUnivApplyInfo (null , univApplyInfoFixture .코펜하겐IT대학_지원_정보 ().getId (), testUser .getId ());
144+
145+ likedUnivApplyInfoRepository .save (likedUnivApplyInfo1 );
146+ likedUnivApplyInfoRepository .save (likedUnivApplyInfo2 );
147+ likedUnivApplyInfoRepository .save (likedUnivApplyInfo3 );
148+ return likedUnivApplyInfoRepository .countBySiteUserId (testUser .getId ());
149+ }
150+
151+ private MockMultipartFile createValidImageFile () {
152+ return new MockMultipartFile (
153+ "image" ,
154+ "test.jpg" ,
155+ "image/jpeg" ,
156+ "test image content" .getBytes ()
157+ );
158+ }
159+
160+ private String createExpectedErrorMessage (LocalDateTime modifiedAt ) {
161+ String formatLastModifiedAt = String .format (
162+ "(마지막 수정 시간 : %s)" ,
163+ NICKNAME_LAST_CHANGE_DATE_FORMAT .format (modifiedAt )
164+ );
165+ return CAN_NOT_CHANGE_NICKNAME_YET .getMessage () + " : " + formatLastModifiedAt ;
166+ }
167+
168+ private SiteUser createSiteUserWithCustomProfile () {
169+ return siteUserFixtureBuilder .siteUser ()
170+ 171+ .authType (AuthType .EMAIL )
172+ .nickname ("커스텀프로필" )
173+ .profileImageUrl ("profile/profileImageUrl" )
174+ .role (Role .MENTEE )
175+ .password ("customPassword123" )
176+ .create ();
177+ }
178+
98179 @ Nested
99180 class 프로필_이미지_수정_테스트 {
100181
@@ -182,87 +263,4 @@ void setUp() {
182263 .hasMessage (createExpectedErrorMessage (modifiedAt ));
183264 }
184265 }
185-
186- @ Nested
187- class 비밀번호_변경_테스트 {
188-
189- private String currentPassword ;
190- private String newPassword ;
191-
192- @ BeforeEach
193- void setUp () {
194- currentPassword = "currentPassword123" ;
195- newPassword = "newPassword123" ;
196-
197- user .updatePassword (passwordEncoder .encode (currentPassword ));
198- siteUserRepository .save (user );
199- }
200-
201- @ Test
202- void 비밀번호를_성공적으로_변경한다 () {
203- // given
204- PasswordUpdateRequest request = new PasswordUpdateRequest (currentPassword , newPassword , newPassword );
205-
206- // when
207- myPageService .updatePassword (user .getId (), request );
208-
209- // then
210- SiteUser updatedUser = siteUserRepository .findById (user .getId ()).get ();
211- assertAll (
212- () -> assertThat (passwordEncoder .matches (newPassword , updatedUser .getPassword ())).isTrue (),
213- () -> assertThat (passwordEncoder .matches (currentPassword , updatedUser .getPassword ())).isFalse ()
214- );
215- }
216-
217- @ Test
218- void 현재_비밀번호가_일치하지_않으면_예외가_발생한다 () {
219- // given
220- String wrongPassword = "wrongPassword" ;
221- PasswordUpdateRequest request = new PasswordUpdateRequest (wrongPassword , newPassword , newPassword );
222-
223- // when & then
224- assertThatThrownBy (() -> myPageService .updatePassword (user .getId (), request ))
225- .isInstanceOf (CustomException .class )
226- .hasMessage (PASSWORD_MISMATCH .getMessage ());
227- }
228- }
229-
230- private int createLikedUnivApplyInfos (SiteUser testUser ) {
231- LikedUnivApplyInfo likedUnivApplyInfo1 = new LikedUnivApplyInfo (null , univApplyInfoFixture .괌대학_A_지원_정보 ().getId (), testUser .getId ());
232- LikedUnivApplyInfo likedUnivApplyInfo2 = new LikedUnivApplyInfo (null , univApplyInfoFixture .메이지대학_지원_정보 ().getId (), testUser .getId ());
233- LikedUnivApplyInfo likedUnivApplyInfo3 = new LikedUnivApplyInfo (null , univApplyInfoFixture .코펜하겐IT대학_지원_정보 ().getId (), testUser .getId ());
234-
235- likedUnivApplyInfoRepository .save (likedUnivApplyInfo1 );
236- likedUnivApplyInfoRepository .save (likedUnivApplyInfo2 );
237- likedUnivApplyInfoRepository .save (likedUnivApplyInfo3 );
238- return likedUnivApplyInfoRepository .countBySiteUserId (testUser .getId ());
239- }
240-
241- private MockMultipartFile createValidImageFile () {
242- return new MockMultipartFile (
243- "image" ,
244- "test.jpg" ,
245- "image/jpeg" ,
246- "test image content" .getBytes ()
247- );
248- }
249-
250- private String createExpectedErrorMessage (LocalDateTime modifiedAt ) {
251- String formatLastModifiedAt = String .format (
252- "(마지막 수정 시간 : %s)" ,
253- NICKNAME_LAST_CHANGE_DATE_FORMAT .format (modifiedAt )
254- );
255- return CAN_NOT_CHANGE_NICKNAME_YET .getMessage () + " : " + formatLastModifiedAt ;
256- }
257-
258- private SiteUser createSiteUserWithCustomProfile () {
259- return siteUserFixtureBuilder .siteUser ()
260- 261- .authType (AuthType .EMAIL )
262- .nickname ("커스텀프로필" )
263- .profileImageUrl ("profile/profileImageUrl" )
264- .role (Role .MENTEE )
265- .password ("customPassword123" )
266- .create ();
267- }
268266}
0 commit comments