longz
2026-08-03 5bae49af49876fe49414d7ae28298df7e98185d1
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
package com.ruoyi.other.emind.service;
 
import com.ruoyi.other.emind.dto.EmindDashboardQuery;
import com.ruoyi.other.emind.mapper.EmindStorageDashboardMapper;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
 
class EmindStorageDashboardServiceTest {
    @Test
    void overviewUsesOnlyLocalMapperAndDefaultsToRecentDays() {
        EmindStorageDashboardMapper mapper=mock(EmindStorageDashboardMapper.class);
        when(mapper.selectOverview(isNull(), any())).thenReturn(Collections.emptyMap());
        when(mapper.selectHistory(isNull(), eq("DAY"), any(), any())).thenReturn(Collections.emptyList());
        when(mapper.selectCurve(isNull())).thenReturn(Collections.emptyList());
        EmindDashboardQuery query=new EmindDashboardQuery();
        assertEquals(3, new EmindStorageDashboardService(mapper).overview(query).size());
        verify(mapper).selectHistory(isNull(), eq("DAY"), eq(LocalDate.now().minusDays(6)), eq(LocalDate.now()));
    }
 
    @Test
    void overviewFillsMissingDailyHistoryWithZeroValues() {
        EmindStorageDashboardMapper mapper = mock(EmindStorageDashboardMapper.class);
        LocalDate start = LocalDate.of(2026, 7, 20);
        LocalDate end = LocalDate.of(2026, 7, 22);
        Map<String, Object> recordedDay = new HashMap<>();
        recordedDay.put("label", "2026-07-21");
        recordedDay.put("income", 12);
        recordedDay.put("chargedEnergy", 5);
        recordedDay.put("dischargedEnergy", 3);
        when(mapper.selectOverview(isNull(), any())).thenReturn(Collections.emptyMap());
        when(mapper.selectHistory(isNull(), eq("DAY"), eq(start), eq(end))).thenReturn(Collections.singletonList(recordedDay));
        when(mapper.selectCurve(isNull())).thenReturn(Collections.emptyList());
        EmindDashboardQuery query = new EmindDashboardQuery();
        query.setPeriod("DAY");
        query.setStartDate(start);
        query.setEndDate(end);
 
        Map<String, Object> result = new EmindStorageDashboardService(mapper).overview(query);
        List<Map<String, Object>> history = (List<Map<String, Object>>) result.get("history");
 
        List<Object> labels = new ArrayList<>();
        for (Map<String, Object> row : history) labels.add(row.get("label"));
        assertEquals(Arrays.asList("2026-07-20", "2026-07-21", "2026-07-22"), labels);
        assertEquals(BigDecimal.ZERO, history.get(0).get("income"));
        assertEquals(12, history.get(1).get("income"));
        assertEquals(BigDecimal.ZERO, history.get(2).get("chargedEnergy"));
    }
}