1+ package com .homeaid .service ;
2+
3+ import static org .assertj .core .api .Assertions .*;
4+ import static org .mockito .BDDMockito .*;
5+
6+ import com .homeaid .domain .Payment ;
7+ import com .homeaid .domain .Settlement ;
8+ import com .homeaid .dto .response .SettlementResponseDto ;
9+ import com .homeaid .exception .CustomException ;
10+ import com .homeaid .exception .SettlementErrorCode ;
11+ import com .homeaid .repository .ManagerRepository ;
12+ import com .homeaid .repository .PaymentRepository ;
13+ import com .homeaid .repository .SettlementRepository ;
14+
15+ import java .time .LocalDate ;
16+ import java .time .LocalDateTime ;
17+ import java .util .List ;
18+ import java .util .Optional ;
19+
20+ import org .junit .jupiter .api .DisplayName ;
21+ import org .junit .jupiter .api .Test ;
22+ import org .mockito .InjectMocks ;
23+ import org .mockito .Mock ;
24+ import org .mockito .MockitoAnnotations ;
25+
26+ class SettlementServiceImplTest {
27+
28+ @ Mock
29+ private PaymentRepository paymentRepository ;
30+ @ Mock
31+ private SettlementRepository settlementRepository ;
32+ @ Mock
33+ private ManagerRepository managerRepository ;
34+
35+ @ InjectMocks
36+ private SettlementServiceImpl settlementService ;
37+
38+ public SettlementServiceImplTest () {
39+ MockitoAnnotations .openMocks (this );
40+ }
41+
42+ @ Test
43+ @ DisplayName ("주간 정산 생성 성공" )
44+ void createWeeklySettlementForManager_success () {
45+ // given
46+ Long managerId = 1L ;
47+ LocalDate weekStart = LocalDate .of (2024 , 6 , 2 );
48+ LocalDate weekEnd = LocalDate .of (2024 , 6 , 8 );
49+
50+ given (managerRepository .existsById (managerId )).willReturn (true );
51+
52+ Payment p1 = mock (Payment .class );
53+ Payment p2 = mock (Payment .class );
54+ given (p1 .getAmount ()).willReturn (10000 );
55+ given (p2 .getAmount ()).willReturn (5000 );
56+
57+ given (paymentRepository .findAllByReservation_ManagerIdAndPaidAtBetween (
58+ eq (managerId ), any (LocalDateTime .class ), any (LocalDateTime .class )
59+ )).willReturn (List .of (p1 , p2 ));
60+
61+ Settlement savedSettlement = Settlement .builder ()
62+ .managerId (managerId )
63+ .totalAmount (15000 )
64+ .managerSettlementPrice (12000 )
65+ .settlementWeekStart (weekStart )
66+ .settlementWeekEnd (weekEnd )
67+ .settledAt (LocalDateTime .now ())
68+ .build ();
69+ given (settlementRepository .save (any (Settlement .class ))).willReturn (savedSettlement );
70+
71+ // when
72+ SettlementResponseDto result = settlementService .createWeeklySettlementForManager (managerId , weekStart , weekEnd );
73+
74+ // then
75+ assertThat (result .getManagerId ()).isEqualTo (managerId );
76+ assertThat (result .getTotalAmount ()).isEqualTo (15000 );
77+ assertThat (result .getManagerAmount ()).isEqualTo (12000 ); // 80% of 150_000
78+ }
79+
80+ @ Test
81+ @ DisplayName ("정산 생성 시 매니저가 없으면 예외 발생" )
82+ void createWeeklySettlementForManager_managerNotFound () {
83+ // given
84+ Long managerId = 99L ;
85+ LocalDate weekStart = LocalDate .of (2024 , 5 , 26 );
86+ LocalDate weekEnd = LocalDate .of (2024 , 6 , 1 );
87+ given (managerRepository .existsById (managerId )).willReturn (false );
88+
89+ // when & then
90+ assertThatThrownBy (() ->
91+ settlementService .createWeeklySettlementForManager (managerId , weekStart , weekEnd )
92+ ).isInstanceOf (CustomException .class )
93+ .hasMessageContaining (SettlementErrorCode .MANAGER_NOT_FOUND .getMessage ());
94+ }
95+
96+ @ Test
97+ @ DisplayName ("정산 생성 시 결제내역이 없으면 예외 발생" )
98+ void createWeeklySettlementForManager_noPaymentsFound () {
99+ // given
100+ Long managerId = 1L ;
101+ LocalDate weekStart = LocalDate .of (2024 , 5 , 26 );
102+ LocalDate weekEnd = LocalDate .of (2024 , 6 , 1 );
103+
104+ given (managerRepository .existsById (managerId )).willReturn (true );
105+ given (paymentRepository .findAllByReservation_ManagerIdAndPaidAtBetween (
106+ eq (managerId ), any (LocalDateTime .class ), any (LocalDateTime .class )
107+ )).willReturn (List .of ());
108+
109+ // when & then
110+ assertThatThrownBy (() ->
111+ settlementService .createWeeklySettlementForManager (managerId , weekStart , weekEnd )
112+ ).isInstanceOf (CustomException .class )
113+ .hasMessageContaining (SettlementErrorCode .NO_PAYMENTS_FOUND .getMessage ());
114+ }
115+
116+ @ Test
117+ @ DisplayName ("findAll: 전체 정산내역 조회" )
118+ void findAll () {
119+ // given
120+ Settlement s1 = mock (Settlement .class );
121+ Settlement s2 = mock (Settlement .class );
122+ given (settlementRepository .findAll ()).willReturn (List .of (s1 , s2 ));
123+
124+ // when
125+ List <Settlement > result = settlementService .findAll ();
126+
127+ // then
128+ assertThat (result ).hasSize (2 );
129+ }
130+
131+ @ Test
132+ @ DisplayName ("findById: 존재하지 않으면 예외 발생" )
133+ void findById_notFound () {
134+ // given
135+ Long id = 123L ;
136+ given (settlementRepository .findById (id )).willReturn (Optional .empty ());
137+
138+ // when & then
139+ assertThatThrownBy (() -> settlementService .findById (id ))
140+ .isInstanceOf (CustomException .class )
141+ .hasMessageContaining (SettlementErrorCode .INVALID_REQUEST .getMessage ());
142+ }
143+
144+ @ Test
145+ @ DisplayName ("findById: 정상 단건 조회" )
146+ void findById_success () {
147+ // given
148+ Long id = 1L ;
149+ Settlement settlement = mock (Settlement .class );
150+ given (settlementRepository .findById (id )).willReturn (Optional .of (settlement ));
151+
152+ // when
153+ Settlement result = settlementService .findById (id );
154+
155+ // then
156+ assertThat (result ).isEqualTo (settlement );
157+ }
158+
159+ @ Test
160+ @ DisplayName ("findByManagerId: 매니저별 정산내역 조회" )
161+ void findByManagerId () {
162+ // given
163+ Long managerId = 10L ;
164+ Settlement s1 = mock (Settlement .class );
165+ Settlement s2 = mock (Settlement .class );
166+ given (settlementRepository .findAllByManagerId (managerId )).willReturn (List .of (s1 , s2 ));
167+
168+ // when
169+ List <Settlement > result = settlementService .findByManagerId (managerId );
170+
171+ // then
172+ assertThat (result ).hasSize (2 );
173+ }
174+ }
0 commit comments