Index: ssts-web/src/test/java/test/forgon/disinfectsystem/reports/RealTimeBulletinBoardWorkloadHelperTests.java =================================================================== diff -u --- ssts-web/src/test/java/test/forgon/disinfectsystem/reports/RealTimeBulletinBoardWorkloadHelperTests.java (revision 0) +++ ssts-web/src/test/java/test/forgon/disinfectsystem/reports/RealTimeBulletinBoardWorkloadHelperTests.java (revision 41354) @@ -0,0 +1,537 @@ +package test.forgon.disinfectsystem.reports; + +import static org.junit.jupiter.api.Assertions.assertEquals; +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.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.when; + +// 数据库相关 +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import com.forgon.databaseadapter.service.DateQueryAdapter; +import com.forgon.disinfectsystem.basedatamanager.supplyroomconfig.service.SupplyRoomConfigManager; +import com.forgon.disinfectsystem.entity.basedatamanager.supplyroomconfig.SupplyRoomConfig; +import com.forgon.disinfectsystem.jasperreports.javabeansource.StatisticalWorkload; +import com.forgon.disinfectsystem.jasperreports.service.dataindex.DataIndex; +import com.forgon.disinfectsystem.jasperreports.util.RealTimeBulletinBoardWorkloadHelper; +import com.forgon.disinfectsystem.jasperreports.util.ReportSqlUtil; +import com.forgon.exception.SystemException; +import com.forgon.tools.db.InitDbConnection; +import com.forgon.tools.hibernate.ObjectDao; +/** + * 数据看板 + * + */ +public class RealTimeBulletinBoardWorkloadHelperTests { + private AutoCloseable closeable; + + + + @InjectMocks + private RealTimeBulletinBoardWorkloadHelper underTest; + + @Mock + private SupplyRoomConfigManager supplyRoomConfigManager; + + @Mock + private ReportSqlUtil reportSqlUtil; + + @Mock + private ObjectDao objectDao; + + @Mock + private DateQueryAdapter dateQueryAdapter; + + @Mock + private DataIndex dataIndex; + + @Mock + private InitDbConnection dbConnection; + + @Mock + private ResultSet resultSet; + + @Mock + private ResultSet resultSet2; + + @Mock + private SupplyRoomConfig supplyRoomConfig; + + private final String testHandleDepartCoding = "TEST_DEPART"; + private final String testStartDate = "2024-01-15 00:00:00"; + private final String testEndDate = "2024-01-15 23:59:59"; + + @Before + public void setUp() { + closeable = MockitoAnnotations.openMocks(this); + // 设置基本的 Mock 行为 + when(supplyRoomConfigManager.getFirstSupplyRoomConfig()).thenReturn(supplyRoomConfig); + when(supplyRoomConfig.getOrgUnitCoding()).thenReturn(testHandleDepartCoding); + when(supplyRoomConfigManager.getSystemParamsObj()).thenReturn(supplyRoomConfig); + when(supplyRoomConfig.getDayStartTime()).thenReturn("00:00"); + when(supplyRoomConfig.getDashboardsQueryCycle()).thenReturn(7); + + // Mock 日期适配器 + when(dateQueryAdapter.dateAdapter(anyString())).thenAnswer(invocation -> { + String date = invocation.getArgument(0); + return "'" + date + "'"; + }); + + // Mock 数据库连接信息 + when(dbConnection.isOracle()).thenReturn(false); + when(dbConnection.isSqlServer()).thenReturn(false); + when(dbConnection.isMySQLOrTiDB()).thenReturn(true); + when(dbConnection.getNoLockSql()).thenReturn(""); + } + @After + public void tearDown() throws Exception { + closeable.close(); + } + // 测试 getRealTimeBulletinBoardWorkloadData 方法 + @Test + public void testGetRealTimeBulletinBoardWorkloadData_Success() throws Exception { + // 准备 Mock 数据 + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + // Mock 各个 SQL 查询的结果 + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(true, false); // 一条记录 + when(resultSet.getString("type")).thenReturn("applyAmount"); + when(resultSet.getInt("amount")).thenReturn(100); + when(resultSet.getInt("urgentAmount")).thenReturn(0); + + // 执行测试 + JSONObject result = underTest.getRealTimeBulletinBoardWorkloadData(); + + // 验证结果 + assertNotNull(result); + assertEquals(100, result.getInt("applyAmount")); + } + + @Test + public void testGetRealTimeBulletinBoardWorkloadData_WithEmptyResult() throws Exception { + // 准备 Mock 数据 + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + // Mock 空结果集 + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(false); + + // 执行测试 + JSONObject result = underTest.getRealTimeBulletinBoardWorkloadData(); + + // 验证结果为空对象 + assertNotNull(result); + + + assertTrue(result.optJSONArray("toBeInvoiceUrgentSum").size() == 0); + } + + // 测试 getEquipmentData 方法 + @Test + public void testGetEquipmentData_Success() throws Exception { + // 准备 Mock 数据 + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + // Mock 设备查询结果 + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(true, true, false); // 两条记录 + when(resultSet.getString("name")).thenReturn("清洗机1", "灭菌炉1"); + when(resultSet.getString("status")).thenReturn("使用中", "空闲"); + when(resultSet.getTimestamp("dateStr")).thenReturn(new Timestamp(System.currentTimeMillis())); + when(resultSet.getInt("runCount")).thenReturn(5, 3); + + // 执行测试 + List result = underTest.getEquipmentData(); + + // 验证结果 + assertNotNull(result); + assertEquals(2, result.size()); + assertEquals("清洗机1", result.get(0).getString("name")); + assertEquals("使用中", result.get(0).getString("status")); + } + + @Test + public void testGetEquipmentData_WithDisplayMod() throws Exception { + // 准备 Mock 数据 + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + // Mock 设备查询结果 + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(true, false); // 一条记录 + when(resultSet.getString("name")).thenReturn("清洗机1"); + when(resultSet.getString("status")).thenReturn("空闲"); + when(resultSet.getTimestamp("dateStr")).thenReturn(null); + when(resultSet.getInt("runCount")).thenReturn(0); + + // 执行测试 - 使用 displayMod = "1" + List result = underTest.getEquipmentData("1"); + + // 验证结果 + assertNotNull(result); + assertEquals(1, result.size()); + assertEquals("清洗机1", result.get(0).getString("name")); + } + + // 测试 getTousseSummary 方法 + @Test + public void testGetTousseSummary_Success() throws Exception { + // 准备 Mock 数据 + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + // Mock SQL 查询结果 + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(true, false); // 一条记录 + when(resultSet.getString("type")).thenReturn("applyAmount"); + when(resultSet.getInt("amount")).thenReturn(50); + when(resultSet.getInt("urgentAmount")).thenReturn(0); + + // 执行测试 + JSONObject result = underTest.getTousseSummary(); + + // 验证结果 + assertNotNull(result); + assertEquals(50, result.getInt("applyAmount")); + } + + // 测试 getForeignTousseSummary 方法 + @Test + public void testGetForeignTousseSummary_Success() throws Exception { + // 准备 Mock 数据 + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + // Mock SQL 查询结果 + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(true, false); // 一条记录 + when(resultSet.getString("type")).thenReturn("foreignApplyAmount"); + when(resultSet.getInt("amount")).thenReturn(20); + + // 执行测试 + JSONObject result = underTest.getForeignTousseSummary(); + + // 验证结果 + assertNotNull(result); + assertEquals(20, result.getInt("foreignApplyAmount")); + } + + // 测试 getUrgentgoodssummary 方法 + @Test + public void testGetUrgentgoodssummary_Success() throws Exception { + // 准备 Mock 数据 + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + // Mock 加急物品查询结果 + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(true, false); // 一条记录 + when(resultSet.getInt("urgentAmount")).thenReturn(5); + when(resultSet.getString("name")).thenReturn("紧急"); + when(resultSet.getString("tousseName")).thenReturn("手术器械包"); + when(resultSet.getString("colorCode")).thenReturn("#FF0000"); + + // 执行测试 + JSONArray result = underTest.getUrgentgoodssummary(); + + // 验证结果 + assertNotNull(result); + assertEquals(1, result.size()); + JSONObject item = result.getJSONObject(0); + assertEquals(5, item.getInt("urgentAmount")); + assertEquals("紧急", item.getString("name")); + } + + // 测试 getDataByDataSources 方法的各种数据源 + @Test + public void testGetDataByDataSources_ForeignTousseSummary() { + // 准备请求参数 + Map requestParameters = new HashMap<>(); + requestParameters.put("dataSources", "foreigntoussesummary"); + + // Mock ForeignTousseSummary 方法 + JSONObject mockResult = new JSONObject(); + mockResult.put("foreignApplyAmount", 30); + // 使用 Spy 来 Mock 具体方法 + RealTimeBulletinBoardWorkloadHelper spyHelper = Mockito.spy(underTest); + doReturn(mockResult).when(spyHelper).getForeignTousseSummary(); + + // 执行测试 + String result = spyHelper.getDataByDataSources(requestParameters); + + // 验证结果 + assertNotNull(result); + assertTrue(result.contains("foreignApplyAmount")); + } + + @Test + public void testGetDataByDataSources_TousseSummary() { + // 准备请求参数 + Map requestParameters = new HashMap<>(); + requestParameters.put("dataSources", "toussesummary"); + + // Mock TousseSummary 方法 + JSONObject mockResult = new JSONObject(); + mockResult.put("applyAmount", 40); + RealTimeBulletinBoardWorkloadHelper spyHelper = Mockito.spy(underTest); + doReturn(mockResult).when(spyHelper).getTousseSummary(); + + // 执行测试 + String result = spyHelper.getDataByDataSources(requestParameters); + + // 验证结果 + assertNotNull(result); + assertTrue(result.contains("applyAmount")); + } + + @Test + public void testGetDataByDataSources_DeviceUsageSummary() throws Exception { + // 准备请求参数 + Map requestParameters = new HashMap<>(); + requestParameters.put("dataSources", "deviceusagesummary"); + requestParameters.put("displayMod", "1"); + + // Mock EquipmentData 方法 + List mockList = new ArrayList<>(); + JSONObject device = new JSONObject(); + device.put("name", "测试设备"); + device.put("status", "空闲"); + mockList.add(device); + + RealTimeBulletinBoardWorkloadHelper spyHelper = Mockito.spy(underTest); + doReturn(mockList).when(spyHelper).getEquipmentData("1"); + + // 执行测试 + String result = spyHelper.getDataByDataSources(requestParameters); + + // 验证结果 + assertNotNull(result); + assertTrue(result.contains("测试设备")); + } + + @Test + public void testGetDataByDataSources_UrgentGoodsSummary() { + // 准备请求参数 + Map requestParameters = new HashMap<>(); + requestParameters.put("dataSources", "urgentgoodssummary"); + + // Mock UrgentGoodsSummary 方法 + JSONArray mockArray = new JSONArray(); + JSONObject urgentItem = new JSONObject(); + urgentItem.put("urgentAmount", 3); + urgentItem.put("name", "加急"); + mockArray.add(urgentItem); + + RealTimeBulletinBoardWorkloadHelper spyHelper = Mockito.spy(underTest); + doReturn(mockArray).when(spyHelper).getUrgentgoodssummary(); + + // 执行测试 + String result = spyHelper.getDataByDataSources(requestParameters); + + // 验证结果 + assertNotNull(result); + assertTrue(result.contains("加急")); + } + + @Test + public void testGetDataByDataSources_WorkloadSummary() { + // 准备请求参数 + Map requestParameters = new HashMap<>(); + requestParameters.put("dataSources", "workloadsummary"); + + // Mock RealTimeBulletinBoardWorkloadData 方法 + JSONObject mockResult = new JSONObject(); + mockResult.put("applyAmount", 25); + mockResult.put("recycledAmount", 20); + + RealTimeBulletinBoardWorkloadHelper spyHelper = Mockito.spy(underTest); + doReturn(mockResult).when(spyHelper).getRealTimeBulletinBoardWorkloadData(); + + // 执行测试 + String result = spyHelper.getDataByDataSources(requestParameters); + + // 验证结果 + assertNotNull(result); + assertTrue(result.contains("applyAmount")); + } + + @Test + public void testGetDataByDataSources_StatisticalWorkload() { + // 准备请求参数 + Map requestParameters = new HashMap<>(); + requestParameters.put("dataSources", "statisticalWorkload"); + requestParameters.put("departCoding", "TEST_DEPART"); + requestParameters.put("link", "回收数量"); + + // Mock StatisticalWorkload 数据 + List mockList = new ArrayList<>(); + StatisticalWorkload workload = new StatisticalWorkload(); + workload.setOperator("测试人员"); + workload.setAmount(10); + workload.setColumnName("回收数量"); + mockList.add(workload); + + RealTimeBulletinBoardWorkloadHelper spyHelper = Mockito.spy(underTest); + doReturn(mockList).when(spyHelper).getStatisticalWorkloadData("TEST_DEPART", "回收数量"); + + // 执行测试 + String result = spyHelper.getDataByDataSources(requestParameters); + + // 验证结果 + assertNotNull(result); + assertTrue(result.contains("测试人员")); + } + + @Test + public void testGetDataByDataSources_UnknownDataSource() { + // 准备请求参数 - 未知数据源 + Map requestParameters = new HashMap<>(); + requestParameters.put("dataSources", "unknownsource"); + + // 执行测试 + String result = underTest.getDataByDataSources(requestParameters); + + // 验证返回空字符串 + assertNotNull(result); + assertEquals("", result); + } + + @Test + public void testGetDataByDataSources_EmptyDataSource() { + // 准备请求参数 - 空数据源 + Map requestParameters = new HashMap<>(); + requestParameters.put("dataSources", ""); + + // 执行测试 + String result = underTest.getDataByDataSources(requestParameters); + + // 验证返回 null + assertNull(result); + } + + @Test + public void testGetDataByDataSources_NullDataSource() { + // 准备请求参数 - null 数据源 + Map requestParameters = new HashMap<>(); + requestParameters.put("dataSources", null); + + // 执行测试 + String result = underTest.getDataByDataSources(requestParameters); + + // 验证返回 null + assertNull(result); + } + + // 测试边界情况 + @Test + public void testGetRealTimeBulletinBoardWorkloadData_WithNullDayStartTime() throws SQLException { + // 设置 dayStartTime 为 null + when(supplyRoomConfig.getDayStartTime()).thenReturn(null); + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(false); + + // 执行测试 - 应该使用默认的 00:00:00 + JSONObject result = underTest.getRealTimeBulletinBoardWorkloadData(); + + assertNotNull(result); + } + + @Test + public void testGetRealTimeBulletinBoardWorkloadData_WithNullDashboardsQueryCycle() throws SQLException { + // 设置 dashboardsQueryCycle 为 null + when(supplyRoomConfig.getDashboardsQueryCycle()).thenReturn(null); + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(false); + + // 执行测试 - 应该使用默认值 7 + JSONObject result = underTest.getRealTimeBulletinBoardWorkloadData(); + + assertNotNull(result); + } + + @Test + public void testGetRealTimeBulletinBoardWorkloadData_WithZeroDashboardsQueryCycle() throws SQLException { + // 设置 dashboardsQueryCycle 为 0 + when(supplyRoomConfig.getDashboardsQueryCycle()).thenReturn(0); + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(false); + + // 执行测试 + JSONObject result = underTest.getRealTimeBulletinBoardWorkloadData(); + + assertNotNull(result); + } + + // 测试异常处理 + @Test(expected = SystemException.class) + public void testGetStatisticalWorkloadData_WithEmptyLink() { + // 执行测试 - 应该抛出异常 + underTest.getStatisticalWorkloadData("TEST_DEPART", ""); + } + + @Test(expected = SystemException.class) + public void testGetStatisticalWorkloadData_WithNullLink() { + // 执行测试 - 应该抛出异常 + underTest.getStatisticalWorkloadData("TEST_DEPART", null); + } + + @Test(expected = SystemException.class) + public void testGetStatisticalWorkloadData_WithInvalidLink() { + // 设置必要的 Mock 行为 + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + // 执行测试 - 应该抛出异常 + underTest.getStatisticalWorkloadData("TEST_DEPART", "无效环节"); + } + + // 性能测试 - 确保方法在合理时间内完成 + @Test(timeout = 5000) // 5秒超时 + public void testGetRealTimeBulletinBoardWorkloadData_Performance() throws Exception { + // 准备 Mock 数据 + String[] dates = {testStartDate, testEndDate}; + when(supplyRoomConfigManager.getStartDateAndEndDate(eq(null), anyString())).thenReturn(dates); + + when(objectDao.executeSql(anyString())).thenReturn(resultSet); + when(resultSet.next()).thenReturn(false); + + // 执行测试 + underTest.getRealTimeBulletinBoardWorkloadData(); + + // 如果方法在超时时间内完成,测试通过 + } + +}