package com.ruoyi.system.service.impl;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.mockito.Mockito.when;
|
import java.time.Instant;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.mockito.Mock;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
import com.ruoyi.system.domain.TTest;
|
import com.ruoyi.system.domain.TTestDataOverride;
|
import com.ruoyi.system.mapper.TTestMapper;
|
import com.ruoyi.system.service.ITTestService;
|
|
/** Ticket01:三类查询直接返回数据库计算的当前有效数据,删除不抹除原始事实。 */
|
@ExtendWith(MockitoExtension.class)
|
class TestDataQueryServiceEffectiveDataTest
|
{
|
@Mock
|
private TTestMapper mapper;
|
@Mock
|
private ITTestService tests;
|
|
@Test
|
void appliesRealtimeEditAndFiltersDeletedRow()
|
{
|
when(tests.selectTestById(7L)).thenReturn(new TTest());
|
Map<String, Object> edited = row("adapterId", 1L, "devtype", 22, "deviceId", 22L, "subdevid", 0, "channelId", 8L, "sequenceId", 3L,
|
"voltage", 3.7d);
|
Map<String, Object> deleted = row("adapterId", 1L, "devtype", 22, "deviceId", 22L, "subdevid", 0, "channelId", 8L, "sequenceId", 4L,
|
"voltage", 3.8d);
|
TTestDataOverride edit = override("1:22:22:0:8:3", "{\"voltage\":\"4.1000\"}", false);
|
TTestDataOverride remove = override("1:22:22:0:8:4", null, true);
|
when(mapper.selectPersistedRecords(7L)).thenReturn(List.of(edited, deleted));
|
when(mapper.selectDataOverrides(7L, "REALTIME")).thenReturn(List.of(edit, remove));
|
|
List<Map<String, Object>> result = new TestDataQueryServiceImpl(mapper, tests).records(7L);
|
|
assertEquals(1, result.size());
|
assertEquals("4.1000", result.get(0).get("voltage"));
|
assertEquals(3L, result.get(0).get("sequenceId"));
|
}
|
|
@Test
|
void usesStepAndCycleStableIdentities()
|
{
|
when(tests.selectTestById(8L)).thenReturn(new TTest());
|
Map<String, Object> step = row("adapterId", 1L, "devtype", 22, "deviceId", 22L, "subdevid", 0, "channelId", 8L, "cycleNo", 2, "stepNo", 4,
|
"capacity", 1d);
|
Map<String, Object> cycle = row("adapterId", 1L, "devtype", 22, "deviceId", 22L, "subdevid", 0, "channelId", 8L, "cycleNo", 2,
|
"efficiency", 90d);
|
when(mapper.selectPersistedSteps(8L)).thenReturn(List.of(step));
|
when(mapper.selectPersistedCycles(8L)).thenReturn(List.of(cycle));
|
when(mapper.selectDataOverrides(8L, "STEP")).thenReturn(List.of(override("step:1:22:22:0:8:2:4",
|
"{\"capacity\":\"2.0000\"}", false)));
|
when(mapper.selectDataOverrides(8L, "CYCLE")).thenReturn(List.of(override("cycle:1:22:22:0:8:2",
|
"{\"efficiency\":\"95.00\"}", false)));
|
TestDataQueryServiceImpl service = new TestDataQueryServiceImpl(mapper, tests);
|
|
assertEquals("2.0000", service.steps(8L).get(0).get("capacity"));
|
assertEquals("95.00", service.cycles(8L).get(0).get("efficiency"));
|
assertTrue(service.cycles(8L).get(0).containsKey("cycleNo"));
|
}
|
|
@Test
|
void assignsCollectorObservationToCabinetStepByDeviceTime()
|
{
|
when(tests.selectTestById(9L)).thenReturn(new TTest());
|
Map<String, Object> cabinet = row("sourceType", "CABINET", "batteryNo", "B-01", "cycleNo", 1,
|
"stepNo", 2, "stepName", "恒流充电", "deviceTime", Instant.parse("2026-09-02T00:00:00Z"),
|
"adapterId", 1L, "devtype", 22, "deviceId", 22L, "subdevid", 0, "channelId", 8L, "sequenceId", 1L);
|
Map<String, Object> collector = row("sourceType", "COLLECTOR", "batteryNo", "B-01", "metricCode", "temperature",
|
"deviceTime", Instant.parse("2026-09-02T00:00:01Z"), "collectorId", 3L, "channelCode", "S-01", "sequenceId", 2L);
|
when(mapper.selectPersistedRecords(9L)).thenReturn(List.of(cabinet));
|
when(mapper.selectPersistedCollectorRecords(9L)).thenReturn(List.of(collector));
|
when(mapper.selectDataOverrides(9L, "REALTIME")).thenReturn(List.of());
|
|
List<Map<String, Object>> result = new TestDataQueryServiceImpl(mapper, tests).records(9L);
|
|
assertEquals(2, result.size());
|
Map<String, Object> assigned = result.stream().filter(item -> "COLLECTOR".equals(item.get("sourceType"))).findFirst().orElseThrow();
|
assertEquals("1", assigned.get("cycleNo"));
|
assertEquals("2", assigned.get("stepNo"));
|
assertEquals("恒流充电", assigned.get("stepName"));
|
}
|
|
private static TTestDataOverride override(String identity, String values, boolean deleted)
|
{
|
TTestDataOverride result = new TTestDataOverride();
|
result.setStableIdentity(identity);
|
result.setFieldValues(values);
|
result.setDeleted(deleted);
|
return result;
|
}
|
|
private static Map<String, Object> row(Object... values)
|
{
|
Map<String, Object> result = new HashMap<>();
|
for (int index = 0; index < values.length; index += 2) result.put(String.valueOf(values[index]), values[index + 1]);
|
return result;
|
}
|
}
|