1+ package com .somemore .recruitboard .service .command ;
2+
3+ import static com .somemore .recruitboard .domain .VolunteerType .ADMINISTRATIVE_SUPPORT ;
4+ import static com .somemore .recruitboard .domain .VolunteerType .OTHER ;
5+ import static org .assertj .core .api .Assertions .assertThat ;
6+
7+ import com .somemore .IntegrationTestSupport ;
8+ import com .somemore .global .exception .BadRequestException ;
9+ import com .somemore .location .domain .Location ;
10+ import com .somemore .location .repository .LocationRepository ;
11+ import com .somemore .recruitboard .domain .RecruitBoard ;
12+ import com .somemore .recruitboard .domain .VolunteerInfo ;
13+ import com .somemore .recruitboard .dto .request .RecruitBoardLocationUpdateRequestDto ;
14+ import com .somemore .recruitboard .dto .request .RecruitBoardUpdateRequestDto ;
15+ import com .somemore .recruitboard .repository .RecruitBoardRepository ;
16+ import java .math .BigDecimal ;
17+ import java .time .LocalDateTime ;
18+ import java .util .UUID ;
19+ import org .assertj .core .api .Assertions ;
20+ import org .junit .jupiter .api .AfterEach ;
21+ import org .junit .jupiter .api .BeforeEach ;
22+ import org .junit .jupiter .api .DisplayName ;
23+ import org .junit .jupiter .api .Test ;
24+ import org .springframework .beans .factory .annotation .Autowired ;
25+
26+ class UpdateRecruitBoardServiceTest extends IntegrationTestSupport {
27+
28+ @ Autowired
29+ private UpdateRecruitBoardService updateRecruitBoardService ;
30+
31+ @ Autowired
32+ private RecruitBoardRepository recruitBoardRepository ;
33+
34+ @ Autowired
35+ private LocationRepository locationRepository ;
36+
37+ private RecruitBoard recruitBoard ;
38+ private Location location ;
39+ private UUID centerId ;
40+
41+ @ BeforeEach
42+ void setUp () {
43+ location = createLocation ();
44+ locationRepository .saveAndFlush (location );
45+ centerId = UUID .randomUUID ();
46+ recruitBoard = createRecruitBoard (centerId , location .getId ());
47+ recruitBoardRepository .saveAndFlush (recruitBoard );
48+
49+ }
50+
51+
52+ @ AfterEach
53+ void tearDown () {
54+ recruitBoardRepository .deleteAllInBatch ();
55+ locationRepository .deleteAllInBatch ();
56+ }
57+
58+ @ DisplayName ("봉사 모집글의 데이터를 업데이트하면 저장소에 반영된다." )
59+ @ Test
60+ void updateRecruitBoard () {
61+ // given
62+ LocalDateTime newStartDateTime = LocalDateTime .now ();
63+ LocalDateTime newEndDateTime = newStartDateTime .plusHours (3 );
64+ String newImgUrl = "https://image.domain.com/updates" ;
65+ RecruitBoardUpdateRequestDto dto = RecruitBoardUpdateRequestDto .builder ()
66+ .title ("업데이트 제목" )
67+ .content ("업데이트 내용" )
68+ .recruitmentCount (1111 )
69+ .volunteerStartDateTime (newStartDateTime )
70+ .volunteerEndDateTime (newEndDateTime )
71+ .volunteerType (ADMINISTRATIVE_SUPPORT )
72+ .admitted (false )
73+ .build ();
74+
75+ // when
76+ updateRecruitBoardService .updateRecruitBoard (dto , recruitBoard .getId (), centerId ,
77+ newImgUrl );
78+
79+ // then
80+ RecruitBoard updatedRecruitBoard = recruitBoardRepository .findById (recruitBoard .getId ())
81+ .orElseThrow ();
82+
83+ assertThat (updatedRecruitBoard .getTitle ()).isEqualTo (dto .title ());
84+ assertThat (updatedRecruitBoard .getContent ()).isEqualTo (dto .content ());
85+ assertThat (updatedRecruitBoard .getImgUrl ()).isEqualTo (newImgUrl );
86+
87+ VolunteerInfo volunteerInfo = updatedRecruitBoard .getVolunteerInfo ();
88+ assertThat (volunteerInfo .getRecruitmentCount ()).isEqualTo (dto .recruitmentCount ());
89+ assertThat (volunteerInfo .getVolunteerStartDateTime ()).isEqualTo (
90+ dto .volunteerStartDateTime ());
91+ assertThat (volunteerInfo .getVolunteerEndDateTime ()).isEqualTo (dto .volunteerEndDateTime ());
92+ assertThat (volunteerInfo .getVolunteerType ()).isEqualTo (dto .volunteerType ());
93+ assertThat (volunteerInfo .getAdmitted ()).isEqualTo (dto .admitted ());
94+ }
95+
96+ @ DisplayName ("봉사 모집글 위치를 수정할 수 있다" )
97+ @ Test
98+ void updateRecruitBoardLocation () {
99+ // given
100+ RecruitBoardLocationUpdateRequestDto dto = RecruitBoardLocationUpdateRequestDto .builder ()
101+ .region ("새로새로지역지역" )
102+ .address ("새로새로주소주소" )
103+ .latitude (BigDecimal .valueOf (37.2222222 ))
104+ .longitude (BigDecimal .valueOf (127.2222222 ))
105+ .build ();
106+
107+ // when
108+ updateRecruitBoardService .updateRecruitBoardLocation (dto , recruitBoard .getId (), centerId );
109+
110+ // then
111+ RecruitBoard updateRecruitBoard = recruitBoardRepository .findById (recruitBoard .getId ())
112+ .orElseThrow ();
113+ Location updateLocation = locationRepository .findById (recruitBoard .getLocationId ())
114+ .orElseThrow ();
115+
116+ assertThat (updateRecruitBoard .getVolunteerInfo ().getRegion ()).isEqualTo (dto .region ());
117+ assertThat (updateLocation .getAddress ()).isEqualTo (dto .address ());
118+ assertThat (updateLocation .getLongitude ().compareTo (dto .longitude ())).isZero ();
119+ assertThat (updateLocation .getLatitude ().compareTo (dto .latitude ())).isZero ();
120+ }
121+
122+ @ DisplayName ("봉사 모집글은 작성자만 수정할 수 있다" )
123+ @ Test
124+ void updateRecruitBoardWhenCenterIdIsWrong () {
125+ // given
126+ UUID wrongCenterId = UUID .randomUUID ();
127+ LocalDateTime newStartDateTime = LocalDateTime .now ();
128+ LocalDateTime newEndDateTime = newStartDateTime .plusHours (3 );
129+ String newImgUrl = "https://image.domain.com/updates" ;
130+ RecruitBoardUpdateRequestDto dto = RecruitBoardUpdateRequestDto .builder ()
131+ .title ("업데이트 제목" )
132+ .content ("업데이트 내용" )
133+ .recruitmentCount (1111 )
134+ .volunteerStartDateTime (newStartDateTime )
135+ .volunteerEndDateTime (newEndDateTime )
136+ .volunteerType (ADMINISTRATIVE_SUPPORT )
137+ .admitted (false )
138+ .build ();
139+
140+ // when
141+ // then
142+ Assertions .assertThatThrownBy (
143+ () -> updateRecruitBoardService .updateRecruitBoard (dto , recruitBoard .getId (),
144+ wrongCenterId ,
145+ newImgUrl )
146+ ).isInstanceOf (BadRequestException .class );
147+
148+ }
149+
150+
151+ private static RecruitBoard createRecruitBoard (UUID centerId , Long locationId ) {
152+ LocalDateTime startDateTime = LocalDateTime .now ();
153+ LocalDateTime endDateTime = startDateTime .plusHours (1 );
154+
155+ return createRecruitBoard (centerId , locationId , startDateTime , endDateTime );
156+ }
157+
158+ private static RecruitBoard createRecruitBoard (UUID centerId , Long locationId ,
159+ LocalDateTime startDateTime ,
160+ LocalDateTime endDateTime ) {
161+
162+ VolunteerInfo volunteerInfo = VolunteerInfo .builder ()
163+ .region ("경기" )
164+ .recruitmentCount (1 )
165+ .volunteerStartDateTime (startDateTime )
166+ .volunteerEndDateTime (endDateTime )
167+ .volunteerType (OTHER )
168+ .admitted (true )
169+ .build ();
170+
171+ return RecruitBoard .builder ()
172+ .centerId (centerId )
173+ .locationId (locationId )
174+ .title ("봉사모집제목" )
175+ .content ("봉사모집내용" )
176+ .imgUrl ("https://image.domain.com/links" )
177+ .volunteerInfo (volunteerInfo )
178+ .build ();
179+ }
180+
181+ private static Location createLocation () {
182+ return Location .builder ()
183+ .address ("주소주소" )
184+ .longitude (BigDecimal .valueOf (37.11111 ))
185+ .latitude (BigDecimal .valueOf (127.11111 ))
186+ .build ();
187+ }
188+ }
0 commit comments