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 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 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 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; } }