forked from ajaynegi45/LibraryMan-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyticsServiceTest.java
More file actions
76 lines (60 loc) · 2.57 KB
/
AnalyticsServiceTest.java
File metadata and controls
76 lines (60 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.libraryman_api.analytics;
import com.libraryman_api.book.BookRepository;
import com.libraryman_api.borrowing.BorrowingRepository;
import com.libraryman_api.member.MemberRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
/**
* Tests the {@link AnalyticsService} class.
*/
@ExtendWith(MockitoExtension.class)
class AnalyticsServiceTest {
@Mock
private BookRepository bookRepository;
@Mock
private BorrowingRepository borrowingRepository;
@Mock
private MemberRepository memberRepository;
@InjectMocks
private AnalyticsService analyticsService;
@Test
void getLibraryOverview() {
when(bookRepository.count()).thenReturn(100L);
when(borrowingRepository.count()).thenReturn(50L);
when(memberRepository.count()).thenReturn(25L);
Map<String, Object> overview = analyticsService.getLibraryOverview();
assertEquals(100L, overview.get("totalBooks"));
assertEquals(25L, overview.get("totalMembers"));
assertEquals(50L, overview.get("totalBorrowings"));
}
@Test
void getPopularBooks() {
List<Map<String, Object>> expectedList = List.of(Map.of("title", "Book A", "borrowCount", 10), Map.of("title", "Book B", "borrowCount", 8));
when(borrowingRepository.findMostBorrowedBooks(2)).thenReturn(expectedList);
List<Map<String, Object>> result = analyticsService.getPopularBooks(2);
assertEquals(expectedList, result);
}
@Test
void getBorrowingTrends() {
Map<String, Long> trends = Map.of("2025-06-01", 5L, "2025-06-02", 3L);
when(borrowingRepository.getBorrowingTrendsBetweenDates(any(), any())).thenReturn(trends);
Map<String, Long> result = analyticsService.getBorrowingTrends(LocalDate.now().minusDays(1), LocalDate.now());
assertEquals(trends, result);
}
@Test
void getMemberActivityReport() {
List<Map<String, Object>> report = List.of(Map.of("memberId", 1, "borrowCount", 10), Map.of("memberId", 2, "borrowCount", 5));
when(memberRepository.getMemberActivityReport()).thenReturn(report);
List<Map<String, Object>> result = analyticsService.getMemberActivityReport();
assertEquals(report, result);
}
}