1+ package dmu .dasom .api .domain .activity ;
2+
3+ import dmu .dasom .api .domain .activity .dto .ActivityHistoryRequestDto ;
4+ import dmu .dasom .api .domain .activity .dto .ActivityHistoryResponseDto ;
5+ import dmu .dasom .api .domain .activity .entity .ActivityHistory ;
6+ import dmu .dasom .api .domain .activity .repository .ActivityHistoryRepository ;
7+ import dmu .dasom .api .domain .activity .service .ActivityHistoryServiceImpl ;
8+ import dmu .dasom .api .domain .common .exception .CustomException ;
9+ import dmu .dasom .api .domain .common .exception .ErrorCode ;
10+ import org .junit .jupiter .api .DisplayName ;
11+ import org .junit .jupiter .api .Test ;
12+ import org .junit .jupiter .api .extension .ExtendWith ;
13+ import org .mockito .InjectMocks ;
14+ import org .mockito .Mock ;
15+ import org .mockito .junit .jupiter .MockitoExtension ;
16+
17+ import java .util .Optional ;
18+
19+ import static org .assertj .core .api .Assertions .assertThat ;
20+ import static org .junit .jupiter .api .Assertions .assertThrows ;
21+ import static org .mockito .ArgumentMatchers .any ;
22+ import static org .mockito .BDDMockito .given ;
23+ import static org .mockito .Mockito .never ;
24+ import static org .mockito .Mockito .verify ;
25+
26+ @ ExtendWith (MockitoExtension .class )
27+ class ActivityHistoryServiceTest {
28+
29+ @ InjectMocks
30+ private ActivityHistoryServiceImpl activityHistoryService ;
31+
32+ @ Mock
33+ private ActivityHistoryRepository historyRepository ;
34+
35+ @ Test
36+ @ DisplayName ("활동 연혁 생성 성공" )
37+ void createHistory () {
38+ // given
39+ ActivityHistoryRequestDto request = createRequestDto ();
40+ ActivityHistory savedHistory = createActivityHistory (1L , "New Title" );
41+ given (historyRepository .save (any (ActivityHistory .class ))).willReturn (savedHistory );
42+
43+ // when
44+ ActivityHistoryResponseDto response = activityHistoryService .createHistory (request );
45+
46+ // then
47+ assertThat (response .getId ()).isEqualTo (savedHistory .getId ());
48+ assertThat (response .getTitle ()).isEqualTo (savedHistory .getTitle ());
49+ verify (historyRepository ).save (any (ActivityHistory .class ));
50+ }
51+
52+ @ Test
53+ @ DisplayName ("활동 연혁 수정 성공" )
54+ void updateHistory () {
55+ // given
56+ long historyId = 1L ;
57+ ActivityHistoryRequestDto request = createRequestDto ();
58+ ActivityHistory existingHistory = createActivityHistory (historyId , "Old Title" );
59+ given (historyRepository .findById (historyId )).willReturn (Optional .of (existingHistory ));
60+
61+ // when
62+ activityHistoryService .updateHistory (historyId , request );
63+
64+ // then
65+ assertThat (existingHistory .getTitle ()).isEqualTo (request .getTitle ());
66+ verify (historyRepository ).findById (historyId );
67+ }
68+
69+ @ Test
70+ @ DisplayName ("활동 연혁 수정 실패 - 존재하지 않는 ID" )
71+ void updateHistory_whenNotFound_thenThrowException () {
72+ // given
73+ long nonExistentId = 99L ;
74+ ActivityHistoryRequestDto request = createRequestDto ();
75+ given (historyRepository .findById (nonExistentId )).willReturn (Optional .empty ());
76+
77+ // when & then
78+ CustomException exception = assertThrows (CustomException .class ,
79+ () -> activityHistoryService .updateHistory (nonExistentId , request ));
80+ assertThat (exception .getErrorCode ()).isEqualTo (ErrorCode .NOT_FOUND );
81+ verify (historyRepository ).findById (nonExistentId );
82+ }
83+
84+ @ Test
85+ @ DisplayName ("활동 연혁 삭제 성공" )
86+ void deleteHistory () {
87+ // given
88+ long historyId = 1L ;
89+ given (historyRepository .existsById (historyId )).willReturn (true );
90+
91+ // when
92+ activityHistoryService .deleteHistory (historyId );
93+
94+ // then
95+ verify (historyRepository ).existsById (historyId );
96+ verify (historyRepository ).deleteById (historyId );
97+ }
98+
99+ @ Test
100+ @ DisplayName ("활동 연혁 삭제 실패 - 존재하지 않는 ID" )
101+ void deleteHistory_whenNotFound_thenThrowException () {
102+ // given
103+ long nonExistentId = 99L ;
104+ given (historyRepository .existsById (nonExistentId )).willReturn (false );
105+
106+ // when & then
107+ CustomException exception = assertThrows (CustomException .class ,
108+ () -> activityHistoryService .deleteHistory (nonExistentId ));
109+ assertThat (exception .getErrorCode ()).isEqualTo (ErrorCode .NOT_FOUND );
110+ verify (historyRepository ).existsById (nonExistentId );
111+ verify (historyRepository , never ()).deleteById (nonExistentId );
112+ }
113+
114+ // --- Helper Methods ---
115+
116+ private ActivityHistoryRequestDto createRequestDto () {
117+ // In a real scenario, you might use a library like TestDataBuilder
118+ // or set fields if the DTO allows it.
119+ // For this example, we assume a default constructor is sufficient.
120+ return new ActivityHistoryRequestDto ();
121+ }
122+
123+ private ActivityHistory createActivityHistory (Long id , String title ) {
124+ return ActivityHistory .builder ()
125+ .id (id )
126+ .year (2024 )
127+ .section ("Test Section" )
128+ .title (title )
129+ .award ("Test Award" )
130+ .build ();
131+ }
132+ }
0 commit comments