|
|
@@ -0,0 +1,133 @@
|
|
|
+package com.zksy.WaterSupply;
|
|
|
+
|
|
|
+import com.zksy.WaterSupply.PipeNetworkStatistics.dto.PipeNetworkStatisticsDashboardDTO;
|
|
|
+import com.zksy.WaterSupply.PipeNetworkStatistics.mapper.PipeNetworkStatisticsMapper;
|
|
|
+import com.zksy.WaterSupply.PipeNetworkStatistics.service.impl.PipeNetworkStatisticsExportServiceImpl;
|
|
|
+import com.zksy.WaterSupply.WaterSupplyGisMap.dto.GisFeatureCollectionDTO;
|
|
|
+import com.zksy.WaterSupply.WaterSupplyGisMap.mapper.WaterSupplyGisMapMapper;
|
|
|
+import com.zksy.WaterSupply.WaterSupplyGisMap.service.impl.WaterSupplyGisMapServiceImpl;
|
|
|
+import org.apache.ibatis.builder.xml.XMLMapperBuilder;
|
|
|
+import org.apache.ibatis.io.Resources;
|
|
|
+import org.apache.ibatis.mapping.MappedStatement;
|
|
|
+import org.apache.ibatis.session.Configuration;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+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 java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
+class WaterUserTerminologyTest {
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private WaterSupplyGisMapMapper gisMapper;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ private WaterSupplyGisMapServiceImpl gisService;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void gisUserLayerReturnsEveryWaterUserCategoryWithUnifiedName() {
|
|
|
+ when(gisMapper.selectUserFeatures()).thenReturn(List.of(
|
|
|
+ userRow(1L, "RESIDENT", "居民用水户"),
|
|
|
+ userRow(2L, "INDUSTRIAL", "工业用水户"),
|
|
|
+ userRow(3L, "COMMERCIAL", "商业用水户"),
|
|
|
+ userRow(4L, "PUBLIC", "行政事业用水户")
|
|
|
+ ));
|
|
|
+
|
|
|
+ GisFeatureCollectionDTO result = gisService.getFeatures(Set.of("user"));
|
|
|
+
|
|
|
+ assertEquals(4, result.getFeatures().size());
|
|
|
+ assertEquals(Set.of("RESIDENT", "INDUSTRIAL", "COMMERCIAL", "PUBLIC"),
|
|
|
+ result.getFeatures().stream()
|
|
|
+ .map(feature -> (String) feature.getProperties().get("code"))
|
|
|
+ .collect(Collectors.toSet()));
|
|
|
+ result.getFeatures().forEach(feature -> {
|
|
|
+ assertEquals("user", feature.getProperties().get("facilityType"));
|
|
|
+ assertEquals("用水户", feature.getProperties().get("facilityTypeName"));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void mapperOverviewKeepsUserCodeAndLabelsAllWaterUsersWithoutCategoryFilter() throws Exception {
|
|
|
+ String gisOverviewSql = mappedSql(
|
|
|
+ "mapper/WaterSupply/WaterSupplyGisMap/WaterSupplyGisMapMapper.xml",
|
|
|
+ WaterSupplyGisMapMapper.class,
|
|
|
+ "selectOverview",
|
|
|
+ null
|
|
|
+ );
|
|
|
+ String gisUsersSql = mappedSql(
|
|
|
+ "mapper/WaterSupply/WaterSupplyGisMap/WaterSupplyGisMapMapper.xml",
|
|
|
+ WaterSupplyGisMapMapper.class,
|
|
|
+ "selectUserFeatures",
|
|
|
+ null
|
|
|
+ );
|
|
|
+ String statisticsOverviewSql = mappedSql(
|
|
|
+ "mapper/WaterSupply/PipeNetworkStatistics/PipeNetworkStatisticsMapper.xml",
|
|
|
+ PipeNetworkStatisticsMapper.class,
|
|
|
+ "selectFacilityOverview",
|
|
|
+ Collections.singletonMap("status", null)
|
|
|
+ );
|
|
|
+
|
|
|
+ assertTrue(gisOverviewSql.contains("SELECT 'user', '用水户'"));
|
|
|
+ assertTrue(statisticsOverviewSql.contains("SELECT 'user', '用水户'"));
|
|
|
+ assertFalse(gisOverviewSql.contains("大用水户"));
|
|
|
+ assertFalse(statisticsOverviewSql.contains("大用水户"));
|
|
|
+ assertTrue(gisUsersSql.contains("FROM app_user.water_user_info"));
|
|
|
+ assertFalse(gisUsersSql.matches("(?is).*\\buser_type\\s*=.*"),
|
|
|
+ "the user GIS layer must not restrict resident, industrial, commercial, or public-service users");
|
|
|
+ assertFalse(statisticsOverviewSql.matches("(?is).*\\buser_type\\s*=.*"),
|
|
|
+ "facility statistics must include every water user category");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void statisticsExportCreatesUnifiedWaterUserWorksheet() throws Exception {
|
|
|
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
|
+ new PipeNetworkStatisticsExportServiceImpl().write(output, new PipeNetworkStatisticsDashboardDTO());
|
|
|
+
|
|
|
+ try (Workbook workbook = new XSSFWorkbook(new ByteArrayInputStream(output.toByteArray()))) {
|
|
|
+ assertNotNull(workbook.getSheet("用水户统计"));
|
|
|
+ assertNull(workbook.getSheet("大用水户统计"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> userRow(Long id, String code, String name) {
|
|
|
+ return Map.of(
|
|
|
+ "id", id,
|
|
|
+ "code", code,
|
|
|
+ "name", name,
|
|
|
+ "status", "0",
|
|
|
+ "location", "测试地址",
|
|
|
+ "longitude", new BigDecimal("113.12345678"),
|
|
|
+ "latitude", new BigDecimal("23.12345678")
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private String mappedSql(String resource, Class<?> mapperType, String statementId, Object parameter) throws Exception {
|
|
|
+ Configuration configuration = new Configuration();
|
|
|
+ configuration.addMapper(mapperType);
|
|
|
+ try (InputStream input = Resources.getResourceAsStream(resource)) {
|
|
|
+ new XMLMapperBuilder(input, configuration, resource, configuration.getSqlFragments()).parse();
|
|
|
+ }
|
|
|
+ MappedStatement statement = configuration.getMappedStatement(mapperType.getName() + "." + statementId);
|
|
|
+ return statement.getBoundSql(parameter).getSql().replaceAll("\\s+", " ").trim();
|
|
|
+ }
|
|
|
+}
|