|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+package com.water.revenue.service;
|
|
|
2
|
+
|
|
|
3
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
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 org.springframework.jdbc.core.JdbcTemplate;
|
|
|
10
|
+
|
|
|
11
|
+import java.math.BigDecimal;
|
|
|
12
|
+import java.util.*;
|
|
|
13
|
+
|
|
|
14
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
15
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
16
|
+import static org.mockito.Mockito.*;
|
|
|
17
|
+
|
|
|
18
|
+@ExtendWith(MockitoExtension.class)
|
|
|
19
|
+class RevenueDashboardServiceTest {
|
|
|
20
|
+
|
|
|
21
|
+ @Mock
|
|
|
22
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
23
|
+
|
|
|
24
|
+ @InjectMocks
|
|
|
25
|
+ private RevenueDashboardService dashboardService;
|
|
|
26
|
+
|
|
|
27
|
+ @Test
|
|
|
28
|
+ void testGetOverview_Success() {
|
|
|
29
|
+ when(jdbcTemplate.queryForObject(eq("SELECT COALESCE(SUM(paid_fee), 0) FROM rev_bill"), eq(BigDecimal.class)))
|
|
|
30
|
+ .thenReturn(new BigDecimal("100000.00"));
|
|
|
31
|
+ when(jdbcTemplate.queryForObject(contains("bill_period"), eq(BigDecimal.class), any()))
|
|
|
32
|
+ .thenReturn(new BigDecimal("15000.00"));
|
|
|
33
|
+ when(jdbcTemplate.queryForObject(contains("CURRENT_DATE"), eq(BigDecimal.class)))
|
|
|
34
|
+ .thenReturn(new BigDecimal("500.00"));
|
|
|
35
|
+ when(jdbcTemplate.queryForObject(contains("status IN ('pending', 'partial')"), eq(Integer.class)))
|
|
|
36
|
+ .thenReturn(20);
|
|
|
37
|
+ when(jdbcTemplate.queryForObject(eq("SELECT COUNT(*) FROM rev_bill WHERE status = 'overdue'"), eq(Integer.class)))
|
|
|
38
|
+ .thenReturn(5);
|
|
|
39
|
+ when(jdbcTemplate.queryForObject(contains("status = 'overdue'"), eq(BigDecimal.class)))
|
|
|
40
|
+ .thenReturn(new BigDecimal("3000.00"));
|
|
|
41
|
+ when(jdbcTemplate.queryForObject(contains("SUM(total_fee)"), eq(BigDecimal.class)))
|
|
|
42
|
+ .thenReturn(new BigDecimal("120000.00"))
|
|
|
43
|
+ .thenReturn(new BigDecimal("95000.00"));
|
|
|
44
|
+ when(jdbcTemplate.queryForObject(eq("SELECT COUNT(*) FROM rev_customer"), eq(Integer.class)))
|
|
|
45
|
+ .thenReturn(500);
|
|
|
46
|
+
|
|
|
47
|
+ Map<String, Object> overview = dashboardService.getOverview();
|
|
|
48
|
+
|
|
|
49
|
+ assertNotNull(overview);
|
|
|
50
|
+ assertEquals(new BigDecimal("100000.00"), overview.get("totalRevenue"));
|
|
|
51
|
+ assertEquals(new BigDecimal("15000.00"), overview.get("monthRevenue"));
|
|
|
52
|
+ assertEquals(new BigDecimal("500.00"), overview.get("todayRevenue"));
|
|
|
53
|
+ assertEquals(20, overview.get("pendingBills"));
|
|
|
54
|
+ assertEquals(5, overview.get("overdueBills"));
|
|
|
55
|
+ assertEquals(500, overview.get("customerCount"));
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
|
58
|
+ @Test
|
|
|
59
|
+ void testGetOverview_ZeroTotalRevenue() {
|
|
|
60
|
+ when(jdbcTemplate.queryForObject(anyString(), eq(BigDecimal.class)))
|
|
|
61
|
+ .thenReturn(BigDecimal.ZERO);
|
|
|
62
|
+ when(jdbcTemplate.queryForObject(anyString(), eq(Integer.class)))
|
|
|
63
|
+ .thenReturn(0);
|
|
|
64
|
+
|
|
|
65
|
+ Map<String, Object> overview = dashboardService.getOverview();
|
|
|
66
|
+
|
|
|
67
|
+ assertNotNull(overview);
|
|
|
68
|
+ assertEquals(BigDecimal.ZERO, overview.get("totalRevenue"));
|
|
|
69
|
+ assertEquals(BigDecimal.ZERO, overview.get("collectionRate"));
|
|
|
70
|
+ }
|
|
|
71
|
+
|
|
|
72
|
+ @Test
|
|
|
73
|
+ void testGetRevenueTrend_Day() {
|
|
|
74
|
+ List<Map<String, Object>> mockData = new ArrayList<>();
|
|
|
75
|
+ Map<String, Object> row = new HashMap<>();
|
|
|
76
|
+ row.put("period", "2026-06-15");
|
|
|
77
|
+ row.put("revenue", new BigDecimal("1200.00"));
|
|
|
78
|
+ row.put("count", 5);
|
|
|
79
|
+ mockData.add(row);
|
|
|
80
|
+
|
|
|
81
|
+ when(jdbcTemplate.queryForList(anyString(), any())).thenReturn(mockData);
|
|
|
82
|
+
|
|
|
83
|
+ List<Map<String, Object>> trend = dashboardService.getRevenueTrend("day");
|
|
|
84
|
+
|
|
|
85
|
+ assertNotNull(trend);
|
|
|
86
|
+ assertEquals(1, trend.size());
|
|
|
87
|
+ verify(jdbcTemplate).queryForList(anyString(), any());
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ @Test
|
|
|
91
|
+ void testGetRevenueTrend_Week() {
|
|
|
92
|
+ when(jdbcTemplate.queryForList(anyString(), any())).thenReturn(Collections.emptyList());
|
|
|
93
|
+
|
|
|
94
|
+ List<Map<String, Object>> trend = dashboardService.getRevenueTrend("week");
|
|
|
95
|
+
|
|
|
96
|
+ assertNotNull(trend);
|
|
|
97
|
+ assertTrue(trend.isEmpty());
|
|
|
98
|
+ }
|
|
|
99
|
+
|
|
|
100
|
+ @Test
|
|
|
101
|
+ void testGetRevenueTrend_Month() {
|
|
|
102
|
+ when(jdbcTemplate.queryForList(anyString(), any())).thenReturn(Collections.emptyList());
|
|
|
103
|
+
|
|
|
104
|
+ List<Map<String, Object>> trend = dashboardService.getRevenueTrend("month");
|
|
|
105
|
+
|
|
|
106
|
+ assertNotNull(trend);
|
|
|
107
|
+ assertTrue(trend.isEmpty());
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+ @Test
|
|
|
111
|
+ void testGetAreaRevenue_Success() {
|
|
|
112
|
+ when(jdbcTemplate.queryForObject(contains("c.area = ?"), eq(BigDecimal.class), eq("东区")))
|
|
|
113
|
+ .thenReturn(new BigDecimal("50000.00"))
|
|
|
114
|
+ .thenReturn(new BigDecimal("2000.00"));
|
|
|
115
|
+ when(jdbcTemplate.queryForObject(contains("COUNT(*)"), eq(Integer.class), eq("东区")))
|
|
|
116
|
+ .thenReturn(100);
|
|
|
117
|
+ when(jdbcTemplate.queryForObject(eq("SELECT COUNT(*) FROM rev_customer WHERE area = ?"), eq(Integer.class), eq("东区")))
|
|
|
118
|
+ .thenReturn(80);
|
|
|
119
|
+
|
|
|
120
|
+ Map<String, Object> areaRevenue = dashboardService.getAreaRevenue("东区");
|
|
|
121
|
+
|
|
|
122
|
+ assertNotNull(areaRevenue);
|
|
|
123
|
+ assertEquals("东区", areaRevenue.get("area"));
|
|
|
124
|
+ assertEquals(new BigDecimal("50000.00"), areaRevenue.get("totalRevenue"));
|
|
|
125
|
+ assertEquals(100, areaRevenue.get("billCount"));
|
|
|
126
|
+ assertEquals(80, areaRevenue.get("customerCount"));
|
|
|
127
|
+ }
|
|
|
128
|
+
|
|
|
129
|
+ @Test
|
|
|
130
|
+ void testGetPaymentChannelStats_Success() {
|
|
|
131
|
+ List<Map<String, Object>> mockStats = new ArrayList<>();
|
|
|
132
|
+ Map<String, Object> row = new HashMap<>();
|
|
|
133
|
+ row.put("pay_channel", "wechat");
|
|
|
134
|
+ row.put("count", 50);
|
|
|
135
|
+ row.put("total", new BigDecimal("25000.00"));
|
|
|
136
|
+ mockStats.add(row);
|
|
|
137
|
+
|
|
|
138
|
+ when(jdbcTemplate.queryForList(anyString())).thenReturn(mockStats);
|
|
|
139
|
+
|
|
|
140
|
+ List<Map<String, Object>> stats = dashboardService.getPaymentChannelStats();
|
|
|
141
|
+
|
|
|
142
|
+ assertNotNull(stats);
|
|
|
143
|
+ assertEquals(1, stats.size());
|
|
|
144
|
+ assertEquals("wechat", stats.get(0).get("pay_channel"));
|
|
|
145
|
+ }
|
|
|
146
|
+
|
|
|
147
|
+ @Test
|
|
|
148
|
+ void testGetCustomerTypeStats_Success() {
|
|
|
149
|
+ when(jdbcTemplate.queryForList(anyString())).thenReturn(Collections.emptyList());
|
|
|
150
|
+
|
|
|
151
|
+ List<Map<String, Object>> stats = dashboardService.getCustomerTypeStats();
|
|
|
152
|
+
|
|
|
153
|
+ assertNotNull(stats);
|
|
|
154
|
+ assertTrue(stats.isEmpty());
|
|
|
155
|
+ }
|
|
|
156
|
+
|
|
|
157
|
+ @Test
|
|
|
158
|
+ void testGetTopOverdueCustomers_Success() {
|
|
|
159
|
+ when(jdbcTemplate.queryForList(anyString(), eq(10))).thenReturn(Collections.emptyList());
|
|
|
160
|
+
|
|
|
161
|
+ List<Map<String, Object>> customers = dashboardService.getTopOverdueCustomers(10);
|
|
|
162
|
+
|
|
|
163
|
+ assertNotNull(customers);
|
|
|
164
|
+ assertTrue(customers.isEmpty());
|
|
|
165
|
+ }
|
|
|
166
|
+
|
|
|
167
|
+ @Test
|
|
|
168
|
+ void testGetTopOverdueCustomers_CustomLimit() {
|
|
|
169
|
+ when(jdbcTemplate.queryForList(anyString(), eq(5))).thenReturn(Collections.emptyList());
|
|
|
170
|
+
|
|
|
171
|
+ List<Map<String, Object>> customers = dashboardService.getTopOverdueCustomers(5);
|
|
|
172
|
+
|
|
|
173
|
+ assertNotNull(customers);
|
|
|
174
|
+ verify(jdbcTemplate).queryForList(anyString(), eq(5));
|
|
|
175
|
+ }
|
|
|
176
|
+}
|