yeg
2026-08-30 ec3308617909c8d6280e0d00ffb18350e372fbdf
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.ruoyi.system.service.impl;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
import java.util.List;
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 com.ruoyi.system.domain.TBatteryGroup;
import com.ruoyi.system.domain.TBatteryGroupRelationField;
import com.ruoyi.system.domain.TTestFieldSnapshot;
import com.ruoyi.system.mapper.TSharedFieldSnapshotMapper;
import com.ruoyi.system.service.ITBatteryGroupService;
 
/** 共享业务字段快照规则测试。 */
@ExtendWith(MockitoExtension.class)
class TSharedFieldSnapshotServiceImplTest
{
    @Mock
    private ITBatteryGroupService batteryGroupService;
 
    @Mock
    private TSharedFieldSnapshotMapper snapshotMapper;
 
    @InjectMocks
    private TSharedFieldSnapshotServiceImpl snapshotService;
 
    @Test
    void createShouldCopyActiveBatteryGroupFields()
    {
        when(batteryGroupService.selectBatteryGroupById(1L)).thenReturn(group("72"));
 
        List<TTestFieldSnapshot> snapshots = snapshotService.createForBatteryGroup(1L);
 
        assertEquals(1, snapshots.size());
        assertEquals(11L, snapshots.get(0).getFieldDefinitionId());
        assertEquals("额定容量", snapshots.get(0).getFieldName());
        assertEquals("number", snapshots.get(0).getFieldType());
        assertEquals("72", snapshots.get(0).getFieldValue());
    }
 
    @Test
    void sameGroupUpdateShouldRetainOriginalSnapshotValue()
    {
        TTestFieldSnapshot original = snapshot("72");
 
        List<TTestFieldSnapshot> resolved = snapshotService.resolveForUpdate(1L, 1L, List.of(original));
 
        assertEquals("72", resolved.get(0).getFieldValue());
        assertNotSame(original, resolved.get(0));
        verify(batteryGroupService, never()).selectBatteryGroupById(1L);
    }
 
    @Test
    void changingGroupShouldCreateSnapshotFromNewGroup()
    {
        when(batteryGroupService.selectBatteryGroupById(2L)).thenReturn(group("99"));
 
        List<TTestFieldSnapshot> resolved = snapshotService.resolveForUpdate(1L, 2L, List.of(snapshot("72")));
 
        assertEquals("99", resolved.get(0).getFieldValue());
        verify(batteryGroupService).selectBatteryGroupById(2L);
    }
 
    private TBatteryGroup group(String value)
    {
        TBatteryGroupRelationField field = new TBatteryGroupRelationField();
        field.setBatteryGroupFieldId(11L);
        field.setFieldName("额定容量");
        field.setFieldType("number");
        field.setFieldValue(value);
        TBatteryGroup group = new TBatteryGroup();
        group.setTemplateFields(List.of(field));
        return group;
    }
 
    private TTestFieldSnapshot snapshot(String value)
    {
        TTestFieldSnapshot snapshot = new TTestFieldSnapshot();
        snapshot.setFieldDefinitionId(11L);
        snapshot.setFieldName("额定容量");
        snapshot.setFieldType("number");
        snapshot.setFieldValue(value);
        return snapshot;
    }
}