yeg
2026-09-03 76cd5948e95ddbde9dd4411b466ee75ba20b562e
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.ruoyi.system.service.impl;
 
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;
import com.ruoyi.device.bts.BtsDevelopmentSimulator;
import com.ruoyi.device.simulator.BtsSimulatorStateStore.BtsHistoryRecord;
import com.ruoyi.device.simulator.BtsSimulatorStateStore.CollectorRealtimeMeasurement;
import com.ruoyi.device.simulator.DevelopmentCollectorSimulation.CollectorRealtimeSnapshot;
import com.ruoyi.system.domain.TTestCollectorChannel;
import com.ruoyi.system.domain.TTest;
import com.ruoyi.system.domain.TTestBattery;
import com.ruoyi.system.domain.TTestChannelBinding;
import com.ruoyi.system.mapper.TTestMapper;
import com.ruoyi.system.service.ITTestService;
 
/** 接收服务只应把本次测试开始后的模拟器事实归入测试。 */
class TestDataReceptionServiceImplTest
{
    @Test
    void ignoresRecordsBeforeReceptionStart()
    {
        TTestMapper mapper = mock(TTestMapper.class);
        ITTestService tests = mock(ITTestService.class);
        BtsDevelopmentSimulator simulator = mock(BtsDevelopmentSimulator.class);
        TTest test = test(Date.from(Instant.parse("2026-09-02T04:00:00Z")));
        when(mapper.selectReceivingTestIds()).thenReturn(List.of(1L));
        when(mapper.selectTestByIdForUpdate(1L)).thenReturn(test);
        when(tests.selectTestById(1L)).thenReturn(test);
        when(simulator.btsRecords(1, 1)).thenReturn(List.of(
                record(1L, "2026-09-02T03:59:59Z"), record(2L, "2026-09-02T04:00:01Z")));
        when(mapper.selectBtsSampleContentHash(any(), anyString(), any())).thenReturn(null);
        when(mapper.insertBtsSample(any(), any(), any(), anyString(), any(), any(Date.class), anyString(),
                any(Date.class), anyInt(), anyInt(), anyString(), anyString(), isNull(), any(BigDecimal.class),
                any(BigDecimal.class), any(BigDecimal.class), any(BigDecimal.class), any(byte[].class))).thenReturn(1);
 
        new TestDataReceptionServiceImpl(mapper, tests, simulator).captureDevelopmentCabinetRecords();
 
        verify(mapper, times(1)).insertBtsSample(eq(1L), eq(2L), eq(3L), anyString(), eq(2L), any(Date.class),
                anyString(), any(Date.class), anyInt(), anyInt(), anyString(), anyString(), isNull(),
                any(BigDecimal.class), any(BigDecimal.class), any(BigDecimal.class), any(BigDecimal.class),
                any(byte[].class));
    }
 
    @Test
    void doesNotWriteSamplesWhenNoSuccessfulRecordsAreAvailable()
    {
        TTestMapper mapper = mock(TTestMapper.class);
        ITTestService tests = mock(ITTestService.class);
        BtsDevelopmentSimulator simulator = mock(BtsDevelopmentSimulator.class);
        TTest test = test(Date.from(Instant.parse("2026-09-02T04:00:00Z")));
        when(mapper.selectReceivingTestIds()).thenReturn(List.of(1L));
        when(mapper.selectTestByIdForUpdate(1L)).thenReturn(test);
        when(tests.selectTestById(1L)).thenReturn(test);
        when(simulator.btsRecords(1, 1)).thenReturn(List.of());
 
        new TestDataReceptionServiceImpl(mapper, tests, simulator).captureDevelopmentCabinetRecords();
 
        verify(mapper, never()).insertBtsSample(any(), any(), any(), anyString(), any(), any(Date.class), anyString(),
                any(Date.class), anyInt(), anyInt(), anyString(), anyString(), isNull(), any(BigDecimal.class),
                any(BigDecimal.class), any(BigDecimal.class), any(BigDecimal.class), any(byte[].class));
    }
 
    @Test
    void capturesOnlyMappedCollectorRealtimeMeasurementsAfterReceptionStart()
    {
        TTestMapper mapper = mock(TTestMapper.class);
        ITTestService tests = mock(ITTestService.class);
        BtsDevelopmentSimulator simulator = mock(BtsDevelopmentSimulator.class);
        TTest test = test(Date.from(Instant.parse("2026-09-02T04:00:00Z")));
        test.getBatteries().get(0).setCollectorId(9L);
        TTestCollectorChannel mapping = new TTestCollectorChannel();
        mapping.setMeasurementCode("temperature"); mapping.setChannelCode("S-01");
        test.getBatteries().get(0).setCollectorChannels(List.of(mapping));
        when(mapper.selectReceivingTestIds()).thenReturn(List.of(1L));
        when(mapper.selectTestByIdForUpdate(1L)).thenReturn(test);
        when(tests.selectTestById(1L)).thenReturn(test);
        CollectorRealtimeMeasurement matched = new CollectorRealtimeMeasurement("dev-sim", "collector-9", "S-01",
                3L, Instant.parse("2026-09-02T04:00:01Z"), "2026-09-02 12:00:01.000",
                Instant.parse("2026-09-02T04:00:02Z"), "temperature", new BigDecimal("25.1"), "℃");
        CollectorRealtimeMeasurement ignored = new CollectorRealtimeMeasurement("dev-sim", "collector-9", "S-02",
                3L, matched.deviceTime(), matched.deviceTimeRaw(), matched.serverReceivedAt(), "temperature",
                new BigDecimal("25.2"), "℃");
        when(simulator.collectorRealtime(9)).thenReturn(new CollectorRealtimeSnapshot("collector-9", true, 1000,
                List.of(matched, ignored)));
        when(mapper.selectCollectorSampleContentHash(any(), anyString(), any(), anyString())).thenReturn(null);
        when(mapper.insertCollectorSample(any(), any(), any(), anyString(), anyString(), any(), any(Date.class), anyString(),
                any(Date.class), anyString(), any(BigDecimal.class), anyString(), any(byte[].class))).thenReturn(1);
 
        new TestDataReceptionServiceImpl(mapper, tests, simulator).captureDevelopmentCollectorMeasurements();
 
        verify(mapper).insertCollectorSample(eq(1L), eq(2L), eq(9L), eq("dev-sim"), eq("S-01"), eq(3L), any(Date.class),
                eq("2026-09-02 12:00:01.000"), any(Date.class), eq("temperature"), eq(new BigDecimal("25.1")),
                eq("℃"), any(byte[].class));
        verify(mapper, never()).insertCollectorSample(eq(1L), eq(2L), eq(9L), anyString(), eq("S-02"), any(), any(Date.class),
                anyString(), any(Date.class), anyString(), any(BigDecimal.class), anyString(), any(byte[].class));
    }
 
    @Test
    void recordsCollectorContentConflictWithoutOverwritingOriginal()
    {
        TTestMapper mapper = mock(TTestMapper.class);
        ITTestService tests = mock(ITTestService.class);
        BtsDevelopmentSimulator simulator = mock(BtsDevelopmentSimulator.class);
        TTest test = test(Date.from(Instant.parse("2026-09-02T04:00:00Z")));
        test.getBatteries().get(0).setCollectorId(9L);
        TTestCollectorChannel mapping = new TTestCollectorChannel(); mapping.setMeasurementCode("temperature"); mapping.setChannelCode("S-01");
        test.getBatteries().get(0).setCollectorChannels(List.of(mapping));
        CollectorRealtimeMeasurement measurement = new CollectorRealtimeMeasurement("dev-sim", "collector-9", "S-01", 3L,
                Instant.parse("2026-09-02T04:00:01Z"), "2026-09-02 12:00:01.000", Instant.now(), "temperature", new BigDecimal("25.1"), "℃");
        when(mapper.selectReceivingTestIds()).thenReturn(List.of(1L)); when(mapper.selectTestByIdForUpdate(1L)).thenReturn(test);
        when(tests.selectTestById(1L)).thenReturn(test); when(simulator.collectorRealtime(9)).thenReturn(new CollectorRealtimeSnapshot("collector-9", true, 1000, List.of(measurement)));
        when(mapper.selectCollectorSampleContentHash(9L, "S-01", 3L, "temperature")).thenReturn("different");
 
        new TestDataReceptionServiceImpl(mapper, tests, simulator).captureDevelopmentCollectorMeasurements();
 
        verify(mapper).insertSampleConflict(eq(1L), eq(3L), org.mockito.ArgumentMatchers.contains("采集器实时接收内容冲突"));
        verify(mapper, never()).insertCollectorSample(any(), any(), any(), anyString(), anyString(), any(), any(Date.class), anyString(), any(Date.class), anyString(), any(BigDecimal.class), anyString(), any(byte[].class));
    }
 
    private static TTest test(Date startedAt)
    {
        TTestChannelBinding binding = new TTestChannelBinding();
        binding.setBindingId(2L);
        binding.setChannelId(3L);
        binding.setDevid(1);
        binding.setChlid(1);
        TTestBattery battery = new TTestBattery();
        battery.setBinding(binding);
        TTest test = new TTest();
        test.setTestId(1L);
        test.setReceptionStatus("RECEIVING");
        test.setStartedAtUtc(startedAt);
        test.setBatteries(List.of(battery));
        return test;
    }
 
    private static BtsHistoryRecord record(long sequenceId, String deviceTime)
    {
        return new BtsHistoryRecord(sequenceId, Instant.parse(deviceTime), 1d, 2d, 3d, 4d,
                1, 1, "恒流充电", "cc");
    }
}