1+ package kattsyn .dev .rentplace .services ;
2+
3+ import kattsyn .dev .rentplace .services .impl .ReservationServiceImpl ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .junit .jupiter .api .extension .ExtendWith ;
6+ import org .mockito .InjectMocks ;
7+ import org .mockito .Mock ;
8+ import org .mockito .junit .jupiter .MockitoExtension ;
9+ import kattsyn .dev .rentplace .entities .Reservation ;
10+ import kattsyn .dev .rentplace .exceptions .ValidationException ;
11+ import java .time .LocalDate ;
12+ import static org .junit .jupiter .api .Assertions .*;
13+ import static org .mockito .Mockito .*;
14+
15+ @ ExtendWith (MockitoExtension .class )
16+ class ReservationServiceTest {
17+
18+ @ Mock
19+ private Reservation reservation ;
20+
21+ @ InjectMocks
22+ private ReservationServiceImpl reservationService ;
23+
24+ @ Test
25+ void countRentPrice_LongTerm_ReturnsCorrectMonths () {
26+ when (reservation .isLongTermRent ()).thenReturn (true );
27+ when (reservation .getStartDate ()).thenReturn (LocalDate .of (2026 , 6 , 1 ));
28+ when (reservation .getEndDate ()).thenReturn (LocalDate .of (2026 , 10 , 1 ));
29+ when (reservation .getCostInPeriod ()).thenReturn (100 );
30+
31+ int result = reservationService .countRentPrice (reservation );
32+ assertEquals (400 , result ); // 4 месяца по 100
33+ }
34+
35+ @ Test
36+ void countRentPrice_ShortTerm_ReturnsCorrectMonths () {
37+ when (reservation .isLongTermRent ()).thenReturn (false );
38+ when (reservation .getStartDate ()).thenReturn (LocalDate .of (2026 , 4 , 1 ));
39+ when (reservation .getEndDate ()).thenReturn (LocalDate .of (2026 , 4 , 15 ));
40+ when (reservation .getCostInPeriod ()).thenReturn (100 );
41+
42+ int result = reservationService .countRentPrice (reservation );
43+ assertEquals (1400 , result ); // 14 дней по 100
44+ }
45+
46+ @ Test
47+ void countRentPrice_LongTermCrossingYear_ReturnsCorrectMonths () {
48+ when (reservation .isLongTermRent ()).thenReturn (true );
49+ when (reservation .getStartDate ()).thenReturn (LocalDate .of (2026 , 12 , 1 ));
50+ when (reservation .getEndDate ()).thenReturn (LocalDate .of (2027 , 1 , 1 ));
51+ when (reservation .getCostInPeriod ()).thenReturn (100 );
52+
53+ int result = reservationService .countRentPrice (reservation );
54+ assertEquals (100 , result ); // 1 месяц
55+ }
56+
57+ @ Test
58+ void countRentPrice_ShortTermCrossingYear_ReturnsCorrectDays () {
59+ when (reservation .isLongTermRent ()).thenReturn (false );
60+ when (reservation .getStartDate ()).thenReturn (LocalDate .of (2026 , 12 , 31 ));
61+ when (reservation .getEndDate ()).thenReturn (LocalDate .of (2027 , 1 , 2 ));
62+ when (reservation .getCostInPeriod ()).thenReturn (50 );
63+
64+ int result = reservationService .countRentPrice (reservation );
65+ assertEquals (2 * 50 , result ); // 2 дня
66+ }
67+
68+ @ Test
69+ void countRentPrice_EndDateBeforeStartDate_ThrowsException () {
70+ when (reservation .getStartDate ()).thenReturn (LocalDate .of (2024 , 1 , 10 ));
71+ when (reservation .getEndDate ()).thenReturn (LocalDate .of (2024 , 1 , 5 ));
72+
73+ assertThrows (ValidationException .class , () -> reservationService .countRentPrice (reservation ));
74+ }
75+
76+ @ Test
77+ void countRentPrice_StartDateInPast_ThrowsException () {
78+ when (reservation .getStartDate ()).thenReturn (LocalDate .now ().minusDays (1 ));
79+
80+ assertThrows (ValidationException .class , () -> reservationService .countRentPrice (reservation ));
81+ }
82+ }
0 commit comments