WaterFacilityQueryServiceTest.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.zksy.WaterSupply;
  2. import com.baomidou.mybatisplus.core.metadata.IPage;
  3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  4. import com.zksy.WaterSupply.WaterPipeInfo.domain.WaterPipeInfo;
  5. import com.zksy.WaterSupply.WaterPipeInfo.mapper.WaterPipeInfoMapper;
  6. import com.zksy.WaterSupply.WaterPipeInfo.service.impl.WaterPipeInfoServiceImpl;
  7. import com.zksy.WaterSupply.WaterPlantInfo.domain.WaterPlantInfo;
  8. import com.zksy.WaterSupply.WaterPlantInfo.mapper.WaterPlantInfoMapper;
  9. import com.zksy.WaterSupply.WaterPlantInfo.service.impl.WaterPlantInfoServiceImpl;
  10. import com.zksy.WaterSupply.WaterPumpStation.domain.WaterPumpStation;
  11. import com.zksy.WaterSupply.WaterPumpStation.mapper.WaterPumpStationMapper;
  12. import com.zksy.WaterSupply.WaterPumpStation.service.impl.WaterPumpStationServiceImpl;
  13. import com.zksy.WaterSupply.WaterSourceInfo.domain.WaterSourceInfo;
  14. import com.zksy.WaterSupply.WaterSourceInfo.mapper.WaterSourceInfoMapper;
  15. import com.zksy.WaterSupply.WaterSourceInfo.service.impl.WaterSourceInfoServiceImpl;
  16. import com.zksy.WaterSupply.WaterUserInfo.domain.WaterUserInfo;
  17. import com.zksy.WaterSupply.WaterUserInfo.mapper.WaterUserInfoMapper;
  18. import com.zksy.WaterSupply.WaterUserInfo.service.impl.WaterUserInfoServiceImpl;
  19. import com.zksy.WaterSupply.export.WaterPipeExportDTO;
  20. import org.junit.jupiter.api.Test;
  21. import org.mockito.ArgumentMatchers;
  22. import org.springframework.test.util.ReflectionTestUtils;
  23. import java.util.List;
  24. import java.io.InputStream;
  25. import java.nio.charset.StandardCharsets;
  26. import java.lang.reflect.Field;
  27. import static org.junit.jupiter.api.Assertions.assertEquals;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.verify;
  30. import static org.mockito.Mockito.when;
  31. class WaterFacilityQueryServiceTest {
  32. @Test
  33. void pipeQueryUsesDatabasePaginationAndFilters() {
  34. WaterPipeInfoMapper mapper = mock(WaterPipeInfoMapper.class);
  35. Page<WaterPipeInfo> expected = pageOf(new WaterPipeInfo());
  36. when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected);
  37. WaterPipeInfoServiceImpl service = new WaterPipeInfoServiceImpl();
  38. ReflectionTestUtils.setField(service, "baseMapper", mapper);
  39. IPage<WaterPipeInfo> actual = service.queryPage(new Page<>(2, 20), "P-1", "主干", "PE", "0", 300, 2020, null, null);
  40. assertEquals(expected, actual);
  41. verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any());
  42. }
  43. @Test
  44. void plantQueryUsesDatabasePagination() {
  45. WaterPlantInfoMapper mapper = mock(WaterPlantInfoMapper.class);
  46. Page<WaterPlantInfo> expected = pageOf(new WaterPlantInfo());
  47. when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected);
  48. WaterPlantInfoServiceImpl service = new WaterPlantInfoServiceImpl();
  49. ReflectionTestUtils.setField(service, "baseMapper", mapper);
  50. assertEquals(expected, service.queryPage(new Page<>(3, 5), "P", "厂", "0", "负责人", null, null));
  51. verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any());
  52. }
  53. @Test
  54. void pumpStationQueryUsesDatabasePagination() {
  55. WaterPumpStationMapper mapper = mock(WaterPumpStationMapper.class);
  56. Page<WaterPumpStation> expected = pageOf(new WaterPumpStation());
  57. when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected);
  58. WaterPumpStationServiceImpl service = new WaterPumpStationServiceImpl();
  59. ReflectionTestUtils.setField(service, "baseMapper", mapper);
  60. assertEquals(expected, service.queryPage(new Page<>(1, 10), "S", "泵站", "0", "负责人", null, null));
  61. verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any());
  62. }
  63. @Test
  64. void sourceQueryUsesDatabasePagination() {
  65. WaterSourceInfoMapper mapper = mock(WaterSourceInfoMapper.class);
  66. Page<WaterSourceInfo> expected = pageOf(new WaterSourceInfo());
  67. when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected);
  68. WaterSourceInfoServiceImpl service = new WaterSourceInfoServiceImpl();
  69. ReflectionTestUtils.setField(service, "baseMapper", mapper);
  70. assertEquals(expected, service.queryPage(new Page<>(1, 10), "W", "水源", "0", "单位", null, null));
  71. verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any());
  72. }
  73. @Test
  74. void waterUserQueryUsesDatabasePagination() {
  75. WaterUserInfoMapper mapper = mock(WaterUserInfoMapper.class);
  76. Page<WaterUserInfo> expected = pageOf(new WaterUserInfo());
  77. when(mapper.selectPage(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(expected);
  78. WaterUserInfoServiceImpl service = new WaterUserInfoServiceImpl();
  79. ReflectionTestUtils.setField(service, "baseMapper", mapper);
  80. assertEquals(expected, service.queryPage(new Page<>(4, 10), "U", "居民", "0", "居民", null, null));
  81. verify(mapper).selectPage(ArgumentMatchers.any(), ArgumentMatchers.any());
  82. }
  83. @Test
  84. void facilityMapperQueriesUseKingbaseAppUserSchema() throws Exception {
  85. String[] mapperPaths = {
  86. "mapper/WaterSupply/WaterPipeInfo/WaterPipeInfoMapper.xml",
  87. "mapper/WaterSupply/WaterPlantInfo/WaterPlantInfoMapper.xml",
  88. "mapper/WaterSupply/WaterPumpStation/WaterPumpStationMapper.xml",
  89. "mapper/WaterSupply/WaterSourceInfo/WaterSourceInfoMapper.xml",
  90. "mapper/WaterSupply/WaterUserInfo/WaterUserInfoMapper.xml"
  91. };
  92. for (String path : mapperPaths) {
  93. try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) {
  94. String xml = new String(input.readAllBytes(), StandardCharsets.UTF_8);
  95. org.junit.jupiter.api.Assertions.assertFalse(
  96. xml.matches("(?s).*\\b(?:FROM|UPDATE|DELETE FROM)\\s+water_(?:pipe_info|plant_info|pump_station|source_info|user_info)\\b.*"),
  97. path + " must qualify facilities with app_user schema");
  98. }
  99. }
  100. }
  101. @Test
  102. void pipeExportIncludesLayingYear() {
  103. WaterPipeInfo pipe = new WaterPipeInfo();
  104. pipe.setLayingYear(1998);
  105. assertEquals(1998, WaterPipeExportDTO.from(pipe).getLayingYear());
  106. try {
  107. Field field = WaterPipeExportDTO.class.getDeclaredField("layingYear");
  108. assertEquals("铺设年份", field.getAnnotation(com.zksy.common.annotation.Excel.class).name());
  109. } catch (ReflectiveOperationException e) {
  110. throw new AssertionError("layingYear must be an exported Excel field", e);
  111. }
  112. }
  113. private static <T> Page<T> pageOf(T record) {
  114. Page<T> page = new Page<>(1, 1);
  115. page.setRecords(List.of(record));
  116. page.setTotal(1);
  117. return page;
  118. }
  119. }