|
|
@@ -0,0 +1,222 @@
|
|
|
+package com.zksy.WaterSupply;
|
|
|
+
|
|
|
+import com.zksy.WaterSupply.WaterPipeInfo.domain.WaterPipeInfo;
|
|
|
+import com.zksy.WaterSupply.WaterPipeInfo.service.IWaterPipeInfoService;
|
|
|
+import com.zksy.WaterSupply.WaterPlantInfo.domain.WaterPlantInfo;
|
|
|
+import com.zksy.WaterSupply.WaterPlantInfo.service.IWaterPlantInfoService;
|
|
|
+import com.zksy.WaterSupply.WaterPumpStation.domain.WaterPumpStation;
|
|
|
+import com.zksy.WaterSupply.WaterPumpStation.service.IWaterPumpStationService;
|
|
|
+import com.zksy.WaterSupply.WaterSourceInfo.domain.WaterSourceInfo;
|
|
|
+import com.zksy.WaterSupply.WaterSourceInfo.service.IWaterSourceInfoService;
|
|
|
+import com.zksy.WaterSupply.WaterUserInfo.domain.WaterUserInfo;
|
|
|
+import com.zksy.WaterSupply.WaterUserInfo.service.IWaterUserInfoService;
|
|
|
+import com.zksy.WaterSupply.export.WaterPipeExportDTO;
|
|
|
+import com.zksy.WaterSupply.export.WaterPlantExportDTO;
|
|
|
+import com.zksy.WaterSupply.export.WaterPumpStationExportDTO;
|
|
|
+import com.zksy.WaterSupply.export.WaterSourceExportDTO;
|
|
|
+import com.zksy.WaterSupply.export.WaterUserExportDTO;
|
|
|
+import com.zksy.WaterSupply.importing.WaterFacilityImportService;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+class WaterFacilityImportServiceTest {
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void sourceImportCreatesRetrievableCoordinatesAndAuditFields() {
|
|
|
+ IWaterSourceInfoService sourceService = mock(IWaterSourceInfoService.class);
|
|
|
+ when(sourceService.getOne(any(), eq(false))).thenReturn(null);
|
|
|
+ when(sourceService.save(any())).thenReturn(true);
|
|
|
+ WaterFacilityImportService service = serviceWith(sourceService);
|
|
|
+
|
|
|
+ WaterSourceExportDTO row = new WaterSourceExportDTO();
|
|
|
+ row.setSourceId("SRC-IMPORT-001");
|
|
|
+ row.setSourceName("东湖水源地");
|
|
|
+ row.setLongitude(new BigDecimal("112.12345678"));
|
|
|
+ row.setLatitude(new BigDecimal("28.12345678"));
|
|
|
+
|
|
|
+ String result = service.importSources(List.of(row), false, "tester");
|
|
|
+
|
|
|
+ ArgumentCaptor<WaterSourceInfo> saved = ArgumentCaptor.forClass(WaterSourceInfo.class);
|
|
|
+ verify(sourceService).save(saved.capture());
|
|
|
+ assertEquals("SRC-IMPORT-001", saved.getValue().getSourceId());
|
|
|
+ assertEquals(new BigDecimal("112.12345678"), saved.getValue().getLongitude());
|
|
|
+ assertEquals(new BigDecimal("28.12345678"), saved.getValue().getLatitude());
|
|
|
+ assertEquals("0", saved.getValue().getStatus());
|
|
|
+ assertEquals("tester", saved.getValue().getCreateBy());
|
|
|
+ assertEquals("成功导入 1 条水源地数据", result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void plantImportCreatesRetrievableCoordinates() {
|
|
|
+ IWaterPlantInfoService plantService = mock(IWaterPlantInfoService.class);
|
|
|
+ when(plantService.getOne(any(), eq(false))).thenReturn(null);
|
|
|
+ when(plantService.save(any())).thenReturn(true);
|
|
|
+ WaterFacilityImportService service = serviceWith(plantService);
|
|
|
+
|
|
|
+ WaterPlantExportDTO row = new WaterPlantExportDTO();
|
|
|
+ row.setPlantCode("PLANT-IMPORT-001");
|
|
|
+ row.setPlantName("第一水厂");
|
|
|
+ row.setLongitude(new BigDecimal("112.33333333"));
|
|
|
+ row.setLatitude(new BigDecimal("28.33333333"));
|
|
|
+
|
|
|
+ String result = service.importPlants(List.of(row), false, "tester");
|
|
|
+
|
|
|
+ ArgumentCaptor<WaterPlantInfo> saved = ArgumentCaptor.forClass(WaterPlantInfo.class);
|
|
|
+ verify(plantService).save(saved.capture());
|
|
|
+ assertEquals("PLANT-IMPORT-001", saved.getValue().getPlantCode());
|
|
|
+ assertEquals(new BigDecimal("112.33333333"), saved.getValue().getLongitude());
|
|
|
+ assertEquals(new BigDecimal("28.33333333"), saved.getValue().getLatitude());
|
|
|
+ assertEquals("成功导入 1 条水厂数据", result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void pumpImportCreatesRetrievableCoordinates() {
|
|
|
+ IWaterPumpStationService pumpService = mock(IWaterPumpStationService.class);
|
|
|
+ when(pumpService.getOne(any(), eq(false))).thenReturn(null);
|
|
|
+ when(pumpService.save(any())).thenReturn(true);
|
|
|
+ WaterFacilityImportService service = serviceWith(pumpService);
|
|
|
+
|
|
|
+ WaterPumpStationExportDTO row = new WaterPumpStationExportDTO();
|
|
|
+ row.setStationCode("PUMP-IMPORT-001");
|
|
|
+ row.setStationName("加压泵站");
|
|
|
+ row.setLongitude(new BigDecimal("112.44444444"));
|
|
|
+ row.setLatitude(new BigDecimal("28.44444444"));
|
|
|
+
|
|
|
+ String result = service.importPumps(List.of(row), false, "tester");
|
|
|
+
|
|
|
+ ArgumentCaptor<WaterPumpStation> saved = ArgumentCaptor.forClass(WaterPumpStation.class);
|
|
|
+ verify(pumpService).save(saved.capture());
|
|
|
+ assertEquals("PUMP-IMPORT-001", saved.getValue().getStationCode());
|
|
|
+ assertEquals(new BigDecimal("112.44444444"), saved.getValue().getLongitude());
|
|
|
+ assertEquals(new BigDecimal("28.44444444"), saved.getValue().getLatitude());
|
|
|
+ assertEquals("成功导入 1 条泵站数据", result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void pipeImportRetainsStartAndEndCoordinates() {
|
|
|
+ IWaterPipeInfoService pipeService = mock(IWaterPipeInfoService.class);
|
|
|
+ when(pipeService.getOne(any(), eq(false))).thenReturn(null);
|
|
|
+ when(pipeService.save(any())).thenReturn(true);
|
|
|
+ WaterFacilityImportService service = new WaterFacilityImportService(
|
|
|
+ mock(IWaterSourceInfoService.class),
|
|
|
+ mock(IWaterPlantInfoService.class),
|
|
|
+ mock(IWaterPumpStationService.class),
|
|
|
+ pipeService,
|
|
|
+ mock(IWaterUserInfoService.class));
|
|
|
+
|
|
|
+ WaterPipeExportDTO row = new WaterPipeExportDTO();
|
|
|
+ row.setPipeCode("PIPE-IMPORT-001");
|
|
|
+ row.setPipeName("输水干线");
|
|
|
+ row.setStartLongitude(new BigDecimal("112.11111111"));
|
|
|
+ row.setStartLatitude(new BigDecimal("28.11111111"));
|
|
|
+ row.setEndLongitude(new BigDecimal("112.22222222"));
|
|
|
+ row.setEndLatitude(new BigDecimal("28.22222222"));
|
|
|
+
|
|
|
+ String result = service.importPipes(List.of(row), false, "tester");
|
|
|
+
|
|
|
+ ArgumentCaptor<WaterPipeInfo> saved = ArgumentCaptor.forClass(WaterPipeInfo.class);
|
|
|
+ verify(pipeService).save(saved.capture());
|
|
|
+ assertEquals("PIPE-IMPORT-001", saved.getValue().getPipeCode());
|
|
|
+ assertEquals(new BigDecimal("112.11111111"), saved.getValue().getStartLongitude());
|
|
|
+ assertEquals(new BigDecimal("28.11111111"), saved.getValue().getStartLatitude());
|
|
|
+ assertEquals(new BigDecimal("112.22222222"), saved.getValue().getEndLongitude());
|
|
|
+ assertEquals(new BigDecimal("28.22222222"), saved.getValue().getEndLatitude());
|
|
|
+ assertEquals("成功导入 1 条管网数据", result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void userImportCreatesRetrievableCoordinates() {
|
|
|
+ IWaterUserInfoService userService = mock(IWaterUserInfoService.class);
|
|
|
+ when(userService.getOne(any(), eq(false))).thenReturn(null);
|
|
|
+ when(userService.save(any())).thenReturn(true);
|
|
|
+ WaterFacilityImportService service = serviceWith(userService);
|
|
|
+
|
|
|
+ WaterUserExportDTO row = new WaterUserExportDTO();
|
|
|
+ row.setUserCode("USER-IMPORT-001");
|
|
|
+ row.setUserName("园区工业用水户");
|
|
|
+ row.setLongitude(new BigDecimal("112.55555555"));
|
|
|
+ row.setLatitude(new BigDecimal("28.55555555"));
|
|
|
+
|
|
|
+ String result = service.importUsers(List.of(row), false, "tester");
|
|
|
+
|
|
|
+ ArgumentCaptor<WaterUserInfo> saved = ArgumentCaptor.forClass(WaterUserInfo.class);
|
|
|
+ verify(userService).save(saved.capture());
|
|
|
+ assertEquals("USER-IMPORT-001", saved.getValue().getUserCode());
|
|
|
+ assertEquals(new BigDecimal("112.55555555"), saved.getValue().getLongitude());
|
|
|
+ assertEquals(new BigDecimal("28.55555555"), saved.getValue().getLatitude());
|
|
|
+ assertEquals("成功导入 1 条用水户数据", result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void existingRowsAreSkippedUntilUpdateSupportIsEnabled() {
|
|
|
+ IWaterSourceInfoService sourceService = mock(IWaterSourceInfoService.class);
|
|
|
+ WaterSourceInfo existing = new WaterSourceInfo();
|
|
|
+ existing.setSourceId("SRC-IMPORT-001");
|
|
|
+ when(sourceService.getOne(any(), eq(false))).thenReturn(existing);
|
|
|
+ WaterFacilityImportService service = serviceWith(sourceService);
|
|
|
+
|
|
|
+ WaterSourceExportDTO row = new WaterSourceExportDTO();
|
|
|
+ row.setSourceId("SRC-IMPORT-001");
|
|
|
+ row.setLongitude(new BigDecimal("112.66666666"));
|
|
|
+ row.setLatitude(new BigDecimal("28.66666666"));
|
|
|
+
|
|
|
+ assertEquals("成功导入 0 条水源地数据", service.importSources(List.of(row), false, "tester"));
|
|
|
+ verify(sourceService, never()).save(any());
|
|
|
+ verify(sourceService, never()).updateById(any());
|
|
|
+
|
|
|
+ when(sourceService.updateById(any())).thenReturn(true);
|
|
|
+ assertEquals("成功导入 1 条水源地数据", service.importSources(List.of(row), true, "tester"));
|
|
|
+
|
|
|
+ ArgumentCaptor<WaterSourceInfo> updated = ArgumentCaptor.forClass(WaterSourceInfo.class);
|
|
|
+ verify(sourceService).updateById(updated.capture());
|
|
|
+ assertEquals("tester", updated.getValue().getUpdateBy());
|
|
|
+ assertEquals(new BigDecimal("112.66666666"), updated.getValue().getLongitude());
|
|
|
+ assertEquals(new BigDecimal("28.66666666"), updated.getValue().getLatitude());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static WaterFacilityImportService serviceWith(IWaterPlantInfoService plantService) {
|
|
|
+ return new WaterFacilityImportService(
|
|
|
+ mock(IWaterSourceInfoService.class),
|
|
|
+ plantService,
|
|
|
+ mock(IWaterPumpStationService.class),
|
|
|
+ mock(IWaterPipeInfoService.class),
|
|
|
+ mock(IWaterUserInfoService.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static WaterFacilityImportService serviceWith(IWaterPumpStationService pumpService) {
|
|
|
+ return new WaterFacilityImportService(
|
|
|
+ mock(IWaterSourceInfoService.class),
|
|
|
+ mock(IWaterPlantInfoService.class),
|
|
|
+ pumpService,
|
|
|
+ mock(IWaterPipeInfoService.class),
|
|
|
+ mock(IWaterUserInfoService.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static WaterFacilityImportService serviceWith(IWaterUserInfoService userService) {
|
|
|
+ return new WaterFacilityImportService(
|
|
|
+ mock(IWaterSourceInfoService.class),
|
|
|
+ mock(IWaterPlantInfoService.class),
|
|
|
+ mock(IWaterPumpStationService.class),
|
|
|
+ mock(IWaterPipeInfoService.class),
|
|
|
+ userService);
|
|
|
+ }
|
|
|
+ private static WaterFacilityImportService serviceWith(IWaterSourceInfoService sourceService) {
|
|
|
+ return new WaterFacilityImportService(
|
|
|
+ sourceService,
|
|
|
+ mock(IWaterPlantInfoService.class),
|
|
|
+ mock(IWaterPumpStationService.class),
|
|
|
+ mock(IWaterPipeInfoService.class),
|
|
|
+ mock(IWaterUserInfoService.class));
|
|
|
+ }
|
|
|
+}
|