|
|
@@ -1,9 +1,14 @@
|
|
|
package com.etotem.cfc.service;
|
|
|
|
|
|
+import com.etotem.cfc.entity.HealthReport;
|
|
|
+import com.etotem.cfc.mapper.HealthReportMapper;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.Mockito;
|
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
@@ -49,4 +54,39 @@ class ReportDateExtractorTest {
|
|
|
assertNotNull(d);
|
|
|
assertEquals("2026-09-01", new SimpleDateFormat("yyyy-MM-dd").format(d));
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void backfillFillsExtractableDateAndTerminates() {
|
|
|
+ ReportDateExtractor e = new ReportDateExtractor();
|
|
|
+ HealthReportMapper mapper = Mockito.mock(HealthReportMapper.class);
|
|
|
+ ReflectionTestUtils.setField(e, "healthReportMapper", mapper);
|
|
|
+ HealthReport r = new HealthReport();
|
|
|
+ r.setId(1L);
|
|
|
+ r.setPayloadJson("{\"reportDate\":\"2026-08-15\"}");
|
|
|
+ Mockito.when(mapper.selectList(Mockito.any()))
|
|
|
+ .thenReturn(Arrays.asList(r), Collections.emptyList());
|
|
|
+ int n = e.backfillHistoricalReports();
|
|
|
+ assertEquals(1, n);
|
|
|
+ Mockito.verify(mapper, Mockito.times(1))
|
|
|
+ .updateById(Mockito.any(HealthReport.class));
|
|
|
+ assertEquals("2026-08-15",
|
|
|
+ new SimpleDateFormat("yyyy-MM-dd").format(r.getReportDate()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void backfillSkipsUnextractableWithoutWrite() {
|
|
|
+ ReportDateExtractor e = new ReportDateExtractor();
|
|
|
+ HealthReportMapper mapper = Mockito.mock(HealthReportMapper.class);
|
|
|
+ ReflectionTestUtils.setField(e, "healthReportMapper", mapper);
|
|
|
+ HealthReport r = new HealthReport();
|
|
|
+ r.setId(7L);
|
|
|
+ r.setPayloadJson("{\"reportDate\":\"\"}"); // 含字段但不可提取
|
|
|
+ Mockito.when(mapper.selectList(Mockito.any()))
|
|
|
+ .thenReturn(Arrays.asList(r), Collections.emptyList());
|
|
|
+ int n = e.backfillHistoricalReports();
|
|
|
+ assertEquals(1, n);
|
|
|
+ Mockito.verify(mapper, Mockito.never())
|
|
|
+ .updateById(Mockito.any(HealthReport.class));
|
|
|
+ assertNull(r.getReportDate());
|
|
|
+ }
|
|
|
}
|