package com.zksy.WaterSupply; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.zksy.WaterSupply.WaterPipeInfo.domain.WaterPipeInfo; import com.zksy.WaterSupply.WaterPipeInfo.mapper.WaterPipeInfoMapper; import com.zksy.WaterSupply.WaterPipeInfo.service.impl.WaterPipeInfoServiceImpl; import com.zksy.WaterSupply.WaterPlantInfo.domain.WaterPlantInfo; import com.zksy.WaterSupply.WaterPlantInfo.mapper.WaterPlantInfoMapper; import com.zksy.WaterSupply.WaterPlantInfo.service.impl.WaterPlantInfoServiceImpl; import com.zksy.WaterSupply.WaterPumpStation.domain.WaterPumpStation; import com.zksy.WaterSupply.WaterPumpStation.mapper.WaterPumpStationMapper; import com.zksy.WaterSupply.WaterPumpStation.service.impl.WaterPumpStationServiceImpl; import com.zksy.WaterSupply.WaterSourceInfo.domain.WaterSourceInfo; import com.zksy.WaterSupply.WaterSourceInfo.mapper.WaterSourceInfoMapper; import com.zksy.WaterSupply.WaterSourceInfo.service.impl.WaterSourceInfoServiceImpl; import com.zksy.WaterSupply.WaterUserInfo.domain.WaterUserInfo; import com.zksy.WaterSupply.WaterUserInfo.mapper.WaterUserInfoMapper; import com.zksy.WaterSupply.WaterUserInfo.service.impl.WaterUserInfoServiceImpl; import com.zksy.WaterSupply.export.WaterPipeExportDTO; import org.junit.jupiter.api.Test; import org.mockito.ArgumentMatchers; import org.springframework.test.util.ReflectionTestUtils; import java.util.List; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.lang.reflect.Field; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class WaterFacilityQueryServiceTest { @Test void pipeQueryUsesDatabasePaginationAndFilters() { WaterPipeInfoMapper mapper = mock(WaterPipeInfoMapper.class); Page expected = pageOf(new WaterPipeInfo()); when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected); WaterPipeInfoServiceImpl service = new WaterPipeInfoServiceImpl(); ReflectionTestUtils.setField(service, "baseMapper", mapper); IPage actual = service.queryPage(new Page<>(2, 20), "P-1", "主干", "PE", "0", 300, 2020, null, null); assertEquals(expected, actual); verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any()); } @Test void plantQueryUsesDatabasePagination() { WaterPlantInfoMapper mapper = mock(WaterPlantInfoMapper.class); Page expected = pageOf(new WaterPlantInfo()); when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected); WaterPlantInfoServiceImpl service = new WaterPlantInfoServiceImpl(); ReflectionTestUtils.setField(service, "baseMapper", mapper); assertEquals(expected, service.queryPage(new Page<>(3, 5), "P", "厂", "0", "负责人", null, null)); verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any()); } @Test void pumpStationQueryUsesDatabasePagination() { WaterPumpStationMapper mapper = mock(WaterPumpStationMapper.class); Page expected = pageOf(new WaterPumpStation()); when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected); WaterPumpStationServiceImpl service = new WaterPumpStationServiceImpl(); ReflectionTestUtils.setField(service, "baseMapper", mapper); assertEquals(expected, service.queryPage(new Page<>(1, 10), "S", "泵站", "0", "负责人", null, null)); verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any()); } @Test void sourceQueryUsesDatabasePagination() { WaterSourceInfoMapper mapper = mock(WaterSourceInfoMapper.class); Page expected = pageOf(new WaterSourceInfo()); when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected); WaterSourceInfoServiceImpl service = new WaterSourceInfoServiceImpl(); ReflectionTestUtils.setField(service, "baseMapper", mapper); assertEquals(expected, service.queryPage(new Page<>(1, 10), "W", "水源", "0", "单位", null, null)); verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any()); } @Test void waterUserQueryUsesDatabasePagination() { WaterUserInfoMapper mapper = mock(WaterUserInfoMapper.class); Page expected = pageOf(new WaterUserInfo()); when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected); WaterUserInfoServiceImpl service = new WaterUserInfoServiceImpl(); ReflectionTestUtils.setField(service, "baseMapper", mapper); assertEquals(expected, service.queryPage(new Page<>(4, 10), "U", "居民", "0", "居民", null, null)); verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any()); } @Test void facilityMapperQueriesUseKingbaseAppUserSchema() throws Exception { String[] mapperPaths = { "mapper/WaterSupply/WaterPipeInfo/WaterPipeInfoMapper.xml", "mapper/WaterSupply/WaterPlantInfo/WaterPlantInfoMapper.xml", "mapper/WaterSupply/WaterPumpStation/WaterPumpStationMapper.xml", "mapper/WaterSupply/WaterSourceInfo/WaterSourceInfoMapper.xml", "mapper/WaterSupply/WaterUserInfo/WaterUserInfoMapper.xml" }; for (String path : mapperPaths) { try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) { String xml = new String(input.readAllBytes(), StandardCharsets.UTF_8); org.junit.jupiter.api.Assertions.assertFalse( xml.matches("(?s).*\\b(?:FROM|UPDATE|DELETE FROM)\\s+water_(?:pipe_info|plant_info|pump_station|source_info|user_info)\\b.*"), path + " must qualify facilities with app_user schema"); } } } @Test void pipeExportIncludesLayingYear() { WaterPipeInfo pipe = new WaterPipeInfo(); pipe.setLayingYear(1998); assertEquals(1998, WaterPipeExportDTO.from(pipe).getLayingYear()); try { Field field = WaterPipeExportDTO.class.getDeclaredField("layingYear"); assertEquals("铺设年份", field.getAnnotation(com.zksy.common.annotation.Excel.class).name()); } catch (ReflectiveOperationException e) { throw new AssertionError("layingYear must be an exported Excel field", e); } } private static Page pageOf(T record) { Page page = new Page<>(1, 1); page.setRecords(List.of(record)); page.setTotal(1); return page; } }